reports.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. index: hooks.adminRequired((session, cb) => {
  58. async.waterfall([
  59. (next) => {
  60. db.models.report.find({ resolved: false }).sort({ released: 'desc' }).exec(next);
  61. }
  62. ], (err, reports) => {
  63. if (err) {
  64. logger.log("REPORTS_INDEX", "ERROR", `Indexing reports failed. "${err.message}"`);
  65. return cb({ 'status': 'failure', 'message': 'Something went wrong'});
  66. }
  67. logger.log("REPORTS_INDEX", "SUCCESS", "Indexing reports successful.");
  68. cb({ status: 'success', data: reports });
  69. });
  70. }),
  71. resolve: hooks.adminRequired((session, reportId, cb, userId) => {
  72. async.waterfall([
  73. (next) => {
  74. db.models.report.findOne({ _id: reportId }).sort({ released: 'desc' }).exec(next);
  75. },
  76. (report, next) => {
  77. if (!report) return next('Report not found.');
  78. db.models.update({ _id: reportId }, next);
  79. }
  80. ], (err) => {
  81. if (err) {
  82. logger.log("REPORTS_RESOLVE", "ERROR", `Resolving report "${reportId}" failed. Mongo error. "${err.message}"`);
  83. return cb({ 'status': 'failure', 'message': 'Something went wrong'});
  84. } else {
  85. cache.pub('report.resolve', reportId);
  86. logger.log("REPORTS_RESOLVE", "SUCCESS", `"${userId}" resolved report "${reportId}".`);
  87. cb({ status: 'success', message: 'Successfully resolved Report' });
  88. }
  89. });
  90. }),
  91. create: hooks.loginRequired((session, data, cb, userId) => {
  92. async.waterfall([
  93. (next) => {
  94. songs.getSong(data.songId, next);
  95. },
  96. (song, next) => {
  97. if (!song) return next('Song not found.');
  98. for (let z = 0; z < data.issues.length; z++) {
  99. if (reportableIssues.filter(issue => { return issue.name == data.issues[z].name; }).length > 0) {
  100. for (let r = 0; r < issues.length; r++) {
  101. if (reportableIssues[r].reasons.every(reason => data.issues[z].reasons.indexOf(reason) < -1)) {
  102. return cb({ 'status': 'failure', 'message': 'Invalid data' });
  103. }
  104. }
  105. } else return cb({ 'status': 'failure', 'message': 'Invalid data' });
  106. }
  107. next();
  108. },
  109. (next) => {
  110. let issues = [];
  111. for (let r = 0; r < data.issues.length; r++) {
  112. if (!data.issues[r].reasons.length <= 0) issues.push(data.issues[r]);
  113. }
  114. data.issues = issues;
  115. next();
  116. },
  117. (next) => {
  118. data.createdBy = userId;
  119. data.createdAt = Date.now();
  120. db.models.report.create(data, next);
  121. }
  122. ], (err, report) => {
  123. if (err) {
  124. let error = 'An error occurred.';
  125. if (typeof err === "string") error = err;
  126. else if (err.message) error = err.message;
  127. logger.log("REPORTS_CREATE", "ERROR", `Creating report for "${data.songId}" failed. "${error}"`);
  128. return cb({ 'status': 'failure', 'message': 'Something went wrong' });
  129. }
  130. else {
  131. cache.pub('report.create', report);
  132. logger.log("REPORTS_CREATE", "SUCCESS", `"${userId}" created report for "${data.songId}".`);
  133. return cb({ 'status': 'success', 'message': 'Successfully created report' });
  134. }
  135. });
  136. })
  137. };