Refactor musicRoutes and remove unused code

pull/3800/merge^2
Anatole Sot 3 months ago
parent fc9fb2b2d7
commit 76ca609c19

@ -668,7 +668,6 @@ components:
example: 19923-12-03
mediaInfo:
$ref: '#/components/schemas/MediaInfo'
ArtistResult:
type: object
properties:
@ -695,18 +694,6 @@ components:
type: array
items:
$ref: '#/components/schemas/ReleaseResult'
recordings:
type: array
items:
$ref: '#/components/schemas/RecordingResult'
releaseGroups:
type: array
items:
$ref: '#/components/schemas/ReleaseGroupResult'
works:
type: array
items:
$ref: '#/components/schemas/WorkResult'
gender:
type: string
area:
@ -719,27 +706,6 @@ components:
type: array
items:
type: string
WorkResult:
type: object
properties:
id:
type: string
example: 87f17f8a-c0e2-406c-a149-8c8e311bf330
mediaType:
type: string
example: work
title:
type: string
artist:
type: array
items:
$ref: '#/components/schemas/ArtistResult'
tags:
type: array
items:
type: string
RecordingResult:
type: object
properties:
@ -797,37 +763,6 @@ components:
$ref: '#/components/schemas/MediaInfo'
releaseGroupType:
type: string
ReleaseGroupResult:
type: object
properties:
id:
type: string
example: 87f17f8a-c0e2-406c-a149-8c8e311bf330
mediaType:
type: string
example: release-group
type:
type: string
enum:
- mbReleaseGroupType
posterPath:
type: string
title:
type: string
artist:
type: array
items:
$ref: '#/components/schemas/ArtistResult'
tags:
type: array
items:
type: string
mediaInfo:
$ref: '#/components/schemas/MediaInfo'
releases:
type: array
items:
$ref: '#/components/schemas/ReleaseResult'
Genre:
type: object
properties:
@ -6179,44 +6114,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ReleaseResult'
/music/release-group/{releaseGroupId}:
get:
summary: Get release group details
description: Returns full release group details in a JSON object.
tags:
- music
parameters:
- in: path
name: releaseGroupId
required: true
schema:
type: string
example: 87f17f8a-c0e2-406c-a149-8c8e311bf330
- in: query
name: full
schema:
type: boolean
example: false
default: false
- in: query
name: maxElements
schema:
type: number
example: 50
default: 25
- in: query
name: offset
schema:
type: number
example: 25
default: 0
responses:
'200':
description: Release Group details
content:
application/json:
schema:
$ref: '#/components/schemas/ReleaseGroupResult'
/media:
get:
summary: Get media

@ -504,26 +504,11 @@ class MusicBrainz extends BaseNodeBrainz {
reject(error);
} else {
const results = convertArtist(data as Artist);
results.releaseGroups = await this.getReleaseGroups(
artistId,
maxElements,
startOffset
);
results.releases = await this.getReleases(
artistId,
maxElements,
startOffset
);
results.recordings = await this.getRecordings(
artistId,
maxElements,
startOffset
);
results.works = await this.getWorks(
artistId,
maxElements,
startOffset
);
resolve(results);
}
}

@ -1,12 +1,9 @@
import MusicBrainz from '@server/api/musicbrainz';
import { MediaType } from '@server/constants/media';
import { MediaType, SecondaryType } from '@server/constants/media';
import Media from '@server/entity/Media';
import logger from '@server/logger';
import {
mapArtistResult,
mapReleaseGroupResult,
mapReleaseResult,
} from '@server/models/Search';
import type { ReleaseResult } from '@server/models/Search';
import { mapArtistResult, mapReleaseResult } from '@server/models/Search';
import { Router } from 'express';
const musicRoutes = Router();
@ -27,10 +24,56 @@ musicRoutes.get('/artist/:id', async (req, res, next) => {
const results = await mapArtistResult(artist, media);
let existingReleaseMedia: Media[] = [];
if (media) {
existingReleaseMedia =
(await Media.getChildMedia(Number(media.ratingKey) ?? 0)) ?? [];
}
let newReleases: ReleaseResult[] = await Promise.all(
existingReleaseMedia.map(async (releaseMedia) => {
return await mapReleaseResult(
{
id: <string>releaseMedia.mbId,
media_type: SecondaryType.RELEASE,
title: <string>releaseMedia.title,
artist: [],
tags: [],
},
releaseMedia
);
})
);
newReleases = newReleases.slice(offset, offset + maxElements);
for (const release of results.releases) {
release.mediaInfo = await Media.getMedia(release.id, MediaType.MUSIC);
if (newReleases.length >= maxElements) {
break;
}
if (newReleases.find((r: ReleaseResult) => r.id === release.id)) {
continue;
}
if (newReleases.find((r: ReleaseResult) => r.title === release.title)) {
if (
newReleases.find(
(r: ReleaseResult) => r.mediaInfo && !release.mediaInfo
)
) {
continue;
}
if (
newReleases.find(
(r: ReleaseResult) => !r.mediaInfo && !release.mediaInfo
)
) {
continue;
}
}
newReleases.push(release);
}
results.releases = newReleases;
return res.status(200).json(results);
} catch (e) {
logger.debug('Something went wrong retrieving artist', {
@ -67,32 +110,4 @@ musicRoutes.get('/release/:id', async (req, res, next) => {
}
});
musicRoutes.get('/release-group/:id', async (req, res, next) => {
const mb = new MusicBrainz();
try {
const releaseGroup = await mb.getReleaseGroup(req.params.id);
const media = await Media.getMedia(releaseGroup.id, MediaType.MUSIC);
const results = await mapReleaseGroupResult(releaseGroup, media);
for (const release of results.releases) {
release.mediaInfo = await Media.getMedia(release.id, MediaType.MUSIC);
}
return res.status(200).json(results);
} catch (e) {
logger.debug('Something went wrong retrieving release group', {
label: 'API',
errorMessage: e.message,
releaseGroupId: req.params.id,
});
return next({
status: 500,
message: 'Unable to retrieve release group.',
});
}
});
export default musicRoutes;

Loading…
Cancel
Save