fix(ActivityPub): 個別ユーザーのInboxに届いたPostでノートが作成される場合通知が作成されてしなう問題を修正 (MisskeyIO#386)

This commit is contained in:
まっちゃとーにゅ 2024-01-26 22:56:58 +09:00 committed by GitHub
parent ec60f23bf3
commit d4e701b179
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 28 deletions

View File

@ -87,13 +87,13 @@ export class ApInboxService {
} }
@bindThis @bindThis
public async performActivity(actor: MiRemoteUser, activity: IObject, additionalTo?: MiLocalUser['id']): Promise<void> { public async performActivity(actor: MiRemoteUser, activity: IObject, additionalCc?: MiLocalUser['id']): Promise<void> {
if (isCollectionOrOrderedCollection(activity)) { if (isCollectionOrOrderedCollection(activity)) {
const resolver = this.apResolverService.createResolver(); const resolver = this.apResolverService.createResolver();
for (const item of toArray(isCollection(activity) ? activity.items : activity.orderedItems)) { for (const item of toArray(isCollection(activity) ? activity.items : activity.orderedItems)) {
const act = await resolver.resolve(item); const act = await resolver.resolve(item);
try { try {
await this.performOneActivity(actor, act, additionalTo); await this.performOneActivity(actor, act, additionalCc);
} catch (err) { } catch (err) {
if (err instanceof Error || typeof err === 'string') { if (err instanceof Error || typeof err === 'string') {
this.logger.error(err); this.logger.error(err);
@ -103,7 +103,7 @@ export class ApInboxService {
} }
} }
} else { } else {
await this.performOneActivity(actor, activity, additionalTo); await this.performOneActivity(actor, activity, additionalCc);
} }
// ついでにリモートユーザーの情報が古かったら更新しておく // ついでにリモートユーザーの情報が古かったら更新しておく
@ -117,15 +117,15 @@ export class ApInboxService {
} }
@bindThis @bindThis
public async performOneActivity(actor: MiRemoteUser, activity: IObject, additionalTo?: MiLocalUser['id']): Promise<void> { public async performOneActivity(actor: MiRemoteUser, activity: IObject, additionalCc?: MiLocalUser['id']): Promise<void> {
if (actor.isSuspended) return; if (actor.isSuspended) return;
if (isCreate(activity)) { if (isCreate(activity)) {
await this.create(actor, activity, additionalTo); await this.create(actor, activity, additionalCc);
} else if (isDelete(activity)) { } else if (isDelete(activity)) {
await this.delete(actor, activity); await this.delete(actor, activity);
} else if (isUpdate(activity)) { } else if (isUpdate(activity)) {
await this.update(actor, activity, additionalTo); await this.update(actor, activity, additionalCc);
} else if (isFollow(activity)) { } else if (isFollow(activity)) {
await this.follow(actor, activity); await this.follow(actor, activity);
} else if (isAccept(activity)) { } else if (isAccept(activity)) {
@ -346,7 +346,7 @@ export class ApInboxService {
} }
@bindThis @bindThis
private async create(actor: MiRemoteUser, activity: ICreate, additionalTo?: MiLocalUser['id']): Promise<void> { private async create(actor: MiRemoteUser, activity: ICreate, additionalCc?: MiLocalUser['id']): Promise<void> {
const uri = getApId(activity); const uri = getApId(activity);
this.logger.info(`Create: ${uri}`); this.logger.info(`Create: ${uri}`);
@ -375,14 +375,14 @@ export class ApInboxService {
}); });
if (isPost(object)) { if (isPost(object)) {
await this.createNote(resolver, actor, object, false, activity, additionalTo); await this.createNote(resolver, actor, object, false, activity, additionalCc);
} else { } else {
this.logger.warn(`Unknown type: ${getApType(object)}`); this.logger.warn(`Unknown type: ${getApType(object)}`);
} }
} }
@bindThis @bindThis
private async createNote(resolver: Resolver, actor: MiRemoteUser, note: IObject, silent = false, activity?: ICreate, additionalTo?: MiLocalUser['id']): Promise<string> { private async createNote(resolver: Resolver, actor: MiRemoteUser, note: IObject, silent = false, activity?: ICreate, additionalCc?: MiLocalUser['id']): Promise<string> {
const uri = getApId(note); const uri = getApId(note);
if (typeof note === 'object') { if (typeof note === 'object') {
@ -401,14 +401,20 @@ export class ApInboxService {
try { try {
const exist = await this.apNoteService.fetchNote(note); const exist = await this.apNoteService.fetchNote(note);
if (additionalTo && exist && !await this.noteEntityService.isVisibleForMe(exist, additionalTo)) { if (exist) {
await this.noteCreateService.appendNoteVisibleUser(actor, exist, additionalTo); if (additionalCc && !await this.noteEntityService.isVisibleForMe(exist, additionalCc)) {
return 'ok: note visible user appended'; await this.noteCreateService.appendNoteVisibleUser(actor, exist, additionalCc);
} else if (exist) { return 'ok: note visible user appended';
}
return 'skip: note exists'; 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'; return 'ok';
} catch (err) { } catch (err) {
if (err instanceof StatusError && !err.isRetryable) { if (err instanceof StatusError && !err.isRetryable) {
@ -736,7 +742,7 @@ export class ApInboxService {
} }
@bindThis @bindThis
private async update(actor: MiRemoteUser, activity: IUpdate, additionalTo?: MiLocalUser['id']): Promise<string> { private async update(actor: MiRemoteUser, activity: IUpdate, additionalCc?: MiLocalUser['id']): Promise<string> {
if (actor.uri !== activity.actor) { if (actor.uri !== activity.actor) {
return 'skip: invalid actor'; return 'skip: invalid actor';
} }
@ -756,14 +762,14 @@ export class ApInboxService {
} else if (getApType(object) === 'Question') { } else if (getApType(object) === 'Question') {
await this.apQuestionService.updateQuestion(object, resolver).catch(err => this.logger.error(`err: failed to update question: ${err}`, { error: err })); await this.apQuestionService.updateQuestion(object, resolver).catch(err => this.logger.error(`err: failed to update question: ${err}`, { error: err }));
return 'ok: Question updated'; return 'ok: Question updated';
} else if (additionalTo && isPost(object)) { } else if (additionalCc && isPost(object)) {
const uri = getApId(object); const uri = getApId(object);
const unlock = await this.appLockService.getApLock(uri); const unlock = await this.appLockService.getApLock(uri);
try { try {
const exist = await this.apNoteService.fetchNote(object); const exist = await this.apNoteService.fetchNote(object);
if (exist && !await this.noteEntityService.isVisibleForMe(exist, additionalTo)) { if (exist && !await this.noteEntityService.isVisibleForMe(exist, additionalCc)) {
await this.noteCreateService.appendNoteVisibleUser(actor, exist, additionalTo); await this.noteCreateService.appendNoteVisibleUser(actor, exist, additionalCc);
return 'ok: note visible user appended'; return 'ok: note visible user appended';
} else { } else {
return 'skip: nothing to do'; return 'skip: nothing to do';

View File

@ -5,11 +5,11 @@
import { forwardRef, Inject, Injectable } from '@nestjs/common'; import { forwardRef, Inject, Injectable } from '@nestjs/common';
import promiseLimit from 'promise-limit'; import promiseLimit from 'promise-limit';
import { In, IsNull } from 'typeorm'; import { In } from 'typeorm';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import type { UsersRepository, PollsRepository, EmojisRepository } from '@/models/_.js'; import type { UsersRepository, PollsRepository, EmojisRepository } from '@/models/_.js';
import type { Config } from '@/config.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 type { MiNote } from '@/models/Note.js';
import { toArray, toSingle, unique } from '@/misc/prelude/array.js'; import { toArray, toSingle, unique } from '@/misc/prelude/array.js';
import type { MiEmoji } from '@/models/Emoji.js'; import type { MiEmoji } from '@/models/Emoji.js';
@ -116,7 +116,7 @@ export class ApNoteService {
* Noteを作成します * Noteを作成します
*/ */
@bindThis @bindThis
public async createNote(value: string | IObject, resolver?: Resolver, silent = false, additionalTo?: MiLocalUser['id']): Promise<MiNote | null> { public async createNote(value: string | IObject, resolver?: Resolver, silent = false): Promise<MiNote | null> {
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
if (resolver == null) resolver = this.apResolverService.createResolver(); if (resolver == null) resolver = this.apResolverService.createResolver();
@ -166,13 +166,6 @@ export class ApNoteService {
let visibility = noteAudience.visibility; let visibility = noteAudience.visibility;
const visibleUsers = noteAudience.visibleUsers; 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) が指定されてなかった場合 // Audience (to, cc) が指定されてなかった場合
if (visibility === 'specified' && visibleUsers.length === 0) { if (visibility === 'specified' && visibleUsers.length === 0) {
if (typeof value === 'string') { // 入力がstringならばresolverでGETが発生している if (typeof value === 'string') { // 入力がstringならばresolverでGETが発生している