From c3f2a9b2337b3809e208ad213fa1734ca609c5bc Mon Sep 17 00:00:00 2001 From: caipira113 Date: Sat, 23 Sep 2023 01:44:04 +0900 Subject: [PATCH] fix(backend): Troubleshoot Messaging Not Working --- packages/backend/src/core/MessagingService.ts | 24 +++++++++++++++---- .../backend/src/core/NoteCreateService.ts | 6 ++--- .../src/core/activitypub/ApRendererService.ts | 2 +- .../core/activitypub/models/ApNoteService.ts | 2 +- .../endpoints/messaging/messages/create.ts | 4 ++-- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/core/MessagingService.ts b/packages/backend/src/core/MessagingService.ts index 4676a4bdc3..0bc0606b3f 100644 --- a/packages/backend/src/core/MessagingService.ts +++ b/packages/backend/src/core/MessagingService.ts @@ -23,6 +23,9 @@ import { ApRendererService } from '@/core/activitypub/ApRendererService.js'; import { MessagingMessageEntityService } from '@/core/entities/MessagingMessageEntityService.js'; import { PushNotificationService } from '@/core/PushNotificationService.js'; import { bindThis } from '@/decorators.js'; +import type { UserProfilesRepository } from "@/models/_.js"; +import {IMentionedRemoteUsers} from '@/models/Note.js'; +import { NoteEntityService } from '@/core/entities/NoteEntityService.js'; @Injectable() export class MessagingService { @@ -33,6 +36,9 @@ export class MessagingService { @Inject(DI.usersRepository) private usersRepository: UsersRepository, + @Inject(DI.userProfilesRepository) + private userProfilesRepository: UserProfilesRepository, + @Inject(DI.messagingMessagesRepository) private messagingMessagesRepository: MessagingMessagesRepository, @@ -42,6 +48,7 @@ export class MessagingService { @Inject(DI.mutingsRepository) private mutingsRepository: MutingsRepository, + private noteEntityService: NoteEntityService, private userEntityService: UserEntityService, private messagingMessageEntityService: MessagingMessageEntityService, private idService: IdService, @@ -53,10 +60,12 @@ export class MessagingService { } @bindThis - public async createMessage(user: { id: MiUser['id']; host: MiUser['host']; }, recipientUser: MiUser | undefined, recipientGroup: MiUserGroup | undefined, text: string | null | undefined, file: MiDriveFile | null, uri?: string) { + public async createMessage(user: { id: MiUser['id']; host: MiUser['host']; }, recipientUser: MiUser | null, recipientGroup: MiUserGroup | null, text: string | null | undefined, file: MiDriveFile | null, uri?: string) { + const data = new Date(); + const message = { - id: this.idService.genId(), - createdAt: new Date(), + id: this.idService.genId(data), + createdAt: data, fileId: file ? file.id : null, recipientId: recipientUser ? recipientUser.id : null, groupId: recipientGroup ? recipientGroup.id : null, @@ -125,6 +134,10 @@ export class MessagingService { }, 2000); if (recipientUser && this.userEntityService.isLocalUser(user) && this.userEntityService.isRemoteUser(recipientUser)) { + const profiles = await this.userProfilesRepository.findBy({ userId: In([recipientUser.id]) }); + const profile = profiles.find(p => p.userId === recipientUser.id); + const url = profile != null ? profile.url : null; + const note = { id: message.id, createdAt: message.createdAt, @@ -132,12 +145,15 @@ export class MessagingService { text: message.text, userId: message.userId, visibility: 'specified', + emojis: [{}], mentions: [recipientUser].map(u => u.id), mentionedRemoteUsers: JSON.stringify([recipientUser].map(u => ({ uri: u.uri, + url: url, username: u.username, host: u.host, - }))), + } as IMentionedRemoteUsers[0] + ))), } as MiNote; const activity = this.apRendererService.addContext(this.apRendererService.renderCreate(await this.apRendererService.renderNote(note, false, true), note)); diff --git a/packages/backend/src/core/NoteCreateService.ts b/packages/backend/src/core/NoteCreateService.ts index 347b7397eb..8e9657570c 100644 --- a/packages/backend/src/core/NoteCreateService.ts +++ b/packages/backend/src/core/NoteCreateService.ts @@ -659,7 +659,7 @@ export class NoteCreateService implements OnApplicationShutdown { //#region AP deliver if (this.userEntityService.isLocalUser(user)) { - (async () => { + await (async () => { const noteActivity = await this.renderNoteOrRenoteActivity(data, note); const dm = this.apDeliverManagerService.createDeliverManager(user, noteActivity); @@ -670,13 +670,13 @@ export class NoteCreateService implements OnApplicationShutdown { // 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送 if (data.reply && data.reply.userHost !== null) { - const u = await this.usersRepository.findOneBy({ id: data.reply.userId }); + const u = await this.usersRepository.findOneBy({id: data.reply.userId}); if (u && this.userEntityService.isRemoteUser(u)) dm.addDirectRecipe(u); } // 投稿がRenoteかつ投稿者がローカルユーザーかつRenote元の投稿の投稿者がリモートユーザーなら配送 if (data.renote && data.renote.userHost !== null) { - const u = await this.usersRepository.findOneBy({ id: data.renote.userId }); + const u = await this.usersRepository.findOneBy({id: data.renote.userId}); if (u && this.userEntityService.isRemoteUser(u)) dm.addDirectRecipe(u); } diff --git a/packages/backend/src/core/activitypub/ApRendererService.ts b/packages/backend/src/core/activitypub/ApRendererService.ts index 9c946a1e89..b0504bdef0 100644 --- a/packages/backend/src/core/activitypub/ApRendererService.ts +++ b/packages/backend/src/core/activitypub/ApRendererService.ts @@ -727,7 +727,7 @@ export class ApRendererService { * @param orderedItems attached objects (optional) */ @bindThis - public renderOrderedCollection(id: string, totalItems: number, first?: string, last?: string, orderedItems?: IObject[]) { + public renderOrderedCollection(id: string | null, totalItems: number, first?: string, last?: string, orderedItems?: IObject[]) { const page: any = { id, type: 'OrderedCollection', diff --git a/packages/backend/src/core/activitypub/models/ApNoteService.ts b/packages/backend/src/core/activitypub/models/ApNoteService.ts index bd2a7033a4..e4480a30ba 100644 --- a/packages/backend/src/core/activitypub/models/ApNoteService.ts +++ b/packages/backend/src/core/activitypub/models/ApNoteService.ts @@ -294,7 +294,7 @@ export class ApNoteService { if (isMessaging) { for (const recipient of visibleUsers) { - await this.messagingService.createMessage(actor, recipient, undefined, text ?? undefined, (files && files.length > 0) ? files[0] : null, object.id); + await this.messagingService.createMessage(actor, recipient, null, text ?? undefined, (files && files.length > 0) ? files[0] : null, object.id); return null; } } diff --git a/packages/backend/src/server/api/endpoints/messaging/messages/create.ts b/packages/backend/src/server/api/endpoints/messaging/messages/create.ts index 0725084397..46268f86b2 100644 --- a/packages/backend/src/server/api/endpoints/messaging/messages/create.ts +++ b/packages/backend/src/server/api/endpoints/messaging/messages/create.ts @@ -110,8 +110,8 @@ export default class extends Endpoint { // eslint- private messagingService: MessagingService, ) { super(meta, paramDef, async (ps, me) => { - let recipientUser: MiUser | null; - let recipientGroup: MiUserGroup | null; + let recipientUser: MiUser | null = null; + let recipientGroup: MiUserGroup | null = null; if (ps.userId != null) { // Myself