From c22cb377599780666ffe2c7ccdbdcb19a49f1969 Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Tue, 3 Apr 2018 20:39:27 +0900 Subject: [PATCH] Remove resolveRemoteUserObjects method of remote/activitypub/resolver The value of the value returned by resolveRemoteUserObjects method of remote/activitypub/resolver was inconsistent. --- src/processor/http/perform-activitypub.ts | 3 ++- src/processor/http/process-inbox.ts | 2 +- src/remote/activitypub/act/index.ts | 20 +++++++------- src/remote/activitypub/create.ts | 16 ++++++++--- src/remote/activitypub/resolver.ts | 33 +---------------------- 5 files changed, 28 insertions(+), 46 deletions(-) diff --git a/src/processor/http/perform-activitypub.ts b/src/processor/http/perform-activitypub.ts index 4fdbb901d..963e532fe 100644 --- a/src/processor/http/perform-activitypub.ts +++ b/src/processor/http/perform-activitypub.ts @@ -3,4 +3,5 @@ import act from '../../remote/activitypub/act'; import Resolver from '../../remote/activitypub/resolver'; export default ({ data }) => User.findOne({ _id: data.actor }) - .then(actor => act(new Resolver(), actor, data.outbox)); + .then(actor => act(new Resolver(), actor, data.outbox)) + .then(Promise.all); diff --git a/src/processor/http/process-inbox.ts b/src/processor/http/process-inbox.ts index e75c0b5c5..8ceb08257 100644 --- a/src/processor/http/process-inbox.ts +++ b/src/processor/http/process-inbox.ts @@ -35,5 +35,5 @@ export default async ({ data }) => { throw 'signature verification failed'; } - await act(new Resolver(), user, data.inbox, true); + await Promise.all(await act(new Resolver(), user, data.inbox, true)); }; diff --git a/src/remote/activitypub/act/index.ts b/src/remote/activitypub/act/index.ts index 2af190221..030f1cf25 100644 --- a/src/remote/activitypub/act/index.ts +++ b/src/remote/activitypub/act/index.ts @@ -4,27 +4,29 @@ import undo from './undo'; import createObject from '../create'; import Resolver from '../resolver'; -export default (resolver: Resolver, actor, value, distribute?: boolean) => { - return resolver.resolve(value).then(resolved => Promise.all(resolved.map(async promisedResult => { - const result = await promisedResult; - const created = await (await createObject(result.resolver, actor, [result.object], distribute))[0]; +export default async (parentResolver: Resolver, actor, value, distribute?: boolean) => { + const collection = await parentResolver.resolveCollection(value); + + return collection.object.map(async element => { + const { resolver, object } = await collection.resolver.resolveOne(element); + const created = await (await createObject(resolver, actor, [object], distribute))[0]; if (created !== null) { return created; } - switch (result.object.type) { + switch (object.type) { case 'Create': - return create(result.resolver, actor, result.object, distribute); + return create(resolver, actor, object, distribute); case 'Follow': - return follow(result.resolver, actor, result.object, distribute); + return follow(resolver, actor, object, distribute); case 'Undo': - return undo(result.resolver, actor, result.object); + return undo(resolver, actor, object); default: return null; } - }))); + }); }; diff --git a/src/remote/activitypub/create.ts b/src/remote/activitypub/create.ts index f70f56b79..8f3e14629 100644 --- a/src/remote/activitypub/create.ts +++ b/src/remote/activitypub/create.ts @@ -93,9 +93,19 @@ class Creator { } public async create(parentResolver, value): Promise>> { - const results = await parentResolver.resolveRemoteUserObjects(value); + const collection = await parentResolver.resolveCollection(value); + + return collection.object.map(async element => { + if (typeof element === 'string') { + const object = RemoteUserObject.findOne({ uri: element }); + + if (object !== null) { + return object; + } + } + + const { resolver, object } = await collection.resolver.resolveOne(element); - return results.map(promisedResult => promisedResult.then(({ resolver, object }) => { switch (object.type) { case 'Image': return this.createImage(object); @@ -105,7 +115,7 @@ class Creator { } return null; - })); + }); } } diff --git a/src/remote/activitypub/resolver.ts b/src/remote/activitypub/resolver.ts index b7e431b91..371ccdcc3 100644 --- a/src/remote/activitypub/resolver.ts +++ b/src/remote/activitypub/resolver.ts @@ -1,12 +1,5 @@ -import RemoteUserObject from '../../models/remote-user-object'; -import { IObject } from './type'; const request = require('request-promise-native'); -type IResult = { - resolver: Resolver; - object: IObject; -}; - export default class Resolver { private requesting: Set; @@ -42,7 +35,7 @@ export default class Resolver { return { resolver, object }; } - private async resolveCollection(value) { + public async resolveCollection(value) { const resolved = typeof value === 'string' ? await this.resolveUnrequestedOne(value) : { resolver: this, object: value }; @@ -66,14 +59,6 @@ export default class Resolver { return resolved; } - public async resolve(value): Promise>> { - const { resolver, object } = await this.resolveCollection(value); - - return object - .filter(element => !resolver.requesting.has(element)) - .map(resolver.resolveUnrequestedOne.bind(resolver)); - } - public resolveOne(value) { if (this.requesting.has(value)) { throw new Error(); @@ -81,20 +66,4 @@ export default class Resolver { return this.resolveUnrequestedOne(value); } - - public async resolveRemoteUserObjects(value) { - const { resolver, object } = await this.resolveCollection(value); - - return object.filter(element => !resolver.requesting.has(element)).map(element => { - if (typeof element === 'string') { - const object = RemoteUserObject.findOne({ uri: element }); - - if (object !== null) { - return object; - } - } - - return resolver.resolveUnrequestedOne(element); - }); - } }