2025-08-02 10:32:51 +02:00
|
|
|
import { get_coords, type Coordinates } from "./gps"
|
2025-08-02 03:54:26 +02:00
|
|
|
import { parseSong, parseSuggestion, type FetchError, type Song, type Suggestion } from "./types"
|
|
|
|
|
2025-08-02 10:32:51 +02:00
|
|
|
export const joinRoom = async function (roomId: string, coords: Coordinates): Promise<[FetchError | null, string]> {
|
2025-08-02 10:06:31 +02:00
|
|
|
let res = await fetch(`/api/join?room=${roomId}&lat=${coords.latitude}&lon=${coords.longitude}`)
|
|
|
|
|
|
|
|
if (res.status != 200) {
|
2025-08-02 03:54:26 +02:00
|
|
|
return [{ code: 400, message: "Cannot join the room" }, ""]
|
|
|
|
}
|
|
|
|
|
|
|
|
return [null, "test"]
|
|
|
|
}
|
|
|
|
|
2025-08-02 10:32:51 +02:00
|
|
|
export const getSuggestions = async function (roomId: string): Promise<[FetchError | null, Suggestion[]]> {
|
2025-08-02 03:54:26 +02:00
|
|
|
let resp = await fetch("/api/room/suggestions?room=" + roomId)
|
|
|
|
|
|
|
|
if (resp.status != 200) {
|
|
|
|
return [{ code: 400, message: "Failed to retrieve suggestions" }, []]
|
|
|
|
}
|
|
|
|
|
|
|
|
let json = await resp.json()
|
|
|
|
let suggestions: Suggestion[] = []
|
|
|
|
|
|
|
|
json["songs"].forEach(async (i: any) => {
|
|
|
|
suggestions.push(await parseSuggestion(i))
|
|
|
|
})
|
|
|
|
|
|
|
|
suggestions = suggestions.sort((a, b) => {
|
|
|
|
return a.upvote - b.upvote
|
|
|
|
})
|
|
|
|
|
|
|
|
return [null, suggestions]
|
|
|
|
}
|
|
|
|
|
2025-08-02 10:32:51 +02:00
|
|
|
export const getQueueSongs = async function (roomId: string): Promise<[FetchError | null, Song[], number]> {
|
2025-08-02 03:54:26 +02:00
|
|
|
let resp = await fetch("/api/queue?room=" + roomId)
|
|
|
|
if (resp.status != 200) {
|
|
|
|
return [{ code: 400, message: "Failed to load queue songs" }, [], 0]
|
|
|
|
}
|
|
|
|
|
|
|
|
let json = await resp.json()
|
|
|
|
let songs: Song[] = []
|
|
|
|
|
|
|
|
json["queue"].forEach(async (i: any) => {
|
|
|
|
songs.push(await parseSong(i))
|
|
|
|
})
|
|
|
|
|
|
|
|
let playingId = json["index"]
|
|
|
|
|
|
|
|
return [null, songs, playingId]
|
|
|
|
}
|
|
|
|
|
2025-08-02 10:32:51 +02:00
|
|
|
export const triggerPlayNext = async function (roomId: string): Promise<[FetchError | null, Song[], number]> {
|
2025-08-02 03:54:26 +02:00
|
|
|
let resp = await fetch("/api/queue/next?room=" + roomId, { method: "POST" })
|
|
|
|
|
|
|
|
if (resp.status != 200) {
|
|
|
|
return [{ code: 400, message: "Failed to trigger next song playback" }, [], 0]
|
|
|
|
}
|
|
|
|
|
|
|
|
let json = await resp.json()
|
|
|
|
|
|
|
|
let songs: Song[] = []
|
|
|
|
|
2025-08-02 05:04:30 +02:00
|
|
|
json["queue"].forEach(async (i: any) => {
|
|
|
|
songs.push(await parseSong(i))
|
|
|
|
})
|
2025-08-02 03:54:26 +02:00
|
|
|
return [null, songs, json["index"]]
|
|
|
|
}
|
2025-08-02 06:07:14 +02:00
|
|
|
|
2025-08-02 10:32:51 +02:00
|
|
|
export const getStreamingUrl = async function (uuid: string) {
|
2025-08-02 06:07:14 +02:00
|
|
|
let resp = await fetch("/api/song/audio?song=" + uuid)
|
|
|
|
|
|
|
|
let json = await resp.json()
|
|
|
|
|
|
|
|
return json["url"]
|
|
|
|
}
|