reports.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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.issue.toggle",
  12. cb: data => {
  13. WSModule.runJob("EMIT_TO_ROOM", {
  14. room: "admin.reports",
  15. args: ["event:admin.report.issue.toggled", { data: { issueId: data.issueId, reportId: data.reportId } }]
  16. });
  17. WSModule.runJob("EMIT_TO_ROOM", {
  18. room: `edit-song.${data.songId}`,
  19. args: ["event:admin.report.issue.toggled", { data: { issueId: data.issueId, reportId: data.reportId } }]
  20. });
  21. }
  22. });
  23. CacheModule.runJob("SUB", {
  24. channel: "report.resolve",
  25. cb: ({ reportId, songId }) => {
  26. WSModule.runJob("EMIT_TO_ROOM", {
  27. room: "admin.reports",
  28. args: ["event:admin.report.resolved", { data: { reportId } }]
  29. });
  30. WSModule.runJob("EMIT_TO_ROOM", {
  31. room: `edit-song.${songId}`,
  32. args: ["event:admin.report.resolved", { data: { reportId } }]
  33. });
  34. }
  35. });
  36. CacheModule.runJob("SUB", {
  37. channel: "report.create",
  38. cb: report => {
  39. console.log(report);
  40. DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  41. userModel
  42. .findById(report.createdBy)
  43. .select({ avatar: -1, name: -1, username: -1 })
  44. .exec((err, { avatar, name, username }) => {
  45. report.createdBy = {
  46. avatar,
  47. name,
  48. username,
  49. _id: report.createdBy
  50. };
  51. WSModule.runJob("EMIT_TO_ROOM", {
  52. room: "admin.reports",
  53. args: ["event:admin.report.created", { data: { report } }]
  54. });
  55. WSModule.runJob("EMIT_TO_ROOM", {
  56. room: `edit-song.${report.song._id}`,
  57. args: ["event:admin.report.created", { data: { report } }]
  58. });
  59. });
  60. });
  61. }
  62. });
  63. export default {
  64. /**
  65. * Gets all reports
  66. *
  67. * @param {object} session - the session object automatically added by the websocket
  68. * @param {Function} cb - gets called with the result
  69. */
  70. index: isAdminRequired(async function index(session, cb) {
  71. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  72. async.waterfall(
  73. [next => reportModel.find({ resolved: false }).sort({ released: "desc" }).exec(next)],
  74. async (err, reports) => {
  75. if (err) {
  76. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  77. this.log("ERROR", "REPORTS_INDEX", `Indexing reports failed. "${err}"`);
  78. return cb({ status: "error", message: err });
  79. }
  80. this.log("SUCCESS", "REPORTS_INDEX", "Indexing reports successful.");
  81. return cb({ status: "success", data: { reports } });
  82. }
  83. );
  84. }),
  85. /**
  86. * Gets a specific report
  87. *
  88. * @param {object} session - the session object automatically added by the websocket
  89. * @param {string} reportId - the id of the report to return
  90. * @param {Function} cb - gets called with the result
  91. */
  92. findOne: isAdminRequired(async function findOne(session, reportId, cb) {
  93. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  94. async.waterfall([next => reportModel.findOne({ _id: reportId }).exec(next)], async (err, report) => {
  95. if (err) {
  96. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  97. this.log("ERROR", "REPORTS_FIND_ONE", `Finding report "${reportId}" failed. "${err}"`);
  98. return cb({ status: "error", message: err });
  99. }
  100. this.log("SUCCESS", "REPORTS_FIND_ONE", `Finding report "${reportId}" successful.`);
  101. return cb({ status: "success", data: { report } });
  102. });
  103. }),
  104. /**
  105. * Gets all reports for a songId
  106. *
  107. * @param {object} session - the session object automatically added by the websocket
  108. * @param {string} songId - the id of the song to index reports for
  109. * @param {Function} cb - gets called with the result
  110. */
  111. getReportsForSong: isAdminRequired(async function getReportsForSong(session, songId, cb) {
  112. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  113. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  114. async.waterfall(
  115. [
  116. next =>
  117. reportModel.find({ "song._id": songId, resolved: false }).sort({ createdAt: "desc" }).exec(next),
  118. (_reports, next) => {
  119. const reports = [];
  120. async.each(
  121. _reports,
  122. (report, cb) => {
  123. userModel
  124. .findById(report.createdBy)
  125. .select({ avatar: -1, name: -1, username: -1 })
  126. .exec((err, { avatar, name, username }) => {
  127. reports.push({
  128. ...report._doc,
  129. createdBy: {
  130. avatar,
  131. name,
  132. username,
  133. _id: report.createdBy
  134. }
  135. });
  136. return cb(err);
  137. });
  138. },
  139. err => next(err, reports)
  140. );
  141. }
  142. ],
  143. async (err, reports) => {
  144. if (err) {
  145. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  146. this.log("ERROR", "GET_REPORTS_FOR_SONG", `Indexing reports for song "${songId}" failed. "${err}"`);
  147. return cb({ status: "error", message: err });
  148. }
  149. this.log("SUCCESS", "GET_REPORTS_FOR_SONG", `Indexing reports for song "${songId}" successful.`);
  150. return cb({ status: "success", data: { reports } });
  151. }
  152. );
  153. }),
  154. /**
  155. * Resolves a report as a whole
  156. *
  157. * @param {object} session - the session object automatically added by the websocket
  158. * @param {string} reportId - the id of the report that is getting resolved
  159. * @param {Function} cb - gets called with the result
  160. */
  161. resolve: isAdminRequired(async function resolve(session, reportId, cb) {
  162. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  163. async.waterfall(
  164. [
  165. next => {
  166. reportModel.findById(reportId).exec(next);
  167. },
  168. (report, next) => {
  169. if (!report) return next("Report not found.");
  170. report.resolved = true;
  171. return report.save(err => {
  172. if (err) return next(err.message);
  173. return next(null, report.song._id);
  174. });
  175. }
  176. ],
  177. async (err, songId) => {
  178. if (err) {
  179. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  180. this.log(
  181. "ERROR",
  182. "REPORTS_RESOLVE",
  183. `Resolving report "${reportId}" failed by user "${session.userId}". "${err}"`
  184. );
  185. return cb({ status: "error", message: err });
  186. }
  187. CacheModule.runJob("PUB", {
  188. channel: "report.resolve",
  189. value: { reportId, songId }
  190. });
  191. this.log("SUCCESS", "REPORTS_RESOLVE", `User "${session.userId}" resolved report "${reportId}".`);
  192. return cb({
  193. status: "success",
  194. message: "Successfully resolved Report"
  195. });
  196. }
  197. );
  198. }),
  199. /**
  200. * Resolves/Unresolves an issue within a report
  201. *
  202. * @param {object} session - the session object automatically added by the websocket
  203. * @param {string} reportId - the id of the report that is getting resolved
  204. * @param {string} issueId - the id of the issue within the report
  205. * @param {Function} cb - gets called with the result
  206. */
  207. toggleIssue: isAdminRequired(async function toggleIssue(session, reportId, issueId, cb) {
  208. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  209. async.waterfall(
  210. [
  211. next => {
  212. reportModel.findById(reportId).exec(next);
  213. },
  214. (report, next) => {
  215. if (!report) return next("Report not found.");
  216. const issue = report.issues.find(issue => issue._id.toString() === issueId);
  217. issue.resolved = !issue.resolved;
  218. return report.save(err => {
  219. if (err) return next(err.message);
  220. return next(null, report.song._id);
  221. });
  222. }
  223. ],
  224. async (err, songId) => {
  225. if (err) {
  226. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  227. this.log(
  228. "ERROR",
  229. "REPORTS_TOGGLE_ISSUE",
  230. `Resolving an issue within report "${reportId}" failed by user "${session.userId}". "${err}"`
  231. );
  232. return cb({ status: "error", message: err });
  233. }
  234. CacheModule.runJob("PUB", {
  235. channel: "report.issue.toggle",
  236. value: { reportId, issueId, songId }
  237. });
  238. this.log(
  239. "SUCCESS",
  240. "REPORTS_TOGGLE_ISSUE",
  241. `User "${session.userId}" resolved an issue in report "${reportId}".`
  242. );
  243. return cb({
  244. status: "success",
  245. message: "Successfully resolved issue within report"
  246. });
  247. }
  248. );
  249. }),
  250. /**
  251. * Creates a new report
  252. *
  253. * @param {object} session - the session object automatically added by the websocket
  254. * @param {object} report - the object of the report data
  255. * @param {string} report.youtubeId - the youtube id of the song that is being reported
  256. * @param {Array} report.issues - all issues reported (custom or defined)
  257. * @param {Function} cb - gets called with the result
  258. */
  259. create: isLoginRequired(async function create(session, report, cb) {
  260. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  261. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  262. const { youtubeId } = report;
  263. async.waterfall(
  264. [
  265. next => songModel.findOne({ youtubeId }).exec(next),
  266. (song, next) => {
  267. if (!song) return next("Song not found.");
  268. return SongsModule.runJob("GET_SONG", { songId: song._id }, this)
  269. .then(res => next(null, res.song))
  270. .catch(next);
  271. },
  272. (song, next) => {
  273. if (!song) return next("Song not found.");
  274. delete report.youtubeId;
  275. report.song = {
  276. _id: song._id,
  277. youtubeId: song.youtubeId
  278. };
  279. return next(null, { title: song.title, artists: song.artists, thumbnail: song.thumbnail });
  280. },
  281. (song, next) =>
  282. reportModel.create(
  283. {
  284. createdBy: session.userId,
  285. createdAt: Date.now(),
  286. ...report
  287. },
  288. (err, report) => next(err, report, song)
  289. )
  290. ],
  291. async (err, report, song) => {
  292. if (err) {
  293. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  294. this.log(
  295. "ERROR",
  296. "REPORTS_CREATE",
  297. `Creating report for "${report.song._id}" failed by user "${session.userId}". "${err}"`
  298. );
  299. return cb({ status: "error", message: err });
  300. }
  301. ActivitiesModule.runJob("ADD_ACTIVITY", {
  302. userId: session.userId,
  303. type: "song__report",
  304. payload: {
  305. message: `Reported song <youtubeId>${song.title} by ${song.artists.join(", ")}</youtubeId>`,
  306. youtubeId: report.song.youtubeId,
  307. thumbnail: song.thumbnail
  308. }
  309. });
  310. CacheModule.runJob("PUB", {
  311. channel: "report.create",
  312. value: report
  313. });
  314. this.log("SUCCESS", "REPORTS_CREATE", `User "${session.userId}" created report for "${youtubeId}".`);
  315. return cb({
  316. status: "success",
  317. message: "Successfully created report"
  318. });
  319. }
  320. );
  321. })
  322. };