diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2018-04-07 16:26:50 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-04-07 16:26:50 +0900 |
| commit | 2547891f940a2872fcfb2b33cd33d4f7a42ca7bc (patch) | |
| tree | 5cba4ae9cdfd63e7e1ef74a002a7b742183e8d3c /src/remote/activitypub/resolver.ts | |
| parent | Merge pull request #1410 from akihikodaki/objec (diff) | |
| parent | Refactor (diff) | |
| download | misskey-2547891f940a2872fcfb2b33cd33d4f7a42ca7bc.tar.gz misskey-2547891f940a2872fcfb2b33cd33d4f7a42ca7bc.tar.bz2 misskey-2547891f940a2872fcfb2b33cd33d4f7a42ca7bc.zip | |
Merge pull request #1397 from syuilo/refactor
Refactor
Diffstat (limited to 'src/remote/activitypub/resolver.ts')
| -rw-r--r-- | src/remote/activitypub/resolver.ts | 83 |
1 files changed, 42 insertions, 41 deletions
diff --git a/src/remote/activitypub/resolver.ts b/src/remote/activitypub/resolver.ts index 371ccdcc30..4a97e2ef66 100644 --- a/src/remote/activitypub/resolver.ts +++ b/src/remote/activitypub/resolver.ts @@ -1,20 +1,51 @@ -const request = require('request-promise-native'); +import * as request from 'request-promise-native'; +import * as debug from 'debug'; +import { IObject } from './type'; + +const log = debug('misskey:activitypub:resolver'); export default class Resolver { - private requesting: Set<string>; + private history: Set<string>; - constructor(iterable?: Iterable<string>) { - this.requesting = new Set(iterable); + constructor() { + this.history = new Set(); } - private async resolveUnrequestedOne(value) { + public async resolveCollection(value) { + const collection = typeof value === 'string' + ? await this.resolve(value) + : value; + + switch (collection.type) { + case 'Collection': + collection.objects = collection.object.items; + break; + + case 'OrderedCollection': + collection.objects = collection.object.orderedItems; + break; + + default: + throw new Error(`unknown collection type: ${collection.type}`); + } + + return collection; + } + + public async resolve(value): Promise<IObject> { + if (value == null) { + throw new Error('resolvee is null (or undefined)'); + } + if (typeof value !== 'string') { - return { resolver: this, object: value }; + return value; } - const resolver = new Resolver(this.requesting); + if (this.history.has(value)) { + throw new Error('cannot resolve already resolved one'); + } - resolver.requesting.add(value); + this.history.add(value); const object = await request({ url: value, @@ -29,41 +60,11 @@ export default class Resolver { !object['@context'].includes('https://www.w3.org/ns/activitystreams') : object['@context'] !== 'https://www.w3.org/ns/activitystreams' )) { - throw new Error(); + throw new Error('invalid response'); } - return { resolver, object }; - } - - public async resolveCollection(value) { - const resolved = typeof value === 'string' ? - await this.resolveUnrequestedOne(value) : - { resolver: this, object: value }; - - switch (resolved.object.type) { - case 'Collection': - resolved.object = resolved.object.items; - break; - - case 'OrderedCollection': - resolved.object = resolved.object.orderedItems; - break; - - default: - if (!Array.isArray(value)) { - resolved.object = [resolved.object]; - } - break; - } - - return resolved; - } - - public resolveOne(value) { - if (this.requesting.has(value)) { - throw new Error(); - } + log(`resolved: ${JSON.stringify(object, null, 2)}`); - return this.resolveUnrequestedOne(value); + return object; } } |