reports.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. * Resolves a report as a whole
  212. *
  213. * @param {object} session - the session object automatically added by the websocket
  214. * @param {string} reportId - the id of the report that is getting resolved
  215. * @param {Function} cb - gets called with the result
  216. */
  217. resolve: isAdminRequired(async function resolve(session, reportId, cb) {
  218. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  219. async.waterfall(
  220. [
  221. next => {
  222. reportModel.findById(reportId).exec(next);
  223. },
  224. (report, next) => {
  225. if (!report) return next("Report not found.");
  226. report.resolved = true;
  227. return report.save(err => {
  228. if (err) return next(err.message);
  229. return next(null, report.song._id);
  230. });
  231. }
  232. ],
  233. async (err, songId) => {
  234. if (err) {
  235. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  236. this.log(
  237. "ERROR",
  238. "REPORTS_RESOLVE",
  239. `Resolving report "${reportId}" failed by user "${session.userId}". "${err}"`
  240. );
  241. return cb({ status: "error", message: err });
  242. }
  243. CacheModule.runJob("PUB", {
  244. channel: "report.resolve",
  245. value: { reportId, songId }
  246. });
  247. this.log("SUCCESS", "REPORTS_RESOLVE", `User "${session.userId}" resolved report "${reportId}".`);
  248. return cb({
  249. status: "success",
  250. message: "Successfully resolved Report"
  251. });
  252. }
  253. );
  254. }),
  255. /**
  256. * Resolves/Unresolves an issue within a report
  257. *
  258. * @param {object} session - the session object automatically added by the websocket
  259. * @param {string} reportId - the id of the report that is getting resolved
  260. * @param {string} issueId - the id of the issue within the report
  261. * @param {Function} cb - gets called with the result
  262. */
  263. toggleIssue: isAdminRequired(async function toggleIssue(session, reportId, issueId, cb) {
  264. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  265. async.waterfall(
  266. [
  267. next => {
  268. reportModel.findById(reportId).exec(next);
  269. },
  270. (report, next) => {
  271. if (!report) return next("Report not found.");
  272. const issue = report.issues.find(issue => issue._id.toString() === issueId);
  273. issue.resolved = !issue.resolved;
  274. return report.save(err => {
  275. if (err) return next(err.message);
  276. return next(null, issue.resolved, report.song._id);
  277. });
  278. }
  279. ],
  280. async (err, resolved, songId) => {
  281. if (err) {
  282. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  283. this.log(
  284. "ERROR",
  285. "REPORTS_TOGGLE_ISSUE",
  286. `Resolving an issue within report "${reportId}" failed by user "${session.userId}". "${err}"`
  287. );
  288. return cb({ status: "error", message: err });
  289. }
  290. CacheModule.runJob("PUB", {
  291. channel: "report.issue.toggle",
  292. value: { reportId, issueId, songId, resolved }
  293. });
  294. this.log(
  295. "SUCCESS",
  296. "REPORTS_TOGGLE_ISSUE",
  297. `User "${session.userId}" resolved an issue in report "${reportId}".`
  298. );
  299. return cb({
  300. status: "success",
  301. message: "Successfully resolved issue within report"
  302. });
  303. }
  304. );
  305. }),
  306. /**
  307. * Creates a new report
  308. *
  309. * @param {object} session - the session object automatically added by the websocket
  310. * @param {object} report - the object of the report data
  311. * @param {string} report.youtubeId - the youtube id of the song that is being reported
  312. * @param {Array} report.issues - all issues reported (custom or defined)
  313. * @param {Function} cb - gets called with the result
  314. */
  315. create: isLoginRequired(async function create(session, report, cb) {
  316. const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
  317. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  318. const { youtubeId } = report;
  319. async.waterfall(
  320. [
  321. next => songModel.findOne({ youtubeId }).exec(next),
  322. (song, next) => {
  323. if (!song) return next("Song not found.");
  324. return SongsModule.runJob("GET_SONG", { songId: song._id }, this)
  325. .then(res => next(null, res.song))
  326. .catch(next);
  327. },
  328. (song, next) => {
  329. if (!song) return next("Song not found.");
  330. delete report.youtubeId;
  331. report.song = {
  332. _id: song._id,
  333. youtubeId: song.youtubeId
  334. };
  335. return next(null, { title: song.title, artists: song.artists, thumbnail: song.thumbnail });
  336. },
  337. (song, next) =>
  338. reportModel.create(
  339. {
  340. createdBy: session.userId,
  341. createdAt: Date.now(),
  342. ...report
  343. },
  344. (err, report) => next(err, report, song)
  345. )
  346. ],
  347. async (err, report, song) => {
  348. if (err) {
  349. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  350. this.log(
  351. "ERROR",
  352. "REPORTS_CREATE",
  353. `Creating report for "${report.song._id}" failed by user "${session.userId}". "${err}"`
  354. );
  355. return cb({ status: "error", message: err });
  356. }
  357. ActivitiesModule.runJob("ADD_ACTIVITY", {
  358. userId: session.userId,
  359. type: "song__report",
  360. payload: {
  361. message: `Reported song <youtubeId>${song.title} by ${song.artists.join(", ")}</youtubeId>`,
  362. youtubeId: report.song.youtubeId,
  363. thumbnail: song.thumbnail
  364. }
  365. });
  366. CacheModule.runJob("PUB", {
  367. channel: "report.create",
  368. value: report
  369. });
  370. this.log("SUCCESS", "REPORTS_CREATE", `User "${session.userId}" created report for "${youtubeId}".`);
  371. return cb({
  372. status: "success",
  373. message: "Successfully created report"
  374. });
  375. }
  376. );
  377. })
  378. };