Implement Navidrome sharing (#575)

* add share item feature

* take care of (mostly) everything

* bugfixes

* allow clicking on notification to open url

* readd the missing modal after router migration

* remove unnecessary extension

---------

Co-authored-by: Kendall Garner <17521368+kgarner7@users.noreply.github.com>
This commit is contained in:
Benjamin 2024-04-21 22:03:22 -05:00 committed by GitHub
parent 0d03b66fe5
commit cb2597d2c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 303 additions and 4 deletions

View file

@ -157,6 +157,16 @@ export const contract = c.router({
500: resultWithHeaders(ndType._response.error),
},
},
shareItem: {
body: ndType._parameters.shareItem,
method: 'POST',
path: 'share',
responses: {
200: resultWithHeaders(ndType._response.shareItem),
404: resultWithHeaders(ndType._response.error),
500: resultWithHeaders(ndType._response.error),
},
},
updatePlaylist: {
body: ndType._parameters.updatePlaylist,
method: 'PUT',

View file

@ -47,6 +47,8 @@ import {
genreListSortMap,
ServerInfo,
ServerInfoArgs,
ShareItemArgs,
ShareItemResponse,
SimilarSongsArgs,
Song,
} from '../types';
@ -484,7 +486,10 @@ const removeFromPlaylist = async (
return null;
};
// The order should be in decreasing version, as the highest version match
// will automatically consider all lower versions matched
const VERSION_INFO: Array<[string, Record<string, number[]>]> = [
['0.49.3', { [ServerFeature.SHARING_ALBUM_SONG]: [1] }],
['0.48.0', { [ServerFeature.PLAYLISTS_SMART]: [1] }],
];
@ -544,11 +549,34 @@ const getServerInfo = async (args: ServerInfoArgs): Promise<ServerInfo> => {
const features: ServerFeatures = {
lyricsMultipleStructured: !!navidromeFeatures[SubsonicExtensions.SONG_LYRICS],
playlistsSmart: !!navidromeFeatures[ServerFeature.PLAYLISTS_SMART],
sharingAlbumSong: !!navidromeFeatures[ServerFeature.SHARING_ALBUM_SONG],
};
return { features, id: apiClientProps.server?.id, version: ping.body.serverVersion! };
};
const shareItem = async (args: ShareItemArgs): Promise<ShareItemResponse> => {
const { body, apiClientProps } = args;
const res = await ndApiClient(apiClientProps).shareItem({
body: {
description: body.description,
downloadable: body.downloadable,
expires: body.expires,
resourceIds: body.resourceIds,
resourceType: body.resourceType,
},
});
if (res.status !== 200) {
throw new Error('Failed to share item');
}
return {
id: res.body.data.id,
};
};
const getSimilarSongs = async (args: SimilarSongsArgs): Promise<Song[]> => {
const { apiClientProps, query } = args;
@ -620,5 +648,6 @@ export const ndController = {
getSongList,
getUserList,
removeFromPlaylist,
shareItem,
updatePlaylist,
};

View file

@ -343,6 +343,18 @@ const removeFromPlaylistParameters = z.object({
id: z.array(z.string()),
});
const shareItem = z.object({
id: z.string(),
});
const shareItemParameters = z.object({
description: z.string(),
downloadable: z.boolean(),
expires: z.number(),
resourceIds: z.string(),
resourceType: z.string(),
});
export const ndType = {
_enum: {
albumArtistList: ndAlbumArtistListSort,
@ -361,6 +373,7 @@ export const ndType = {
genreList: genreListParameters,
playlistList: playlistListParameters,
removeFromPlaylist: removeFromPlaylistParameters,
shareItem: shareItemParameters,
songList: songListParameters,
updatePlaylist: updatePlaylistParameters,
userList: userListParameters,
@ -382,6 +395,7 @@ export const ndType = {
playlistSong,
playlistSongList,
removeFromPlaylist,
shareItem,
song,
songList,
updatePlaylist,