123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- import async from "async";
- import { useHasPermission } from "../hooks/hasPermission";
- // eslint-disable-next-line
- import moduleManager from "../../index";
- const DBModule = moduleManager.modules.db;
- const UtilsModule = moduleManager.modules.utils;
- const WSModule = moduleManager.modules.ws;
- const CacheModule = moduleManager.modules.cache;
- const MusicBrainzModule = moduleManager.modules.musicbrainz;
- const YouTubeModule = moduleManager.modules.youtube;
- CacheModule.runJob("SUB", {
- channel: "artists.create",
- cb: artist => {
- WSModule.runJob("EMIT_TO_ROOM", {
- room: "admin.artists",
- args: ["event:admin.artists.created", { data: { artist } }]
- });
- }
- });
- CacheModule.runJob("SUB", {
- channel: "artists.remove",
- cb: artistId => {
- WSModule.runJob("EMIT_TO_ROOM", {
- room: "admin.artists",
- args: ["event:admin.artists.deleted", { data: { artistId } }]
- });
- }
- });
- CacheModule.runJob("SUB", {
- channel: "artists.update",
- cb: artist => {
- WSModule.runJob("EMIT_TO_ROOM", {
- room: "admin.artists",
- args: ["event:admin.artists.updated", { data: { artist } }]
- });
- }
- });
- export default {
- /**
- * Gets artist items, used in the admin artist page by the AdvancedTable component
- * @param {object} session - the session object automatically added by the websocket
- * @param page - the page
- * @param pageSize - the size per page
- * @param properties - the properties to return for each artist item
- * @param sort - the sort object
- * @param queries - the queries array
- * @param operator - the operator for queries
- * @param cb
- */
- getData: useHasPermission(
- "admin.view.artists",
- async function getSet(session, page, pageSize, properties, sort, queries, operator, cb) {
- async.waterfall(
- [
- next => {
- DBModule.runJob(
- "GET_DATA",
- {
- page,
- pageSize,
- properties,
- sort,
- queries,
- operator,
- modelName: "artist",
- blacklistedProperties: [],
- specialProperties: {
- createdBy: [
- {
- $addFields: {
- createdByOID: {
- $convert: {
- input: "$createdBy",
- to: "objectId",
- onError: "unknown",
- onNull: "unknown"
- }
- }
- }
- },
- {
- $lookup: {
- from: "users",
- localField: "createdByOID",
- foreignField: "_id",
- as: "createdByUser"
- }
- },
- {
- $unwind: {
- path: "$createdByUser",
- preserveNullAndEmptyArrays: true
- }
- },
- {
- $addFields: {
- createdByUsername: {
- $ifNull: ["$createdByUser.username", "unknown"]
- }
- }
- },
- {
- $project: {
- createdByOID: 0,
- createdByUser: 0
- }
- }
- ]
- },
- specialQueries: {
- createdBy: newQuery => ({
- $or: [newQuery, { createdByUsername: newQuery.createdBy }]
- })
- }
- },
- this
- )
- .then(response => {
- next(null, response);
- })
- .catch(err => {
- next(err);
- });
- }
- ],
- async (err, response) => {
- if (err && err !== true) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "ARTISTS_GET_DATA", `Failed to get data from artists. "${err}"`);
- return cb({ status: "error", message: err });
- }
- this.log("SUCCESS", "ARTISTS_GET_DATA", `Got data from artists successfully.`);
- return cb({
- status: "success",
- message: "Successfully got data from artists.",
- data: response
- });
- }
- );
- }
- ),
- /**
- * Creates an artist item
- * @param {object} session - the session object automatically added by the websocket
- * @param {object} data - the object of the artist data
- * @param {Function} cb - gets called with the result
- */
- create: useHasPermission("artists.create", async function create(session, data, cb) {
- const artistModel = await DBModule.runJob("GET_MODEL", { modelName: "artist" }, this);
- async.waterfall(
- [
- next => {
- if (data?.musicbrainzData?.id !== data?.musicbrainzIdentifier)
- return next("MusicBrainz data must match the provided identifier.");
- return next();
- },
- next => {
- data.createdBy = session.userId;
- data.createdAt = Date.now();
- artistModel.create(data, next);
- }
- ],
- async (err, artist) => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "ARTIST_CREATE", `Creating artist failed. "${err}"`);
- return cb({ status: "error", message: err });
- }
- CacheModule.runJob("PUB", { channel: "artists.create", value: artist });
- this.log("SUCCESS", "ARTIST_CREATE", `Created artist successful.`);
- return cb({
- status: "success",
- message: "Successfully created artist"
- });
- }
- );
- }),
- /**
- * Gets a artist item by id
- * @param {object} session - the session object automatically added by the websocket
- * @param {string} artistId - the artist item id
- * @param {Function} cb - gets called with the result
- */
- async getArtistFromId(session, artistId, cb) {
- const artistModel = await DBModule.runJob("GET_MODEL", { modelName: "artist" }, this);
- async.waterfall(
- [
- next => {
- artistModel.findById(artistId, next);
- }
- ],
- async (err, artist) => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log("ERROR", "GET_ARTIST_FROM_ID", `Getting artist item ${artistId} failed. "${err}"`);
- return cb({ status: "error", message: err });
- }
- this.log("SUCCESS", "GET_ARTIST_FROM_ID", `Got artist item ${artistId} successfully.`, false);
- return cb({ status: "success", data: { artist } });
- }
- );
- },
- /**
- * Updates an artist item
- * @param {object} session - the session object automatically added by the websocket
- * @param {string} artistId - the id of the artist item
- * @param {object} item - the artist item object
- * @param {Function} cb - gets called with the result
- */
- update: useHasPermission("artists.update", async function update(session, artistId, item, 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.");
- if (item?.musicbrainzData?.id !== item?.musicbrainzIdentifier)
- return next("MusicBrainz data must match the provided identifier.");
- return next();
- },
- next => {
- artistModel.updateOne({ _id: artistId }, item, { upsert: true }, err => next(err));
- }
- ],
- async err => {
- if (err) {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- this.log(
- "ERROR",
- "ARTIST_UPDATE",
- `Updating artist item "${artistId}" failed for user "${session.userId}". "${err}"`
- );
- return cb({ status: "error", message: err });
- }
- CacheModule.runJob("PUB", { channel: "artists.update", value: { ...item, _id: artistId } });
- this.log(
- "SUCCESS",
- "ARTIST_UPDATE",
- `Updated artist item "${artistId}" successful for user "${session.userId}".`
- );
- return cb({
- status: "success",
- message: "Successfully updated 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}/`,
- params: {
- fmt: "json",
- inc: "aliases"
- }
- },
- this
- );
- return cb({
- data: ArtistApiResponse
- });
- }
- ),
- getMusicbrainzRelatedUrls: useHasPermission(
- "artists.update",
- async function getMusicbrainzRelatedUrls(session, musicbrainzIdentifier, cb) {
- const ArtistApiResponse = await MusicBrainzModule.runJob(
- "API_CALL",
- {
- url: `https://musicbrainz.org/ws/2/artist/${musicbrainzIdentifier}/`,
- params: {
- fmt: "json",
- inc: "url-rels"
- }
- },
- this
- );
- return cb({
- data: ArtistApiResponse
- });
- }
- ),
- getIdFromUrl: useHasPermission("artists.update", async function getIdFromUrl(session, type, url, cb) {
- if (type === "youtube") {
- YouTubeModule.runJob("GET_CHANNEL_ID", {
- url,
- disableSearch: false
- })
- .then(({ channelId }) => {
- if (channelId) {
- cb({
- status: "success",
- channelId
- });
- } else {
- cb({
- status: "error",
- message: "Playlist id not found"
- });
- }
- })
- .catch(async err => {
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
- cb({ status: "error", message: err });
- });
- } else
- cb({
- status: "error",
- message: "Invalid type"
- });
- })
- };
|