reports.js 12 KB

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