reports.js 8.6 KB

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