Browse Source

fix(News): removing a news item shouldn't require the entire news object

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 3 years ago
parent
commit
b008de5346

+ 18 - 11
backend/logic/actions/news.js

@@ -21,10 +21,10 @@ CacheModule.runJob("SUB", {
 
 CacheModule.runJob("SUB", {
 	channel: "news.remove",
-	cb: news => {
+	cb: newsId => {
 		WSModule.runJob("EMIT_TO_ROOM", {
 			room: "admin.news",
-			args: ["event:admin.news.removed", { data: { news } }]
+			args: ["event:admin.news.removed", { data: { newsId } }]
 		});
 	}
 });
@@ -60,7 +60,9 @@ export default {
 					this.log("ERROR", "NEWS_INDEX", `Indexing news failed. "${err}"`);
 					return cb({ status: "error", message: err });
 				}
+
 				this.log("SUCCESS", "NEWS_INDEX", `Indexing news successful.`, false);
+
 				return cb({ status: "success", data: { news } });
 			}
 		);
@@ -75,10 +77,11 @@ export default {
 	 */
 	async getNewsFromId(session, newsId, cb) {
 		const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
+
 		async.waterfall(
 			[
 				next => {
-					newsModel.findOne({ _id: newsId }, next);
+					newsModel.findById(newsId, next);
 				}
 			],
 			async (err, news) => {
@@ -87,7 +90,9 @@ export default {
 					this.log("ERROR", "GET_NEWS_FROM_ID", `Getting news failed. "${err}"`);
 					return cb({ status: "error", message: err });
 				}
+
 				this.log("SUCCESS", "GET_NEWS_FROM_ID", `Got news successful.`, false);
+
 				return cb({ status: "success", data: { news } });
 			}
 		);
@@ -156,25 +161,27 @@ export default {
 	 * Removes a news item
 	 *
 	 * @param {object} session - the session object automatically added by the websocket
-	 * @param {object} news - the news object
+	 * @param {object} newsId - the id of the news object we want to remove
 	 * @param {Function} cb - gets called with the result
 	 */
-	// TODO Pass in an id, not an object
-	// TODO Fix this
-	remove: isAdminRequired(async function remove(session, news, cb) {
+	remove: isAdminRequired(async function remove(session, newsId, cb) {
 		const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
-		newsModel.deleteOne({ _id: news._id }, async err => {
+
+		newsModel.deleteOne({ _id: newsId }, async err => {
 			if (err) {
 				err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 				this.log(
 					"ERROR",
 					"NEWS_REMOVE",
-					`Removing news "${news._id}" failed for user "${session.userId}". "${err}"`
+					`Removing news "${newsId}" failed for user "${session.userId}". "${err}"`
 				);
 				return cb({ status: "error", message: err });
 			}
-			CacheModule.runJob("PUB", { channel: "news.remove", value: news });
-			this.log("SUCCESS", "NEWS_REMOVE", `Removing news "${news._id}" successful by user "${session.userId}".`);
+
+			CacheModule.runJob("PUB", { channel: "news.remove", value: newsId });
+
+			this.log("SUCCESS", "NEWS_REMOVE", `Removing news "${newsId}" successful by user "${session.userId}".`);
+
 			return cb({
 				status: "success",
 				message: "Successfully removed News"

+ 4 - 4
frontend/src/pages/Admin/tabs/News.vue

@@ -31,7 +31,7 @@
 							>
 								Edit
 							</button>
-							<confirm @confirm="remove(news)">
+							<confirm @confirm="remove(news._id)">
 								<button class="button is-danger">Remove</button>
 							</confirm>
 						</td>
@@ -263,7 +263,7 @@ export default {
 		);
 
 		this.socket.on("event:admin.news.removed", res =>
-			this.removeNews(res.data.news._id)
+			this.removeNews(res.data.newsId)
 		);
 
 		if (this.socket.readyState === 1) this.init();
@@ -300,10 +300,10 @@ export default {
 					};
 			});
 		},
-		remove(news) {
+		remove(id) {
 			this.socket.dispatch(
 				"news.remove",
-				news,
+				id,
 				res => new Toast(res.message)
 			);
 		},

+ 1 - 3
frontend/src/pages/News.vue

@@ -119,9 +119,7 @@ export default {
 			}
 		});
 		this.socket.on("event:admin.news.removed", res => {
-			this.news = this.news.filter(
-				item => item._id !== res.data.news._id
-			);
+			this.news = this.news.filter(item => item._id !== res.data.newsId);
 			if (this.news.length === 0) this.noFound = true;
 		});
 	},

+ 2 - 2
frontend/src/pages/Station/Sidebar/Playlists.vue

@@ -93,9 +93,9 @@ export default {
 	mixins: [SortablePlaylists],
 	computed: {
 		currentPlaylists() {
-			if (this.station.type === "community" && this.station.partyMode) {
+			if (this.station.type === "community" && this.station.partyMode)
 				return this.partyPlaylists;
-			}
+
 			return this.includedPlaylists;
 		},
 		...mapState({

+ 3 - 6
frontend/src/pages/Station/index.vue

@@ -913,21 +913,18 @@ export default {
 		});
 
 		this.socket.on("event:privatePlaylist.selected", res => {
-			if (this.station.type === "community") {
+			if (this.station.type === "community")
 				this.station.privatePlaylist = res.data.playlistId;
-			}
 		});
 
 		this.socket.on("event:privatePlaylist.deselected", () => {
-			if (this.station.type === "community") {
+			if (this.station.type === "community")
 				this.station.privatePlaylist = null;
-			}
 		});
 
 		this.socket.on("event:partyMode.updated", res => {
-			if (this.station.type === "community") {
+			if (this.station.type === "community")
 				this.station.partyMode = res.data.partyMode;
-			}
 		});
 
 		this.socket.on("event:station.themeUpdated", res => {