reports.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. 'use strict';
  2. const async = require('async');
  3. const db = require('../db');
  4. const cache = require('../cache');
  5. const utils = require('../utils');
  6. const logger = require('../logger');
  7. const hooks = require('./hooks');
  8. const songs = require('../songs');
  9. const reportableIssues = [
  10. {
  11. name: 'Video',
  12. reasons: [
  13. 'Doesn\'t exist',
  14. 'It\'s private',
  15. 'It\'s not available in my country'
  16. ]
  17. },
  18. {
  19. name: 'Title',
  20. reasons: [
  21. 'Incorrect',
  22. 'Inappropriate'
  23. ]
  24. },
  25. {
  26. name: 'Duration',
  27. reasons: [
  28. 'Skips too soon',
  29. 'Skips too late',
  30. 'Starts too soon',
  31. 'Skips too late'
  32. ]
  33. },
  34. {
  35. name: 'Artists',
  36. reasons: [
  37. 'Incorrect',
  38. 'Inappropriate'
  39. ]
  40. },
  41. {
  42. name: 'Thumbnail',
  43. reasons: [
  44. 'Incorrect',
  45. 'Inappropriate',
  46. 'Doesn\'t exist'
  47. ]
  48. }
  49. ];
  50. cache.sub('report.resolve', reportId => {
  51. utils.emitToRoom('admin.reports', 'event:admin.report.resolved', reportId);
  52. });
  53. cache.sub('report.create', report => {
  54. utils.emitToRoom('admin.reports', 'event:admin.report.created', report);
  55. });
  56. module.exports = {
  57. /**
  58. * Gets all reports
  59. *
  60. * @param {Object} session - the session object automatically added by socket.io
  61. * @param {Function} cb - gets called with the result
  62. */
  63. index: hooks.adminRequired((session, cb) => {
  64. async.waterfall([
  65. (next) => {
  66. db.models.report.find({ resolved: false }).sort({ released: 'desc' }).exec(next);
  67. }
  68. ], (err, reports) => {
  69. if (err) {
  70. logger.error("REPORTS_INDEX", `Indexing reports failed. "${err.message}"`);
  71. return cb({ 'status': 'failure', 'message': 'Something went wrong'});
  72. }
  73. logger.success("REPORTS_INDEX", "Indexing reports successful.");
  74. cb({ status: 'success', data: reports });
  75. });
  76. }),
  77. /**
  78. * Gets all reports for a songId
  79. *
  80. * @param {Object} session - the session object automatically added by socket.io
  81. * @param {String} songId - the id of the song to index reports for
  82. * @param {Function} cb - gets called with the result
  83. */
  84. getReportsForSong: hooks.adminRequired((session, songId, cb) => {
  85. async.waterfall([
  86. (next) => {
  87. db.models.report.find({ songId, resolved: false }).sort({ released: 'desc' }).exec(next);
  88. }
  89. ], (err, reports) => {
  90. if (err) {
  91. logger.error("REPORTS_GETFORSONG", `Indexing reports for song "${songId}" failed. "${err.message}"`);
  92. return cb({ 'status': 'failure', 'message': 'Something went wrong'});
  93. } else {
  94. logger.success("REPORTS_GETFORSONG", `Indexing report for song "{songId}" successful.`);
  95. return cb({ status: 'success', data: reports.length });
  96. }
  97. });
  98. }),
  99. /**
  100. * Resolves a report
  101. *
  102. * @param {Object} session - the session object automatically added by socket.io
  103. * @param {String} reportId - the id of the report that is getting resolved
  104. * @param {Function} cb - gets called with the result
  105. * @param {String} userId - the userId automatically added by hooks
  106. */
  107. resolve: hooks.adminRequired((session, reportId, cb, userId) => {
  108. async.waterfall([
  109. (next) => {
  110. db.models.report.findOne({ _id: reportId }).exec(next);
  111. },
  112. (report, next) => {
  113. if (!report) return next('Report not found.');
  114. report.resolved = true;
  115. report.save(err => {
  116. if (err) next(err.message);
  117. else next();
  118. });
  119. }
  120. ], (err) => {
  121. if (err) {
  122. logger.error("REPORTS_RESOLVE", `Resolving report "${reportId}" failed by user "${userId}". Mongo error. "${err.message}"`);
  123. return cb({ 'status': 'failure', 'message': 'Something went wrong'});
  124. } else {
  125. cache.pub('report.resolve', reportId);
  126. logger.success("REPORTS_RESOLVE", `User "${userId}" resolved report "${reportId}".`);
  127. cb({ status: 'success', message: 'Successfully resolved Report' });
  128. }
  129. });
  130. }),
  131. /**
  132. * Creates a new report
  133. *
  134. * @param {Object} session - the session object automatically added by socket.io
  135. * @param {Object} data - the object of the report data
  136. * @param {Function} cb - gets called with the result
  137. * @param {String} userId - the userId automatically added by hooks
  138. */
  139. create: hooks.loginRequired((session, data, cb, userId) => {
  140. async.waterfall([
  141. (next) => {
  142. songs.getSong(data.songId, next);
  143. },
  144. (song, next) => {
  145. if (!song) return next('Song not found.');
  146. for (let z = 0; z < data.issues.length; z++) {
  147. if (reportableIssues.filter(issue => { return issue.name == data.issues[z].name; }).length > 0) {
  148. for (let r = 0; r < reportableIssues.length; r++) {
  149. if (reportableIssues[r].reasons.every(reason => data.issues[z].reasons.indexOf(reason) < -1)) {
  150. return cb({ status: 'failure', message: 'Invalid data' });
  151. }
  152. }
  153. } else return cb({ status: 'failure', message: 'Invalid data' });
  154. }
  155. next();
  156. },
  157. (next) => {
  158. let issues = [];
  159. for (let r = 0; r < data.issues.length; r++) {
  160. if (!data.issues[r].reasons.length <= 0) issues.push(data.issues[r]);
  161. }
  162. data.issues = issues;
  163. next();
  164. },
  165. (next) => {
  166. data.createdBy = userId;
  167. data.createdAt = Date.now();
  168. db.models.report.create(data, next);
  169. }
  170. ], (err, report) => {
  171. if (err) {
  172. let error = 'An error occurred.';
  173. if (typeof err === "string") error = err;
  174. else if (err.message) error = err.message;
  175. logger.error("REPORTS_CREATE", `Creating report for "${data.songId}" failed by user "${userId}". "${error}"`);
  176. return cb({ 'status': 'failure', 'message': 'Something went wrong' });
  177. }
  178. else {
  179. cache.pub('report.create', report);
  180. logger.success("REPORTS_CREATE", `User "${userId}" created report for "${data.songId}".`);
  181. return cb({ 'status': 'success', 'message': 'Successfully created report' });
  182. }
  183. });
  184. })
  185. };