This commit is contained in:
syuilo 2018-04-01 21:56:11 +09:00
parent a20c1c8547
commit cd2c23e390
1 changed files with 54 additions and 54 deletions

View File

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