refactor: fix types

This commit is contained in:
syuilo 2023-02-13 15:28:07 +09:00
parent a71682f6f0
commit f34f9f6ea5
19 changed files with 95 additions and 102 deletions

View file

@ -2,7 +2,7 @@ import { Inject, Injectable } from '@nestjs/common';
import Redis from 'ioredis';
import type { UsersRepository } from '@/models/index.js';
import { Cache } from '@/misc/cache.js';
import type { CacheableLocalUser, CacheableUser, ILocalUser, User } from '@/models/entities/User.js';
import type { ILocalUser, User } from '@/models/entities/User.js';
import { DI } from '@/di-symbols.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { bindThis } from '@/decorators.js';
@ -11,10 +11,10 @@ import type { OnApplicationShutdown } from '@nestjs/common';
@Injectable()
export class UserCacheService implements OnApplicationShutdown {
public userByIdCache: Cache<CacheableUser>;
public localUserByNativeTokenCache: Cache<CacheableLocalUser | null>;
public localUserByIdCache: Cache<CacheableLocalUser>;
public uriPersonCache: Cache<CacheableUser | null>;
public userByIdCache: Cache<User>;
public localUserByNativeTokenCache: Cache<ILocalUser | null>;
public localUserByIdCache: Cache<ILocalUser>;
public uriPersonCache: Cache<User | null>;
constructor(
@Inject(DI.redisSubscriber)
@ -27,10 +27,10 @@ export class UserCacheService implements OnApplicationShutdown {
) {
//this.onMessage = this.onMessage.bind(this);
this.userByIdCache = new Cache<CacheableUser>(Infinity);
this.localUserByNativeTokenCache = new Cache<CacheableLocalUser | null>(Infinity);
this.localUserByIdCache = new Cache<CacheableLocalUser>(Infinity);
this.uriPersonCache = new Cache<CacheableUser | null>(Infinity);
this.userByIdCache = new Cache<User>(Infinity);
this.localUserByNativeTokenCache = new Cache<ILocalUser | null>(Infinity);
this.localUserByIdCache = new Cache<ILocalUser>(Infinity);
this.uriPersonCache = new Cache<User | null>(Infinity);
this.redisSubscriber.on('message', this.onMessage);
}
@ -45,10 +45,10 @@ export class UserCacheService implements OnApplicationShutdown {
case 'userChangeSuspendedState':
case 'remoteUserUpdated': {
const user = await this.usersRepository.findOneByOrFail({ id: body.id });
this.userByIdCache.set(user.id, user as CacheableUser);
this.userByIdCache.set(user.id, user);
for (const [k, v] of this.uriPersonCache.cache.entries()) {
if (v.value?.id === user.id) {
this.uriPersonCache.set(k, user as CacheableUser);
this.uriPersonCache.set(k, user);
}
}
if (this.userEntityService.isLocalUser(user)) {
@ -78,7 +78,7 @@ export class UserCacheService implements OnApplicationShutdown {
@bindThis
public findById(userId: User['id']) {
return this.userByIdCache.fetch(userId, () => this.usersRepository.findOneByOrFail({ id: userId }) as Promise<CacheableUser>);
return this.userByIdCache.fetch(userId, () => this.usersRepository.findOneByOrFail({ id: userId }));
}
@bindThis