Skip to content

feat: add item to queue #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
PlaySpecificList,
PlaySpecificTracks,
PreviousSong,
QueueItem,
SeekPosition,
SkipSong,
TransferPlayback,
Expand All @@ -20,6 +21,7 @@ export enum ActionId {
PlaySpecificList = 'playSpecificList',
PlaySpecificTracks = 'playSpecificTracks',
Pause = 'pause',
QueueItem = 'queueItem',
VolumeUp = 'volumeUp',
VolumeDown = 'volumeDown',
VolumeSpecific = 'volumeSpecific',
Expand Down Expand Up @@ -347,6 +349,29 @@ export function GetActionsList(executeAction: (fcn: DoAction) => Promise<void>):
})
},
},
[ActionId.QueueItem]: {
name: 'Add Track to Queue',
options: [
{
tooltip: 'Provide the ID for the track',
required: true,
type: 'textinput',
label: 'Track ID',
id: 'context_uri',
useVariables: true,
},
],
callback: async (action, context) => {
if (typeof action.options.context_uri === 'string') {
const context_uri_portion = await context.parseVariablesInString(String(action.options.context_uri))
const context_uri = `spotify:track:${context_uri_portion}`

await executeAction(async (instance, deviceId) => {
if (deviceId) await QueueItem(instance, deviceId, context_uri)
})
}
},
},
}

return actions
Expand Down
13 changes: 13 additions & 0 deletions src/api/playback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ import {
BodyParameters,
} from './util.js'

export async function addItemToQueue(
reqOptions: RequestOptionsBase,
context_uri: string,
options?: DeviceOptions,
): Promise<Response<void>> {
const params: QueryParameters = {
uri: context_uri,
}
if (options && 'deviceId' in options) params.device_id = options.deviceId

return doPostRequest(reqOptions, '/v1/me/player/queue', params)
}

export async function getMyCurrentPlaybackState(
reqOptions: RequestOptionsBase,
): Promise<Response<SpotifyApi.CurrentPlaybackResponse | undefined>> {
Expand Down
22 changes: 22 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getMyDevices, setVolume } from './api/device.js'
import {
addItemToQueue,
getMyCurrentPlaybackState,
pause,
play,
Expand Down Expand Up @@ -53,6 +54,27 @@ export async function ChangeVolume(
}
}

export async function QueueItem(
instance: SpotifyInstanceBase,
deviceId: string,
context_uri: string,
attempt = 0,
): Promise<void> {
const reqOptions = instance.getRequestOptionsBase()
if (!reqOptions) return

try {
await addItemToQueue(reqOptions, context_uri, { deviceId })
} catch (err) {
const retry = await instance.checkIfApiErrorShouldRetry(err)
if (retry && attempt <= MAX_ATTEMPTS) {
return QueueItem(instance, deviceId, context_uri, attempt + 1)
} else {
throw err
}
}
}

export async function SkipSong(instance: SpotifyInstanceBase, deviceId: string, attempt = 0): Promise<void> {
const reqOptions = instance.getRequestOptionsBase()
if (!reqOptions) return
Expand Down
Loading