2022-09-17 13:27:08 -05:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import Redis from 'ioredis';
|
2022-09-20 15:33:11 -05:00
|
|
|
import type { UsersRepository } from '@/models/index.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
import { Cache } from '@/misc/cache.js';
|
2023-02-13 00:50:22 -06:00
|
|
|
import type { LocalUser, User } from '@/models/entities/User.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-03 19:16:03 -06:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2022-12-04 00:03:09 -06:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-13 17:27:23 -06:00
|
|
|
import { StreamMessages } from '@/server/api/stream/types.js';
|
2023-01-12 06:02:26 -06:00
|
|
|
import type { OnApplicationShutdown } from '@nestjs/common';
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class UserCacheService implements OnApplicationShutdown {
|
2023-02-13 00:28:07 -06:00
|
|
|
public userByIdCache: Cache<User>;
|
2023-02-13 00:50:22 -06:00
|
|
|
public localUserByNativeTokenCache: Cache<LocalUser | null>;
|
|
|
|
public localUserByIdCache: Cache<LocalUser>;
|
2023-02-13 00:28:07 -06:00
|
|
|
public uriPersonCache: Cache<User | null>;
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.redisSubscriber)
|
|
|
|
private redisSubscriber: Redis.Redis,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
) {
|
2022-12-04 00:03:09 -06:00
|
|
|
//this.onMessage = this.onMessage.bind(this);
|
2022-09-17 13:27:08 -05:00
|
|
|
|
2023-02-13 00:28:07 -06:00
|
|
|
this.userByIdCache = new Cache<User>(Infinity);
|
2023-02-13 00:50:22 -06:00
|
|
|
this.localUserByNativeTokenCache = new Cache<LocalUser | null>(Infinity);
|
|
|
|
this.localUserByIdCache = new Cache<LocalUser>(Infinity);
|
2023-02-13 00:28:07 -06:00
|
|
|
this.uriPersonCache = new Cache<User | null>(Infinity);
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
this.redisSubscriber.on('message', this.onMessage);
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-09-23 17:12:11 -05:00
|
|
|
private async onMessage(_: string, data: string): Promise<void> {
|
2022-09-17 13:27:08 -05:00
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
|
|
|
if (obj.channel === 'internal') {
|
2023-01-13 17:27:23 -06:00
|
|
|
const { type, body } = obj.message as StreamMessages['internal']['payload'];
|
2022-09-17 13:27:08 -05:00
|
|
|
switch (type) {
|
|
|
|
case 'userChangeSuspendedState':
|
|
|
|
case 'remoteUserUpdated': {
|
|
|
|
const user = await this.usersRepository.findOneByOrFail({ id: body.id });
|
2023-02-13 00:28:07 -06:00
|
|
|
this.userByIdCache.set(user.id, user);
|
2022-09-17 13:27:08 -05:00
|
|
|
for (const [k, v] of this.uriPersonCache.cache.entries()) {
|
|
|
|
if (v.value?.id === user.id) {
|
2023-02-13 00:28:07 -06:00
|
|
|
this.uriPersonCache.set(k, user);
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.userEntityService.isLocalUser(user)) {
|
|
|
|
this.localUserByNativeTokenCache.set(user.token, user);
|
|
|
|
this.localUserByIdCache.set(user.id, user);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'userTokenRegenerated': {
|
2023-02-13 00:50:22 -06:00
|
|
|
const user = await this.usersRepository.findOneByOrFail({ id: body.id }) as LocalUser;
|
2022-09-17 13:27:08 -05:00
|
|
|
this.localUserByNativeTokenCache.delete(body.oldToken);
|
|
|
|
this.localUserByNativeTokenCache.set(body.newToken, user);
|
|
|
|
break;
|
|
|
|
}
|
2023-01-13 17:27:23 -06:00
|
|
|
case 'follow': {
|
|
|
|
const follower = this.userByIdCache.get(body.followerId);
|
|
|
|
if (follower) follower.followingCount++;
|
|
|
|
const followee = this.userByIdCache.get(body.followeeId);
|
|
|
|
if (followee) followee.followersCount++;
|
|
|
|
break;
|
|
|
|
}
|
2022-09-17 13:27:08 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 17:56:06 -06:00
|
|
|
@bindThis
|
|
|
|
public findById(userId: User['id']) {
|
2023-02-13 00:28:07 -06:00
|
|
|
return this.userByIdCache.fetch(userId, () => this.usersRepository.findOneByOrFail({ id: userId }));
|
2023-01-12 17:56:06 -06:00
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-09-17 13:27:08 -05:00
|
|
|
public onApplicationShutdown(signal?: string | undefined) {
|
|
|
|
this.redisSubscriber.off('message', this.onMessage);
|
|
|
|
}
|
|
|
|
}
|