reports.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import async from "async";
  2. import { isAdminRequired, isLoginRequired } 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 SongsModule = moduleManager.modules.songs;
  8. const CacheModule = moduleManager.modules.cache;
  9. const ActivitiesModule = moduleManager.modules.activities;
  10. CacheModule.runJob("SUB", {
  11. channel: "report.resolve",
  12. cb: reportId => {
  13. WSModule.runJob("EMIT_TO_ROOM", {
  14. room: "admin.reports",
  15. args: ["event:admin.report.resolved", { data: { reportId } }]
  16. });
  17. }
  18. });
  19. CacheModule.runJob("SUB", {
  20. channel: "report.create",
  21. cb: report => {
  22. WSModule.runJob("EMIT_TO_ROOM", {
  23. room: "admin.reports",
  24. args: ["event:admin.report.created", { data: { report } }]
  25. });
  26. }
  27. });
  28. export default {
  29. /**
  30. * Gets all reports
  31. *
  32. * @param {object} session - the session object automatically added by the websocket
  33. * @param {Function} cb - gets called with the result
  34. */
  35. index: isAdminRequired(async function index(session, cb) {
  36. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  37. async.waterfall(
  38. [next => reportModel.find({ resolved: false }).sort({ released: "desc" }).exec(next)],
  39. async (err, reports) => {
  40. if (err) {
  41. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  42. this.log("ERROR", "REPORTS_INDEX", `Indexing reports failed. "${err}"`);
  43. return cb({ status: "error", message: err });
  44. }
  45. this.log("SUCCESS", "REPORTS_INDEX", "Indexing reports successful.");
  46. return cb({ status: "success", data: { reports } });
  47. }
  48. );
  49. }),
  50. /**
  51. * Gets a specific report
  52. *
  53. * @param {object} session - the session object automatically added by the websocket
  54. * @param {string} reportId - the id of the report to return
  55. * @param {Function} cb - gets called with the result
  56. */
  57. findOne: isAdminRequired(async function findOne(session, reportId, cb) {
  58. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  59. async.waterfall([next => reportModel.findOne({ _id: reportId }).exec(next)], async (err, report) => {
  60. if (err) {
  61. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  62. this.log("ERROR", "REPORTS_FIND_ONE", `Finding report "${reportId}" failed. "${err}"`);
  63. return cb({ status: "error", message: err });
  64. }
  65. this.log("SUCCESS", "REPORTS_FIND_ONE", `Finding report "${reportId}" successful.`);
  66. return cb({ status: "success", data: { report } });
  67. });
  68. }),
  69. /**
  70. * Gets all reports for a songId
  71. *
  72. * @param {object} session - the session object automatically added by the websocket
  73. * @param {string} songId - the id of the song to index reports for
  74. * @param {Function} cb - gets called with the result
  75. */
  76. getReportsForSong: isAdminRequired(async function getReportsForSong(session, songId, cb) {
  77. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  78. async.waterfall(
  79. [
  80. next => {
  81. reportModel
  82. .find({ song: { _id: songId }, resolved: false })
  83. .sort({ released: "desc" })
  84. .exec(next);
  85. },
  86. (_reports, next) => {
  87. const reports = [];
  88. for (let i = 0; i < _reports.length; i += 1) {
  89. reports.push(_reports[i]._id);
  90. }
  91. next(null, reports);
  92. }
  93. ],
  94. async (err, reports) => {
  95. if (err) {
  96. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  97. this.log("ERROR", "GET_REPORTS_FOR_SONG", `Indexing reports for song "${songId}" failed. "${err}"`);
  98. return cb({ status: "error", message: err });
  99. }
  100. this.log("SUCCESS", "GET_REPORTS_FOR_SONG", `Indexing reports for song "${songId}" successful.`);
  101. return cb({ status: "success", data: { reports } });
  102. }
  103. );
  104. }),
  105. /**
  106. * Resolves a reported issue
  107. *
  108. * @param {object} session - the session object automatically added by the websocket
  109. * @param {string} reportId - the id of the report that is getting resolved
  110. * @param {Function} cb - gets called with the result
  111. */
  112. resolve: isAdminRequired(async function resolve(session, reportId, cb) {
  113. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  114. async.waterfall(
  115. [
  116. next => {
  117. reportModel.findOne({ _id: reportId }).exec(next);
  118. },
  119. (report, next) => {
  120. if (!report) return next("Report not found.");
  121. report.resolved = true;
  122. return report.save(err => {
  123. if (err) return next(err.message);
  124. return next();
  125. });
  126. }
  127. ],
  128. async err => {
  129. if (err) {
  130. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  131. this.log(
  132. "ERROR",
  133. "REPORTS_RESOLVE",
  134. `Resolving report "${reportId}" failed by user "${session.userId}". "${err}"`
  135. );
  136. return cb({ status: "error", message: err });
  137. }
  138. CacheModule.runJob("PUB", {
  139. channel: "report.resolve",
  140. value: reportId
  141. });
  142. this.log("SUCCESS", "REPORTS_RESOLVE", `User "${session.userId}" resolved report "${reportId}".`);
  143. return cb({
  144. status: "success",
  145. message: "Successfully resolved Report"
  146. });
  147. }
  148. );
  149. }),
  150. /**
  151. * Creates a new report
  152. *
  153. * @param {object} session - the session object automatically added by the websocket
  154. * @param {object} report - the object of the report data
  155. * @param {string} report.youtubeId - the youtube id of the song that is being reported
  156. * @param {Array} report.issues - all issues reported (custom or defined)
  157. * @param {Function} cb - gets called with the result
  158. */
  159. create: isLoginRequired(async function create(session, report, cb) {
  160. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  161. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  162. const { youtubeId } = report;
  163. async.waterfall(
  164. [
  165. next => songModel.findOne({ youtubeId }).exec(next),
  166. (song, next) => {
  167. if (!song) return next("Song not found.");
  168. return SongsModule.runJob("GET_SONG", { songId: song._id }, this)
  169. .then(res => next(null, res.song))
  170. .catch(next);
  171. },
  172. (song, next) => {
  173. if (!song) return next("Song not found.");
  174. delete report.youtubeId;
  175. report.song = {
  176. _id: song._id,
  177. youtubeId: song.youtubeId
  178. };
  179. return next(null, { title: song.title, artists: song.artists, thumbnail: song.thumbnail });
  180. },
  181. (song, next) =>
  182. reportModel.create(
  183. {
  184. createdBy: session.userId,
  185. createdAt: Date.now(),
  186. ...report
  187. },
  188. err => next(err, song)
  189. )
  190. ],
  191. async (err, song) => {
  192. if (err) {
  193. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  194. this.log(
  195. "ERROR",
  196. "REPORTS_CREATE",
  197. `Creating report for "${report.song._id}" failed by user "${session.userId}". "${err}"`
  198. );
  199. return cb({ status: "error", message: err });
  200. }
  201. ActivitiesModule.runJob("ADD_ACTIVITY", {
  202. userId: report.createdBy,
  203. type: "song__report",
  204. payload: {
  205. message: `Reported song <youtubeId>${song.title} by ${song.artists.join(", ")}</youtubeId>`,
  206. youtubeId: report.song.youtubeId,
  207. thumbnail: song.thumbnail
  208. }
  209. });
  210. CacheModule.runJob("PUB", {
  211. channel: "report.create",
  212. report
  213. });
  214. this.log("SUCCESS", "REPORTS_CREATE", `User "${session.userId}" created report for "${youtubeId}".`);
  215. return cb({
  216. status: "success",
  217. message: "Successfully created report"
  218. });
  219. }
  220. );
  221. })
  222. };