2024-05-28 00:46:34 -04:00
|
|
|
import { DB } from './db'
|
2024-04-05 02:00:17 -04:00
|
|
|
import { Jellyfin } from './jellyfin'
|
|
|
|
|
import { YouTubeMusic } from './youtube-music'
|
2024-04-03 23:28:38 -04:00
|
|
|
|
2024-05-28 00:46:34 -04:00
|
|
|
const constructConnection = (connectionInfo: ReturnType<typeof DB.getConnectionInfo>): Connection | undefined => {
|
|
|
|
|
if (!connectionInfo) return undefined
|
|
|
|
|
|
2024-04-03 23:28:38 -04:00
|
|
|
const { id, userId, type, service, tokens } = connectionInfo
|
2024-03-24 16:03:31 -04:00
|
|
|
switch (type) {
|
|
|
|
|
case 'jellyfin':
|
2024-04-05 02:00:17 -04:00
|
|
|
return new Jellyfin(id, userId, service.userId, service.serverUrl, tokens.accessToken)
|
2024-03-24 16:03:31 -04:00
|
|
|
case 'youtube-music':
|
2024-04-05 02:00:17 -04:00
|
|
|
return new YouTubeMusic(id, userId, service.userId, tokens.accessToken, tokens.refreshToken, tokens.expiry)
|
2024-03-24 16:03:31 -04:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-28 00:46:34 -04:00
|
|
|
function getConnection(id: string): Connection | undefined {
|
|
|
|
|
return constructConnection(DB.getConnectionInfo(id))
|
2024-03-24 16:03:31 -04:00
|
|
|
}
|
|
|
|
|
|
2024-05-28 00:46:34 -04:00
|
|
|
const getUserConnections = (userId: string): Connection[] | undefined => {
|
|
|
|
|
return DB.getUserConnectionInfo(userId)?.map((info) => constructConnection(info)!)
|
2024-03-24 16:03:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const Connections = {
|
2024-05-28 00:46:34 -04:00
|
|
|
getConnection,
|
2024-03-24 16:03:31 -04:00
|
|
|
getUserConnections,
|
|
|
|
|
}
|