news.js 7.1 KB

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