2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import type { User } from '@/models/entities/User.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { UserKeypairsRepository } from '@/models/index.js';
|
2023-04-04 15:56:47 +09:00
|
|
|
import { MemoryKVCache } from '@/misc/cache.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import type { UserKeypair } from '@/models/entities/UserKeypair.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class UserKeypairStoreService {
|
2023-04-04 15:56:47 +09:00
|
|
|
private cache: MemoryKVCache<UserKeypair>;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.userKeypairsRepository)
|
|
|
|
private userKeypairsRepository: UserKeypairsRepository,
|
|
|
|
) {
|
2023-04-04 15:56:47 +09:00
|
|
|
this.cache = new MemoryKVCache<UserKeypair>(Infinity);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public async getUserKeypair(userId: User['id']): Promise<UserKeypair> {
|
2022-09-19 03:11:50 +09:00
|
|
|
return await this.cache.fetch(userId, () => this.userKeypairsRepository.findOneByOrFail({ userId: userId }));
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
}
|