reports.js 12 KB

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