diff --git a/src/app.d.ts b/src/app.d.ts index a1e9f5e..717fedc 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -49,6 +49,8 @@ declare global { connectionId: string serviceType: serviceType username: string + serverName?: string + profilePicture?: string } // These Schemas should only contain general info data that is necessary for data fetching purposes. @@ -117,23 +119,16 @@ declare global { tokens: JFTokens } - interface JFConnectionInfo extends ConnectionInfo { - serviceType: 'jellyfin' - servername: string - } - - interface AuthData { - User: { - Id: string - } - AccessToken: string - } - interface User { Name: string Id: string } + interface AuthData { + User: Jellyfin.User + AccessToken: string + } + interface System { ServerName: string } @@ -204,11 +199,6 @@ declare global { service: YTService tokens: YTTokens } - - interface YTConnectionInfo extends ConnectionInfo { - serviceType: 'youtube-music' - profilePicture?: string - } } } diff --git a/src/lib/server/users.db b/src/lib/server/users.db index b050acc..8552a2c 100644 Binary files a/src/lib/server/users.db and b/src/lib/server/users.db differ diff --git a/src/lib/server/users.ts b/src/lib/server/users.ts index d8c0625..b3248ec 100644 --- a/src/lib/server/users.ts +++ b/src/lib/server/users.ts @@ -60,10 +60,10 @@ export class Connections { static getUserConnections = (userId: string): Connection[] => { const connectionRows = db.prepare('SELECT * FROM Connections WHERE userId = ?').all(userId) as ConnectionsTableSchema[] const connections: Connection[] = [] - connectionRows.forEach((row) => { + for (const row of connectionRows) { const { id, service, tokens } = row connections.push({ id, userId, service: JSON.parse(service), tokens: JSON.parse(tokens) }) - }) + } return connections } @@ -78,4 +78,9 @@ export class Connections { const commandInfo = db.prepare('DELETE FROM Connections WHERE id = ?').run(id) if (commandInfo.changes === 0) throw new Error(`Connection with id: ${id} does not exist`) } + + static updateTokens = (id: string, tokens: Tokens): void => { + const commandInfo = db.prepare('UPDATE Connections SET tokens = ? WHERE id = ?').run(JSON.stringify(tokens), id) + if (commandInfo.changes === 0) throw new Error('Failed to update tokens') + } } diff --git a/src/lib/service-managers/jellyfin.ts b/src/lib/service-managers/jellyfin.ts index a0344ec..9b23bf3 100644 --- a/src/lib/service-managers/jellyfin.ts +++ b/src/lib/service-managers/jellyfin.ts @@ -100,4 +100,24 @@ export class Jellyfin { thumbnail, } } + + static connectionInfo = async (connection: Jellyfin.JFConnection): Promise => { + const reqHeaders = new Headers({ Authorization: `MediaBrowser Token="${connection.tokens.accessToken}"` }) + + const userUrl = new URL(`Users/${connection.service.userId}`, connection.service.urlOrigin).href + const systemUrl = new URL('System/Info', connection.service.urlOrigin).href + + const userResponse = await fetch(userUrl, { headers: reqHeaders }) + const systemResponse = await fetch(systemUrl, { headers: reqHeaders }) + + const userData: Jellyfin.User = await userResponse.json() + const systemData: Jellyfin.System = await systemResponse.json() + + return { + connectionId: connection.id, + serviceType: 'jellyfin', + username: userData.Name, + serverName: systemData.ServerName, + } + } } diff --git a/src/lib/service-managers/youtubeMusic.ts b/src/lib/service-managers/youtubeMusic.ts new file mode 100644 index 0000000..1ea399f --- /dev/null +++ b/src/lib/service-managers/youtubeMusic.ts @@ -0,0 +1,46 @@ +import { YOUTUBE_API_CLIENT_SECRET } from '$env/static/private' +import { PUBLIC_YOUTUBE_API_CLIENT_ID } from '$env/static/public' +import { Connections } from '$lib/server/users' +import { google } from 'googleapis' + +export class YouTubeMusic { + static refreshAccessToken = async (connectionId: string, refreshToken: string): Promise => { + // Again DON'T SHIP THIS, CLIENT SECRET SHOULD NOT BE EXPOSED TO USERS + const response = await fetch('https://oauth2.googleapis.com/token', { + method: 'POST', + body: JSON.stringify({ + client_id: PUBLIC_YOUTUBE_API_CLIENT_ID, + client_secret: YOUTUBE_API_CLIENT_SECRET, + refresh_token: refreshToken, + grant_type: 'refresh_token', + }), + }) + const { access_token, expires_in } = await response.json() + const newTokens: Tokens = { + accessToken: access_token, + refreshToken, + expiry: Date.now() + expires_in * 1000, + } + Connections.updateTokens(connectionId, newTokens) + return newTokens + } + + static connectionInfo = async (connection: YouTubeMusic.YTConnection): Promise => { + let accessToken = connection.tokens.accessToken + if (Date.now() > connection.tokens.expiry) { + const newTokenData = await this.refreshAccessToken(connection.id, connection.tokens.refreshToken) + accessToken = newTokenData.accessToken + } + + const youtube = google.youtube('v3') + const userChannelResponse = await youtube.channels.list({ mine: true, part: ['snippet'], access_token: accessToken }) + const userChannel = userChannelResponse.data.items![0] + + return { + connectionId: connection.id, + serviceType: connection.service.type, + username: userChannel.snippet?.title as string, + profilePicture: userChannel.snippet?.thumbnails?.default?.url as string | undefined, + } + } +} diff --git a/src/routes/api/users/[userId]/connectionInfo/+server.ts b/src/routes/api/users/[userId]/connectionInfo/+server.ts index 0a55391..749545d 100644 --- a/src/routes/api/users/[userId]/connectionInfo/+server.ts +++ b/src/routes/api/users/[userId]/connectionInfo/+server.ts @@ -1,39 +1,7 @@ import type { RequestHandler } from '@sveltejs/kit' +import { YouTubeMusic } from '$lib/service-managers/youtubeMusic' +import { Jellyfin } from '$lib/service-managers/jellyfin' import { Connections } from '$lib/server/users' -import { google } from 'googleapis' - -const jellyfinInfo = async (connection: Jellyfin.JFConnection): Promise => { - const reqHeaders = new Headers({ Authorization: `MediaBrowser Token="${connection.tokens.accessToken}"` }) - - const userUrl = new URL(`Users/${connection.service.userId}`, connection.service.urlOrigin).href - const systemUrl = new URL('System/Info', connection.service.urlOrigin).href - - const userResponse = await fetch(userUrl, { headers: reqHeaders }) - const systemResponse = await fetch(systemUrl, { headers: reqHeaders }) - - const userData: Jellyfin.User = await userResponse.json() - const systemData: Jellyfin.System = await systemResponse.json() - - return { - connectionId: connection.id, - serviceType: 'jellyfin', - username: userData.Name, - servername: systemData.ServerName, - } -} - -const youtubeInfo = async (connection: YouTubeMusic.YTConnection): Promise => { - const youtube = google.youtube('v3') - const userChannelResponse = await youtube.channels.list({ mine: true, part: ['snippet'], access_token: connection.tokens.accessToken }) - const userChannel = userChannelResponse.data.items![0] - - return { - connectionId: connection.id, - serviceType: connection.service.type, - username: userChannel.snippet?.title as string, - profilePicture: userChannel.snippet?.thumbnails?.default?.url as string | undefined, - } -} export const GET: RequestHandler = async ({ params, url }) => { const userId = params.userId as string @@ -47,10 +15,10 @@ export const GET: RequestHandler = async ({ params, url }) => { let info: ConnectionInfo switch (connection.service.type) { case 'jellyfin': - info = await jellyfinInfo(connection as Jellyfin.JFConnection) + info = await Jellyfin.connectionInfo(connection as Jellyfin.JFConnection) break case 'youtube-music': - info = await youtubeInfo(connection as YouTubeMusic.YTConnection) + info = await YouTubeMusic.connectionInfo(connection as YouTubeMusic.YTConnection) break } connectionInfo.push(info)