yumechi-no-kuni/src/remote/activitypub/resolver.ts

101 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-04-01 14:15:27 -05:00
import RemoteUserObject from '../../models/remote-user-object';
2018-03-31 05:55:00 -05:00
import { IObject } from './type';
const request = require('request-promise-native');
type IResult = {
resolver: Resolver;
object: IObject;
};
2018-04-01 07:56:11 -05:00
export default class Resolver {
private requesting: Set<string>;
constructor(iterable?: Iterable<string>) {
this.requesting = new Set(iterable);
2018-03-31 05:55:00 -05:00
}
2018-04-01 07:56:11 -05:00
private async resolveUnrequestedOne(value) {
if (typeof value !== 'string') {
return { resolver: this, object: value };
}
2018-03-31 05:55:00 -05:00
2018-04-01 07:56:11 -05:00
const resolver = new Resolver(this.requesting);
2018-03-31 05:55:00 -05:00
2018-04-01 07:56:11 -05:00
resolver.requesting.add(value);
2018-03-31 05:55:00 -05:00
2018-04-01 07:56:11 -05:00
const object = await request({
url: value,
headers: {
Accept: 'application/activity+json, application/ld+json'
},
json: true
});
2018-03-31 05:55:00 -05:00
2018-04-01 07:56:11 -05:00
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();
}
2018-03-31 05:55:00 -05:00
2018-04-01 07:56:11 -05:00
return { resolver, object };
2018-03-31 05:55:00 -05:00
}
2018-04-01 07:56:11 -05:00
private async resolveCollection(value) {
const resolved = typeof value === 'string' ?
await this.resolveUnrequestedOne(value) :
{ resolver: this, object: value };
2018-03-31 05:55:00 -05:00
switch (resolved.object.type) {
2018-04-01 07:56:11 -05:00
case 'Collection':
resolved.object = resolved.object.items;
break;
2018-03-31 05:55:00 -05:00
2018-04-01 07:56:11 -05:00
case 'OrderedCollection':
resolved.object = resolved.object.orderedItems;
break;
2018-03-31 05:55:00 -05:00
2018-04-01 07:56:11 -05:00
default:
if (!Array.isArray(value)) {
resolved.object = [resolved.object];
}
break;
2018-04-01 07:56:11 -05:00
}
return resolved;
2018-03-31 05:55:00 -05:00
}
2018-04-01 07:24:25 -05:00
public async resolve(value): Promise<Array<Promise<IResult>>> {
const { resolver, object } = await this.resolveCollection(value);
2018-03-31 05:55:00 -05:00
return object
.filter(element => !resolver.requesting.has(element))
.map(resolver.resolveUnrequestedOne.bind(resolver));
2018-03-31 05:55:00 -05:00
}
2018-04-01 07:24:25 -05:00
public resolveOne(value) {
2018-03-31 05:55:00 -05:00
if (this.requesting.has(value)) {
2018-04-01 07:24:25 -05:00
throw new Error();
2018-03-31 05:55:00 -05:00
}
2018-04-01 07:56:11 -05:00
return this.resolveUnrequestedOne(value);
2018-03-31 05:55:00 -05:00
}
2018-04-01 07:24:25 -05:00
public async resolveRemoteUserObjects(value) {
const { resolver, object } = await this.resolveCollection(value);
2018-03-31 05:55:00 -05:00
return object.filter(element => !resolver.requesting.has(element)).map(element => {
2018-03-31 05:55:00 -05:00
if (typeof element === 'string') {
const object = RemoteUserObject.findOne({ uri: element });
if (object !== null) {
return object;
}
}
return resolver.resolveUnrequestedOne(element);
2018-03-31 05:55:00 -05:00
});
}
}