2023-07-27 05:31:52 +00:00
|
|
|
/*
|
2024-02-13 15:50:11 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
2023-07-27 05:31:52 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-09-22 07:43:01 +00:00
|
|
|
import { IsNull, Not } from 'typeorm';
|
2023-09-20 02:33:36 +00:00
|
|
|
import type { MiLocalUser, MiRemoteUser } from '@/models/User.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { InstanceActorService } from '@/core/InstanceActorService.js';
|
2023-09-22 07:43:01 +00:00
|
|
|
import type { NotesRepository, PollsRepository, NoteReactionsRepository, UsersRepository, FollowRequestsRepository } from '@/models/_.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2023-01-25 03:00:04 +00:00
|
|
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-23 23:31:02 +00:00
|
|
|
import { LoggerService } from '@/core/LoggerService.js';
|
|
|
|
import type Logger from '@/logger.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
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';
|
|
|
|
|
|
|
|
export class Resolver {
|
|
|
|
private history: Set<string>;
|
2023-08-16 08:51:28 +00:00
|
|
|
private user?: MiLocalUser;
|
2023-01-12 12:03:02 +00:00
|
|
|
private logger: Logger;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private config: Config,
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
private pollsRepository: PollsRepository,
|
|
|
|
private noteReactionsRepository: NoteReactionsRepository,
|
2023-09-22 07:43:01 +00:00
|
|
|
private followRequestsRepository: FollowRequestsRepository,
|
2022-09-17 18:27:08 +00:00
|
|
|
private utilityService: UtilityService,
|
|
|
|
private instanceActorService: InstanceActorService,
|
|
|
|
private metaService: MetaService,
|
|
|
|
private apRequestService: ApRequestService,
|
|
|
|
private httpRequestService: HttpRequestService,
|
|
|
|
private apRendererService: ApRendererService,
|
|
|
|
private apDbResolverService: ApDbResolverService,
|
2023-01-12 12:03:02 +00:00
|
|
|
private loggerService: LoggerService,
|
2022-12-04 06:03:09 +00:00
|
|
|
private recursionLimit = 100,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
this.history = new Set();
|
2023-02-12 09:47:30 +00:00
|
|
|
this.logger = this.loggerService.getLogger('ap-resolve');
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public getHistory(): string[] {
|
|
|
|
return Array.from(this.history);
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
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}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async resolve(value: string | IObject): Promise<IObject> {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2022-12-02 21:13:36 +00:00
|
|
|
if (this.history.size > this.recursionLimit) {
|
|
|
|
throw new Error(`hit recursion limit: ${this.utilityService.extractDbHost(value)}`);
|
|
|
|
}
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
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();
|
2023-01-13 09:21:07 +00:00
|
|
|
if (this.utilityService.isBlockedHost(meta.blockedHosts, host)) {
|
2022-09-17 18:27:08 +00:00
|
|
|
throw new Error('Instance is blocked');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.config.signToActivityPubGet && !this.user) {
|
|
|
|
this.user = await this.instanceActorService.getInstanceActor();
|
|
|
|
}
|
|
|
|
|
|
|
|
const object = (this.user
|
2023-01-12 12:03:02 +00:00
|
|
|
? await this.apRequestService.signedGet(value, this.user) as IObject
|
2023-01-25 03:00:04 +00:00
|
|
|
: await this.httpRequestService.getJson(value, 'application/activity+json, application/ld+json')) as IObject;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2023-07-13 03:48:34 +00:00
|
|
|
if (
|
2022-09-17 18:27:08 +00:00
|
|
|
Array.isArray(object['@context']) ?
|
|
|
|
!(object['@context'] as unknown[]).includes('https://www.w3.org/ns/activitystreams') :
|
|
|
|
object['@context'] !== 'https://www.w3.org/ns/activitystreams'
|
2023-07-13 03:48:34 +00:00
|
|
|
) {
|
2022-09-17 18:27:08 +00:00
|
|
|
throw new Error('invalid response');
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
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 })
|
2023-02-12 09:47:30 +00:00
|
|
|
.then(async note => {
|
2022-09-17 18:27:08 +00:00
|
|
|
if (parsed.rest === 'activity') {
|
2022-09-23 23:41:27 +00:00
|
|
|
// this refers to the create activity and not the note itself
|
2023-02-12 09:47:30 +00:00
|
|
|
return this.apRendererService.addContext(this.apRendererService.renderCreate(await 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 })
|
2023-08-16 08:51:28 +00:00
|
|
|
.then(user => this.apRendererService.renderPerson(user as MiLocalUser));
|
2022-09-17 18:27:08 +00:00
|
|
|
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':
|
2023-02-12 09:47:30 +00:00
|
|
|
return this.noteReactionsRepository.findOneByOrFail({ id: parsed.id }).then(async reaction =>
|
|
|
|
this.apRendererService.addContext(await this.apRendererService.renderLike(reaction, { uri: null })));
|
2022-09-17 18:27:08 +00:00
|
|
|
case 'follows':
|
2023-09-22 07:43:01 +00:00
|
|
|
return this.followRequestsRepository.findOneBy({ id: parsed.id })
|
|
|
|
.then(async followRequest => {
|
|
|
|
if (followRequest == null) throw new Error('resolveLocal: invalid follow request ID');
|
|
|
|
const [follower, followee] = await Promise.all([
|
|
|
|
this.usersRepository.findOneBy({
|
|
|
|
id: followRequest.followerId,
|
|
|
|
host: IsNull(),
|
|
|
|
}),
|
|
|
|
this.usersRepository.findOneBy({
|
|
|
|
id: followRequest.followeeId,
|
|
|
|
host: Not(IsNull()),
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
if (follower == null || followee == null) {
|
|
|
|
throw new Error('resolveLocal: follower or followee does not exist');
|
|
|
|
}
|
|
|
|
return this.apRendererService.addContext(this.apRendererService.renderFollow(follower as MiLocalUser | MiRemoteUser, followee as MiLocalUser | MiRemoteUser, url));
|
|
|
|
});
|
2022-09-17 18:27:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-04 06:03:09 +00:00
|
|
|
|
|
|
|
@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,
|
|
|
|
|
2023-09-22 07:43:01 +00:00
|
|
|
@Inject(DI.followRequestsRepository)
|
|
|
|
private followRequestsRepository: FollowRequestsRepository,
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
private utilityService: UtilityService,
|
|
|
|
private instanceActorService: InstanceActorService,
|
|
|
|
private metaService: MetaService,
|
|
|
|
private apRequestService: ApRequestService,
|
|
|
|
private httpRequestService: HttpRequestService,
|
|
|
|
private apRendererService: ApRendererService,
|
|
|
|
private apDbResolverService: ApDbResolverService,
|
2023-02-12 09:47:30 +00:00
|
|
|
private loggerService: LoggerService,
|
2022-12-04 06:03:09 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public createResolver(): Resolver {
|
|
|
|
return new Resolver(
|
|
|
|
this.config,
|
|
|
|
this.usersRepository,
|
|
|
|
this.notesRepository,
|
|
|
|
this.pollsRepository,
|
|
|
|
this.noteReactionsRepository,
|
2023-09-22 07:43:01 +00:00
|
|
|
this.followRequestsRepository,
|
2022-12-04 06:03:09 +00:00
|
|
|
this.utilityService,
|
|
|
|
this.instanceActorService,
|
|
|
|
this.metaService,
|
|
|
|
this.apRequestService,
|
|
|
|
this.httpRequestService,
|
|
|
|
this.apRendererService,
|
|
|
|
this.apDbResolverService,
|
2023-02-12 09:47:30 +00:00
|
|
|
this.loggerService,
|
2022-12-04 06:03:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|