From d4e701b17903fdf3ed1ab14c1d7092ed4699b6e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BE=E3=81=A3=E3=81=A1=E3=82=83=E3=81=A8=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E3=82=85?= <17376330+u1-liquid@users.noreply.github.com> Date: Fri, 26 Jan 2024 22:56:58 +0900 Subject: [PATCH] =?UTF-8?q?fix(ActivityPub):=20=E5=80=8B=E5=88=A5=E3=83=A6?= =?UTF-8?q?=E3=83=BC=E3=82=B6=E3=83=BC=E3=81=AEInbox=E3=81=AB=E5=B1=8A?= =?UTF-8?q?=E3=81=84=E3=81=9FPost=E3=81=A7=E3=83=8E=E3=83=BC=E3=83=88?= =?UTF-8?q?=E3=81=8C=E4=BD=9C=E6=88=90=E3=81=95=E3=82=8C=E3=82=8B=E5=A0=B4?= =?UTF-8?q?=E5=90=88=E9=80=9A=E7=9F=A5=E3=81=8C=E4=BD=9C=E6=88=90=E3=81=95?= =?UTF-8?q?=E3=82=8C=E3=81=A6=E3=81=97=E3=81=AA=E3=81=86=E5=95=8F=E9=A1=8C?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3=20(MisskeyIO#386)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/core/activitypub/ApInboxService.ts | 42 +++++++++++-------- .../core/activitypub/models/ApNoteService.ts | 13 ++---- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/packages/backend/src/core/activitypub/ApInboxService.ts b/packages/backend/src/core/activitypub/ApInboxService.ts index 546e1fb756..41cc8757f7 100644 --- a/packages/backend/src/core/activitypub/ApInboxService.ts +++ b/packages/backend/src/core/activitypub/ApInboxService.ts @@ -87,13 +87,13 @@ export class ApInboxService { } @bindThis - public async performActivity(actor: MiRemoteUser, activity: IObject, additionalTo?: MiLocalUser['id']): Promise { + public async performActivity(actor: MiRemoteUser, activity: IObject, additionalCc?: MiLocalUser['id']): Promise { if (isCollectionOrOrderedCollection(activity)) { const resolver = this.apResolverService.createResolver(); for (const item of toArray(isCollection(activity) ? activity.items : activity.orderedItems)) { const act = await resolver.resolve(item); try { - await this.performOneActivity(actor, act, additionalTo); + await this.performOneActivity(actor, act, additionalCc); } catch (err) { if (err instanceof Error || typeof err === 'string') { this.logger.error(err); @@ -103,7 +103,7 @@ export class ApInboxService { } } } else { - await this.performOneActivity(actor, activity, additionalTo); + await this.performOneActivity(actor, activity, additionalCc); } // ついでにリモートユーザーの情報が古かったら更新しておく @@ -117,15 +117,15 @@ export class ApInboxService { } @bindThis - public async performOneActivity(actor: MiRemoteUser, activity: IObject, additionalTo?: MiLocalUser['id']): Promise { + public async performOneActivity(actor: MiRemoteUser, activity: IObject, additionalCc?: MiLocalUser['id']): Promise { if (actor.isSuspended) return; if (isCreate(activity)) { - await this.create(actor, activity, additionalTo); + await this.create(actor, activity, additionalCc); } else if (isDelete(activity)) { await this.delete(actor, activity); } else if (isUpdate(activity)) { - await this.update(actor, activity, additionalTo); + await this.update(actor, activity, additionalCc); } else if (isFollow(activity)) { await this.follow(actor, activity); } else if (isAccept(activity)) { @@ -346,7 +346,7 @@ export class ApInboxService { } @bindThis - private async create(actor: MiRemoteUser, activity: ICreate, additionalTo?: MiLocalUser['id']): Promise { + private async create(actor: MiRemoteUser, activity: ICreate, additionalCc?: MiLocalUser['id']): Promise { const uri = getApId(activity); this.logger.info(`Create: ${uri}`); @@ -375,14 +375,14 @@ export class ApInboxService { }); if (isPost(object)) { - await this.createNote(resolver, actor, object, false, activity, additionalTo); + await this.createNote(resolver, actor, object, false, activity, additionalCc); } else { this.logger.warn(`Unknown type: ${getApType(object)}`); } } @bindThis - private async createNote(resolver: Resolver, actor: MiRemoteUser, note: IObject, silent = false, activity?: ICreate, additionalTo?: MiLocalUser['id']): Promise { + private async createNote(resolver: Resolver, actor: MiRemoteUser, note: IObject, silent = false, activity?: ICreate, additionalCc?: MiLocalUser['id']): Promise { const uri = getApId(note); if (typeof note === 'object') { @@ -401,14 +401,20 @@ export class ApInboxService { try { const exist = await this.apNoteService.fetchNote(note); - if (additionalTo && exist && !await this.noteEntityService.isVisibleForMe(exist, additionalTo)) { - await this.noteCreateService.appendNoteVisibleUser(actor, exist, additionalTo); - return 'ok: note visible user appended'; - } else if (exist) { + if (exist) { + if (additionalCc && !await this.noteEntityService.isVisibleForMe(exist, additionalCc)) { + await this.noteCreateService.appendNoteVisibleUser(actor, exist, additionalCc); + return 'ok: note visible user appended'; + } return 'skip: note exists'; } - await this.apNoteService.createNote(note, resolver, silent, additionalTo); + const createdNote = await this.apNoteService.createNote(note, resolver, silent); + if (createdNote && additionalCc && !await this.noteEntityService.isVisibleForMe(createdNote, additionalCc)) { + await this.noteCreateService.appendNoteVisibleUser(actor, createdNote, additionalCc); + return 'ok: note visible user appended'; + } + return 'ok'; } catch (err) { if (err instanceof StatusError && !err.isRetryable) { @@ -736,7 +742,7 @@ export class ApInboxService { } @bindThis - private async update(actor: MiRemoteUser, activity: IUpdate, additionalTo?: MiLocalUser['id']): Promise { + private async update(actor: MiRemoteUser, activity: IUpdate, additionalCc?: MiLocalUser['id']): Promise { if (actor.uri !== activity.actor) { return 'skip: invalid actor'; } @@ -756,14 +762,14 @@ export class ApInboxService { } else if (getApType(object) === 'Question') { await this.apQuestionService.updateQuestion(object, resolver).catch(err => this.logger.error(`err: failed to update question: ${err}`, { error: err })); return 'ok: Question updated'; - } else if (additionalTo && isPost(object)) { + } else if (additionalCc && isPost(object)) { const uri = getApId(object); const unlock = await this.appLockService.getApLock(uri); try { const exist = await this.apNoteService.fetchNote(object); - if (exist && !await this.noteEntityService.isVisibleForMe(exist, additionalTo)) { - await this.noteCreateService.appendNoteVisibleUser(actor, exist, additionalTo); + if (exist && !await this.noteEntityService.isVisibleForMe(exist, additionalCc)) { + await this.noteCreateService.appendNoteVisibleUser(actor, exist, additionalCc); return 'ok: note visible user appended'; } else { return 'skip: nothing to do'; diff --git a/packages/backend/src/core/activitypub/models/ApNoteService.ts b/packages/backend/src/core/activitypub/models/ApNoteService.ts index 9c3f3cd897..cd581fa423 100644 --- a/packages/backend/src/core/activitypub/models/ApNoteService.ts +++ b/packages/backend/src/core/activitypub/models/ApNoteService.ts @@ -5,11 +5,11 @@ import { forwardRef, Inject, Injectable } from '@nestjs/common'; import promiseLimit from 'promise-limit'; -import { In, IsNull } from 'typeorm'; +import { In } from 'typeorm'; import { DI } from '@/di-symbols.js'; import type { UsersRepository, PollsRepository, EmojisRepository } from '@/models/_.js'; import type { Config } from '@/config.js'; -import type { MiLocalUser, MiRemoteUser } from '@/models/User.js'; +import type { MiRemoteUser } from '@/models/User.js'; import type { MiNote } from '@/models/Note.js'; import { toArray, toSingle, unique } from '@/misc/prelude/array.js'; import type { MiEmoji } from '@/models/Emoji.js'; @@ -116,7 +116,7 @@ export class ApNoteService { * Noteを作成します。 */ @bindThis - public async createNote(value: string | IObject, resolver?: Resolver, silent = false, additionalTo?: MiLocalUser['id']): Promise { + public async createNote(value: string | IObject, resolver?: Resolver, silent = false): Promise { // eslint-disable-next-line no-param-reassign if (resolver == null) resolver = this.apResolverService.createResolver(); @@ -166,13 +166,6 @@ export class ApNoteService { let visibility = noteAudience.visibility; const visibleUsers = noteAudience.visibleUsers; - if (additionalTo) { - const additionalUser = await this.usersRepository.findOneBy({ id: additionalTo, host: IsNull() }); - if (additionalUser && !visibleUsers.some(x => x.id === additionalUser.id)) { - visibleUsers.push(additionalUser); - } - } - // Audience (to, cc) が指定されてなかった場合 if (visibility === 'specified' && visibleUsers.length === 0) { if (typeof value === 'string') { // 入力がstringならばresolverでGETが発生している