2023-07-27 00:31:52 -05:00
|
|
|
/*
|
2024-02-13 09:59:27 -06:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 00:31:52 -05:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { IsNull } from 'typeorm';
|
2023-09-19 21:33:36 -05:00
|
|
|
import type { MiLocalUser, MiUser } from '@/models/User.js';
|
2023-09-15 00:28:29 -05:00
|
|
|
import type { RelaysRepository, UsersRepository } from '@/models/_.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-04-05 21:14:43 -05:00
|
|
|
import { MemorySingleCache } from '@/misc/cache.js';
|
2023-09-19 21:33:36 -05:00
|
|
|
import type { MiRelay } from '@/models/Relay.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
import { QueueService } from '@/core/QueueService.js';
|
|
|
|
import { CreateSystemUserService } from '@/core/CreateSystemUserService.js';
|
2022-12-03 19:16:03 -06:00
|
|
|
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-11-16 18:31:07 -06:00
|
|
|
import { deepClone } from '@/misc/clone.js';
|
2022-12-04 02:05:32 -06:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
const ACTOR_USERNAME = 'relay.actor' as const;
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class RelayService {
|
2023-08-16 03:51:28 -05:00
|
|
|
private relaysCache: MemorySingleCache<MiRelay[]>;
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.relaysRepository)
|
|
|
|
private relaysRepository: RelaysRepository,
|
|
|
|
|
|
|
|
private idService: IdService,
|
|
|
|
private queueService: QueueService,
|
|
|
|
private createSystemUserService: CreateSystemUserService,
|
|
|
|
private apRendererService: ApRendererService,
|
|
|
|
) {
|
2023-08-16 03:51:28 -05:00
|
|
|
this.relaysCache = new MemorySingleCache<MiRelay[]>(1000 * 60 * 10);
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2023-08-16 03:51:28 -05:00
|
|
|
private async getRelayActor(): Promise<MiLocalUser> {
|
2022-09-17 13:27:08 -05:00
|
|
|
const user = await this.usersRepository.findOneBy({
|
|
|
|
host: IsNull(),
|
|
|
|
username: ACTOR_USERNAME,
|
|
|
|
});
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2023-08-16 03:51:28 -05:00
|
|
|
if (user) return user as MiLocalUser;
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
const created = await this.createSystemUserService.createSystemUser(ACTOR_USERNAME);
|
2023-08-16 03:51:28 -05:00
|
|
|
return created as MiLocalUser;
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2023-08-16 03:51:28 -05:00
|
|
|
public async addRelay(inbox: string): Promise<MiRelay> {
|
2022-09-17 13:27:08 -05:00
|
|
|
const relay = await this.relaysRepository.insert({
|
2023-10-15 20:45:22 -05:00
|
|
|
id: this.idService.gen(),
|
2022-09-17 13:27:08 -05:00
|
|
|
inbox,
|
|
|
|
status: 'requesting',
|
|
|
|
}).then(x => this.relaysRepository.findOneByOrFail(x.identifiers[0]));
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-18 13:11:50 -05:00
|
|
|
const relayActor = await this.getRelayActor();
|
2022-09-17 13:27:08 -05:00
|
|
|
const follow = await this.apRendererService.renderFollowRelay(relay, relayActor);
|
2023-02-12 03:47:30 -06:00
|
|
|
const activity = this.apRendererService.addContext(follow);
|
2023-03-14 05:11:31 -05:00
|
|
|
this.queueService.deliver(relayActor, activity, relay.inbox, false);
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
return relay;
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-09-17 13:27:08 -05:00
|
|
|
public async removeRelay(inbox: string): Promise<void> {
|
|
|
|
const relay = await this.relaysRepository.findOneBy({
|
|
|
|
inbox,
|
|
|
|
});
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
if (relay == null) {
|
|
|
|
throw new Error('relay not found');
|
|
|
|
}
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-18 13:11:50 -05:00
|
|
|
const relayActor = await this.getRelayActor();
|
2022-09-17 13:27:08 -05:00
|
|
|
const follow = this.apRendererService.renderFollowRelay(relay, relayActor);
|
|
|
|
const undo = this.apRendererService.renderUndo(follow, relayActor);
|
2023-02-12 03:47:30 -06:00
|
|
|
const activity = this.apRendererService.addContext(undo);
|
2023-03-14 05:11:31 -05:00
|
|
|
this.queueService.deliver(relayActor, activity, relay.inbox, false);
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
await this.relaysRepository.delete(relay.id);
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2023-08-16 03:51:28 -05:00
|
|
|
public async listRelay(): Promise<MiRelay[]> {
|
2022-09-17 13:27:08 -05:00
|
|
|
const relays = await this.relaysRepository.find();
|
|
|
|
return relays;
|
|
|
|
}
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-09-17 13:27:08 -05:00
|
|
|
public async relayAccepted(id: string): Promise<string> {
|
|
|
|
const result = await this.relaysRepository.update(id, {
|
|
|
|
status: 'accepted',
|
|
|
|
});
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
return JSON.stringify(result);
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-09-17 13:27:08 -05:00
|
|
|
public async relayRejected(id: string): Promise<string> {
|
|
|
|
const result = await this.relaysRepository.update(id, {
|
|
|
|
status: 'rejected',
|
|
|
|
});
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
return JSON.stringify(result);
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2023-08-16 03:51:28 -05:00
|
|
|
public async deliverToRelays(user: { id: MiUser['id']; host: null; }, activity: any): Promise<void> {
|
2022-09-17 13:27:08 -05:00
|
|
|
if (activity == null) return;
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2023-04-04 03:32:09 -05:00
|
|
|
const relays = await this.relaysCache.fetch(() => this.relaysRepository.findBy({
|
2022-09-17 13:27:08 -05:00
|
|
|
status: 'accepted',
|
|
|
|
}));
|
|
|
|
if (relays.length === 0) return;
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-11-16 18:31:07 -06:00
|
|
|
const copy = deepClone(activity);
|
2022-09-17 13:27:08 -05:00
|
|
|
if (!copy.to) copy.to = ['https://www.w3.org/ns/activitystreams#Public'];
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
const signed = await this.apRendererService.attachLdSignature(copy, user);
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
for (const relay of relays) {
|
2023-03-14 05:11:31 -05:00
|
|
|
this.queueService.deliver(user, signed, relay.inbox, false);
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|