2023-07-27 00:31:52 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-06-09 23:45:11 -05:00
|
|
|
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
2023-04-13 23:50:05 -05:00
|
|
|
import * as Redis from 'ioredis';
|
2023-09-15 00:28:29 -05:00
|
|
|
import type { InstancesRepository } from '@/models/_.js';
|
2023-09-19 21:33:36 -05:00
|
|
|
import type { MiInstance } from '@/models/Instance.js';
|
2023-04-07 04:48:45 -05:00
|
|
|
import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-03 19:16:03 -06:00
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
2022-12-04 00:03:09 -06:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
@Injectable()
|
2023-06-09 23:45:11 -05:00
|
|
|
export class FederatedInstanceService implements OnApplicationShutdown {
|
2023-08-16 03:51:28 -05:00
|
|
|
public federatedInstanceCache: RedisKVCache<MiInstance | null>;
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
constructor(
|
2023-04-07 04:48:45 -05:00
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
@Inject(DI.instancesRepository)
|
|
|
|
private instancesRepository: InstancesRepository,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private idService: IdService,
|
|
|
|
) {
|
2023-08-16 03:51:28 -05:00
|
|
|
this.federatedInstanceCache = new RedisKVCache<MiInstance | null>(this.redisClient, 'federatedInstance', {
|
2023-04-22 05:59:08 -05:00
|
|
|
lifetime: 1000 * 60 * 30, // 30m
|
|
|
|
memoryCacheLifetime: 1000 * 60 * 3, // 3m
|
2023-04-07 04:48:45 -05:00
|
|
|
fetcher: (key) => this.instancesRepository.findOneBy({ host: key }),
|
|
|
|
toRedisConverter: (value) => JSON.stringify(value),
|
2023-04-07 04:55:11 -05:00
|
|
|
fromRedisConverter: (value) => {
|
|
|
|
const parsed = JSON.parse(value);
|
2023-04-08 18:02:52 -05:00
|
|
|
if (parsed == null) return null;
|
2023-04-07 04:55:11 -05: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 04:48:45 -05:00
|
|
|
});
|
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 fetch(host: string): Promise<MiInstance> {
|
2022-09-17 13:27:08 -05:00
|
|
|
host = this.utilityService.toPuny(host);
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2023-04-07 04:48:45 -05:00
|
|
|
const cached = await this.federatedInstanceCache.get(host);
|
2022-09-17 13:27:08 -05:00
|
|
|
if (cached) return cached;
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
const index = await this.instancesRepository.findOneBy({ host });
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
if (index == null) {
|
|
|
|
const i = await this.instancesRepository.insert({
|
2023-10-15 20:45:22 -05:00
|
|
|
id: this.idService.gen(),
|
2022-09-17 13:27:08 -05:00
|
|
|
host,
|
2023-01-15 14:02:38 -06:00
|
|
|
firstRetrievedAt: new Date(),
|
2022-09-17 13:27:08 -05:00
|
|
|
}).then(x => this.instancesRepository.findOneByOrFail(x.identifiers[0]));
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2023-04-07 04:48:45 -05:00
|
|
|
this.federatedInstanceCache.set(host, i);
|
2022-09-17 13:27:08 -05:00
|
|
|
return i;
|
|
|
|
} else {
|
2023-04-07 04:48:45 -05:00
|
|
|
this.federatedInstanceCache.set(host, index);
|
2022-09-17 13:27:08 -05:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
2023-01-02 18:32:36 -06:00
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 03:51:28 -05:00
|
|
|
public async update(id: MiInstance['id'], data: Partial<MiInstance>): Promise<void> {
|
2023-04-22 06:12:41 -05:00
|
|
|
const result = await this.instancesRepository.createQueryBuilder().update()
|
|
|
|
.set(data)
|
|
|
|
.where('id = :id', { id })
|
|
|
|
.returning('*')
|
|
|
|
.execute()
|
|
|
|
.then((response) => {
|
|
|
|
return response.raw[0];
|
|
|
|
});
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2023-05-09 00:57:42 -05:00
|
|
|
this.federatedInstanceCache.set(result.host, result);
|
2023-01-02 18:32:36 -06:00
|
|
|
}
|
2023-06-09 23:45:11 -05:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public dispose(): void {
|
|
|
|
this.federatedInstanceCache.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
this.dispose();
|
|
|
|
}
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|