From 4b24cce2c51b6cb81303e2fedcd809da2c5f1c3d Mon Sep 17 00:00:00 2001 From: Hazel Koehler Date: Fri, 3 May 2024 00:40:53 -0400 Subject: [PATCH] fix: revert previous change and filter in the note processing instead --- .../core/activitypub/models/ApImageService.ts | 21 +++++++++---------- .../core/activitypub/models/ApNoteService.ts | 21 ++++++++++++------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/packages/backend/src/core/activitypub/models/ApImageService.ts b/packages/backend/src/core/activitypub/models/ApImageService.ts index 5f381108a3..1017a5018a 100644 --- a/packages/backend/src/core/activitypub/models/ApImageService.ts +++ b/packages/backend/src/core/activitypub/models/ApImageService.ts @@ -48,21 +48,20 @@ export class ApImageService { } const image = await this.apResolverService.createResolver().resolve(value); - const imageUrl = image.url ?? image.href; - if (imageUrl == null) { + if (image.url == null) { throw new Error('invalid image: url not provided'); } - if (typeof imageUrl !== 'string') { - throw new Error('invalid image: unexpected type of url: ' + JSON.stringify(imageUrl, null, 2)); + if (typeof image.url !== 'string') { + throw new Error('invalid image: unexpected type of url: ' + JSON.stringify(image.url, null, 2)); } - if (!checkHttps(imageUrl)) { - throw new Error('invalid image: unexpected schema of url: ' + imageUrl); + if (!checkHttps(image.url)) { + throw new Error('invalid image: unexpected schema of url: ' + image.url); } - this.logger.info(`Creating the Image: ${imageUrl}`); + this.logger.info(`Creating the Image: ${image.url}`); const instance = await this.metaService.fetch(); @@ -78,17 +77,17 @@ export class ApImageService { }); const file = await this.driveService.uploadFromUrl({ - url: imageUrl, + url: image.url, user: actor, - uri: imageUrl, + uri: image.url, sensitive: image.sensitive, isLink: !shouldBeCached, comment: truncate(image.name ?? undefined, DB_MAX_IMAGE_COMMENT_LENGTH), }); - if (!file.isLink || file.url === imageUrl) return file; + if (!file.isLink || file.url === image.url) return file; // URLが異なっている場合、同じ画像が以前に異なるURLで登録されていたということなので、URLを更新する - await this.driveFilesRepository.update({ id: file.id }, { url: imageUrl, uri: imageUrl }); + await this.driveFilesRepository.update({ id: file.id }, { url: image.url, uri: image.url }); return await this.driveFilesRepository.findOneByOrFail({ id: file.id }); } diff --git a/packages/backend/src/core/activitypub/models/ApNoteService.ts b/packages/backend/src/core/activitypub/models/ApNoteService.ts index 6d9dc86c16..0e0bbba09e 100644 --- a/packages/backend/src/core/activitypub/models/ApNoteService.ts +++ b/packages/backend/src/core/activitypub/models/ApNoteService.ts @@ -441,15 +441,20 @@ export class ApNoteService { } // 添付ファイル - // TODO: attachmentは必ずしもImageではない - // TODO: attachmentは必ずしも配列ではない + const attachments = toArray(note.attachment); + if (note.image) + attachments.push(note.image); + const limit = promiseLimit(2); - const files = (await Promise.all(toArray(note.attachment).map(attach => ( - limit(() => this.apImageService.resolveImage(actor, { - ...attach, - sensitive: note.sensitive, // Noteがsensitiveなら添付もsensitiveにする - })) - )))); + const filePromises = attachments + .filter(attach => toArray(attach.type)?.includes('Image')) + .map(attach => ( + limit(() => this.apImageService.resolveImage(actor, { + ...attach, + sensitive: note.sensitive, // Noteがsensitiveなら添付もsensitiveにする + })) + )); + const files = await Promise.all(filePromises); // リプライ const reply: MiNote | null = note.inReplyTo