mirror of
https://github.com/paricafe/misskey.git
synced 2024-12-01 20:36:44 -06:00
b9cb6d1c10
将来ESMに移行しやすいように Related: #7658 なんかmochaが起動しなくなってるけど理由不明 すぐ直したい
65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
import config from '@/config/index.js';
|
|
import { getJson } from '@/misc/fetch.js';
|
|
import { ILocalUser } from '@/models/entities/user.js';
|
|
import { getInstanceActor } from '@/services/instance-actor.js';
|
|
import { signedGet } from './request.js';
|
|
import { IObject, isCollectionOrOrderedCollection, ICollection, IOrderedCollection } from './type.js';
|
|
|
|
export default class Resolver {
|
|
private history: Set<string>;
|
|
private user?: ILocalUser;
|
|
|
|
constructor() {
|
|
this.history = new Set();
|
|
}
|
|
|
|
public getHistory(): string[] {
|
|
return Array.from(this.history);
|
|
}
|
|
|
|
public async resolveCollection(value: string | IObject): Promise<ICollection | IOrderedCollection> {
|
|
const collection = typeof value === 'string'
|
|
? await this.resolve(value)
|
|
: value;
|
|
|
|
if (isCollectionOrOrderedCollection(collection)) {
|
|
return collection;
|
|
} else {
|
|
throw new Error(`unrecognized collection type: ${collection.type}`);
|
|
}
|
|
}
|
|
|
|
public async resolve(value: string | IObject): Promise<IObject> {
|
|
if (value == null) {
|
|
throw new Error('resolvee is null (or undefined)');
|
|
}
|
|
|
|
if (typeof value !== 'string') {
|
|
return value;
|
|
}
|
|
|
|
if (this.history.has(value)) {
|
|
throw new Error('cannot resolve already resolved one');
|
|
}
|
|
|
|
this.history.add(value);
|
|
|
|
if (config.signToActivityPubGet && !this.user) {
|
|
this.user = await getInstanceActor();
|
|
}
|
|
|
|
const object = this.user
|
|
? await signedGet(value, this.user)
|
|
: await getJson(value, 'application/activity+json, application/ld+json');
|
|
|
|
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('invalid response');
|
|
}
|
|
|
|
return object;
|
|
}
|
|
}
|