reports.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. "use strict";
  2. const async = require("async");
  3. const hooks = require("./hooks");
  4. const moduleManager = require("../../index");
  5. const db = require("../db");
  6. const cache = require("../cache");
  7. const utils = require("../utils");
  8. // const logger = require("../logger");
  9. const songs = require("../songs");
  10. const reportableIssues = [
  11. {
  12. name: "Video",
  13. reasons: [
  14. "Doesn't exist",
  15. "It's private",
  16. "It's not available in my country",
  17. ],
  18. },
  19. {
  20. name: "Title",
  21. reasons: ["Incorrect", "Inappropriate"],
  22. },
  23. {
  24. name: "Duration",
  25. reasons: [
  26. "Skips too soon",
  27. "Skips too late",
  28. "Starts too soon",
  29. "Skips too late",
  30. ],
  31. },
  32. {
  33. name: "Artists",
  34. reasons: ["Incorrect", "Inappropriate"],
  35. },
  36. {
  37. name: "Thumbnail",
  38. reasons: ["Incorrect", "Inappropriate", "Doesn't exist"],
  39. },
  40. ];
  41. cache.runJob("SUB", {
  42. channel: "report.resolve",
  43. cb: (reportId) => {
  44. utils.runJob("EMIT_TO_ROOM", {
  45. room: "admin.reports",
  46. args: ["event:admin.report.resolved", reportId],
  47. });
  48. },
  49. });
  50. cache.runJob("SUB", {
  51. channel: "report.create",
  52. cb: (report) => {
  53. utils.runJob("EMIT_TO_ROOM", {
  54. room: "admin.reports",
  55. args: ["event:admin.report.created", report],
  56. });
  57. },
  58. });
  59. module.exports = {
  60. /**
  61. * Gets all reports
  62. *
  63. * @param {Object} session - the session object automatically added by socket.io
  64. * @param {Function} cb - gets called with the result
  65. */
  66. index: hooks.adminRequired(async (session, cb) => {
  67. const reportModel = await db.runJob("GET_MODEL", {
  68. modelName: "report",
  69. });
  70. async.waterfall(
  71. [
  72. (next) => {
  73. reportModel
  74. .find({ resolved: false })
  75. .sort({ released: "desc" })
  76. .exec(next);
  77. },
  78. ],
  79. async (err, reports) => {
  80. if (err) {
  81. err = await utils.runJob("GET_ERROR", { error: err });
  82. console.log(
  83. "ERROR",
  84. "REPORTS_INDEX",
  85. `Indexing reports failed. "${err}"`
  86. );
  87. return cb({ status: "failure", message: err });
  88. }
  89. console.log(
  90. "SUCCESS",
  91. "REPORTS_INDEX",
  92. "Indexing reports successful."
  93. );
  94. cb({ status: "success", data: reports });
  95. }
  96. );
  97. }),
  98. /**
  99. * Gets a specific report
  100. *
  101. * @param {Object} session - the session object automatically added by socket.io
  102. * @param {String} reportId - the id of the report to return
  103. * @param {Function} cb - gets called with the result
  104. */
  105. findOne: hooks.adminRequired(async (session, reportId, cb) => {
  106. const reportModel = await db.runJob("GET_MODEL", {
  107. modelName: "report",
  108. });
  109. async.waterfall(
  110. [
  111. (next) => {
  112. reportModel.findOne({ _id: reportId }).exec(next);
  113. },
  114. ],
  115. async (err, report) => {
  116. if (err) {
  117. err = await utils.runJob("GET_ERROR", { error: err });
  118. console.log(
  119. "ERROR",
  120. "REPORTS_FIND_ONE",
  121. `Finding report "${reportId}" failed. "${err}"`
  122. );
  123. return cb({ status: "failure", message: err });
  124. }
  125. console.log(
  126. "SUCCESS",
  127. "REPORTS_FIND_ONE",
  128. `Finding report "${reportId}" successful.`
  129. );
  130. cb({ status: "success", data: report });
  131. }
  132. );
  133. }),
  134. /**
  135. * Gets all reports for a songId (_id)
  136. *
  137. * @param {Object} session - the session object automatically added by socket.io
  138. * @param {String} songId - the id of the song to index reports for
  139. * @param {Function} cb - gets called with the result
  140. */
  141. getReportsForSong: hooks.adminRequired(async (session, songId, cb) => {
  142. const reportModel = await db.runJob("GET_MODEL", {
  143. modelName: "report",
  144. });
  145. async.waterfall(
  146. [
  147. (next) => {
  148. reportModel
  149. .find({ song: { _id: songId }, resolved: false })
  150. .sort({ released: "desc" })
  151. .exec(next);
  152. },
  153. (reports, next) => {
  154. let data = [];
  155. for (let i = 0; i < reports.length; i++) {
  156. data.push(reports[i]._id);
  157. }
  158. next(null, data);
  159. },
  160. ],
  161. async (err, data) => {
  162. if (err) {
  163. err = await utils.runJob("GET_ERROR", { error: err });
  164. console.log(
  165. "ERROR",
  166. "GET_REPORTS_FOR_SONG",
  167. `Indexing reports for song "${songId}" failed. "${err}"`
  168. );
  169. return cb({ status: "failure", message: err });
  170. } else {
  171. console.log(
  172. "SUCCESS",
  173. "GET_REPORTS_FOR_SONG",
  174. `Indexing reports for song "${songId}" successful.`
  175. );
  176. return cb({ status: "success", data });
  177. }
  178. }
  179. );
  180. }),
  181. /**
  182. * Resolves a report
  183. *
  184. * @param {Object} session - the session object automatically added by socket.io
  185. * @param {String} reportId - the id of the report that is getting resolved
  186. * @param {Function} cb - gets called with the result
  187. */
  188. resolve: hooks.adminRequired(async (session, reportId, cb) => {
  189. const reportModel = await db.runJob("GET_MODEL", {
  190. modelName: "report",
  191. });
  192. async.waterfall(
  193. [
  194. (next) => {
  195. reportModel.findOne({ _id: reportId }).exec(next);
  196. },
  197. (report, next) => {
  198. if (!report) return next("Report not found.");
  199. report.resolved = true;
  200. report.save((err) => {
  201. if (err) next(err.message);
  202. else next();
  203. });
  204. },
  205. ],
  206. async (err) => {
  207. if (err) {
  208. err = await utils.runJob("GET_ERROR", { error: err });
  209. console.log(
  210. "ERROR",
  211. "REPORTS_RESOLVE",
  212. `Resolving report "${reportId}" failed by user "${session.userId}". "${err}"`
  213. );
  214. return cb({ status: "failure", message: err });
  215. } else {
  216. cache.runJob("PUB", {
  217. channel: "report.resolve",
  218. value: reportId,
  219. });
  220. console.log(
  221. "SUCCESS",
  222. "REPORTS_RESOLVE",
  223. `User "${session.userId}" resolved report "${reportId}".`
  224. );
  225. cb({
  226. status: "success",
  227. message: "Successfully resolved Report",
  228. });
  229. }
  230. }
  231. );
  232. }),
  233. /**
  234. * Creates a new report
  235. *
  236. * @param {Object} session - the session object automatically added by socket.io
  237. * @param {Object} data - the object of the report data
  238. * @param {Function} cb - gets called with the result
  239. */
  240. create: hooks.loginRequired(async (session, data, cb) => {
  241. const reportModel = await db.runJob("GET_MODEL", {
  242. modelName: "report",
  243. });
  244. const songModel = await db.runJob("GET_MODEL", { modelName: "song" });
  245. async.waterfall(
  246. [
  247. (next) => {
  248. songModel.findOne({ songId: data.songId }).exec(next);
  249. },
  250. (song, next) => {
  251. if (!song) return next("Song not found.");
  252. songs
  253. .runJob("GET_SONG", { id: song._id })
  254. .then((response) => next(null, response.song))
  255. .catch(next);
  256. },
  257. (song, next) => {
  258. if (!song) return next("Song not found.");
  259. delete data.songId;
  260. data.song = {
  261. _id: song._id,
  262. songId: song.songId,
  263. };
  264. for (let z = 0; z < data.issues.length; z++) {
  265. if (
  266. reportableIssues.filter((issue) => {
  267. return issue.name == data.issues[z].name;
  268. }).length > 0
  269. ) {
  270. for (let r = 0; r < reportableIssues.length; r++) {
  271. if (
  272. reportableIssues[r].reasons.every(
  273. (reason) =>
  274. data.issues[z].reasons.indexOf(
  275. reason
  276. ) < -1
  277. )
  278. ) {
  279. return cb({
  280. status: "failure",
  281. message: "Invalid data",
  282. });
  283. }
  284. }
  285. } else
  286. return cb({
  287. status: "failure",
  288. message: "Invalid data",
  289. });
  290. }
  291. next();
  292. },
  293. (next) => {
  294. let issues = [];
  295. for (let r = 0; r < data.issues.length; r++) {
  296. if (!data.issues[r].reasons.length <= 0)
  297. issues.push(data.issues[r]);
  298. }
  299. data.issues = issues;
  300. next();
  301. },
  302. (next) => {
  303. data.createdBy = session.userId;
  304. data.createdAt = Date.now();
  305. reportModel.create(data, next);
  306. },
  307. ],
  308. async (err, report) => {
  309. if (err) {
  310. err = await utils.runJob("GET_ERROR", { error: err });
  311. console.log(
  312. "ERROR",
  313. "REPORTS_CREATE",
  314. `Creating report for "${data.song._id}" failed by user "${session.userId}". "${err}"`
  315. );
  316. return cb({ status: "failure", message: err });
  317. } else {
  318. cache.runJob("PUB", {
  319. channel: "report.create",
  320. value: report,
  321. });
  322. console.log(
  323. "SUCCESS",
  324. "REPORTS_CREATE",
  325. `User "${session.userId}" created report for "${data.songId}".`
  326. );
  327. return cb({
  328. status: "success",
  329. message: "Successfully created report",
  330. });
  331. }
  332. }
  333. );
  334. }),
  335. };