fix(music-together): fix unknown user

This commit is contained in:
JellyBrick
2025-05-12 02:34:10 +09:00
parent dfa427f2ed
commit 91a2eb9063
2 changed files with 68 additions and 30 deletions

View File

@ -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': {

View File

@ -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,
},
});
}