2024-02-03 02:47:23 -05:00
|
|
|
import type { RequestHandler } from '@sveltejs/kit'
|
2024-06-21 03:35:00 -04:00
|
|
|
import { buildUserConnections } from '$lib/server/api-helper'
|
2024-02-03 02:47:23 -05:00
|
|
|
|
|
|
|
|
// This is temporary functionally for the sake of developing the app.
|
2024-02-04 01:01:37 -05:00
|
|
|
// In the future will implement more robust algorithm for offering recommendations
|
2024-03-24 16:03:31 -04:00
|
|
|
export const GET: RequestHandler = async ({ params }) => {
|
2024-06-21 03:35:00 -04:00
|
|
|
const userConnections = await buildUserConnections(params.userId!).catch(() => null)
|
2024-05-28 00:46:34 -04:00
|
|
|
if (!userConnections) return new Response('Invalid user id', { status: 400 })
|
|
|
|
|
|
2024-06-21 03:35:00 -04:00
|
|
|
const getRecommendations = (connection: Connection) =>
|
|
|
|
|
connection.getRecommendations().catch((reason) => {
|
|
|
|
|
console.log(`Failed to fetch recommendations: ${reason}`)
|
|
|
|
|
return null
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const recommendations = (await Promise.all(userConnections.map(getRecommendations))).flat().filter((recommendation): recommendation is Song | Album | Artist | Playlist => recommendation?.id !== undefined)
|
2024-02-04 01:01:37 -05:00
|
|
|
|
|
|
|
|
return Response.json({ recommendations })
|
2024-02-03 02:47:23 -05:00
|
|
|
}
|