reports.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. // properties for every report issue that is saved to db
  164. const template = {};
  165. async.waterfall(
  166. [
  167. next => songModel.findOne({ youtubeId }).exec(next),
  168. (song, next) => {
  169. if (!song) return next("Song not found.");
  170. return SongsModule.runJob("GET_SONG", { songId: song._id }, this)
  171. .then(res => next(null, res.song))
  172. .catch(next);
  173. },
  174. (song, next) => {
  175. if (!song) return next("Song not found.");
  176. template.song = {
  177. _id: song._id,
  178. youtubeId: song.youtubeId
  179. };
  180. return next(null, { title: song.title, artists: song.artists, thumbnail: song.thumbnail });
  181. },
  182. (song, next) => {
  183. template.createdBy = session.userId;
  184. template.createdAt = Date.now();
  185. return async.each(
  186. report.issues,
  187. (issue, next) => {
  188. reportModel.create({ ...issue, ...template }, (err, value) => {
  189. CacheModule.runJob("PUB", {
  190. channel: "report.create",
  191. value
  192. });
  193. return next(err);
  194. });
  195. },
  196. err => next(err, report, song)
  197. );
  198. }
  199. ],
  200. async (err, report, song) => {
  201. if (err) {
  202. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  203. this.log(
  204. "ERROR",
  205. "REPORTS_CREATE",
  206. `Creating report for "${template.song._id}" failed by user "${session.userId}". "${err}"`
  207. );
  208. return cb({ status: "error", message: err });
  209. }
  210. ActivitiesModule.runJob("ADD_ACTIVITY", {
  211. userId: template.createdBy,
  212. type: "song__report",
  213. payload: {
  214. message: `Reported song <youtubeId>${song.title} by ${song.artists.join(", ")}</youtubeId>`,
  215. youtubeId: template.song.youtubeId,
  216. thumbnail: song.thumbnail
  217. }
  218. });
  219. this.log("SUCCESS", "REPORTS_CREATE", `User "${session.userId}" created report for "${youtubeId}".`);
  220. return cb({
  221. status: "success",
  222. message: "Successfully created report"
  223. });
  224. }
  225. );
  226. })
  227. };