From 91a2eb906385bc7100163a5508f7e02092f4d477 Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Mon, 12 May 2025 02:34:10 +0900 Subject: [PATCH] fix(music-together): fix unknown user --- src/plugins/music-together/index.ts | 53 +++++++++++++++-------- src/plugins/music-together/queue/queue.ts | 45 +++++++++++++------ 2 files changed, 68 insertions(+), 30 deletions(-) diff --git a/src/plugins/music-together/index.ts b/src/plugins/music-together/index.ts index 2bd8cfb2..1e50d21e 100644 --- a/src/plugins/music-together/index.ts +++ b/src/plugins/music-together/index.ts @@ -165,6 +165,17 @@ export default createPlugin< if (!wait) return false; if (!this.me) this.me = getDefaultProfile(this.connection.id); + + this.profiles = {}; + this.putProfile(this.connection.id, { + id: this.connection.id, + ...this.me, + }); + + this.queue?.setOwner({ + id: this.connection.id, + ...this.me, + }); const rawItems = this.queue?.flatItems?.map( (it) => @@ -173,16 +184,11 @@ export default createPlugin< ownerId: this.connection!.id, }) satisfies VideoData, ) ?? []; - this.queue?.setOwner({ - id: this.connection.id, - ...this.me, - }); this.queue?.setVideoList(rawItems, false); this.queue?.syncQueueOwner(); this.queue?.initQueue(); this.queue?.injection(); - this.profiles = {}; this.connection.onConnections((connection) => { if (!connection) { this.api?.toastService?.show( @@ -201,10 +207,6 @@ export default createPlugin< this.putProfile(connection.peer, undefined); } }); - this.putProfile(this.connection.id, { - id: this.connection.id, - ...this.me, - }); const listener = async ( event: ConnectionEventUnion, @@ -216,11 +218,18 @@ export default createPlugin< case 'ADD_SONGS': { if (conn && this.permission === 'host-only') return; - await this.queue?.addVideos( - event.payload.videoList, - event.payload.index, + const videoList: VideoData[] = event.payload.videoList.map( + (it) => ({ + ...it, + ownerId: it.ownerId ?? conn?.peer ?? this.connection!.id, + }), ); - await this.connection?.broadcast('ADD_SONGS', event.payload); + + await this.queue?.addVideos(videoList, event.payload.index); + await this.connection?.broadcast('ADD_SONGS', { + ...event.payload, + videoList, + }); break; } case 'REMOVE_SONG': { @@ -367,7 +376,13 @@ export default createPlugin< this.ignoreChange = true; switch (event.type) { case 'ADD_SONGS': { - await this.connection?.broadcast('ADD_SONGS', event.payload); + await this.connection?.broadcast('ADD_SONGS', { + ...event.payload, + videoList: event.payload.videoList.map((it) => ({ + ...it, + ownerId: it.ownerId ?? this.connection!.id, + })), + }); await this.connection?.broadcast('SYNC_QUEUE', undefined); break; } @@ -398,10 +413,14 @@ export default createPlugin< this.ignoreChange = true; switch (event.type) { case 'ADD_SONGS': { - await this.queue?.addVideos( - event.payload.videoList, - event.payload.index, + const videoList: VideoData[] = event.payload.videoList.map( + (it) => ({ + ...it, + ownerId: it.ownerId ?? this.connection!.id, + }), ); + + await this.queue?.addVideos(videoList, event.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 5bdf2ed2..b3b83162 100644 --- a/src/plugins/music-together/queue/queue.ts +++ b/src/plugins/music-together/queue/queue.ts @@ -177,7 +177,12 @@ export class Queue { if (!items) return false; this.internalDispatch = true; - this._videoList.push(...videos); + this._videoList.push( + ...videos.map((it) => ({ + ...it, + ownerId: it.ownerId ?? this.owner?.id, + })), + ); this.queue?.dispatch({ type: 'ADD_ITEMS', payload: { @@ -286,6 +291,7 @@ export class Queue { } if (!this.internalDispatch) { + console.log('Music Together: Queue event', event); if (event.type === 'CLEAR') { this.ignoreFlag = true; } @@ -307,6 +313,12 @@ export class Queue { const index = this._videoList.length + videoList.length - 1; if (videoList.length > 0) { + this._videoList.push( + ...videoList.map((it) => ({ + ...it, + ownerId: it.ownerId ?? this.owner?.id, + })), + ); this.broadcast({ // play type: 'ADD_SONGS', @@ -330,23 +342,30 @@ export class Queue { } ).items.length === 1 ) { + const videoList = mapQueueItem( + (it) => + ({ + videoId: it!.videoId, + ownerId: this.owner!.id, + }) satisfies VideoData, + ( + event.payload! as { + items: QueueItem[]; + } + ).items, + ); + this._videoList.push( + ...videoList.map((it) => ({ + ...it, + ownerId: it.ownerId ?? this.owner?.id, + })), + ); this.broadcast({ // add playlist type: 'ADD_SONGS', payload: { // index: (event.payload as any).index, - videoList: mapQueueItem( - (it) => - ({ - videoId: it!.videoId, - ownerId: this.owner!.id, - }) satisfies VideoData, - ( - event.payload! as { - items: QueueItem[]; - } - ).items, - ), + videoList, }, }); }