hippofish/packages/backend/src/core/RelayService.ts

132 lines
3.8 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
2022-09-17 20:27:08 +02:00
import { Inject, Injectable } from '@nestjs/common';
import { IsNull } from 'typeorm';
import type { MiLocalUser, MiUser } from '@/models/User.js';
import type { RelaysRepository, UsersRepository } from '@/models/_.js';
2022-09-17 20:27:08 +02:00
import { IdService } from '@/core/IdService.js';
import { MemorySingleCache } from '@/misc/cache.js';
import type { MiRelay } from '@/models/Relay.js';
2022-09-17 20:27:08 +02:00
import { QueueService } from '@/core/QueueService.js';
import { CreateSystemUserService } from '@/core/CreateSystemUserService.js';
2022-12-04 02:16:03 +01:00
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
2022-09-17 20:27:08 +02:00
import { DI } from '@/di-symbols.js';
2022-11-17 01:31:07 +01:00
import { deepClone } from '@/misc/clone.js';
2022-12-04 09:05:32 +01:00
import { bindThis } from '@/decorators.js';
2022-09-17 20:27:08 +02:00
const ACTOR_USERNAME = 'relay.actor' as const;
@Injectable()
export class RelayService {
private relaysCache: MemorySingleCache<MiRelay[]>;
2022-09-17 20:27:08 +02: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,
) {
this.relaysCache = new MemorySingleCache<MiRelay[]>(1000 * 60 * 10); // 10m
2022-09-17 20:27:08 +02:00
}
@bindThis
private async getRelayActor(): Promise<MiLocalUser> {
2022-09-17 20:27:08 +02:00
const user = await this.usersRepository.findOneBy({
host: IsNull(),
username: ACTOR_USERNAME,
});
if (user) return user as MiLocalUser;
2022-09-17 20:27:08 +02:00
const created = await this.createSystemUserService.createSystemUser(ACTOR_USERNAME);
return created as MiLocalUser;
2022-09-17 20:27:08 +02:00
}
@bindThis
public async addRelay(inbox: string): Promise<MiRelay> {
fix(backend): use insertOne insteadof insert/findOneOrFail combination (#13908) * fix(backend): use insertOne insteadof insert/findOneOrFail combination * fix: typo * fix(backend): inherit mainAlias? * refactor(backend): use extend * fix(backend): invalid entityTarget * fix(backend): fake where * chore: debug * chore: debug * test: log * fix(backend): column names * fix(backend): remove dummy from * revert: log * fix(backend): position * fix(backend): automatic aliasing * chore(backend): alias * chore(backend): remove from * fix(backend): type * fix(backend): avoid pure name * test(backend): fix type * chore(backend): use cte * fix(backend): avoid useless alias * fix(backend): fix typo * fix(backend): __disambiguation__ * fix(backend): quote * chore(backend): t * chore(backend): accessible * chore(backend): concrete returning * fix(backend): quote * chore: log more * chore: log metadata * chore(backend): use raw * fix(backend): returning column name * fix(backend): transform * build(backend): wanna logging * build(backend): transform empty * build(backend): build alias * build(backend): restore name * chore: return entity * fix: test case * test(backend): 204 * chore(backend): log sql * chore(backend): assert user joined * fix(backend): typo * chore(backend): log long sql * chore(backend): log join * chore(backend): log join depth null * chore(backend): joinAttributes * chore(backend): override createJoinExpression * chore: join log * fix(backend): escape * test(backend): log log * chore(backend): join gonna success? * chore(backend): relations * chore(backend): undefined * chore(backend): target * chore(backend): remove log * chore(backend): log chart update * chore(backend): log columns * chore(backend): check hasMetadata * chore(backend): unshift id when not included * chore(backend): missing select * chore(backend): remove debug code
2024-06-01 04:16:44 +02:00
const relay = await this.relaysRepository.insertOne({
id: this.idService.gen(),
2022-09-17 20:27:08 +02:00
inbox,
status: 'requesting',
fix(backend): use insertOne insteadof insert/findOneOrFail combination (#13908) * fix(backend): use insertOne insteadof insert/findOneOrFail combination * fix: typo * fix(backend): inherit mainAlias? * refactor(backend): use extend * fix(backend): invalid entityTarget * fix(backend): fake where * chore: debug * chore: debug * test: log * fix(backend): column names * fix(backend): remove dummy from * revert: log * fix(backend): position * fix(backend): automatic aliasing * chore(backend): alias * chore(backend): remove from * fix(backend): type * fix(backend): avoid pure name * test(backend): fix type * chore(backend): use cte * fix(backend): avoid useless alias * fix(backend): fix typo * fix(backend): __disambiguation__ * fix(backend): quote * chore(backend): t * chore(backend): accessible * chore(backend): concrete returning * fix(backend): quote * chore: log more * chore: log metadata * chore(backend): use raw * fix(backend): returning column name * fix(backend): transform * build(backend): wanna logging * build(backend): transform empty * build(backend): build alias * build(backend): restore name * chore: return entity * fix: test case * test(backend): 204 * chore(backend): log sql * chore(backend): assert user joined * fix(backend): typo * chore(backend): log long sql * chore(backend): log join * chore(backend): log join depth null * chore(backend): joinAttributes * chore(backend): override createJoinExpression * chore: join log * fix(backend): escape * test(backend): log log * chore(backend): join gonna success? * chore(backend): relations * chore(backend): undefined * chore(backend): target * chore(backend): remove log * chore(backend): log chart update * chore(backend): log columns * chore(backend): check hasMetadata * chore(backend): unshift id when not included * chore(backend): missing select * chore(backend): remove debug code
2024-06-01 04:16:44 +02:00
});
2022-09-18 20:11:50 +02:00
const relayActor = await this.getRelayActor();
2022-09-17 20:27:08 +02:00
const follow = await this.apRendererService.renderFollowRelay(relay, relayActor);
2023-02-12 10:47:30 +01:00
const activity = this.apRendererService.addContext(follow);
this.queueService.deliver(relayActor, activity, relay.inbox, false);
2022-09-17 20:27:08 +02:00
return relay;
}
@bindThis
2022-09-17 20:27:08 +02:00
public async removeRelay(inbox: string): Promise<void> {
const relay = await this.relaysRepository.findOneBy({
inbox,
});
2022-09-17 20:27:08 +02:00
if (relay == null) {
throw new Error('relay not found');
}
2022-09-18 20:11:50 +02:00
const relayActor = await this.getRelayActor();
2022-09-17 20:27:08 +02:00
const follow = this.apRendererService.renderFollowRelay(relay, relayActor);
const undo = this.apRendererService.renderUndo(follow, relayActor);
2023-02-12 10:47:30 +01:00
const activity = this.apRendererService.addContext(undo);
this.queueService.deliver(relayActor, activity, relay.inbox, false);
2022-09-17 20:27:08 +02:00
await this.relaysRepository.delete(relay.id);
}
@bindThis
public async listRelay(): Promise<MiRelay[]> {
2022-09-17 20:27:08 +02:00
const relays = await this.relaysRepository.find();
return relays;
}
@bindThis
2022-09-17 20:27:08 +02:00
public async relayAccepted(id: string): Promise<string> {
const result = await this.relaysRepository.update(id, {
status: 'accepted',
});
2022-09-17 20:27:08 +02:00
return JSON.stringify(result);
}
@bindThis
2022-09-17 20:27:08 +02:00
public async relayRejected(id: string): Promise<string> {
const result = await this.relaysRepository.update(id, {
status: 'rejected',
});
2022-09-17 20:27:08 +02:00
return JSON.stringify(result);
}
@bindThis
public async deliverToRelays(user: { id: MiUser['id']; host: null; }, activity: any): Promise<void> {
2022-09-17 20:27:08 +02:00
if (activity == null) return;
2023-04-04 10:32:09 +02:00
const relays = await this.relaysCache.fetch(() => this.relaysRepository.findBy({
2022-09-17 20:27:08 +02:00
status: 'accepted',
}));
if (relays.length === 0) return;
2022-11-17 01:31:07 +01:00
const copy = deepClone(activity);
2022-09-17 20:27:08 +02:00
if (!copy.to) copy.to = ['https://www.w3.org/ns/activitystreams#Public'];
2022-09-17 20:27:08 +02:00
const signed = await this.apRendererService.attachLdSignature(copy, user);
2022-09-17 20:27:08 +02:00
for (const relay of relays) {
this.queueService.deliver(user, signed, relay.inbox, false);
2022-09-17 20:27:08 +02:00
}
}
}