core: cache attachment key for faster attachment reading

This commit is contained in:
Abdullah Atta
2024-08-13 14:57:52 +05:00
committed by Abdullah Atta
parent 6797b9b621
commit 5a49941003

View File

@@ -42,6 +42,7 @@ const ENDPOINTS = {
class UserManager {
private tokenManager: TokenManager;
private cachedAttachmentKey?: SerializedKey;
constructor(private readonly db: Database) {
this.tokenManager = new TokenManager(this.db.kv);
@@ -246,6 +247,7 @@ class UserManager {
} catch (e) {
logger.error(e, "Error logging out user.", { revoke, reason });
} finally {
this.cachedAttachmentKey = undefined;
await this.db.reset();
EV.publish(EVENTS.userLoggedOut, reason);
EV.publish(EVENTS.appRefreshRequested);
@@ -385,6 +387,7 @@ class UserManager {
}
async getAttachmentsKey() {
if (this.cachedAttachmentKey) return this.cachedAttachmentKey;
try {
let user = await this.getUser();
if (!user) return;
@@ -412,7 +415,8 @@ class UserManager {
.storage()
.decrypt(userEncryptionKey, user.attachmentsKey);
if (!plainData) return;
return JSON.parse(plainData) as SerializedKey;
this.cachedAttachmentKey = JSON.parse(plainData) as SerializedKey;
return this.cachedAttachmentKey;
} catch (e) {
logger.error(e, "Could not get attachments encryption key.");
if (e instanceof Error)