Added library mixin to YTMusic and Jellyfin

This commit is contained in:
Eclypsed
2024-06-04 22:37:19 -04:00
parent 292dc1425e
commit cb4cc1d949
24 changed files with 1017 additions and 1109 deletions
+15 -23
View File
@@ -1,5 +1,5 @@
import { fail } from '@sveltejs/kit'
import { SECRET_INTERNAL_API_KEY, YOUTUBE_API_CLIENT_SECRET } from '$env/static/private'
import { YOUTUBE_API_CLIENT_SECRET } from '$env/static/private'
import { PUBLIC_YOUTUBE_API_CLIENT_ID } from '$env/static/public'
import type { PageServerLoad, Actions } from './$types'
import { DB } from '$lib/server/db'
@@ -7,13 +7,11 @@ import { Jellyfin, JellyfinFetchError } from '$lib/server/jellyfin'
import { google } from 'googleapis'
export const load: PageServerLoad = async ({ fetch, locals }) => {
const getConnectionInfo = async (): Promise<ConnectionInfo[]> => {
const connectionInfoResponse = await fetch(`/api/users/${locals.user.id}/connections`, {
method: 'GET',
headers: { apikey: SECRET_INTERNAL_API_KEY },
}).then((response) => response.json())
return connectionInfoResponse.connections
}
const getConnectionInfo = async () =>
fetch(`/api/users/${locals.user.id}/connections`)
.then((response) => response.json() as Promise<{ connections: ConnectionInfo[] }>)
.then((data) => data.connections)
.catch(() => ({ error: 'Failed to retrieve connections' }))
return { connections: getConnectionInfo() }
}
@@ -31,19 +29,16 @@ export const actions: Actions = {
const newConnectionId = DB.addConnectionInfo({ userId: locals.user.id, type: 'jellyfin', service: { userId: authData.User.Id, serverUrl: serverUrl.toString() }, tokens: { accessToken: authData.AccessToken } })
const response = await fetch(`/api/connections?id=${newConnectionId}`, {
method: 'GET',
headers: { apikey: SECRET_INTERNAL_API_KEY },
}).then((response) => {
return response.json()
})
const newConnection = await fetch(`/api/connections?id=${newConnectionId}`)
.then((response) => response.json() as Promise<{ connections: ConnectionInfo[] }>)
.then((data) => data.connections[0])
return { newConnection: response.connections[0] }
return { newConnection }
},
youtubeMusicLogin: async ({ request, fetch, locals }) => {
const formData = await request.formData()
const { code } = Object.fromEntries(formData)
const client = new google.auth.OAuth2({ clientId: PUBLIC_YOUTUBE_API_CLIENT_ID, clientSecret: YOUTUBE_API_CLIENT_SECRET, redirectUri: 'http://localhost:5173' }) // DO NOT SHIP THIS. THE CLIENT SECRET SHOULD NOT BE MADE AVAILABLE TO USERS. MAKE A REQUEST TO THE LAZULI WEBSITE INSTEAD.
const client = new google.auth.OAuth2({ clientId: PUBLIC_YOUTUBE_API_CLIENT_ID, clientSecret: YOUTUBE_API_CLIENT_SECRET, redirectUri: 'http://localhost:5173' }) // ! DO NOT SHIP THIS. THE CLIENT SECRET SHOULD NOT BE MADE AVAILABLE TO USERS. MAKE A REQUEST TO THE LAZULI WEBSITE INSTEAD.
const { tokens } = await client.getToken(code.toString())
const youtube = google.youtube('v3')
@@ -57,14 +52,11 @@ export const actions: Actions = {
tokens: { accessToken: tokens.access_token!, refreshToken: tokens.refresh_token!, expiry: tokens.expiry_date! },
})
const response = await fetch(`/api/connections?id=${newConnectionId}`, {
method: 'GET',
headers: { apikey: SECRET_INTERNAL_API_KEY },
})
const newConnection = await fetch(`/api/connections?id=${newConnectionId}`)
.then((response) => response.json() as Promise<{ connections: ConnectionInfo[] }>)
.then((data) => data.connections[0])
const responseData = await response.json()
return { newConnection: responseData.connections[0] }
return { newConnection }
},
deleteConnection: async ({ request }) => {
const formData = await request.formData()
+16 -8
View File
@@ -14,13 +14,13 @@
import ConnectionProfile from './connectionProfile.svelte'
import { enhance } from '$app/forms'
import { PUBLIC_YOUTUBE_API_CLIENT_ID } from '$env/static/public'
import { onMount } from 'svelte'
import Loader from '$lib/components/util/loader.svelte'
export let data: PageServerData & LayoutData
let connections: ConnectionInfo[]
onMount(async () => {
connections = await data.connections
})
let errorMessage: string
data.connections.then((userConnections) => ('error' in userConnections ? (errorMessage = userConnections.error) : (connections = userConnections)))
const authenticateJellyfin: SubmitFunction = ({ formData, cancel }) => {
const { serverUrl, username, password } = Object.fromEntries(formData)
@@ -138,13 +138,21 @@
</form>
</div>
</section>
<div id="connection-profile-grid" class="grid gap-8">
{#if connections}
{#if connections}
<div id="connection-profile-grid" class="grid gap-8">
{#each connections as connectionInfo}
<ConnectionProfile {connectionInfo} submitFunction={profileActions} />
{/each}
{/if}
</div>
</div>
{:else if errorMessage}
<div class="grid h-40 place-items-center">
<span class="text-4xl">{errorMessage}</span>
</div>
{:else}
<div class="relative h-40">
<Loader size={5} />
</div>
{/if}
{#if newConnectionModal !== null}
<svelte:component this={newConnectionModal} submitFunction={authenticateJellyfin} on:close={() => (newConnectionModal = null)} />
{/if}