2022-09-17 21:27:08 +03:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-04-14 07:50:05 +03:00
|
|
|
import * as Redis from 'ioredis';
|
2022-09-20 23:33:11 +03:00
|
|
|
import type { InstancesRepository } from '@/models/index.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type { Instance } from '@/models/entities/Instance.js';
|
2023-04-07 12:48:45 +03:00
|
|
|
import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-04 03:16:03 +02:00
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
2022-12-04 08:03:09 +02:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class FederatedInstanceService {
|
2023-04-07 12:48:45 +03:00
|
|
|
public federatedInstanceCache: RedisKVCache<Instance | null>;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
constructor(
|
2023-04-07 12:48:45 +03:00
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
@Inject(DI.instancesRepository)
|
|
|
|
private instancesRepository: InstancesRepository,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private idService: IdService,
|
|
|
|
) {
|
2023-04-07 12:48:45 +03:00
|
|
|
this.federatedInstanceCache = new RedisKVCache<Instance | null>(this.redisClient, 'federatedInstance', {
|
2023-04-22 13:59:08 +03:00
|
|
|
lifetime: 1000 * 60 * 30, // 30m
|
|
|
|
memoryCacheLifetime: 1000 * 60 * 3, // 3m
|
2023-04-07 12:48:45 +03:00
|
|
|
fetcher: (key) => this.instancesRepository.findOneBy({ host: key }),
|
|
|
|
toRedisConverter: (value) => JSON.stringify(value),
|
2023-04-07 12:55:11 +03:00
|
|
|
fromRedisConverter: (value) => {
|
|
|
|
const parsed = JSON.parse(value);
|
2023-04-09 02:02:52 +03:00
|
|
|
if (parsed == null) return null;
|
2023-04-07 12:55:11 +03:00
|
|
|
return {
|
|
|
|
...parsed,
|
|
|
|
firstRetrievedAt: new Date(parsed.firstRetrievedAt),
|
|
|
|
latestRequestReceivedAt: parsed.latestRequestReceivedAt ? new Date(parsed.latestRequestReceivedAt) : null,
|
|
|
|
infoUpdatedAt: parsed.infoUpdatedAt ? new Date(parsed.infoUpdatedAt) : null,
|
|
|
|
};
|
|
|
|
},
|
2023-04-07 12:48:45 +03:00
|
|
|
});
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2023-01-03 02:32:36 +02:00
|
|
|
public async fetch(host: string): Promise<Instance> {
|
2022-09-17 21:27:08 +03:00
|
|
|
host = this.utilityService.toPuny(host);
|
|
|
|
|
2023-04-07 12:48:45 +03:00
|
|
|
const cached = await this.federatedInstanceCache.get(host);
|
2022-09-17 21:27:08 +03:00
|
|
|
if (cached) return cached;
|
|
|
|
|
|
|
|
const index = await this.instancesRepository.findOneBy({ host });
|
|
|
|
|
|
|
|
if (index == null) {
|
|
|
|
const i = await this.instancesRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
host,
|
2023-01-15 22:02:38 +02:00
|
|
|
firstRetrievedAt: new Date(),
|
2022-09-17 21:27:08 +03:00
|
|
|
}).then(x => this.instancesRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
|
2023-04-07 12:48:45 +03:00
|
|
|
this.federatedInstanceCache.set(host, i);
|
2022-09-17 21:27:08 +03:00
|
|
|
return i;
|
|
|
|
} else {
|
2023-04-07 12:48:45 +03:00
|
|
|
this.federatedInstanceCache.set(host, index);
|
2022-09-17 21:27:08 +03:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
2023-01-03 02:32:36 +02:00
|
|
|
|
|
|
|
@bindThis
|
2023-04-22 13:59:08 +03:00
|
|
|
public async update(id: Instance['id'], data: Partial<Instance>): Promise<void> {
|
2023-04-22 14:12:41 +03:00
|
|
|
const result = await this.instancesRepository.createQueryBuilder().update()
|
|
|
|
.set(data)
|
|
|
|
.where('id = :id', { id })
|
|
|
|
.returning('*')
|
|
|
|
.execute()
|
|
|
|
.then((response) => {
|
|
|
|
return response.raw[0];
|
|
|
|
});
|
|
|
|
|
2023-04-22 13:59:08 +03:00
|
|
|
const updated = result.raw[0];
|
2023-01-03 02:32:36 +02:00
|
|
|
|
2023-04-22 13:59:08 +03:00
|
|
|
this.federatedInstanceCache.set(updated.host, updated);
|
2023-01-03 02:32:36 +02:00
|
|
|
}
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|