reports.js 7.8 KB

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