reports.js 8.1 KB

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