reports.js 7.8 KB

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