summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/FederatedInstanceService.ts
diff options
context:
space:
mode:
authorJulia <julia@insertdomain.name>2025-06-19 21:35:18 +0000
committerJulia <julia@insertdomain.name>2025-06-19 21:35:18 +0000
commita77c32b17da63d3932b219f74152cce023a30f4a (patch)
treed2a05796e942c8f250bbd01369eab0cbe5a14531 /packages/backend/src/core/FederatedInstanceService.ts
parentmerge: release 2025.4.2 (!1051) (diff)
parentMerge branch 'develop' into release/2025.4.3 (diff)
downloadsharkey-a77c32b17da63d3932b219f74152cce023a30f4a.tar.gz
sharkey-a77c32b17da63d3932b219f74152cce023a30f4a.tar.bz2
sharkey-a77c32b17da63d3932b219f74152cce023a30f4a.zip
merge: prepare release 2025.4.3 (!1125)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/1125 Approved-by: Marie <github@yuugi.dev> Approved-by: Julia <julia@insertdomain.name>
Diffstat (limited to 'packages/backend/src/core/FederatedInstanceService.ts')
-rw-r--r--packages/backend/src/core/FederatedInstanceService.ts103
1 files changed, 55 insertions, 48 deletions
diff --git a/packages/backend/src/core/FederatedInstanceService.ts b/packages/backend/src/core/FederatedInstanceService.ts
index 3f7ed99348..34df10f0ff 100644
--- a/packages/backend/src/core/FederatedInstanceService.ts
+++ b/packages/backend/src/core/FederatedInstanceService.ts
@@ -5,23 +5,24 @@
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import * as Redis from 'ioredis';
-import { QueryFailedError } from 'typeorm';
-import type { InstancesRepository } from '@/models/_.js';
+import type { InstancesRepository, MiMeta } from '@/models/_.js';
import type { MiInstance } from '@/models/Instance.js';
-import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js';
+import { MemoryKVCache } from '@/misc/cache.js';
import { IdService } from '@/core/IdService.js';
import { DI } from '@/di-symbols.js';
import { UtilityService } from '@/core/UtilityService.js';
import { bindThis } from '@/decorators.js';
-import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
+import type { GlobalEvents } from '@/core/GlobalEventService.js';
+import { Serialized } from '@/types.js';
+import { diffArrays, diffArraysSimple } from '@/misc/diff-arrays.js';
@Injectable()
export class FederatedInstanceService implements OnApplicationShutdown {
- public federatedInstanceCache: RedisKVCache<MiInstance | null>;
+ private readonly federatedInstanceCache: MemoryKVCache<MiInstance | null>;
constructor(
- @Inject(DI.redis)
- private redisClient: Redis.Redis,
+ @Inject(DI.redisForSub)
+ private redisForSub: Redis.Redis,
@Inject(DI.instancesRepository)
private instancesRepository: InstancesRepository,
@@ -29,67 +30,46 @@ export class FederatedInstanceService implements OnApplicationShutdown {
private utilityService: UtilityService,
private idService: IdService,
) {
- this.federatedInstanceCache = new RedisKVCache<MiInstance | null>(this.redisClient, 'federatedInstance', {
- lifetime: 1000 * 60 * 30, // 30m
- memoryCacheLifetime: 1000 * 60 * 3, // 3m
- fetcher: (key) => this.instancesRepository.findOneBy({ host: key }),
- toRedisConverter: (value) => JSON.stringify(value),
- fromRedisConverter: (value) => {
- const parsed = JSON.parse(value);
- if (parsed == null) return null;
- return {
- ...parsed,
- firstRetrievedAt: new Date(parsed.firstRetrievedAt),
- latestRequestReceivedAt: parsed.latestRequestReceivedAt ? new Date(parsed.latestRequestReceivedAt) : null,
- infoUpdatedAt: parsed.infoUpdatedAt ? new Date(parsed.infoUpdatedAt) : null,
- notRespondingSince: parsed.notRespondingSince ? new Date(parsed.notRespondingSince) : null,
- };
- },
- });
+ this.federatedInstanceCache = new MemoryKVCache(1000 * 60 * 3); // 3m
+ this.redisForSub.on('message', this.onMessage);
}
@bindThis
public async fetchOrRegister(host: string): Promise<MiInstance> {
host = this.utilityService.toPuny(host);
- const cached = await this.federatedInstanceCache.get(host);
+ const cached = this.federatedInstanceCache.get(host);
if (cached) return cached;
- const index = await this.instancesRepository.findOneBy({ host });
-
+ let index = await this.instancesRepository.findOneBy({ host });
if (index == null) {
- let i;
- try {
- i = await this.instancesRepository.insertOne({
+ await this.instancesRepository.createQueryBuilder('instance')
+ .insert()
+ .values({
id: this.idService.gen(),
host,
firstRetrievedAt: new Date(),
- });
- } catch (e: unknown) {
- if (e instanceof QueryFailedError) {
- if (isDuplicateKeyValueError(e)) {
- i = await this.instancesRepository.findOneBy({ host });
- }
- }
+ isBlocked: this.utilityService.isBlockedHost(host),
+ isSilenced: this.utilityService.isSilencedHost(host),
+ isMediaSilenced: this.utilityService.isMediaSilencedHost(host),
+ isAllowListed: this.utilityService.isAllowListedHost(host),
+ isBubbled: this.utilityService.isBubbledHost(host),
+ })
+ .orIgnore()
+ .execute();
- if (i == null) {
- throw e;
- }
- }
-
- this.federatedInstanceCache.set(host, i);
- return i;
- } else {
- this.federatedInstanceCache.set(host, index);
- return index;
+ index = await this.instancesRepository.findOneByOrFail({ host });
}
+
+ this.federatedInstanceCache.set(host, index);
+ return index;
}
@bindThis
public async fetch(host: string): Promise<MiInstance | null> {
host = this.utilityService.toPuny(host);
- const cached = await this.federatedInstanceCache.get(host);
+ const cached = this.federatedInstanceCache.get(host);
if (cached !== undefined) return cached;
const index = await this.instancesRepository.findOneBy({ host });
@@ -117,8 +97,35 @@ export class FederatedInstanceService implements OnApplicationShutdown {
this.federatedInstanceCache.set(result.host, result);
}
+ private syncCache(before: Serialized<MiMeta | undefined>, after: Serialized<MiMeta>): void {
+ const changed =
+ diffArraysSimple(before?.blockedHosts, after.blockedHosts) ||
+ diffArraysSimple(before?.silencedHosts, after.silencedHosts) ||
+ diffArraysSimple(before?.mediaSilencedHosts, after.mediaSilencedHosts) ||
+ diffArraysSimple(before?.federationHosts, after.federationHosts) ||
+ diffArraysSimple(before?.bubbleInstances, after.bubbleInstances);
+
+ if (changed) {
+ // We have to clear the whole thing, otherwise subdomains won't be synced.
+ this.federatedInstanceCache.clear();
+ }
+ }
+
+ @bindThis
+ private async onMessage(_: string, data: string): Promise<void> {
+ const obj = JSON.parse(data);
+
+ if (obj.channel === 'internal') {
+ const { type, body } = obj.message as GlobalEvents['internal']['payload'];
+ if (type === 'metaUpdated') {
+ this.syncCache(body.before, body.after);
+ }
+ }
+ }
+
@bindThis
public dispose(): void {
+ this.redisForSub.off('message', this.onMessage);
this.federatedInstanceCache.dispose();
}