Added ConnectionInfo
This commit is contained in:
16
src/app.d.ts
vendored
16
src/app.d.ts
vendored
@@ -45,6 +45,12 @@ declare global {
|
|||||||
tokens: Tokens
|
tokens: Tokens
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ConnectionInfo {
|
||||||
|
connectionId: string
|
||||||
|
serviceType: serviceType
|
||||||
|
username: string
|
||||||
|
}
|
||||||
|
|
||||||
// These Schemas should only contain general info data that is necessary for data fetching purposes.
|
// These Schemas should only contain general info data that is necessary for data fetching purposes.
|
||||||
// They are NOT meant to be stores for large amounts of data, i.e. Don't include the data for every single song the Playlist type.
|
// They are NOT meant to be stores for large amounts of data, i.e. Don't include the data for every single song the Playlist type.
|
||||||
// Big data should be fetched as needed in the app, these exist to ensure that the info necessary to fetch that data is there.
|
// Big data should be fetched as needed in the app, these exist to ensure that the info necessary to fetch that data is there.
|
||||||
@@ -111,9 +117,9 @@ declare global {
|
|||||||
tokens: JFTokens
|
tokens: JFTokens
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AccountInfo {
|
interface JFConnectionInfo extends ConnectionInfo {
|
||||||
username: string
|
serviceType: 'jellyfin'
|
||||||
servername?: string
|
servername: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AuthData {
|
interface AuthData {
|
||||||
@@ -199,8 +205,8 @@ declare global {
|
|||||||
tokens: YTTokens
|
tokens: YTTokens
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AccountInfo {
|
interface YTConnectionInfo extends ConnectionInfo {
|
||||||
username: string
|
serviceType: 'youtube-music'
|
||||||
profilePicture?: string
|
profilePicture?: string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
60
src/routes/api/users/[userId]/connectionInfo/+server.ts
Normal file
60
src/routes/api/users/[userId]/connectionInfo/+server.ts
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import type { RequestHandler } from '@sveltejs/kit'
|
||||||
|
import { Connections } from '$lib/server/users'
|
||||||
|
import { google } from 'googleapis'
|
||||||
|
|
||||||
|
const jellyfinInfo = async (connection: Jellyfin.JFConnection): Promise<Jellyfin.JFConnectionInfo> => {
|
||||||
|
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<YouTubeMusic.YTConnectionInfo> => {
|
||||||
|
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
|
||||||
|
const requestedConnectionIds = url.searchParams.get('connectionIds')?.split(',')
|
||||||
|
|
||||||
|
const connectionInfo: ConnectionInfo[] = []
|
||||||
|
|
||||||
|
const userConnections: Connection[] = requestedConnectionIds ? Array.from(requestedConnectionIds, (id) => Connections.getConnection(id)) : Connections.getUserConnections(userId)
|
||||||
|
|
||||||
|
for (const connection of userConnections) {
|
||||||
|
let info: ConnectionInfo
|
||||||
|
switch (connection.service.type) {
|
||||||
|
case 'jellyfin':
|
||||||
|
info = await jellyfinInfo(connection as Jellyfin.JFConnection)
|
||||||
|
break
|
||||||
|
case 'youtube-music':
|
||||||
|
info = await youtubeInfo(connection as YouTubeMusic.YTConnection)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
connectionInfo.push(info)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Response.json({ connectionInfo })
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ export const load: PageServerLoad = async ({ fetch, locals }) => {
|
|||||||
headers: { apikey: SECRET_INTERNAL_API_KEY },
|
headers: { apikey: SECRET_INTERNAL_API_KEY },
|
||||||
})
|
})
|
||||||
|
|
||||||
|
console.log(locals.user.id)
|
||||||
const userConnections: Connection[] = await connectionsResponse.json()
|
const userConnections: Connection[] = await connectionsResponse.json()
|
||||||
return { userConnections }
|
return { userConnections }
|
||||||
}
|
}
|
||||||
@@ -37,23 +38,10 @@ export const actions: Actions = {
|
|||||||
|
|
||||||
const authData: Jellyfin.AuthData = await jellyfinAuthResponse.json()
|
const authData: Jellyfin.AuthData = await jellyfinAuthResponse.json()
|
||||||
|
|
||||||
const userUrl = new URL(`Users/${authData.User.Id}`, serverUrl.toString()).href
|
|
||||||
const systemUrl = new URL('System/Info', serverUrl.toString()).href
|
|
||||||
|
|
||||||
const reqHeaders = new Headers({ Authorization: `MediaBrowser Token="${authData.AccessToken}"` })
|
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
const serviceData: Jellyfin.JFService = {
|
const serviceData: Jellyfin.JFService = {
|
||||||
type: 'jellyfin',
|
type: 'jellyfin',
|
||||||
userId: authData.User.Id,
|
userId: authData.User.Id,
|
||||||
// username: userData.Name,
|
|
||||||
urlOrigin: serverUrl.toString(),
|
urlOrigin: serverUrl.toString(),
|
||||||
// serverName: systemData.ServerName,
|
|
||||||
}
|
}
|
||||||
const tokenData: Jellyfin.JFTokens = {
|
const tokenData: Jellyfin.JFTokens = {
|
||||||
accessToken: authData.AccessToken,
|
accessToken: authData.AccessToken,
|
||||||
@@ -89,9 +77,7 @@ export const actions: Actions = {
|
|||||||
const serviceData: YouTubeMusic.YTService = {
|
const serviceData: YouTubeMusic.YTService = {
|
||||||
type: 'youtube-music',
|
type: 'youtube-music',
|
||||||
userId: userChannel.id as string,
|
userId: userChannel.id as string,
|
||||||
// username: userChannel.snippet?.title as string,
|
|
||||||
urlOrigin: 'https://www.googleapis.com/youtube/v3',
|
urlOrigin: 'https://www.googleapis.com/youtube/v3',
|
||||||
// profilePicture: userChannel.snippet?.thumbnails?.default?.url as string | undefined,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const newConnectionResponse = await fetch(`/api/users/${locals.user.id}/connections`, {
|
const newConnectionResponse = await fetch(`/api/users/${locals.user.id}/connections`, {
|
||||||
|
|||||||
Reference in New Issue
Block a user