news.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import async from "async";
  2. import { isAdminRequired } from "./hooks";
  3. import moduleManager from "../../index";
  4. const DBModule = moduleManager.modules.db;
  5. const UtilsModule = moduleManager.modules.utils;
  6. const WSModule = moduleManager.modules.ws;
  7. const CacheModule = moduleManager.modules.cache;
  8. CacheModule.runJob("SUB", {
  9. channel: "news.create",
  10. cb: news => {
  11. WSModule.runJob("EMIT_TO_ROOM", {
  12. room: "admin.news",
  13. args: ["event:admin.news.created", { data: { news } }]
  14. });
  15. if (news.status === "published")
  16. WSModule.runJob("EMIT_TO_ROOM", {
  17. room: "news",
  18. args: ["event:news.created", { data: { news } }]
  19. });
  20. }
  21. });
  22. CacheModule.runJob("SUB", {
  23. channel: "news.remove",
  24. cb: newsId => {
  25. WSModule.runJob("EMIT_TO_ROOM", {
  26. room: "admin.news",
  27. args: ["event:admin.news.deleted", { data: { newsId } }]
  28. });
  29. WSModule.runJob("EMIT_TO_ROOM", {
  30. room: "news",
  31. args: ["event:news.deleted", { data: { newsId } }]
  32. });
  33. }
  34. });
  35. CacheModule.runJob("SUB", {
  36. channel: "news.update",
  37. cb: news => {
  38. WSModule.runJob("EMIT_TO_ROOM", {
  39. room: "admin.news",
  40. args: ["event:admin.news.updated", { data: { news } }]
  41. });
  42. WSModule.runJob("EMIT_TO_ROOM", {
  43. room: "news",
  44. args: ["event:news.updated", { data: { news } }]
  45. });
  46. }
  47. });
  48. export default {
  49. /**
  50. * Gets all news items that are published
  51. *
  52. * @param {object} session - the session object automatically added by the websocket
  53. * @param {Function} cb - gets called with the result
  54. */
  55. async index(session, cb) {
  56. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  57. async.waterfall(
  58. [
  59. next => {
  60. newsModel.find({ status: "published" }).sort({ createdAt: "desc" }).exec(next);
  61. }
  62. ],
  63. async (err, news) => {
  64. if (err) {
  65. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  66. this.log("ERROR", "NEWS_INDEX", `Indexing news failed. "${err}"`);
  67. return cb({ status: "error", message: err });
  68. }
  69. this.log("SUCCESS", "NEWS_INDEX", `Indexing news successful.`, false);
  70. return cb({ status: "success", data: { news } });
  71. }
  72. );
  73. },
  74. /**
  75. * Gets a news item by id
  76. *
  77. * @param {object} session - the session object automatically added by the websocket
  78. * @param {string} newsId - the news item id
  79. * @param {Function} cb - gets called with the result
  80. */
  81. async getNewsFromId(session, newsId, cb) {
  82. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  83. async.waterfall(
  84. [
  85. next => {
  86. newsModel.findById(newsId, next);
  87. }
  88. ],
  89. async (err, news) => {
  90. if (err) {
  91. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  92. this.log("ERROR", "GET_NEWS_FROM_ID", `Getting news item ${newsId} failed. "${err}"`);
  93. return cb({ status: "error", message: err });
  94. }
  95. this.log("SUCCESS", "GET_NEWS_FROM_ID", `Got news item ${newsId} successfully.`, false);
  96. return cb({ status: "success", data: { news } });
  97. }
  98. );
  99. },
  100. /**
  101. * Creates a news item
  102. *
  103. * @param {object} session - the session object automatically added by the websocket
  104. * @param {object} data - the object of the news data
  105. * @param {Function} cb - gets called with the result
  106. */
  107. create: isAdminRequired(async function create(session, data, cb) {
  108. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  109. async.waterfall(
  110. [
  111. next => {
  112. data.createdBy = session.userId;
  113. data.createdAt = Date.now();
  114. newsModel.create(data, next);
  115. }
  116. ],
  117. async (err, news) => {
  118. if (err) {
  119. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  120. this.log("ERROR", "NEWS_CREATE", `Creating news failed. "${err}"`);
  121. return cb({ status: "error", message: err });
  122. }
  123. CacheModule.runJob("PUB", { channel: "news.create", value: news });
  124. this.log("SUCCESS", "NEWS_CREATE", `Creating news successful.`);
  125. return cb({
  126. status: "success",
  127. message: "Successfully created News"
  128. });
  129. }
  130. );
  131. }),
  132. /**
  133. * Gets the latest news item
  134. *
  135. * @param {object} session - the session object automatically added by the websocket
  136. * @param {Function} cb - gets called with the result
  137. */
  138. async newest(session, cb) {
  139. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  140. async.waterfall([next => newsModel.findOne({}).sort({ createdAt: "desc" }).exec(next)], async (err, news) => {
  141. if (err) {
  142. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  143. this.log("ERROR", "NEWS_NEWEST", `Getting the latest news failed. "${err}"`);
  144. return cb({ status: "error", message: err });
  145. }
  146. this.log("SUCCESS", "NEWS_NEWEST", `Successfully got the latest news.`, false);
  147. return cb({ status: "success", data: { news } });
  148. });
  149. },
  150. /**
  151. * Removes a news item
  152. *
  153. * @param {object} session - the session object automatically added by the websocket
  154. * @param {object} newsId - the id of the news item we want to remove
  155. * @param {Function} cb - gets called with the result
  156. */
  157. remove: isAdminRequired(async function remove(session, newsId, cb) {
  158. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  159. async.waterfall(
  160. [
  161. next => {
  162. if (!newsId) return next("Please provide a news item id to update.");
  163. return next();
  164. },
  165. next => {
  166. newsModel.deleteOne({ _id: newsId }, err => next(err));
  167. }
  168. ],
  169. async err => {
  170. if (err) {
  171. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  172. this.log(
  173. "ERROR",
  174. "NEWS_REMOVE",
  175. `Removing news "${newsId}" failed for user "${session.userId}". "${err}"`
  176. );
  177. return cb({ status: "error", message: err });
  178. }
  179. CacheModule.runJob("PUB", { channel: "news.remove", value: newsId });
  180. this.log("SUCCESS", "NEWS_REMOVE", `Removing news "${newsId}" successful by user "${session.userId}".`);
  181. return cb({
  182. status: "success",
  183. message: "Successfully removed News"
  184. });
  185. }
  186. );
  187. }),
  188. /**
  189. * Updates a news item
  190. *
  191. * @param {object} session - the session object automatically added by the websocket
  192. * @param {string} newsId - the id of the news item
  193. * @param {object} item - the news item object
  194. * @param {string} item.status - the status of the news e.g. published
  195. * @param {string} item.title - taken from a level-1 heading at the top of the markdown
  196. * @param {string} item.markdown - the markdown that forms the content of the news
  197. * @param {Function} cb - gets called with the result
  198. */
  199. update: isAdminRequired(async function update(session, newsId, item, cb) {
  200. const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
  201. async.waterfall(
  202. [
  203. next => {
  204. if (!newsId) return next("Please provide a news item id to update.");
  205. return next();
  206. },
  207. next => {
  208. newsModel.updateOne({ _id: newsId }, item, { upsert: true }, err => next(err));
  209. }
  210. ],
  211. async err => {
  212. if (err) {
  213. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  214. this.log(
  215. "ERROR",
  216. "NEWS_UPDATE",
  217. `Updating news item "${newsId}" failed for user "${session.userId}". "${err}"`
  218. );
  219. return cb({ status: "error", message: err });
  220. }
  221. CacheModule.runJob("PUB", { channel: "news.update", value: { ...item, _id: newsId } });
  222. this.log(
  223. "SUCCESS",
  224. "NEWS_UPDATE",
  225. `Updating news item "${newsId}" successful for user "${session.userId}".`
  226. );
  227. return cb({
  228. status: "success",
  229. message: "Successfully updated news item"
  230. });
  231. }
  232. );
  233. })
  234. };