2023-01-13 05:40:33 +01:00
|
|
|
import type {
|
|
|
|
CacheableLocalUser,
|
|
|
|
CacheableUser,
|
|
|
|
ILocalUser,
|
|
|
|
} from "@/models/entities/user.js";
|
|
|
|
import { User } from "@/models/entities/user.js";
|
|
|
|
import { Users } from "@/models/index.js";
|
|
|
|
import { Cache } from "@/misc/cache.js";
|
|
|
|
import { subscriber } from "@/db/redis.js";
|
2022-03-25 08:27:41 +01:00
|
|
|
|
|
|
|
export const userByIdCache = new Cache<CacheableUser>(Infinity);
|
2023-01-13 05:40:33 +01:00
|
|
|
export const localUserByNativeTokenCache = new Cache<CacheableLocalUser | null>(
|
|
|
|
Infinity,
|
|
|
|
);
|
2022-03-25 08:27:41 +01:00
|
|
|
export const localUserByIdCache = new Cache<CacheableLocalUser>(Infinity);
|
|
|
|
export const uriPersonCache = new Cache<CacheableUser | null>(Infinity);
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
subscriber.on("message", async (_, data) => {
|
2022-03-25 08:27:41 +01:00
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
if (obj.channel === "internal") {
|
2022-03-25 08:27:41 +01:00
|
|
|
const { type, body } = obj.message;
|
|
|
|
switch (type) {
|
2023-01-13 05:40:33 +01:00
|
|
|
case "userChangeSuspendedState":
|
|
|
|
case "userChangeSilencedState":
|
|
|
|
case "userChangeModeratorState":
|
|
|
|
case "remoteUserUpdated": {
|
2022-03-26 07:34:00 +01:00
|
|
|
const user = await Users.findOneByOrFail({ id: body.id });
|
2022-03-25 08:27:41 +01:00
|
|
|
userByIdCache.set(user.id, user);
|
|
|
|
for (const [k, v] of uriPersonCache.cache.entries()) {
|
|
|
|
if (v.value?.id === user.id) {
|
|
|
|
uriPersonCache.set(k, user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Users.isLocalUser(user)) {
|
|
|
|
localUserByNativeTokenCache.set(user.token, user);
|
|
|
|
localUserByIdCache.set(user.id, user);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-01-13 05:40:33 +01:00
|
|
|
case "userTokenRegenerated": {
|
|
|
|
const user = (await Users.findOneByOrFail({
|
|
|
|
id: body.id,
|
|
|
|
})) as ILocalUser;
|
2022-03-25 08:27:41 +01:00
|
|
|
localUserByNativeTokenCache.delete(body.oldToken);
|
|
|
|
localUserByNativeTokenCache.set(body.newToken, user);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|