forked from FoundKeyGang/FoundKey
server: properly expire public key cache
Changelog: Fixed
This commit is contained in:
parent
5636534d03
commit
fc733a4a86
2 changed files with 16 additions and 12 deletions
|
@ -1,8 +1,6 @@
|
|||
import { Cache } from '@/misc/cache.js';
|
||||
import { UserPublickeys } from '@/models/index.js';
|
||||
import { IRemoteUser } from '@/models/entities/user.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 { Resolver } from '@/remote/activitypub/resolver.js';
|
||||
import { HOUR } from '@/const.js';
|
||||
|
@ -12,15 +10,6 @@ export type AuthUser = {
|
|||
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> {
|
||||
return uriPersonCache.fetch(uri)
|
||||
.then(async user => {
|
||||
|
|
|
@ -17,6 +17,15 @@ export const uriPersonCache = new Cache<User>(
|
|||
15 * MINUTE,
|
||||
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) => {
|
||||
const obj = JSON.parse(data);
|
||||
|
@ -35,6 +44,12 @@ subscriber.on('message', async (_, data) => {
|
|||
if (Users.isLocalUser(user)) {
|
||||
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 {
|
||||
userByIdCache.set(user.id, user);
|
||||
uriPersonCache.set(user.uri, user);
|
||||
|
|
Loading…
Reference in a new issue