From c6cdfa2f5a0fe3b83acf25d5e27f5b6b80a3540c Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Sun, 10 Mar 2019 22:27:25 +0900 Subject: [PATCH] Ignore 4xx references in AP (#4463) * Ignore 4xx references * remove unnecessary comment --- src/remote/activitypub/kernel/announce/note.ts | 14 +++++++++++++- src/remote/activitypub/models/image.ts | 12 +++++++++++- src/remote/activitypub/models/note.ts | 15 +++++++++++++-- src/remote/activitypub/resolver.ts | 16 ---------------- src/services/drive/upload-from-url.ts | 2 +- 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/remote/activitypub/kernel/announce/note.ts b/src/remote/activitypub/kernel/announce/note.ts index 128ca6da8..8d090902c 100644 --- a/src/remote/activitypub/kernel/announce/note.ts +++ b/src/remote/activitypub/kernel/announce/note.ts @@ -29,7 +29,19 @@ export default async function(resolver: Resolver, actor: IRemoteUser, activity: return; } - const renote = await resolveNote(note); + // Announce対象をresolve + let renote; + try { + renote = await resolveNote(note); + } catch (e) { + // 対象が4xxならスキップ + if (e.statusCode >= 400 && e.statusCode < 500) { + logger.warn(`Ignored announce target ${note.inReplyTo} - ${e.statusCode}`); + return; + } + logger.warn(`Error in announce target ${note.inReplyTo} - ${e.statusCode || e}`); + throw e; + } logger.info(`Creating the (Re)Note: ${uri}`); diff --git a/src/remote/activitypub/models/image.ts b/src/remote/activitypub/models/image.ts index ef0b24e89..bd97d13d2 100644 --- a/src/remote/activitypub/models/image.ts +++ b/src/remote/activitypub/models/image.ts @@ -27,7 +27,17 @@ export async function createImage(actor: IRemoteUser, value: any): Promise= 400 && e < 500) { + logger.warn(`Ignored image: ${image.url} - ${e}`); + return null; + } + throw e; + } if (file.metadata.isRemote) { // URLが異なっている場合、同じ画像が以前に異なるURLで登録されていたということなので、 diff --git a/src/remote/activitypub/models/note.ts b/src/remote/activitypub/models/note.ts index a87257dff..7078eb2ce 100644 --- a/src/remote/activitypub/models/note.ts +++ b/src/remote/activitypub/models/note.ts @@ -111,11 +111,22 @@ export async function createNote(value: any, resolver?: Resolver, silent = false note.attachment = Array.isArray(note.attachment) ? note.attachment : note.attachment ? [note.attachment] : []; const files = note.attachment .map(attach => attach.sensitive = note.sensitive) - ? await Promise.all(note.attachment.map(x => limit(() => resolveImage(actor, x)) as Promise)) + ? (await Promise.all(note.attachment.map(x => limit(() => resolveImage(actor, x)) as Promise))) + .filter(image => image != null) : []; // リプライ - const reply = note.inReplyTo ? await resolveNote(note.inReplyTo, resolver) : null; + const reply = note.inReplyTo + ? await resolveNote(note.inReplyTo, resolver).catch(e => { + // 4xxの場合はリプライしてないことにする + if (e.statusCode >= 400 && e.statusCode < 500) { + logger.warn(`Ignored inReplyTo ${note.inReplyTo} - ${e.statusCode} `); + return null; + } + logger.warn(`Error in inReplyTo ${note.inReplyTo} - ${e.statusCode || e}`); + throw e; + }) + : null; // 引用 let quote: INote; diff --git a/src/remote/activitypub/resolver.ts b/src/remote/activitypub/resolver.ts index 6291d133e..05152993e 100644 --- a/src/remote/activitypub/resolver.ts +++ b/src/remote/activitypub/resolver.ts @@ -1,9 +1,6 @@ import * as request from 'request-promise-native'; import { IObject } from './type'; import config from '../../config'; -import { apLogger } from './logger'; - -export const logger = apLogger.createSubLogger('resolver'); export default class Resolver { private history: Set; @@ -34,7 +31,6 @@ export default class Resolver { } default: { - logger.error(`unknown collection type: ${collection.type}`); throw new Error(`unknown collection type: ${collection.type}`); } } @@ -44,7 +40,6 @@ export default class Resolver { public async resolve(value: any): Promise { if (value == null) { - logger.error('resolvee is null (or undefined)'); throw new Error('resolvee is null (or undefined)'); } @@ -53,7 +48,6 @@ export default class Resolver { } if (this.history.has(value)) { - logger.error(`cannot resolve already resolved one: ${value}`); throw new Error('cannot resolve already resolved one'); } @@ -68,12 +62,6 @@ export default class Resolver { Accept: 'application/activity+json, application/ld+json' }, json: true - }).catch(e => { - logger.error(`request error: ${value}: ${e.message}`, { - url: value, - e: e - }); - throw new Error(`request error: ${e.message}`); }); if (object === null || ( @@ -81,10 +69,6 @@ export default class Resolver { !object['@context'].includes('https://www.w3.org/ns/activitystreams') : object['@context'] !== 'https://www.w3.org/ns/activitystreams' )) { - logger.error(`invalid response: ${value}`, { - url: value, - object: object - }); throw new Error('invalid response'); } diff --git a/src/services/drive/upload-from-url.ts b/src/services/drive/upload-from-url.ts index 558f020a3..89efe8a50 100644 --- a/src/services/drive/upload-from-url.ts +++ b/src/services/drive/upload-from-url.ts @@ -42,7 +42,7 @@ export default async ( const writable = fs.createWriteStream(path); writable.on('finish', () => { - logger.succ(`Download succeeded: ${chalk.cyan(url)}`); + logger.succ(`Download finished: ${chalk.cyan(url)}`); res(); });