diff --git a/packages/backend/src/server/api/stream/channel.ts b/packages/backend/src/server/api/stream/channel.ts index 84cb552369..f77a410ad2 100644 --- a/packages/backend/src/server/api/stream/channel.ts +++ b/packages/backend/src/server/api/stream/channel.ts @@ -10,6 +10,7 @@ import { isRenotePacked, isQuotePacked } from '@/misc/is-renote.js'; import type { Packed } from '@/misc/json-schema.js'; import type { JsonObject, JsonValue } from '@/misc/json-value.js'; import type Connection from './Connection.js'; +import { NoteEntityService } from '@/core/entities/NoteEntityService.js'; /** * Stream channel @@ -101,6 +102,14 @@ export default abstract class Channel { public dispose?(): void; public onMessage?(type: string, body: JsonValue): void; + + public async assignMyReaction(note: Packed<'Note'>, noteEntityService: NoteEntityService) { + if (this.user === undefined) { return; } + if (Object.keys(note.reactions).length > 0) { + const myReaction = await noteEntityService.populateMyReaction(note, this.user.id); + note.myReaction = myReaction; + } + } } export type MiChannelService = { diff --git a/packages/backend/src/server/api/stream/channels/global-timeline.ts b/packages/backend/src/server/api/stream/channels/global-timeline.ts index ed51988c21..91d1178187 100644 --- a/packages/backend/src/server/api/stream/channels/global-timeline.ts +++ b/packages/backend/src/server/api/stream/channels/global-timeline.ts @@ -55,25 +55,19 @@ class GlobalTimelineChannel extends Channel { if (this.isNoteMutedOrBlocked(note)) return; + const reactionsToFetch = []; if (this.user && isRenotePacked(note) && !isQuotePacked(note)) { - if (note.renote && Object.keys(note.renote.reactions).length > 0) { - const myRenoteReaction = await this.noteEntityService.populateMyReaction(note.renote, this.user.id); - note.renote.myReaction = myRenoteReaction; - } - if (note.renote && note.renote.reply) { - if (Object.keys(note.renote.reply.reactions).length > 0) { - const myReplyReaction = await this.noteEntityService.populateMyReaction(note.renote.reply, this.user.id); - note.renote.reply.myReaction = myReplyReaction; + if (note.renote) { + reactionsToFetch.push(this.assignMyReaction(note.renote, this.noteEntityService)); + if (note.renote.reply) { + reactionsToFetch.push(this.assignMyReaction(note.renote.reply, this.noteEntityService)); } } + } else if (this.user && note.reply) { + reactionsToFetch.push(this.assignMyReaction(note.reply, this.noteEntityService)); } - if (this.user && note.reply) { - if (Object.keys(note.reply.reactions).length > 0) { - const myReplyReaction = await this.noteEntityService.populateMyReaction(note.reply, this.user.id); - note.reply.myReaction = myReplyReaction; - } - } + await Promise.all(reactionsToFetch); this.connection.cacheNote(note); diff --git a/packages/backend/src/server/api/stream/channels/home-timeline.ts b/packages/backend/src/server/api/stream/channels/home-timeline.ts index e4fbc97a32..32d3e2a6be 100644 --- a/packages/backend/src/server/api/stream/channels/home-timeline.ts +++ b/packages/backend/src/server/api/stream/channels/home-timeline.ts @@ -79,25 +79,19 @@ class HomeTimelineChannel extends Channel { if (this.isNoteMutedOrBlocked(note)) return; + const reactionsToFetch = []; if (this.user && isRenotePacked(note) && !isQuotePacked(note)) { - if (note.renote && Object.keys(note.renote.reactions).length > 0) { - const myRenoteReaction = await this.noteEntityService.populateMyReaction(note.renote, this.user.id); - note.renote.myReaction = myRenoteReaction; - } - if (note.renote && note.renote.reply) { - if (Object.keys(note.renote.reply.reactions).length > 0) { - const myReplyReaction = await this.noteEntityService.populateMyReaction(note.renote.reply, this.user.id); - note.renote.reply.myReaction = myReplyReaction; + if (note.renote) { + reactionsToFetch.push(this.assignMyReaction(note.renote, this.noteEntityService)); + if (note.renote.reply) { + reactionsToFetch.push(this.assignMyReaction(note.renote.reply, this.noteEntityService)); } } + } else if (this.user && note.reply) { + reactionsToFetch.push(this.assignMyReaction(note.reply, this.noteEntityService)); } - if (this.user && note.reply) { - if (Object.keys(note.reply.reactions).length > 0) { - const myReplyReaction = await this.noteEntityService.populateMyReaction(note.reply, this.user.id); - note.reply.myReaction = myReplyReaction; - } - } + await Promise.all(reactionsToFetch); this.connection.cacheNote(note); diff --git a/packages/backend/src/server/api/stream/channels/hybrid-timeline.ts b/packages/backend/src/server/api/stream/channels/hybrid-timeline.ts index 00ee195013..6412e9d510 100644 --- a/packages/backend/src/server/api/stream/channels/hybrid-timeline.ts +++ b/packages/backend/src/server/api/stream/channels/hybrid-timeline.ts @@ -93,26 +93,19 @@ class HybridTimelineChannel extends Channel { } } - if (this.user && note.renoteId && !note.text) { - if (note.renote && Object.keys(note.renote.reactions).length > 0) { - console.log(note.renote.reactionAndUserPairCache); - const myRenoteReaction = await this.noteEntityService.populateMyReaction(note.renote, this.user.id); - note.renote.myReaction = myRenoteReaction; - } - if (note.renote && note.renote.reply) { - if (Object.keys(note.renote.reply.reactions).length > 0) { - const myReplyReaction = await this.noteEntityService.populateMyReaction(note.renote.reply, this.user.id); - note.renote.reply.myReaction = myReplyReaction; + const reactionsToFetch = []; + if (this.user && isRenotePacked(note) && !isQuotePacked(note)) { + if (note.renote) { + reactionsToFetch.push(this.assignMyReaction(note.renote, this.noteEntityService)); + if (note.renote.reply) { + reactionsToFetch.push(this.assignMyReaction(note.renote.reply, this.noteEntityService)); } } + } else if (this.user && note.reply) { + reactionsToFetch.push(this.assignMyReaction(note.reply, this.noteEntityService)); } - if (this.user && note.reply) { - if (Object.keys(note.reply.reactions).length > 0) { - const myReplyReaction = await this.noteEntityService.populateMyReaction(note.reply, this.user.id); - note.reply.myReaction = myReplyReaction; - } - } + await Promise.all(reactionsToFetch); this.connection.cacheNote(note); diff --git a/packages/backend/src/server/api/stream/channels/local-timeline.ts b/packages/backend/src/server/api/stream/channels/local-timeline.ts index 09708b4cb2..588f067330 100644 --- a/packages/backend/src/server/api/stream/channels/local-timeline.ts +++ b/packages/backend/src/server/api/stream/channels/local-timeline.ts @@ -65,25 +65,19 @@ class LocalTimelineChannel extends Channel { if (this.isNoteMutedOrBlocked(note)) return; + const reactionsToFetch = []; if (this.user && isRenotePacked(note) && !isQuotePacked(note)) { - if (note.renote && Object.keys(note.renote.reactions).length > 0) { - const myRenoteReaction = await this.noteEntityService.populateMyReaction(note.renote, this.user.id); - note.renote.myReaction = myRenoteReaction; - } - if (note.renote && note.renote.reply) { - if (Object.keys(note.renote.reply.reactions).length > 0) { - const myReplyReaction = await this.noteEntityService.populateMyReaction(note.renote.reply, this.user.id); - note.renote.reply.myReaction = myReplyReaction; + if (note.renote) { + reactionsToFetch.push(this.assignMyReaction(note.renote, this.noteEntityService)); + if (note.renote.reply) { + reactionsToFetch.push(this.assignMyReaction(note.renote.reply, this.noteEntityService)); } } + } else if (this.user && note.reply) { + reactionsToFetch.push(this.assignMyReaction(note.reply, this.noteEntityService)); } - if (this.user && note.reply) { - if (Object.keys(note.reply.reactions).length > 0) { - const myReplyReaction = await this.noteEntityService.populateMyReaction(note.reply, this.user.id); - note.reply.myReaction = myReplyReaction; - } - } + await Promise.all(reactionsToFetch); this.connection.cacheNote(note);