Merge pull request #1384 from akihikodaki/resolve

Remove resolveRemoteUserObjects method of remote/activitypub/resolver
This commit is contained in:
syuilo 2018-04-03 20:46:13 +09:00 committed by GitHub
commit 7dc71c09fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 46 deletions

View file

@ -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);

View file

@ -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));
};

View file

@ -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;
}
})));
});
};

View file

@ -93,9 +93,19 @@ class Creator {
}
public async create(parentResolver, value): Promise<Array<Promise<IRemoteUserObject>>> {
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;
}));
});
}
}

View file

@ -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<string>;
@ -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<Array<Promise<IResult>>> {
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);
});
}
}