From 26ead2b4fe02f2f1eea893b7d679eb763ecfa896 Mon Sep 17 00:00:00 2001 From: Bevan Kay Date: Sun, 20 Oct 2024 14:30:02 +1100 Subject: [PATCH] (feat) action: add item to queue --- src/actions.ts | 25 +++++++++++++++++++++++++ src/api/playback.ts | 13 +++++++++++++ src/helpers.ts | 22 ++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/src/actions.ts b/src/actions.ts index 1a4270b..b2ec4cd 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -8,6 +8,7 @@ import { PlaySpecificList, PlaySpecificTracks, PreviousSong, + QueueItem, SeekPosition, SkipSong, TransferPlayback, @@ -20,6 +21,7 @@ export enum ActionId { PlaySpecificList = 'playSpecificList', PlaySpecificTracks = 'playSpecificTracks', Pause = 'pause', + QueueItem = 'queueItem', VolumeUp = 'volumeUp', VolumeDown = 'volumeDown', VolumeSpecific = 'volumeSpecific', @@ -347,6 +349,29 @@ export function GetActionsList(executeAction: (fcn: DoAction) => Promise): }) }, }, + [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 diff --git a/src/api/playback.ts b/src/api/playback.ts index 59a87f6..71bf2ae 100644 --- a/src/api/playback.ts +++ b/src/api/playback.ts @@ -9,6 +9,19 @@ import { BodyParameters, } from './util.js' +export async function addItemToQueue( + reqOptions: RequestOptionsBase, + context_uri: string, + options?: DeviceOptions, +): Promise> { + 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> { diff --git a/src/helpers.ts b/src/helpers.ts index 1f9dfc9..bbab4f4 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,5 +1,6 @@ import { getMyDevices, setVolume } from './api/device.js' import { + addItemToQueue, getMyCurrentPlaybackState, pause, play, @@ -53,6 +54,27 @@ export async function ChangeVolume( } } +export async function QueueItem( + instance: SpotifyInstanceBase, + deviceId: string, + context_uri: string, + attempt = 0, +): Promise { + 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 { const reqOptions = instance.getRequestOptionsBase() if (!reqOptions) return