reports.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 hooks = require('./hooks');
  7. const songs = require('../songs');
  8. cache.sub('report.resolve', reportId => {
  9. utils.emitToRoom('admin.reports', 'event:admin.report.resolved', reportId);
  10. });
  11. cache.sub('report.create', report => {
  12. utils.emitToRoom('admin.reports', 'event:admin.report.created', report);
  13. });
  14. module.exports = {
  15. index: hooks.adminRequired((session, cb) => {
  16. db.models.report.find({ resolved: false }).sort({ released: 'desc' }).exec((err, reports) => {
  17. if (err) {
  18. console.error(err);
  19. cb({ 'status': 'failure', 'message': 'Something went wrong'});
  20. }
  21. cb({ status: 'success', data: reports });
  22. });
  23. }),
  24. resolve: hooks.adminRequired((session, _id, cb) => {
  25. db.models.report.findOne({ _id }).sort({ released: 'desc' }).exec((err, report) => {
  26. if (err) {
  27. console.error(err);
  28. cb({ 'status': 'failure', 'message': 'Something went wrong'});
  29. }
  30. report.resolved = true;
  31. report.save(err => {
  32. if (err) console.error(err);
  33. else {
  34. cache.pub('report.resolve', _id);
  35. cb({ status: 'success', message: 'Successfully resolved Report' });
  36. }
  37. });
  38. });
  39. }),
  40. create: hooks.loginRequired((session, data, cb, userId) => {
  41. async.waterfall([
  42. (next) => {
  43. songs.getSong(data.songId, (err, song) => {
  44. if (err) return next(err);
  45. if (!song) return next('Song does not exist in our Database');
  46. next();
  47. });
  48. },
  49. (next) => {
  50. let issues = [
  51. {
  52. name: 'Video',
  53. reasons: [
  54. 'Doesn\'t exist',
  55. 'It\'s private',
  56. 'It\'s not available in my country'
  57. ]
  58. },
  59. {
  60. name: 'Title',
  61. reasons: [
  62. 'Incorrect',
  63. 'Inappropriate'
  64. ]
  65. },
  66. {
  67. name: 'Duration',
  68. reasons: [
  69. 'Skips too soon',
  70. 'Skips too late',
  71. 'Starts too soon',
  72. 'Skips too late'
  73. ]
  74. },
  75. {
  76. name: 'Artists',
  77. reasons: [
  78. 'Incorrect',
  79. 'Inappropriate'
  80. ]
  81. },
  82. {
  83. name: 'Thumbnail',
  84. reasons: [
  85. 'Incorrect',
  86. 'Inappropriate',
  87. 'Doesn\'t exist'
  88. ]
  89. }
  90. ];
  91. for (let z = 0; z < data.issues.length; z++) {
  92. if (issues.filter(issue => { return issue.name == data.issues[z].name; }).length > 0) {
  93. for (let r = 0; r < issues.length; r++) {
  94. if (issues[r].reasons.every(reason => data.issues[z].reasons.indexOf(reason) < -1)) {
  95. return cb({ 'status': 'failure', 'message': 'Invalid data' });
  96. }
  97. }
  98. } else return cb({ 'status': 'failure', 'message': 'Invalid data' });
  99. }
  100. next();
  101. },
  102. (next) => {
  103. for (let r = 0; r < data.issues.length; r++) {
  104. if (data.issues[r].reasons.length === 0) data.issues.splice(r, 1);
  105. }
  106. next();
  107. },
  108. (next) => {
  109. data.createdBy = userId;
  110. data.createdAt = Date.now();
  111. db.models.report.create(data, next);
  112. }
  113. ], (err, report) => {
  114. if (err) return cb({ 'status': 'failure', 'message': 'Something went wrong'});
  115. else {
  116. cache.pub('report.create', report);
  117. return cb({ 'status': 'success', 'message': 'Successfully created report' });
  118. }
  119. });
  120. })
  121. };