فهرست منبع

feat: add artist actions for searching for musicbrainz artists, saving linking data, removing artists, and small other changes

Kristian Vos 17 ساعت پیش
والد
کامیت
3365a222d5
2فایلهای تغییر یافته به همراه118 افزوده شده و 3 حذف شده
  1. 117 3
      backend/logic/actions/artists.js
  2. 1 0
      backend/logic/hooks/hasPermission.js

+ 117 - 3
backend/logic/actions/artists.js

@@ -181,7 +181,10 @@ export default {
 
 				return cb({
 					status: "success",
-					message: "Successfully created artist"
+					message: "Successfully created artist",
+					data: {
+						artistId: artist._id
+					}
 				});
 			}
 		);
@@ -265,13 +268,59 @@ export default {
 		);
 	}),
 
+	/**
+	 * Deletes an artist - shouldn't be used outside of testing
+	 * @param {object} session - the session object automatically added by the websocket
+	 * @param {string} artistId - the id of the artist item
+	 * @param {Function} cb - gets called with the result
+	 */
+	remove: useHasPermission("artists.update", async function remove(session, artistId, cb) {
+		const artistModel = await DBModule.runJob("GET_MODEL", { modelName: "artist" }, this);
+
+		async.waterfall(
+			[
+				next => {
+					if (!artistId) return next("Please provide an artist item id to remove.");
+					return next();
+				},
+
+				next => {
+					artistModel.remove({ _id: artistId }, err => next(err));
+				}
+			],
+			async err => {
+				if (err) {
+					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+					this.log(
+						"ERROR",
+						"ARTIST_REMOVE",
+						`Removing artist item "${artistId}" failed for user "${session.userId}". "${err}"`
+					);
+					return cb({ status: "error", message: err });
+				}
+
+				CacheModule.runJob("PUB", { channel: "artists.remove", value: artistId });
+
+				this.log(
+					"SUCCESS",
+					"ARTIST_REMOVE",
+					`Removing artist item "${artistId}" successful for user "${session.userId}".`
+				);
+				return cb({
+					status: "success",
+					message: "Successfully removed artist item"
+				});
+			}
+		);
+	}),
+
 	getMusicbrainzArtist: useHasPermission(
 		"artists.update",
 		async function getMusicbrainzArtist(session, musicbrainzIdentifier, cb) {
 			const ArtistApiResponse = await MusicBrainzModule.runJob(
 				"API_CALL",
 				{
-					url: `https://musicbrainz.org/ws/2/artist/${musicbrainzIdentifier}/`,
+					url: `https://musicbrainz.org/ws/2/artist/${musicbrainzIdentifier}`,
 					params: {
 						fmt: "json",
 						inc: "aliases"
@@ -335,5 +384,70 @@ export default {
 				status: "error",
 				message: "Invalid type"
 			});
-	})
+	}),
+
+	saveLinkingData: useHasPermission("artists.update", async function saveLinkingData(session, artistId, data, cb) {
+		const artistModel = await DBModule.runJob("GET_MODEL", { modelName: "artist" }, this);
+
+		async.waterfall(
+			[
+				next => {
+					if (!artistId) return next("Please provide an artist item id to update.");
+					return next();
+				},
+
+				next => {
+					artistModel.updateOne({ _id: artistId }, { $set: { linkingData: data } }, err => next(err));
+				},
+
+				next => {
+					artistModel.findOne({ _id: artistId }, next);
+				}
+			],
+			async (err, artist) => {
+				if (err) {
+					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+					this.log(
+						"ERROR",
+						"ARTIST_SAVE_LINKING_DATA",
+						`Saving linking data for artist "${artistId}" failed for user "${session.userId}". "${err}"`
+					);
+					return cb({ status: "error", message: err });
+				}
+
+				CacheModule.runJob("PUB", { channel: "artists.update", value: artist });
+
+				this.log(
+					"SUCCESS",
+					"ARTIST_SAVE_LINKING_DATA",
+					`Saving linking data for artist "${artistId}" was successful for user "${session.userId}".`
+				);
+				return cb({
+					status: "success",
+					message: "Successfully saved linking data"
+				});
+			}
+		);
+	}),
+
+	searchMusicbrainzArtists: useHasPermission(
+		"artists.update",
+		async function searchMusicbrainzArtists(session, query, cb) {
+			MusicBrainzModule.runJob("SEARCH_MUSICBRAINZ_ARTISTS", {
+				query
+			})
+				.then(({ musicbrainzArtists }) => {
+					cb({
+						status: "success",
+						data: {
+							musicbrainzArtists
+						}
+					});
+				})
+				.catch(async err => {
+					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+					cb({ status: "error", message: err });
+				});
+		}
+	)
 };

+ 1 - 0
backend/logic/hooks/hasPermission.js

@@ -61,6 +61,7 @@ permissions.moderator = {
 	"songs.verify": true,
 	"artists.create": true,
 	"artists.update": true,
+	"artist.remove": true,
 	"albums.create": true,
 	"albums.update": true,
 	"stations.create.official": true,