reports.js 11 KB

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