Browse Source

Added official song searching to manage station modal

Kristian Vos 3 years ago
parent
commit
d231712ffa

+ 41 - 0
backend/logic/actions/songs.js

@@ -567,6 +567,47 @@ export default {
 	// 	);
 	// }),
 
+	/**
+	 * Searches through official songs
+	 *
+	 * @param {object} session - the session object automatically added by the websocket
+	 * @param {string} query - the query
+	 * @param {Function} cb - gets called with the result
+	 */
+	searchOfficial: isLoginRequired(async function searchOfficial(session, query, cb) {
+		async.waterfall(
+			[
+				next => {
+					if ((!query && query !== "") || typeof query !== "string") next("Invalid query.");
+					else next();
+				},
+
+				next => {
+					SongsModule.runJob("SEARCH", {
+						query,
+						includeVerified: true,
+						trimmed: true
+					})
+						.then(response => {
+							next(null, response.songs);
+						})
+						.catch(err => {
+							next(err);
+						});
+				}
+			],
+			async (err, songs) => {
+				if (err) {
+					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+					this.log("ERROR", "SONGS_SEARCH_OFFICIAL", `Searching songs failed. "${err}"`);
+					return cb({ status: "error", message: err });
+				}
+				this.log("SUCCESS", "SONGS_SEARCH_OFFICIAL", "Searching songs successful.");
+				return cb({ status: "success", data: { songs } });
+			}
+		);
+	}),
+
 	/**
 	 * Requests a song
 	 *

+ 69 - 0
backend/logic/songs.js

@@ -407,6 +407,75 @@ class _SongsModule extends CoreClass {
 		);
 	}
 
+	/**
+	 * Searches through songs
+	 *
+	 * @param {object} payload - object that contains the payload
+	 * @param {string} payload.query - the query
+	 * @param {string} payload.includeHidden - include hidden songs
+	 * @param {string} payload.includeUnverified - include unverified songs
+	 * @param {string} payload.includeVerified - include verified songs
+	 * @param {string} payload.trimmed - include trimmed songs
+	 * @returns {Promise} - returns promise (reject, resolve)
+	 */
+	SEARCH(payload) {
+		return new Promise((resolve, reject) =>
+			async.waterfall(
+				[
+					next => {
+						const statuses = [];
+						if (payload.includeHidden) statuses.push("hidden");
+						if (payload.includeUnverified) statuses.push("unverified");
+						if (payload.includeVerified) statuses.push("verified");
+						if (statuses.length === 0) return next("No statuses have been included.");
+
+						const filterArray = [
+							{
+								title: new RegExp(`${payload.query}`, "i"),
+								status: { $in: statuses }
+							},
+							{
+								artists: new RegExp(`${payload.query}`, "i"),
+								status: { $in: statuses }
+							}
+						];
+
+						return next(null, filterArray);
+					},
+
+					(filterArray, next) => {
+						SongsModule.SongModel.find({ $or: filterArray }, next);
+					},
+
+					(songs, next) => {
+						if (songs.length === 0) next("No songs found");
+						else if (payload.trimmed) {
+							next(
+								null,
+								songs.map(song => {
+									const { _id, youtubeId, title, artists, thumbnail, duration, status } = song;
+									return {
+										_id,
+										youtubeId,
+										title,
+										artists,
+										thumbnail,
+										duration,
+										status
+									};
+								})
+							);
+						} else next(null, songs);
+					}
+				],
+				(err, songs) => {
+					if (err && err !== true) return reject(new Error(err));
+					return resolve({ songs });
+				}
+			)
+		);
+	}
+
 	/**
 	 * Recalculates dislikes and likes for a song
 	 *

+ 0 - 1
frontend/src/components/modals/ManageStation/Tabs/Playlists.vue

@@ -491,7 +491,6 @@ export default {
 		},
 		searchForPlaylists() {
 			const { query } = this.search;
-			console.log(111, query);
 			const action =
 				this.station.type === "official"
 					? "playlists.searchOfficial"

+ 20 - 0
frontend/src/components/modals/ManageStation/Tabs/Search.vue

@@ -8,6 +8,8 @@
 						class="input"
 						type="text"
 						placeholder="Enter your song query here..."
+						v-model="musareSongsQuery"
+						@keyup.enter="searchForMusareSongs()"
 					/>
 				</p>
 				<p class="control">
@@ -104,6 +106,11 @@ export default {
 		SearchQueryItem
 	},
 	mixins: [SearchYoutube],
+	data() {
+		return {
+			musareSongsQuery: ""
+		};
+	},
 	computed: {
 		...mapState("modals/manageStation", {
 			station: state => state.station,
@@ -143,6 +150,19 @@ export default {
 					}
 				});
 			}
+		},
+		searchForMusareSongs() {
+			const query = this.musareSongsQuery;
+
+			this.socket.dispatch("songs.searchOfficial", query, res => {
+				console.log(111, res);
+				if (res.status === "success") {
+					// this.search.results = res.data.playlists;
+				} else if (res.status === "error") {
+					// this.search.results = [];
+					new Toast(res.message);
+				}
+			});
 		}
 	}
 };