From 0ceec9245de585f1a7ce09573c3dbe8327879bee Mon Sep 17 00:00:00 2001 From: Armin Date: Fri, 7 Nov 2025 20:23:39 +0100 Subject: [PATCH 1/2] set index after guest overwrites queue --- src/plugins/music-together/connection.ts | 5 ++- src/plugins/music-together/index.ts | 50 ++++++++++++++++++++++- src/plugins/music-together/queue/queue.ts | 7 +++- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/plugins/music-together/connection.ts b/src/plugins/music-together/connection.ts index a85ab0611c..7758fc5e2d 100644 --- a/src/plugins/music-together/connection.ts +++ b/src/plugins/music-together/connection.ts @@ -9,9 +9,11 @@ import delay from 'delay'; import type { Permission, Profile, VideoData } from './types'; export type ConnectionEventMap = { + CLEAR_QUEUE: {}; ADD_SONGS: { videoList: VideoData[]; index?: number }; REMOVE_SONG: { index: number }; MOVE_SONG: { fromIndex: number; toIndex: number }; + SET_INDEX: { index: number }; IDENTIFY: { profile: Profile } | undefined; SYNC_PROFILE: { profiles: Record } | undefined; SYNC_QUEUE: { videoList: VideoData[] } | undefined; @@ -171,9 +173,10 @@ export class Connection { public async broadcast( type: Event, payload: ConnectionEventMap[Event], + after?: ConnectionEventUnion[], ) { await Promise.all( - this.getConnections().map((conn) => conn.send({ type, payload })), + this.getConnections().map((conn) => conn.send({ type, payload, after })), ); } diff --git a/src/plugins/music-together/index.ts b/src/plugins/music-together/index.ts index 0c8dad218a..294834bd92 100644 --- a/src/plugins/music-together/index.ts +++ b/src/plugins/music-together/index.ts @@ -215,6 +215,23 @@ export default createPlugin< this.ignoreChange = true; switch (event.type) { + case 'CLEAR_QUEUE': { + if (conn && this.permission === 'host-only') { + await this.connection?.broadcast('SYNC_QUEUE', { + videoList: this.queue?.videoList ?? [], + }); + return; + } + + this.queue?.clear(); + await this.connection?.broadcast('CLEAR_QUEUE', {}); + break; + } + case 'SET_INDEX': { + this.queue?.setIndex(event.payload.index); + await this.connection?.broadcast('SET_INDEX', { index: event.payload.index }); + break; + } case 'ADD_SONGS': { if (conn && this.permission === 'host-only') { await this.connection?.broadcast('SYNC_QUEUE', { @@ -234,7 +251,13 @@ export default createPlugin< await this.connection?.broadcast('ADD_SONGS', { ...event.payload, videoList, - }); + }, event.after); + + const afterevent = event.after?.at(0); + if (afterevent?.type === 'SET_INDEX') { + this.queue?.setIndex(afterevent.payload.index); + } + break; } case 'REMOVE_SONG': { @@ -385,6 +408,14 @@ export default createPlugin< const queueListener = async (event: ConnectionEventUnion) => { this.ignoreChange = true; switch (event.type) { + case 'CLEAR_QUEUE': { + await this.connection?.broadcast('CLEAR_QUEUE', {}); + break; + } + case 'SET_INDEX': { + await this.connection?.broadcast('SET_INDEX', { index: event.payload.index }); + break; + } case 'ADD_SONGS': { await this.connection?.broadcast('ADD_SONGS', { ...event.payload, @@ -392,7 +423,7 @@ export default createPlugin< ...it, ownerId: it.ownerId ?? this.connection!.id, })), - }); + }, event.after); break; } case 'REMOVE_SONG': { @@ -420,6 +451,14 @@ export default createPlugin< const listener = async (event: ConnectionEventUnion) => { this.ignoreChange = true; switch (event.type) { + case 'CLEAR_QUEUE': { + this.queue?.clear(); + break; + } + case 'SET_INDEX': { + this.queue?.setIndex(event.payload.index); + break; + } case 'ADD_SONGS': { const videoList: VideoData[] = event.payload.videoList.map( (it) => ({ @@ -429,6 +468,13 @@ export default createPlugin< ); await this.queue?.addVideos(videoList, event.payload.index); + + const afterevent = event.after?.at(0); + if (afterevent?.type === 'SET_INDEX') { + this.queue?.setIndex(afterevent.payload.index); + } + + break; } case 'REMOVE_SONG': { diff --git a/src/plugins/music-together/queue/queue.ts b/src/plugins/music-together/queue/queue.ts index 3f6f6d0a9a..dc5c13e4dc 100644 --- a/src/plugins/music-together/queue/queue.ts +++ b/src/plugins/music-together/queue/queue.ts @@ -314,6 +314,11 @@ export class Queue { if (!this.internalDispatch) { if (event.type === 'CLEAR') { this.ignoreFlag = true; + this.broadcast({ + type: 'CLEAR_QUEUE', + payload: {}, + }) + return; } if (event.type === 'ADD_ITEMS') { if (this.ignoreFlag) { @@ -347,7 +352,7 @@ export class Queue { }, after: [ { - type: 'SYNC_PROGRESS', + type: 'SET_INDEX', payload: { index, }, From d81e23045f38b172d350c3929d81bd59385735cc Mon Sep 17 00:00:00 2001 From: HasselAssel <71652213+HasselAssel@users.noreply.github.com> Date: Fri, 7 Nov 2025 20:43:51 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/plugins/music-together/index.ts | 16 ++++++++++++---- src/plugins/music-together/queue/queue.ts | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/plugins/music-together/index.ts b/src/plugins/music-together/index.ts index 294834bd92..d9590ca5a1 100644 --- a/src/plugins/music-together/index.ts +++ b/src/plugins/music-together/index.ts @@ -229,7 +229,9 @@ export default createPlugin< } case 'SET_INDEX': { this.queue?.setIndex(event.payload.index); - await this.connection?.broadcast('SET_INDEX', { index: event.payload.index }); + await this.connection?.broadcast('SET_INDEX', { + index: event.payload.index, + }); break; } case 'ADD_SONGS': { @@ -251,7 +253,9 @@ export default createPlugin< await this.connection?.broadcast('ADD_SONGS', { ...event.payload, videoList, - }, event.after); + }, + event.after, + ); const afterevent = event.after?.at(0); if (afterevent?.type === 'SET_INDEX') { @@ -413,7 +417,9 @@ export default createPlugin< break; } case 'SET_INDEX': { - await this.connection?.broadcast('SET_INDEX', { index: event.payload.index }); + await this.connection?.broadcast('SET_INDEX', { + index: event.payload.index, + }); break; } case 'ADD_SONGS': { @@ -423,7 +429,9 @@ export default createPlugin< ...it, ownerId: it.ownerId ?? this.connection!.id, })), - }, event.after); + }, + event.after, + ); break; } case 'REMOVE_SONG': { diff --git a/src/plugins/music-together/queue/queue.ts b/src/plugins/music-together/queue/queue.ts index dc5c13e4dc..0503af7616 100644 --- a/src/plugins/music-together/queue/queue.ts +++ b/src/plugins/music-together/queue/queue.ts @@ -317,7 +317,7 @@ export class Queue { this.broadcast({ type: 'CLEAR_QUEUE', payload: {}, - }) + }); return; } if (event.type === 'ADD_ITEMS') {