-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create spotify state controller, spotify responds 502 error
- Loading branch information
Showing
9 changed files
with
428 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,81 @@ | ||
import type { Model } from 'mongoose' | ||
import { Group, SpotifyAuth, type User } from 'server/models' | ||
import { SpotifyService } from 'server/services' | ||
import { NotFoundError } from 'server/utils' | ||
|
||
export const assignSpotifyToGroup = async (user: User, groupId: string, spotifyEmail: string) => { | ||
const getOrError = async <T extends Model<any>>(id: string, model: T): Promise<InstanceType<T>> => { | ||
const query = await model.findById(id) | ||
if (!query) { | ||
throw new NotFoundError(`${model.name} with id ${id} not found.`) | ||
} | ||
|
||
return query | ||
} | ||
|
||
export const assignSpotifyToGroup = async ( | ||
user: User, | ||
groupId: string, | ||
spotifyEmail: string | ||
): Promise<Group> => { | ||
const auth = await SpotifyAuth.findOne({ userId: user._id.toString(), spotifyEmail }) | ||
|
||
if (!auth) | ||
throw new Error(`User ${user.email} is not connected to spotify account ${spotifyEmail}.`) | ||
|
||
const group = await Group.findById(groupId) | ||
if (!group) throw new NotFoundError(`Group with id ${groupId} not found.`) | ||
const group = await getOrError(groupId, Group) | ||
|
||
await group.updateOne({ spotifyAuthId: auth._id }, { new: true }) | ||
return group | ||
} | ||
|
||
export const getGroupSpotify = async (groupId: string) => { | ||
const group = await Group.findById(groupId) | ||
if (!group) throw new NotFoundError(`Group with id ${groupId} not found.`) | ||
const group = await getOrError(groupId, Group) | ||
|
||
const auth = await SpotifyAuth.findById(group.spotifyAuthId) | ||
if (!auth) throw new Error(`No linked Spotify accounts for group ${group.name}.`) | ||
|
||
return SpotifyService.connect(auth.spotifyEmail) | ||
} | ||
|
||
export const getGroupTrack = async (groupId: string) => { | ||
const spotify = await getGroupSpotify(groupId) | ||
return await spotify.sdk.player.getCurrentlyPlayingTrack() | ||
} | ||
|
||
export const getGroupDevices = async (groupId: string) => { | ||
const spotify = await getGroupSpotify(groupId) | ||
return await spotify.sdk.player.getAvailableDevices() | ||
} | ||
|
||
export const setGroupDefaultDevice = async (groupId: string, deviceId: string) => { | ||
const group = await getOrError(groupId, Group) | ||
group.defaultDeviceId = deviceId | ||
await group.save() | ||
|
||
return group | ||
} | ||
|
||
export const setGroupPlayerState = async ( | ||
groupId: string, | ||
state: 'play' | 'pause' | 'next' | 'previous' | ||
) => { | ||
const spotify = await getGroupSpotify(groupId) | ||
const group = await getOrError(groupId, Group) | ||
|
||
switch (state) { | ||
case 'play': | ||
await spotify.sdk.player.startResumePlayback(group.defaultDeviceId ?? '') | ||
break | ||
case 'pause': | ||
await spotify.sdk.player.pausePlayback(group.defaultDeviceId ?? '') | ||
break | ||
case 'next': | ||
await spotify.sdk.player.skipToNext(group.defaultDeviceId ?? '') | ||
break | ||
case 'previous': | ||
await spotify.sdk.player.skipToPrevious(group.defaultDeviceId ?? '') | ||
break | ||
default: | ||
throw new Error(`Cannot set player state to ${state}.`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.