server: properly expire public key cache

Changelog: Fixed
This commit is contained in:
Johann150 2024-03-19 18:40:34 +01:00
parent 5636534d03
commit fc733a4a86
Signed by: Johann150
GPG key ID: 9EE6577A2A06F8F1
2 changed files with 16 additions and 12 deletions

View file

@ -1,8 +1,6 @@
import { Cache } from '@/misc/cache.js';
import { UserPublickeys } from '@/models/index.js';
import { IRemoteUser } from '@/models/entities/user.js'; import { IRemoteUser } from '@/models/entities/user.js';
import { UserPublickey } from '@/models/entities/user-publickey.js'; import { UserPublickey } from '@/models/entities/user-publickey.js';
import { uriPersonCache, userByIdCache } from '@/services/user-cache.js'; import { uriPersonCache, userByIdCache, publicKeyCache, publicKeyByUserIdCache } from '@/services/user-cache.js';
import { createPerson } from '@/remote/activitypub/models/person.js'; import { createPerson } from '@/remote/activitypub/models/person.js';
import { Resolver } from '@/remote/activitypub/resolver.js'; import { Resolver } from '@/remote/activitypub/resolver.js';
import { HOUR } from '@/const.js'; import { HOUR } from '@/const.js';
@ -12,15 +10,6 @@ export type AuthUser = {
key: UserPublickey; key: UserPublickey;
}; };
const publicKeyCache = new Cache<UserPublickey>(
2 * HOUR,
(keyId) => UserPublickeys.findOneBy({ keyId }).then(x => x ?? undefined),
);
const publicKeyByUserIdCache = new Cache<UserPublickey>(
2 * HOUR,
(userId) => UserPublickeys.findOneBy({ userId }).then(x => x ?? undefined),
);
function authUserFromApId(uri: string): Promise<AuthUser | null> { function authUserFromApId(uri: string): Promise<AuthUser | null> {
return uriPersonCache.fetch(uri) return uriPersonCache.fetch(uri)
.then(async user => { .then(async user => {

View file

@ -17,6 +17,15 @@ export const uriPersonCache = new Cache<User>(
15 * MINUTE, 15 * MINUTE,
async (uri) => await Users.findOneBy({ uri, isDeleted: IsNull() }) ?? undefined, async (uri) => await Users.findOneBy({ uri, isDeleted: IsNull() }) ?? undefined,
); );
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),
);
subscriber.on('message', async (_, data) => { subscriber.on('message', async (_, data) => {
const obj = JSON.parse(data); const obj = JSON.parse(data);
@ -35,6 +44,12 @@ subscriber.on('message', async (_, data) => {
if (Users.isLocalUser(user)) { if (Users.isLocalUser(user)) {
localUserByNativeTokenCache.delete(user.token); localUserByNativeTokenCache.delete(user.token);
} }
// 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);
}
} else { } else {
userByIdCache.set(user.id, user); userByIdCache.set(user.id, user);
uriPersonCache.set(user.uri, user); uriPersonCache.set(user.uri, user);