refactor(server): fix a few cyclic dependencies regarding protected_session

This commit is contained in:
Elian Doran 2025-10-09 12:43:38 +03:00
parent b2fcf5fa6c
commit a8011e4755
No known key found for this signature in database
2 changed files with 19 additions and 18 deletions

View File

@ -1,9 +1,6 @@
"use strict";
import log from "./log.js";
import dataEncryptionService from "./encryption/data_encryption.js";
import options from "./options.js";
import ws from "./ws.js";
let dataKey: Buffer | null = null;
@ -15,11 +12,11 @@ function getDataKey() {
return dataKey;
}
function resetDataKey() {
export function resetDataKey() {
dataKey = null;
}
function isProtectedSessionAvailable() {
export function isProtectedSessionAvailable() {
return !!dataKey;
}
@ -57,15 +54,8 @@ function touchProtectedSession() {
}
}
function checkProtectedSessionExpiration() {
const protectedSessionTimeout = options.getOptionInt("protectedSessionTimeout");
if (isProtectedSessionAvailable() && lastProtectedSessionOperationDate && Date.now() - lastProtectedSessionOperationDate > protectedSessionTimeout * 1000) {
resetDataKey();
log.info("Expiring protected session");
ws.reloadFrontend("leaving protected session");
}
export function getLastProtectedSessionOperationDate() {
return lastProtectedSessionOperationDate;
}
export default {
@ -75,6 +65,5 @@ export default {
encrypt,
decrypt,
decryptString,
touchProtectedSession,
checkProtectedSessionExpiration
touchProtectedSession
};

View File

@ -4,9 +4,11 @@ import sqlInit from "./sql_init.js";
import config from "./config.js";
import log from "./log.js";
import attributeService from "../services/attributes.js";
import protectedSessionService from "../services/protected_session.js";
import hiddenSubtreeService from "./hidden_subtree.js";
import type BNote from "../becca/entities/bnote.js";
import options from "./options.js";
import { getLastProtectedSessionOperationDate, isProtectedSessionAvailable, resetDataKey } from "./protected_session.js";
import ws from "./ws.js";
function getRunAtHours(note: BNote): number[] {
try {
@ -64,5 +66,15 @@ sqlInit.dbReady.then(() => {
);
}
setInterval(() => protectedSessionService.checkProtectedSessionExpiration(), 30000);
setInterval(() => checkProtectedSessionExpiration(), 1);
});
function checkProtectedSessionExpiration() {
const protectedSessionTimeout = options.getOptionInt("protectedSessionTimeout");
const lastProtectedSessionOperationDate = getLastProtectedSessionOperationDate();
if (isProtectedSessionAvailable() && lastProtectedSessionOperationDate && Date.now() - lastProtectedSessionOperationDate > protectedSessionTimeout * 1000) {
resetDataKey();
log.info("Expiring protected session");
ws.reloadFrontend("leaving protected session");
}
}