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

feat(api-server): add insertPosition for addSongToQueue #2808

Merged
merged 1 commit into from
Jan 1, 2025
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
4 changes: 2 additions & 2 deletions src/plugins/api-server/backend/routes/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,8 @@ export const register = (
app.openapi(routes.queueInfo, queueInfo);

app.openapi(routes.addSongToQueue, (ctx) => {
const { videoId } = ctx.req.valid('json');
controller.addSongToQueue(videoId);
const { videoId, insertPosition } = ctx.req.valid('json');
controller.addSongToQueue(videoId, insertPosition);

ctx.status(204);
return ctx.body(null);
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/api-server/backend/scheme/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export const QueueParamsSchema = z.object({

export const AddSongToQueueSchema = z.object({
videoId: z.string(),
insertPosition: z
.enum(['INSERT_AT_END', 'INSERT_AFTER_CURRENT_VIDEO'])
.optional()
.default('INSERT_AT_END'),
});
export const MoveSongInQueueSchema = z.object({
toIndex: z.number(),
Expand Down
8 changes: 6 additions & 2 deletions src/providers/song-controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,15 @@ export default (win: BrowserWindow) => {
});
},
// Queue
addSongToQueue: (videoId: string) => {
addSongToQueue: (videoId: string, queueInsertPosition: string) => {
const videoIdValue = parseStringFromArgsType(videoId);
if (videoIdValue === null) return;

win.webContents.send('ytmd:add-to-queue', videoIdValue);
win.webContents.send(
'ytmd:add-to-queue',
videoIdValue,
queueInsertPosition,
);
},
moveSongInQueue: (
fromIndex: ArgsType<number>,
Expand Down
95 changes: 55 additions & 40 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,46 +157,61 @@ async function onApiLoaded() {
} satisfies QueueResponse);
});

window.ipcRenderer.on('ytmd:add-to-queue', (_, videoId: string) => {
const queue = document.querySelector<QueueElement>('#queue');
const app = document.querySelector<YouTubeMusicAppElement>('ytmusic-app');
if (!app) return;

const store = queue?.queue.store.store;
if (!store) return;

app.networkManager
.fetch('/music/get_queue', {
queueContextParams: store.getState().queue.queueContextParams,
queueInsertPosition: 'INSERT_AT_END',
videoIds: [videoId],
})
.then((result) => {
if (
result &&
typeof result === 'object' &&
'queueDatas' in result &&
Array.isArray(result.queueDatas)
) {
queue?.dispatch({
type: 'ADD_ITEMS',
payload: {
nextQueueItemId: store.getState().queue.nextQueueItemId,
index: store.getState().queue.items.length ?? 0,
items: result.queueDatas
.map((it) =>
typeof it === 'object' && it && 'content' in it
? it.content
: null,
)
.filter(Boolean),
shuffleEnabled: false,
shouldAssignIds: true,
},
});
}
});
});
window.ipcRenderer.on(
'ytmd:add-to-queue',
(_, videoId: string, queueInsertPosition: string) => {
const queue = document.querySelector<QueueElement>('#queue');
const app = document.querySelector<YouTubeMusicAppElement>('ytmusic-app');
if (!app) return;

const store = queue?.queue.store.store;
if (!store) return;

app.networkManager
.fetch('/music/get_queue', {
queueContextParams: store.getState().queue.queueContextParams,
queueInsertPosition,
videoIds: [videoId],
})
.then((result) => {
if (
result &&
typeof result === 'object' &&
'queueDatas' in result &&
Array.isArray(result.queueDatas)
) {
const queueItems = store.getState().queue.items;
const queueItemsLength = queueItems.length ?? 0;
queue?.dispatch({
type: 'ADD_ITEMS',
payload: {
nextQueueItemId: store.getState().queue.nextQueueItemId,
index:
queueInsertPosition === 'INSERT_AFTER_CURRENT_VIDEO'
? queueItems.findIndex(
(it) =>
(
it.playlistPanelVideoRenderer ||
it.playlistPanelVideoWrapperRenderer
?.primaryRenderer.playlistPanelVideoRenderer
)?.selected,
) + 1 || queueItemsLength
: queueItemsLength,
items: result.queueDatas
.map((it) =>
typeof it === 'object' && it && 'content' in it
? it.content
: null,
)
.filter(Boolean),
shuffleEnabled: false,
shouldAssignIds: true,
},
});
}
});
},
);
window.ipcRenderer.on(
'ytmd:move-in-queue',
(_, fromIndex: number, toIndex: number) => {
Expand Down
Loading