reports.js 12 KB

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