From af5a742d7c20a43bab4dd98dc243bdceff16925d Mon Sep 17 00:00:00 2001 From: Sander Vocke Date: Sat, 19 Dec 2020 01:19:38 +0100 Subject: [PATCH] Spotify integration works again. Youtube music not yet. --- .../windows/manage_links/BatchLinkDialog.tsx | 3 ++- client/src/lib/saveChanges.tsx | 6 +++--- server/app.ts | 2 +- server/db/Artist.ts | 18 +++++++++--------- server/db/Integration.ts | 4 ++-- server/integrations/integrations.ts | 16 +++++++++------- 6 files changed, 26 insertions(+), 23 deletions(-) diff --git a/client/src/components/windows/manage_links/BatchLinkDialog.tsx b/client/src/components/windows/manage_links/BatchLinkDialog.tsx index 0184e90..737abe9 100644 --- a/client/src/components/windows/manage_links/BatchLinkDialog.tsx +++ b/client/src/components/windows/manage_links/BatchLinkDialog.tsx @@ -195,7 +195,8 @@ async function doLinking( await modifyFuncs[t.itemType]( t.itemId, { - storeLinks: [...item.storeLinks, candidates[0].url] + mbApi_typename: t.itemType, + storeLinks: [...item.storeLinks, candidates[0].url], } ) success = true; diff --git a/client/src/lib/saveChanges.tsx b/client/src/lib/saveChanges.tsx index ef786bc..4b3ce4b 100644 --- a/client/src/lib/saveChanges.tsx +++ b/client/src/lib/saveChanges.tsx @@ -3,7 +3,7 @@ import backendRequest from './backend/request'; export async function modifyTrack(id: number, change: serverApi.PatchTrackRequest) { const requestOpts = { - method: 'PUT', + method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(change), }; @@ -17,7 +17,7 @@ export async function modifyTrack(id: number, change: serverApi.PatchTrackReques export async function modifyArtist(id: number, change: serverApi.PatchArtistRequest) { const requestOpts = { - method: 'PUT', + method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(change), }; @@ -31,7 +31,7 @@ export async function modifyArtist(id: number, change: serverApi.PatchArtistRequ export async function modifyAlbum(id: number, change: serverApi.PatchAlbumRequest) { const requestOpts = { - method: 'PUT', + method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(change), }; diff --git a/server/app.ts b/server/app.ts index f9dab92..c9cf5f2 100644 --- a/server/app.ts +++ b/server/app.ts @@ -101,7 +101,7 @@ const SetupApp = (app: any, knex: Knex, apiBaseUrl: string) => { } // Set up integration proxies - app.use('/integrations', checkLogin(), createIntegrations(knex)); + app.use(apiBaseUrl + '/integrations', checkLogin(), createIntegrations(knex, apiBaseUrl)); // Set up auth endpoints app.post(apiBaseUrl + api.LoginEndpoint, passport.authenticate('local'), (req: any, res: any) => { diff --git a/server/db/Artist.ts b/server/db/Artist.ts index 604bf30..41a0e1c 100644 --- a/server/db/Artist.ts +++ b/server/db/Artist.ts @@ -160,21 +160,21 @@ export async function createArtist(userId: number, artist: ArtistWithRefs, knex: export async function modifyArtist(userId: number, artistId: number, artist: ArtistBaseWithRefs, knex: Knex): Promise { await knex.transaction(async (trx) => { // Start retrieving the artist itself. - const artistIdPromise: Promise = + const artistIdPromise: Promise = trx.select('id') .from('artists') .where({ 'user': userId }) .where({ id: artistId }) - .then((r: any) => (r && r[0]) ? r[0]['id'] : undefined); + .then((r: any) => (r && r[0]) ? r[0]['id'] : null); // Start retrieving albums if we are modifying those. - const albumIdsPromise: Promise = + const albumIdsPromise: Promise = artist.albumIds ? trx.select('id') .from('albums') .whereIn('id', artist.albumIds) .then((as: any) => as.map((a: any) => a['id'])) - : (async () => undefined)(); + : (async () => null)(); // Start retrieving tracks if we are modifying those. const trackIdsPromise: Promise = @@ -183,7 +183,7 @@ export async function modifyArtist(userId: number, artistId: number, artist: Art .from('tracks') .whereIn('id', artist.trackIds) .then((as: any) => as.map((a: any) => a['id'])) - : (async () => undefined)(); + : (async () => null)(); // Start retrieving tags if we are modifying those. const tagIdsPromise = @@ -192,15 +192,15 @@ export async function modifyArtist(userId: number, artistId: number, artist: Art .from('tags') .whereIn('id', artist.tagIds) .then((ts: any) => ts.map((t: any) => t['id'])) : - (async () => undefined)(); + (async () => null)(); // Wait for the requests to finish. var [oldArtist, albums, tags, tracks] = await Promise.all([artistIdPromise, albumIdsPromise, tagIdsPromise, trackIdsPromise]);; // Check that we found all objects we need. - if ((!albums || !_.isEqual(albums.sort(), (artist.albumIds || []).sort())) || - (!tags || !_.isEqual(tags.sort(), (artist.tagIds || []).sort())) || - (!tracks || !_.isEqual(tracks.sort(), (artist.trackIds || []).sort())) || + if ((albums === undefined || !_.isEqual((albums || []).sort(), (artist.albumIds || []).sort())) || + (tags === undefined || !_.isEqual((tags || []).sort(), (artist.tagIds || []).sort())) || + (tracks === undefined || !_.isEqual((tracks || []).sort(), (artist.trackIds || []).sort())) || !oldArtist) { throw makeNotFoundError(); } diff --git a/server/db/Integration.ts b/server/db/Integration.ts index cb19e0f..4ef9d00 100644 --- a/server/db/Integration.ts +++ b/server/db/Integration.ts @@ -8,7 +8,7 @@ import { makeNotFoundError } from './common'; export async function createIntegration(userId: number, integration: api.IntegrationDataWithSecret, knex: Knex): Promise { return await knex.transaction(async (trx) => { // Create the new integration. - var integration: any = { + var dbIntegration: any = { name: integration.name, user: userId, type: integration.type, @@ -16,7 +16,7 @@ export async function createIntegration(userId: number, integration: api.Integra secretDetails: JSON.stringify(integration.secretDetails), } const integrationId = (await trx('integrations') - .insert(integration) + .insert(dbIntegration) .returning('id') // Needed for Postgres )[0]; diff --git a/server/integrations/integrations.ts b/server/integrations/integrations.ts index b9988e0..1063f30 100644 --- a/server/integrations/integrations.ts +++ b/server/integrations/integrations.ts @@ -29,7 +29,7 @@ async function getSpotifyCCAuthToken(clientId: string, clientSecret: string) { return (await response).data.access_token; } -export function createIntegrations(knex: Knex) { +export function createIntegrations(knex: Knex, apiBaseUrl: string) { // This will enable the app to redirect requests like: // /integrations/5/v1/search?q=query // To the external API represented by integration 5, e.g. for spotify: @@ -41,9 +41,10 @@ export function createIntegrations(knex: Knex) { changeOrigin: true, logLevel: 'debug', pathRewrite: (path: string, req: any) => { - // Remove e.g. "/integrations/5" - console.log("Rewrite URL:", path); - return path.replace(/^\/integrations\/[0-9]+/, ''); + // Remove e.g. "/api/integrations/5" + let replaced = path.replace(new RegExp(`${apiBaseUrl}/integrations/[0-9]+/`), ''); + console.log("Rewrite URL:", path, replaced); + return replaced; } }); @@ -52,9 +53,10 @@ export function createIntegrations(knex: Knex) { changeOrigin: true, logLevel: 'debug', pathRewrite: (path: string, req: any) => { - // Remove e.g. "/integrations/5" - console.log("Rewrite URL:", path); - return path.replace(/^\/integrations\/[0-9]+/, ''); + // Remove e.g. "/api/integrations/5" + let replaced = path.replace(new RegExp(`${apiBaseUrl}/integrations/[0-9]+/`), ''); + console.log("Rewrite URL:", path, replaced); + return replaced; } })