瀏覽代碼

refactor(Reports): db objects should contain all issues instead of splitting per issue

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 3 年之前
父節點
當前提交
c01ab8ab04

+ 19 - 26
backend/logic/actions/reports.js

@@ -184,9 +184,6 @@ export default {
 
 		const { youtubeId } = report;
 
-		// properties for every report issue that is saved to db
-		const template = {};
-
 		async.waterfall(
 			[
 				next => songModel.findOne({ youtubeId }).exec(next),
@@ -202,7 +199,8 @@ export default {
 				(song, next) => {
 					if (!song) return next("Song not found.");
 
-					template.song = {
+					delete report.youtubeId;
+					report.song = {
 						_id: song._id,
 						youtubeId: song.youtubeId
 					};
@@ -210,47 +208,42 @@ export default {
 					return next(null, { title: song.title, artists: song.artists, thumbnail: song.thumbnail });
 				},
 
-				(song, next) => {
-					template.createdBy = session.userId;
-					template.createdAt = Date.now();
-
-					return async.each(
-						report.issues,
-						(issue, next) => {
-							reportModel.create({ ...issue, ...template }, (err, value) => {
-								CacheModule.runJob("PUB", {
-									channel: "report.create",
-									value
-								});
-
-								return next(err);
-							});
+				(song, next) =>
+					reportModel.create(
+						{
+							createdBy: session.userId,
+							createdAt: Date.now(),
+							...report
 						},
-						err => next(err, report, song)
-					);
-				}
+						err => next(err, song)
+					)
 			],
-			async (err, report, song) => {
+			async (err, song) => {
 				if (err) {
 					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 					this.log(
 						"ERROR",
 						"REPORTS_CREATE",
-						`Creating report for "${template.song._id}" failed by user "${session.userId}". "${err}"`
+						`Creating report for "${report.song._id}" failed by user "${session.userId}". "${err}"`
 					);
 					return cb({ status: "error", message: err });
 				}
 
 				ActivitiesModule.runJob("ADD_ACTIVITY", {
-					userId: template.createdBy,
+					userId: report.createdBy,
 					type: "song__report",
 					payload: {
 						message: `Reported song <youtubeId>${song.title} by ${song.artists.join(", ")}</youtubeId>`,
-						youtubeId: template.song.youtubeId,
+						youtubeId: report.song.youtubeId,
 						thumbnail: song.thumbnail
 					}
 				});
 
+				CacheModule.runJob("PUB", {
+					channel: "report.create",
+					report
+				});
+
 				this.log("SUCCESS", "REPORTS_CREATE", `User "${session.userId}" created report for "${youtubeId}".`);
 
 				return cb({

+ 1 - 1
backend/logic/db/index.js

@@ -11,7 +11,7 @@ const REQUIRED_DOCUMENT_VERSIONS = {
 	playlist: 4,
 	punishment: 1,
 	queueSong: 1,
-	report: 3,
+	report: 4,
 	song: 5,
 	station: 5,
 	user: 3

+ 11 - 3
backend/logic/db/schemas/report.js

@@ -4,9 +4,17 @@ export default {
 		_id: { type: String, required: true },
 		youtubeId: { type: String, required: true }
 	},
-	category: { type: String, enum: ["custom", "video", "title", "duration", "artists", "thumbnail"], required: true },
-	info: { type: String, required: true },
+	issues: [
+		{
+			category: {
+				type: String,
+				enum: ["custom", "video", "title", "duration", "artists", "thumbnail"],
+				required: true
+			},
+			info: { type: String, required: true }
+		}
+	],
 	createdBy: { type: String, required: true },
 	createdAt: { type: Date, default: Date.now, required: true },
-	documentVersion: { type: Number, default: 3, required: true }
+	documentVersion: { type: Number, default: 4, required: true }
 };

+ 2 - 2
frontend/src/components/modals/Report.vue

@@ -69,7 +69,7 @@
 								<button
 									class="button tab-actionable-button "
 									content="Add an issue that isn't listed"
-									v-tippy="{ theme: 'info' }"
+									v-tippy
 									@click="customIssues.push('')"
 								>
 									<i class="material-icons icon-with-button"
@@ -98,7 +98,7 @@
 									<button
 										class="button is-danger"
 										content="Remove custom issue"
-										v-tippy="{ theme: 'info' }"
+										v-tippy
 										@click="customIssues.splice(index, 1)"
 									>
 										<i class="material-icons">

+ 14 - 7
frontend/src/components/modals/ViewReport.vue

@@ -40,13 +40,16 @@
 					</span>
 					<br />
 					<span>
-						<strong>Category:</strong>
-						{{ report.category }}
-					</span>
-					<br />
-					<span>
-						<strong>Info:</strong>
-						{{ report.info }}
+						<strong>Issues:</strong>
+						<ul id="issues">
+							<li
+								v-for="(issue, index) in report.issues"
+								:key="index"
+							>
+								<strong> {{ issue.category }}:</strong>
+								{{ issue.info }}
+							</li>
+						</ul>
 					</span>
 				</div>
 			</article>
@@ -135,4 +138,8 @@ export default {
 	display: flex;
 	margin-bottom: 20px;
 }
+
+#issues {
+	list-style: inside;
+}
 </style>

+ 15 - 7
frontend/src/pages/Admin/tabs/Reports.vue

@@ -8,8 +8,7 @@
 						<td>Song ID</td>
 						<td>Author</td>
 						<td>Time of report</td>
-						<td>Category</td>
-						<td>Info</td>
+						<td>Issues</td>
 						<td>Options</td>
 					</tr>
 				</thead>
@@ -42,10 +41,15 @@
 							>
 						</td>
 						<td>
-							<span>{{ report.category }}</span>
-						</td>
-						<td>
-							<span>{{ report.info }}</span>
+							<ul>
+								<li
+									v-for="(issue, index) in report.issues"
+									:key="index"
+								>
+									<strong> {{ issue.category }}:</strong>
+									{{ issue.info }}
+								</li>
+							</ul>
 						</td>
 						<td>
 							<a
@@ -113,6 +117,7 @@ export default {
 		});
 
 		this.socket.on("event:admin.report.resolved", res => {
+			console.log("report resolved event", res);
 			this.reports = this.reports.filter(report => {
 				return report._id !== res.data.reportId;
 			});
@@ -139,7 +144,6 @@ export default {
 			this.socket.dispatch("apis.joinAdminRoom", "reports", () => {});
 		},
 		view(report) {
-			// this.viewReport(report);
 			this.viewingReportId = report._id;
 			this.openModal("viewReport");
 		},
@@ -193,4 +197,8 @@ td {
 	max-width: 10vw;
 	vertical-align: middle;
 }
+
+li {
+	list-style: inside;
+}
 </style>