Files
Trilium/apps/client/src/services/protected_session.ts

139 lines
4.4 KiB
TypeScript
Raw Normal View History

2025-01-09 18:07:02 +02:00
import server from "./server.js";
import protectedSessionHolder from "./protected_session_holder.js";
2019-10-20 10:00:18 +02:00
import toastService from "./toast.js";
import type { ToastOptions } from "./toast.js";
import ws from "./ws.js";
2022-12-01 13:07:23 +01:00
import appContext from "../components/app_context.js";
2021-04-16 23:01:56 +02:00
import froca from "./froca.js";
import utils from "./utils.js";
import options from "./options.js";
2025-01-09 18:07:02 +02:00
import { t } from "./i18n.js";
let protectedSessionDeferred: JQuery.Deferred<any, any, any> | null = null;
// TODO: Deduplicate with server when possible.
interface Response {
success: boolean;
}
interface Message {
taskId: string;
data: {
2025-01-09 18:07:02 +02:00
protect: boolean;
};
}
async function leaveProtectedSession() {
if (protectedSessionHolder.isProtectedSessionAvailable()) {
await protectedSessionHolder.resetProtectedSession();
}
}
/** returned promise resolves with true if new protected session was established, false if no action was necessary */
function enterProtectedSession() {
const dfd = $.Deferred();
if (!options.is("isPasswordSet")) {
appContext.triggerCommand("showPasswordNotSet");
return dfd;
}
if (protectedSessionHolder.isProtectedSessionAvailable()) {
dfd.resolve(false);
2025-01-09 18:07:02 +02:00
} else {
2023-06-30 11:18:34 +02:00
// using deferred instead of promise because it allows resolving from the outside
protectedSessionDeferred = dfd;
appContext.triggerCommand("showProtectedSessionPasswordDialog");
}
return dfd.promise();
}
async function reloadData() {
2021-04-16 22:57:37 +02:00
const allNoteIds = Object.keys(froca.notes);
2021-04-16 22:57:37 +02:00
await froca.loadInitialTree();
// make sure that all notes used in the application are loaded, including the ones not shown in the tree
await froca.reloadNotes(allNoteIds);
}
async function setupProtectedSession(password: string) {
2025-01-09 18:07:02 +02:00
const response = await server.post<Response>("login/protected", { password: password });
if (!response.success) {
2024-10-20 02:19:47 +03:00
toastService.showError(t("protected_session.wrong_password"), 3000);
return;
2017-11-04 18:18:55 -04:00
}
protectedSessionHolder.enableProtectedSession();
}
2017-11-10 22:55:19 -05:00
2025-01-09 18:07:02 +02:00
ws.subscribeToMessages(async (message) => {
if (message.type === "protectedSessionLogin") {
await reloadData();
await appContext.triggerEvent("frocaReloaded", {});
appContext.triggerEvent("protectedSessionStarted", {});
appContext.triggerCommand("closeProtectedSessionPasswordDialog");
if (protectedSessionDeferred !== null) {
protectedSessionDeferred.resolve(true);
protectedSessionDeferred = null;
}
2024-10-20 02:06:08 +03:00
toastService.showMessage(t("protected_session.started"));
2025-01-09 18:07:02 +02:00
} else if (message.type === "protectedSessionLogout") {
2021-09-17 22:34:23 +02:00
utils.reloadFrontendApp(`Protected session logout`);
}
});
2017-11-10 22:55:19 -05:00
async function protectNote(noteId: string, protect: boolean, includingSubtree: boolean) {
await enterProtectedSession();
2017-11-22 20:46:42 -05:00
await server.put(`notes/${noteId}/protect/${protect ? 1 : 0}?subtree=${includingSubtree ? 1 : 0}`);
}
2017-11-15 00:04:26 -05:00
function makeToast(message: Message, title: string, text: string): ToastOptions {
return {
id: message.taskId,
2024-10-20 02:34:33 +03:00
title,
message: text,
2019-11-09 13:32:06 +01:00
icon: message.data.protect ? "check-shield" : "shield"
};
}
2025-01-09 18:07:02 +02:00
ws.subscribeToMessages(async (message) => {
if (!("taskType" in message) || message.taskType !== "protectNotes") {
return;
}
const isProtecting = message.data?.protect;
2024-10-20 02:34:33 +03:00
const title = isProtecting ? t("protected_session.protecting-title") : t("protected_session.unprotecting-title");
2025-01-09 18:07:02 +02:00
if (message.type === "taskError") {
2019-10-20 10:00:18 +02:00
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
2025-01-09 18:07:02 +02:00
} else if (message.type === "taskProgressCount") {
2024-10-20 02:34:33 +03:00
const count = message.progressCount;
2025-01-09 18:07:02 +02:00
const text = isProtecting ? t("protected_session.protecting-in-progress", { count }) : t("protected_session.unprotecting-in-progress-count", { count });
2024-10-20 02:34:33 +03:00
toastService.showPersistent(makeToast(message, title, text));
2025-01-09 18:07:02 +02:00
} else if (message.type === "taskSucceeded") {
const text = isProtecting ? t("protected_session.protecting-finished-successfully") : t("protected_session.unprotecting-finished-successfully");
2024-10-20 02:34:33 +03:00
const toast = makeToast(message, title, text);
toast.closeAfter = 3000;
2019-10-20 10:00:18 +02:00
toastService.showPersistent(toast);
}
});
export default {
protectNote,
enterProtectedSession,
leaveProtectedSession,
setupProtectedSession
};