From cd2c23e3900e4b1ee5f70fdf5dbb6408320c2751 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 1 Apr 2018 21:56:11 +0900 Subject: [PATCH] Refactor --- src/common/remote/activitypub/resolver.ts | 108 +++++++++++----------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/src/common/remote/activitypub/resolver.ts b/src/common/remote/activitypub/resolver.ts index 43f0d63cb3..a167fa1339 100644 --- a/src/common/remote/activitypub/resolver.ts +++ b/src/common/remote/activitypub/resolver.ts @@ -7,55 +7,6 @@ type IResult = { object: IObject; }; -async function resolveUnrequestedOne(this: Resolver, value) { - if (typeof value !== 'string') { - return { resolver: this, object: value }; - } - - const resolver = new Resolver(this.requesting); - - resolver.requesting.add(value); - - const object = await request({ - url: value, - headers: { - Accept: 'application/activity+json, application/ld+json' - }, - json: true - }); - - if (object === null || ( - Array.isArray(object['@context']) ? - !object['@context'].includes('https://www.w3.org/ns/activitystreams') : - object['@context'] !== 'https://www.w3.org/ns/activitystreams' - )) { - throw new Error(); - } - - return { resolver, object }; -} - -async function resolveCollection(this: Resolver, value) { - if (Array.isArray(value)) { - return value; - } - - const resolved = typeof value === 'string' ? - await resolveUnrequestedOne.call(this, value) : - value; - - switch (resolved.type) { - case 'Collection': - return resolved.items; - - case 'OrderedCollection': - return resolved.orderedItems; - - default: - return [resolved]; - } -} - export default class Resolver { private requesting: Set; @@ -63,12 +14,61 @@ export default class Resolver { this.requesting = new Set(iterable); } + private async resolveUnrequestedOne(value) { + if (typeof value !== 'string') { + return { resolver: this, object: value }; + } + + const resolver = new Resolver(this.requesting); + + resolver.requesting.add(value); + + const object = await request({ + url: value, + headers: { + Accept: 'application/activity+json, application/ld+json' + }, + json: true + }); + + if (object === null || ( + Array.isArray(object['@context']) ? + !object['@context'].includes('https://www.w3.org/ns/activitystreams') : + object['@context'] !== 'https://www.w3.org/ns/activitystreams' + )) { + throw new Error(); + } + + return { resolver, object }; + } + + private async resolveCollection(value) { + if (Array.isArray(value)) { + return value; + } + + const resolved = typeof value === 'string' ? + await this.resolveUnrequestedOne(value) : + value; + + switch (resolved.type) { + case 'Collection': + return resolved.items; + + case 'OrderedCollection': + return resolved.orderedItems; + + default: + return [resolved]; + } + } + public async resolve(value): Promise>> { - const collection = await resolveCollection.call(this, value); + const collection = await this.resolveCollection(value); return collection .filter(element => !this.requesting.has(element)) - .map(resolveUnrequestedOne.bind(this)); + .map(this.resolveUnrequestedOne.bind(this)); } public resolveOne(value) { @@ -76,11 +76,11 @@ export default class Resolver { throw new Error(); } - return resolveUnrequestedOne.call(this, value); + return this.resolveUnrequestedOne(value); } public async resolveRemoteUserObjects(value) { - const collection = await resolveCollection.call(this, value); + const collection = await this.resolveCollection(value); return collection.filter(element => !this.requesting.has(element)).map(element => { if (typeof element === 'string') { @@ -91,7 +91,7 @@ export default class Resolver { } } - return resolveUnrequestedOne.call(this, element); + return this.resolveUnrequestedOne(element); }); } }