Skip to content
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

Bugfix/walk path user queues #85

Merged
merged 6 commits into from
Sep 26, 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
2 changes: 1 addition & 1 deletion src/firebot/effects/spotifySeekToPositionEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const SpotifySeekToPositionEffect: Firebot.EffectType<EffectParams> = {
<firebot-input
model="effect.seekPosition"
input-title="Seek position"
placeholder-text="Enter position in miliseconds"
placeholder-text="Enter position in milliseconds"
menu-position="under" />
</eos-container>
`,
Expand Down
32 changes: 13 additions & 19 deletions src/firebot/variables/queue/spotifyUserQueues.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { getTestTrackSummary, testTrigger, testUser } from "@/testData";
import { getTestTrackSummary, testTrigger } from "@/testData";
import { jest } from "@jest/globals";
import { SpotifyService } from "@/utils/spotify";
import { SpotifyUserQueueEntry } from "@/utils/spotify/player/queue";
import { SpotifyUserQueuesVariable } from "./spotifyUserQueues";

// Mocking the entire @/main module to provide the mocked spotify instance
let testUserQueues: SpotifyUserQueueEntry[];
let testUserQueues: SpotifyTrackSummary[];
jest.mock("@/main", () => ({
spotify: {
player: {
queue: {
getTracksQueuedByUser: jest.fn(),
userHasQueuedTracks: jest.fn(),
},
},
},
Expand All @@ -26,34 +26,29 @@ describe("Spotify - User Queues Replace Variable", () => {
spotify = require("@/main").spotify;
testUserQueues = [
{
position: 1,
queuePosition: 1,
queuedBy: expectedUsername,
track: getTestTrackSummary("track 1"),
skip: false,
...getTestTrackSummary("track 1"),
},
{
position: 2,
queuePosition: 2,
queuedBy: expectedUsername,
track: getTestTrackSummary("track 2"),
skip: false,
...getTestTrackSummary("track 2"),
},
{
position: 3,
queuePosition: 3,
queuedBy: expectedUsername,
track: getTestTrackSummary("track 3"),
skip: true,
...getTestTrackSummary("track 3"),
},
{
position: 4,
queuePosition: 4,
queuedBy: unexpectedUsername,
track: getTestTrackSummary("track 4"),
skip: true,
...getTestTrackSummary("track 4"),
},
{
position: 5,
queuePosition: 5,
queuedBy: unexpectedUsername,
track: getTestTrackSummary("track 5"),
skip: false,
...getTestTrackSummary("track 5"),
},
];

Expand Down Expand Up @@ -82,7 +77,6 @@ describe("Spotify - User Queues Replace Variable", () => {
undefined
);

expect(spotify.player.queue.getTracksQueuedByUser).toHaveBeenCalledTimes(1);
expect(Array.isArray(response)).toBe(true);
expect(response).toHaveLength(
testUserQueues.filter((uq) => !uq.skip).length
Expand Down
27 changes: 24 additions & 3 deletions src/firebot/variables/queue/spotifyUserQueues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,35 @@ export const SpotifyUserQueuesVariable: ReplaceVariable = {
description:
"Gets an array containing the tracks queued by the specified user",
},
{
usage: "spotifyUserQueues[$username, 0]",
description:
"Gets the first track queued by the specified user",
},
{
usage: "spotifyUserQueues[0]",
description:
"Gets the first track in the user queued tracks list",
}
],
},

evaluator: async (_trigger, subject: string = "") => {
const [username, key] = subject.split(",").map((s) => s.trim());
evaluator: async (_trigger, username?: string, subject?: string) => {
if (!username) return objectWalkPath(spotify.player.queue.userQueues, subject);

const subjectRegex = /^(\d+)(\.\S+)*$/;
let swappedSubject = false;

// Check if username should be considered subject
if (!spotify.player.queue.userHasQueuedTracks(username) && !subject && subjectRegex.test(username)) {
subject = username;
username = undefined;

swappedSubject = true;
}

let queue = spotify.player.queue.getTracksQueuedByUser(username);

return objectWalkPath(queue, key);
return objectWalkPath(queue, subject) ?? (!!subject || swappedSubject ? undefined : []);
},
};
1 change: 1 addition & 0 deletions src/types/spotify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ type SpotifyTrackSummary = {
uri: string;
queuePosition?: number;
queuedBy?: string;
skip?: boolean;
};

type SpotifyTrackSummaryWithPosition = SpotifyTrackSummary & {
Expand Down
23 changes: 16 additions & 7 deletions src/utils/spotify/player/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { SpotifyService } from "@utils/spotify";
import {
cleanUsername,
} from "@oceanity/firebot-helpers/string";
import { now } from "@/utils/time";
import { trackSummaryFromDetails } from "./track";

export type SpotifyUserQueueEntry = {
Expand Down Expand Up @@ -62,13 +61,10 @@ export class SpotifyQueueService {
return this._currentlyPlaying ?? null;
}

public get userQueues(): SpotifyUserQueueEntry[] {
public get userQueues(): SpotifyTrackSummary[] {
return this._userQueues
.filter((queue) => !queue.skip)
.map((queue, index) => ({
...queue,
position: index + 1,
}));
.map((queue, index) => this.trackSummaryFromQueueEntry(queue, index));
}

public get trackWasUserQueued(): boolean {
Expand All @@ -79,6 +75,9 @@ export class SpotifyQueueService {
return this._queuedBy;
}

public userHasQueuedTracks = (username?: string): boolean =>
!!username && this._userQueues.some((queue) => queue.queuedBy === cleanUsername(username));

public async getAsync(): Promise<SpotifyQueueResponse | null> {
try {
const response = await this.spotify.api.fetch<SpotifyQueueResponse>(
Expand Down Expand Up @@ -178,5 +177,15 @@ export class SpotifyQueueService {
}

private trackInQueue = (trackUri: string) =>
this.userQueues.some((queue) => queue.track.uri === trackUri);
this.userQueues.some((queue) => queue.uri === trackUri);

private trackSummaryFromQueueEntry = (
queue: SpotifyUserQueueEntry,
index: number
): SpotifyTrackSummary => ({
...queue.track,
queuedBy: queue.queuedBy,
queuePosition: index + 1,
skip: queue.skip,
});
}
Loading