|
@@ -11,29 +11,6 @@ const SongsModule = moduleManager.modules.songs;
|
|
|
const CacheModule = moduleManager.modules.cache;
|
|
|
const ActivitiesModule = moduleManager.modules.activities;
|
|
|
|
|
|
-const reportableIssues = [
|
|
|
- {
|
|
|
- name: "Video",
|
|
|
- reasons: ["Doesn't exist", "It's private", "It's not available in my country"]
|
|
|
- },
|
|
|
- {
|
|
|
- name: "Title",
|
|
|
- reasons: ["Incorrect", "Inappropriate"]
|
|
|
- },
|
|
|
- {
|
|
|
- name: "Duration",
|
|
|
- reasons: ["Skips too soon", "Skips too late", "Starts too soon", "Skips too late"]
|
|
|
- },
|
|
|
- {
|
|
|
- name: "Artists",
|
|
|
- reasons: ["Incorrect", "Inappropriate"]
|
|
|
- },
|
|
|
- {
|
|
|
- name: "Thumbnail",
|
|
|
- reasons: ["Incorrect", "Inappropriate", "Doesn't exist"]
|
|
|
- }
|
|
|
-];
|
|
|
-
|
|
|
CacheModule.runJob("SUB", {
|
|
|
channel: "report.resolve",
|
|
|
cb: reportId => {
|
|
@@ -62,25 +39,17 @@ export default {
|
|
|
* @param {Function} cb - gets called with the result
|
|
|
*/
|
|
|
index: isAdminRequired(async function index(session, cb) {
|
|
|
- const reportModel = await DBModule.runJob(
|
|
|
- "GET_MODEL",
|
|
|
- {
|
|
|
- modelName: "report"
|
|
|
- },
|
|
|
- this
|
|
|
- );
|
|
|
+ const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
|
|
|
+
|
|
|
async.waterfall(
|
|
|
- [
|
|
|
- next => {
|
|
|
- reportModel.find({ resolved: false }).sort({ released: "desc" }).exec(next);
|
|
|
- }
|
|
|
- ],
|
|
|
+ [next => reportModel.find({ resolved: false }).sort({ released: "desc" }).exec(next)],
|
|
|
async (err, reports) => {
|
|
|
if (err) {
|
|
|
err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
|
|
|
this.log("ERROR", "REPORTS_INDEX", `Indexing reports failed. "${err}"`);
|
|
|
return cb({ status: "error", message: err });
|
|
|
}
|
|
|
+
|
|
|
this.log("SUCCESS", "REPORTS_INDEX", "Indexing reports successful.");
|
|
|
return cb({ status: "success", data: { reports } });
|
|
|
}
|
|
@@ -95,29 +64,18 @@ export default {
|
|
|
* @param {Function} cb - gets called with the result
|
|
|
*/
|
|
|
findOne: isAdminRequired(async function findOne(session, reportId, cb) {
|
|
|
- const reportModel = await DBModule.runJob(
|
|
|
- "GET_MODEL",
|
|
|
- {
|
|
|
- modelName: "report"
|
|
|
- },
|
|
|
- this
|
|
|
- );
|
|
|
- async.waterfall(
|
|
|
- [
|
|
|
- next => {
|
|
|
- reportModel.findOne({ _id: reportId }).exec(next);
|
|
|
- }
|
|
|
- ],
|
|
|
- async (err, report) => {
|
|
|
- if (err) {
|
|
|
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
|
|
|
- this.log("ERROR", "REPORTS_FIND_ONE", `Finding report "${reportId}" failed. "${err}"`);
|
|
|
- return cb({ status: "error", message: err });
|
|
|
- }
|
|
|
- this.log("SUCCESS", "REPORTS_FIND_ONE", `Finding report "${reportId}" successful.`);
|
|
|
- return cb({ status: "success", data: { report } });
|
|
|
+ const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
|
|
|
+
|
|
|
+ async.waterfall([next => reportModel.findOne({ _id: reportId }).exec(next)], async (err, report) => {
|
|
|
+ if (err) {
|
|
|
+ err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
|
|
|
+ this.log("ERROR", "REPORTS_FIND_ONE", `Finding report "${reportId}" failed. "${err}"`);
|
|
|
+ return cb({ status: "error", message: err });
|
|
|
}
|
|
|
- );
|
|
|
+
|
|
|
+ this.log("SUCCESS", "REPORTS_FIND_ONE", `Finding report "${reportId}" successful.`);
|
|
|
+ return cb({ status: "success", data: { report } });
|
|
|
+ });
|
|
|
}),
|
|
|
|
|
|
/**
|
|
@@ -128,13 +86,8 @@ export default {
|
|
|
* @param {Function} cb - gets called with the result
|
|
|
*/
|
|
|
getReportsForSong: isAdminRequired(async function getReportsForSong(session, songId, cb) {
|
|
|
- const reportModel = await DBModule.runJob(
|
|
|
- "GET_MODEL",
|
|
|
- {
|
|
|
- modelName: "report"
|
|
|
- },
|
|
|
- this
|
|
|
- );
|
|
|
+ const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
|
|
|
+
|
|
|
async.waterfall(
|
|
|
[
|
|
|
next => {
|
|
@@ -147,7 +100,7 @@ export default {
|
|
|
(_reports, next) => {
|
|
|
const reports = [];
|
|
|
for (let i = 0; i < _reports.length; i += 1) {
|
|
|
- data.push(_reports[i]._id);
|
|
|
+ reports.push(_reports[i]._id);
|
|
|
}
|
|
|
next(null, reports);
|
|
|
}
|
|
@@ -158,6 +111,7 @@ export default {
|
|
|
this.log("ERROR", "GET_REPORTS_FOR_SONG", `Indexing reports for song "${songId}" failed. "${err}"`);
|
|
|
return cb({ status: "error", message: err });
|
|
|
}
|
|
|
+
|
|
|
this.log("SUCCESS", "GET_REPORTS_FOR_SONG", `Indexing reports for song "${songId}" successful.`);
|
|
|
return cb({ status: "success", data: { reports } });
|
|
|
}
|
|
@@ -165,20 +119,15 @@ export default {
|
|
|
}),
|
|
|
|
|
|
/**
|
|
|
- * Resolves a report
|
|
|
+ * Resolves a reported issue
|
|
|
*
|
|
|
* @param {object} session - the session object automatically added by the websocket
|
|
|
* @param {string} reportId - the id of the report that is getting resolved
|
|
|
* @param {Function} cb - gets called with the result
|
|
|
*/
|
|
|
resolve: isAdminRequired(async function resolve(session, reportId, cb) {
|
|
|
- const reportModel = await DBModule.runJob(
|
|
|
- "GET_MODEL",
|
|
|
- {
|
|
|
- modelName: "report"
|
|
|
- },
|
|
|
- this
|
|
|
- );
|
|
|
+ const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
|
|
|
+
|
|
|
async.waterfall(
|
|
|
[
|
|
|
next => {
|
|
@@ -204,11 +153,14 @@ export default {
|
|
|
);
|
|
|
return cb({ status: "error", message: err });
|
|
|
}
|
|
|
+
|
|
|
CacheModule.runJob("PUB", {
|
|
|
channel: "report.resolve",
|
|
|
value: reportId
|
|
|
});
|
|
|
+
|
|
|
this.log("SUCCESS", "REPORTS_RESOLVE", `User "${session.userId}" resolved report "${reportId}".`);
|
|
|
+
|
|
|
return cb({
|
|
|
status: "success",
|
|
|
message: "Successfully resolved Report"
|
|
@@ -221,18 +173,23 @@ export default {
|
|
|
* Creates a new report
|
|
|
*
|
|
|
* @param {object} session - the session object automatically added by the websocket
|
|
|
- * @param {object} data - the object of the report data
|
|
|
+ * @param {object} report - the object of the report data
|
|
|
+ * @param {string} report.youtubeId - the youtube id of the song that is being reported
|
|
|
+ * @param {Array} report.issues - all issues reported (custom or defined)
|
|
|
* @param {Function} cb - gets called with the result
|
|
|
*/
|
|
|
- create: isLoginRequired(async function create(session, data, cb) {
|
|
|
+ create: isLoginRequired(async function create(session, report, cb) {
|
|
|
const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
|
|
|
const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
|
|
|
|
|
|
+ const { youtubeId } = report;
|
|
|
+
|
|
|
+ // properties for every report issue that is saved to db
|
|
|
+ const template = {};
|
|
|
+
|
|
|
async.waterfall(
|
|
|
[
|
|
|
- next => {
|
|
|
- songModel.findOne({ youtubeId: data.youtubeId }).exec(next);
|
|
|
- },
|
|
|
+ next => songModel.findOne({ youtubeId }).exec(next),
|
|
|
|
|
|
(song, next) => {
|
|
|
if (!song) return next("Song not found.");
|
|
@@ -245,53 +202,32 @@ export default {
|
|
|
(song, next) => {
|
|
|
if (!song) return next("Song not found.");
|
|
|
|
|
|
- delete data.youtubeId;
|
|
|
- data.song = {
|
|
|
+ template.song = {
|
|
|
_id: song._id,
|
|
|
youtubeId: song.youtubeId
|
|
|
};
|
|
|
|
|
|
- for (let z = 0; z < data.issues.length; z += 1) {
|
|
|
- if (reportableIssues.filter(issue => issue.name === data.issues[z].name).length > 0) {
|
|
|
- for (let r = 0; r < reportableIssues.length; r += 1) {
|
|
|
- if (
|
|
|
- reportableIssues[r].reasons.every(
|
|
|
- reason => data.issues[z].reasons.indexOf(reason) < -1
|
|
|
- )
|
|
|
- ) {
|
|
|
- return cb({
|
|
|
- status: "error",
|
|
|
- message: "Invalid data"
|
|
|
- });
|
|
|
- }
|
|
|
- }
|
|
|
- } else
|
|
|
- return cb({
|
|
|
- status: "error",
|
|
|
- message: "Invalid data"
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
return next(null, { title: song.title, artists: song.artists, thumbnail: song.thumbnail });
|
|
|
},
|
|
|
|
|
|
(song, next) => {
|
|
|
- const issues = [];
|
|
|
-
|
|
|
- for (let r = 0; r < data.issues.length; r += 1) {
|
|
|
- if (!data.issues[r].reasons.length <= 0) issues.push(data.issues[r]);
|
|
|
- }
|
|
|
+ template.createdBy = session.userId;
|
|
|
+ template.createdAt = Date.now();
|
|
|
|
|
|
- data.issues = issues;
|
|
|
+ return async.each(
|
|
|
+ report.issues,
|
|
|
+ (issue, next) => {
|
|
|
+ reportModel.create({ ...issue, ...template }, (err, value) => {
|
|
|
+ CacheModule.runJob("PUB", {
|
|
|
+ channel: "report.create",
|
|
|
+ value
|
|
|
+ });
|
|
|
|
|
|
- next(null, song);
|
|
|
- },
|
|
|
-
|
|
|
- (song, next) => {
|
|
|
- data.createdBy = session.userId;
|
|
|
- data.createdAt = Date.now();
|
|
|
-
|
|
|
- reportModel.create(data, (err, report) => next(err, report, song));
|
|
|
+ return next(err);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ err => next(err, report, song)
|
|
|
+ );
|
|
|
}
|
|
|
],
|
|
|
async (err, report, song) => {
|
|
@@ -300,31 +236,22 @@ export default {
|
|
|
this.log(
|
|
|
"ERROR",
|
|
|
"REPORTS_CREATE",
|
|
|
- `Creating report for "${data.song._id}" failed by user "${session.userId}". "${err}"`
|
|
|
+ `Creating report for "${template.song._id}" failed by user "${session.userId}". "${err}"`
|
|
|
);
|
|
|
return cb({ status: "error", message: err });
|
|
|
}
|
|
|
|
|
|
- CacheModule.runJob("PUB", {
|
|
|
- channel: "report.create",
|
|
|
- value: report
|
|
|
- });
|
|
|
-
|
|
|
ActivitiesModule.runJob("ADD_ACTIVITY", {
|
|
|
- userId: report.createdBy,
|
|
|
+ userId: template.createdBy,
|
|
|
type: "song__report",
|
|
|
payload: {
|
|
|
message: `Reported song <youtubeId>${song.title} by ${song.artists.join(", ")}</youtubeId>`,
|
|
|
- youtubeId: data.song.youtubeId,
|
|
|
+ youtubeId: template.song.youtubeId,
|
|
|
thumbnail: song.thumbnail
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- this.log(
|
|
|
- "SUCCESS",
|
|
|
- "REPORTS_CREATE",
|
|
|
- `User "${session.userId}" created report for "${data.youtubeId}".`
|
|
|
- );
|
|
|
+ this.log("SUCCESS", "REPORTS_CREATE", `User "${session.userId}" created report for "${youtubeId}".`);
|
|
|
|
|
|
return cb({
|
|
|
status: "success",
|