2022-12-07 19:59:08 +00:00
|
|
|
import { IsNull } from 'typeorm';
|
2023-05-18 11:25:57 +00:00
|
|
|
import { ILocalUser, User } from '@/models/entities/user.js';
|
2022-03-25 07:27:41 +00:00
|
|
|
import { Users } from '@/models/index.js';
|
|
|
|
import { Cache } from '@/misc/cache.js';
|
2022-08-04 08:20:48 +00:00
|
|
|
import { subscriber } from '@/db/redis.js';
|
2023-11-04 10:54:13 +00:00
|
|
|
import { MINUTE } from '@/const.js';
|
2022-03-25 07:27:41 +00:00
|
|
|
|
2022-12-08 00:32:05 +00:00
|
|
|
export const userByIdCache = new Cache<User>(
|
2023-11-04 10:54:13 +00:00
|
|
|
15 * MINUTE,
|
2023-05-01 10:50:39 +00:00
|
|
|
async (id) => await Users.findOneBy({ id, isDeleted: IsNull() }) ?? undefined,
|
2022-11-13 17:45:31 +00:00
|
|
|
);
|
2023-05-18 11:25:57 +00:00
|
|
|
export const localUserByNativeTokenCache = new Cache<ILocalUser>(
|
2023-11-04 10:54:13 +00:00
|
|
|
15 * MINUTE,
|
2023-05-23 20:44:15 +00:00
|
|
|
async (token) => await Users.findOneBy({ token, host: IsNull(), isDeleted: IsNull() }) as ILocalUser | null ?? undefined,
|
2022-11-13 17:45:31 +00:00
|
|
|
);
|
2022-12-08 00:32:05 +00:00
|
|
|
export const uriPersonCache = new Cache<User>(
|
2023-11-04 10:54:13 +00:00
|
|
|
15 * MINUTE,
|
2023-05-01 10:50:39 +00:00
|
|
|
async (uri) => await Users.findOneBy({ uri, isDeleted: IsNull() }) ?? undefined,
|
2022-11-13 17:45:31 +00:00
|
|
|
);
|
2024-03-19 17:40:34 +00:00
|
|
|
export const publicKeyCache = new Cache<UserPublickey>(
|
|
|
|
2 * HOUR,
|
|
|
|
(keyId) => UserPublickeys.findOneBy({ keyId }).then(x => x ?? undefined),
|
|
|
|
);
|
|
|
|
export const publicKeyByUserIdCache = new Cache<UserPublickey>(
|
|
|
|
2 * HOUR,
|
|
|
|
(userId) => UserPublickeys.findOneBy({ userId }).then(x => x ?? undefined),
|
|
|
|
);
|
|
|
|
|
2022-03-25 07:27:41 +00:00
|
|
|
|
2022-08-04 08:20:48 +00:00
|
|
|
subscriber.on('message', async (_, data) => {
|
2022-03-25 07:27:41 +00:00
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
|
|
|
if (obj.channel === 'internal') {
|
|
|
|
const { type, body } = obj.message;
|
|
|
|
switch (type) {
|
|
|
|
case 'userChangeSuspendedState':
|
|
|
|
case 'userChangeSilencedState':
|
|
|
|
case 'userChangeModeratorState':
|
|
|
|
case 'remoteUserUpdated': {
|
2022-03-26 06:34:00 +00:00
|
|
|
const user = await Users.findOneByOrFail({ id: body.id });
|
2023-03-22 17:29:19 +00:00
|
|
|
if (user.isDeleted) {
|
|
|
|
userByIdCache.delete(user.id);
|
|
|
|
uriPersonCache.delete(user.uri);
|
|
|
|
if (Users.isLocalUser(user)) {
|
|
|
|
localUserByNativeTokenCache.delete(user.token);
|
|
|
|
}
|
2024-03-19 17:40:34 +00:00
|
|
|
// using get here because it does not fetch from the database
|
|
|
|
const publicKey = publicKeyByUserIdCache.get(user.id);
|
|
|
|
if (publicKey != null) {
|
|
|
|
publicKeyByUserIdCache.delete(user.id);
|
|
|
|
publicKeyCache.delete(publicKey.keyId);
|
|
|
|
}
|
2023-03-22 17:29:19 +00:00
|
|
|
} else {
|
|
|
|
userByIdCache.set(user.id, user);
|
|
|
|
uriPersonCache.set(user.uri, user);
|
|
|
|
if (Users.isLocalUser(user)) {
|
|
|
|
localUserByNativeTokenCache.set(user.token, user);
|
2022-03-25 07:27:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'userTokenRegenerated': {
|
2022-03-26 06:34:00 +00:00
|
|
|
const user = await Users.findOneByOrFail({ id: body.id }) as ILocalUser;
|
2022-03-25 07:27:41 +00:00
|
|
|
localUserByNativeTokenCache.delete(body.oldToken);
|
|
|
|
localUserByNativeTokenCache.set(body.newToken, user);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|