reports.js 14 KB

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