2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import type { ILocalUser } from '@/models/entities/User.js';
|
|
|
|
import { InstanceActorService } from '@/core/InstanceActorService.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { NotesRepository, PollsRepository, NoteReactionsRepository, UsersRepository } from '@/models/index.js';
|
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
|
|
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
|
|
|
import { isCollectionOrOrderedCollection } from './type.js';
|
|
|
|
import { ApDbResolverService } from './ApDbResolverService.js';
|
|
|
|
import { ApRendererService } from './ApRendererService.js';
|
|
|
|
import { ApRequestService } from './ApRequestService.js';
|
|
|
|
import type { IObject, ICollection, IOrderedCollection } from './type.js';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ApResolverService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.pollsRepository)
|
|
|
|
private pollsRepository: PollsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.noteReactionsRepository)
|
|
|
|
private noteReactionsRepository: NoteReactionsRepository,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private instanceActorService: InstanceActorService,
|
|
|
|
private metaService: MetaService,
|
|
|
|
private apRequestService: ApRequestService,
|
|
|
|
private httpRequestService: HttpRequestService,
|
|
|
|
private apRendererService: ApRendererService,
|
|
|
|
private apDbResolverService: ApDbResolverService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public createResolver(): Resolver {
|
|
|
|
return new Resolver(
|
|
|
|
this.config,
|
|
|
|
this.usersRepository,
|
|
|
|
this.notesRepository,
|
|
|
|
this.pollsRepository,
|
|
|
|
this.noteReactionsRepository,
|
|
|
|
this.utilityService,
|
|
|
|
this.instanceActorService,
|
|
|
|
this.metaService,
|
|
|
|
this.apRequestService,
|
|
|
|
this.httpRequestService,
|
|
|
|
this.apRendererService,
|
|
|
|
this.apDbResolverService,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Resolver {
|
|
|
|
private history: Set<string>;
|
|
|
|
private user?: ILocalUser;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private config: Config,
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
private pollsRepository: PollsRepository,
|
|
|
|
private noteReactionsRepository: NoteReactionsRepository,
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private instanceActorService: InstanceActorService,
|
|
|
|
private metaService: MetaService,
|
|
|
|
private apRequestService: ApRequestService,
|
|
|
|
private httpRequestService: HttpRequestService,
|
|
|
|
private apRendererService: ApRendererService,
|
|
|
|
private apDbResolverService: ApDbResolverService,
|
|
|
|
) {
|
|
|
|
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 (value.includes('#')) {
|
|
|
|
// URLs with fragment parts cannot be resolved correctly because
|
|
|
|
// the fragment part does not get transmitted over HTTP(S).
|
|
|
|
// Avoid strange behaviour by not trying to resolve these at all.
|
|
|
|
throw new Error(`cannot resolve URL with fragment: ${value}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.history.has(value)) {
|
|
|
|
throw new Error('cannot resolve already resolved one');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.history.add(value);
|
|
|
|
|
|
|
|
const host = this.utilityService.extractDbHost(value);
|
|
|
|
if (this.utilityService.isSelfHost(host)) {
|
|
|
|
return await this.resolveLocal(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const meta = await this.metaService.fetch();
|
|
|
|
if (meta.blockedHosts.includes(host)) {
|
|
|
|
throw new Error('Instance is blocked');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.config.signToActivityPubGet && !this.user) {
|
|
|
|
this.user = await this.instanceActorService.getInstanceActor();
|
|
|
|
}
|
|
|
|
|
|
|
|
const object = (this.user
|
|
|
|
? await this.apRequestService.signedGet(value, this.user)
|
|
|
|
: await this.httpRequestService.getJson(value, 'application/activity+json, application/ld+json')) as IObject;
|
|
|
|
|
|
|
|
if (object == null || (
|
|
|
|
Array.isArray(object['@context']) ?
|
|
|
|
!(object['@context'] as unknown[]).includes('https://www.w3.org/ns/activitystreams') :
|
|
|
|
object['@context'] !== 'https://www.w3.org/ns/activitystreams'
|
|
|
|
)) {
|
|
|
|
throw new Error('invalid response');
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
private resolveLocal(url: string): Promise<IObject> {
|
|
|
|
const parsed = this.apDbResolverService.parseUri(url);
|
|
|
|
if (!parsed.local) throw new Error('resolveLocal: not local');
|
|
|
|
|
|
|
|
switch (parsed.type) {
|
|
|
|
case 'notes':
|
|
|
|
return this.notesRepository.findOneByOrFail({ id: parsed.id })
|
|
|
|
.then(note => {
|
|
|
|
if (parsed.rest === 'activity') {
|
2022-09-23 23:41:27 +00:00
|
|
|
// this refers to the create activity and not the note itself
|
|
|
|
return this.apRendererService.renderActivity(this.apRendererService.renderCreate(this.apRendererService.renderNote(note), note));
|
2022-09-17 18:27:08 +00:00
|
|
|
} else {
|
|
|
|
return this.apRendererService.renderNote(note);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
case 'users':
|
|
|
|
return this.usersRepository.findOneByOrFail({ id: parsed.id })
|
|
|
|
.then(user => this.apRendererService.renderPerson(user as ILocalUser));
|
|
|
|
case 'questions':
|
|
|
|
// Polls are indexed by the note they are attached to.
|
|
|
|
return Promise.all([
|
|
|
|
this.notesRepository.findOneByOrFail({ id: parsed.id }),
|
|
|
|
this.pollsRepository.findOneByOrFail({ noteId: parsed.id }),
|
|
|
|
])
|
|
|
|
.then(([note, poll]) => this.apRendererService.renderQuestion({ id: note.userId }, note, poll));
|
|
|
|
case 'likes':
|
|
|
|
return this.noteReactionsRepository.findOneByOrFail({ id: parsed.id }).then(reaction =>
|
2022-09-23 23:41:27 +00:00
|
|
|
this.apRendererService.renderActivity(this.apRendererService.renderLike(reaction, { uri: null }))!);
|
2022-09-17 18:27:08 +00:00
|
|
|
case 'follows':
|
|
|
|
// rest should be <followee id>
|
|
|
|
if (parsed.rest == null || !/^\w+$/.test(parsed.rest)) throw new Error('resolveLocal: invalid follow URI');
|
|
|
|
|
|
|
|
return Promise.all(
|
|
|
|
[parsed.id, parsed.rest].map(id => this.usersRepository.findOneByOrFail({ id })),
|
|
|
|
)
|
|
|
|
.then(([follower, followee]) => this.apRendererService.renderActivity(this.apRendererService.renderFollow(follower, followee, url)));
|
|
|
|
default:
|
2022-09-23 23:41:27 +00:00
|
|
|
throw new Error(`resolveLocal: type ${parsed.type} unhandled`);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|