Skip to content

Commit

Permalink
create spotify state controller, spotify responds 502 error
Browse files Browse the repository at this point in the history
  • Loading branch information
IkeHunter committed Sep 8, 2024
1 parent 81aa301 commit cec5350
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 90 deletions.
65 changes: 60 additions & 5 deletions server/controllers/groupController.ts
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}.`)
}
}
21 changes: 7 additions & 14 deletions server/docs/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,15 @@ const doc = {
description: 'Communicate with Spotify'
}
],
components: {
securitySchemes: {
Bearer: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
in: 'header',
description: 'Token used to authenticate with network.'
}
components: {},
securityDefinitions: {
Bearer: {
type: 'apiKey',
name: 'Authorization',
in: 'header',
description: 'The token for authentication into system.'
}
},
security: [
{
Bearer: []
}
],
definitions: {
Group: { name: '', ownerId: '' } as IGroup
}
Expand Down
Loading

0 comments on commit cec5350

Please sign in to comment.