reports.js 14 KB

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