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";
|
2024-12-19 22:25:48 +02:00
|
|
|
import type { ToastOptions } from "./toast.js";
|
2019-10-19 09:58:18 +02:00
|
|
|
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";
|
2021-05-07 22:23:49 +02:00
|
|
|
import utils from "./utils.js";
|
2021-12-30 23:55:36 +01:00
|
|
|
import options from "./options.js";
|
2025-01-09 18:07:02 +02:00
|
|
|
import { t } from "./i18n.js";
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2024-12-19 22:25:48 +02:00
|
|
|
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;
|
|
|
|
|
};
|
2024-12-19 22:25:48 +02:00
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2018-05-31 20:00:39 -04:00
|
|
|
async function leaveProtectedSession() {
|
|
|
|
|
if (protectedSessionHolder.isProtectedSessionAvailable()) {
|
2021-05-07 22:23:49 +02:00
|
|
|
await protectedSessionHolder.resetProtectedSession();
|
2018-05-31 20:00:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-28 15:03:23 +02:00
|
|
|
/** returned promise resolves with true if new protected session was established, false if no action was necessary */
|
2019-05-05 20:45:07 +02:00
|
|
|
function enterProtectedSession() {
|
2018-03-25 11:09:17 -04:00
|
|
|
const dfd = $.Deferred();
|
|
|
|
|
|
2021-12-30 23:55:36 +01:00
|
|
|
if (!options.is("isPasswordSet")) {
|
2022-06-14 23:32:16 +02:00
|
|
|
appContext.triggerCommand("showPasswordNotSet");
|
2021-12-30 23:55:36 +01:00
|
|
|
return dfd;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-05 20:45:07 +02:00
|
|
|
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
|
2018-03-25 11:09:17 -04:00
|
|
|
protectedSessionDeferred = dfd;
|
|
|
|
|
|
2022-06-16 15:28:51 +02:00
|
|
|
appContext.triggerCommand("showProtectedSessionPasswordDialog");
|
2017-09-06 22:06:43 -04:00
|
|
|
}
|
2017-11-03 20:01:32 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
return dfd.promise();
|
|
|
|
|
}
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2020-05-03 22:49:20 +02:00
|
|
|
async function reloadData() {
|
2021-04-16 22:57:37 +02:00
|
|
|
const allNoteIds = Object.keys(froca.notes);
|
2020-05-03 22:49:20 +02:00
|
|
|
|
2021-04-16 22:57:37 +02:00
|
|
|
await froca.loadInitialTree();
|
2020-05-03 22:49:20 +02:00
|
|
|
|
|
|
|
|
// make sure that all notes used in the application are loaded, including the ones not shown in the tree
|
2024-12-19 22:25:48 +02:00
|
|
|
await froca.reloadNotes(allNoteIds);
|
2020-05-03 22:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-19 22:25:48 +02:00
|
|
|
async function setupProtectedSession(password: string) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const response = await server.post<Response>("login/protected", { password: password });
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (!response.success) {
|
2024-10-20 02:19:47 +03:00
|
|
|
toastService.showError(t("protected_session.wrong_password"), 3000);
|
2018-03-25 11:09:17 -04:00
|
|
|
return;
|
2017-11-04 18:18:55 -04:00
|
|
|
}
|
2017-09-08 20:55:24 -04:00
|
|
|
|
2021-05-07 22:23:49 +02: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") {
|
2021-05-07 22:23:49 +02:00
|
|
|
await reloadData();
|
2020-04-25 17:15:57 +02:00
|
|
|
|
2025-01-14 20:08:57 +02:00
|
|
|
await appContext.triggerEvent("frocaReloaded", {});
|
2020-04-25 17:15:57 +02:00
|
|
|
|
2025-01-14 20:08:57 +02:00
|
|
|
appContext.triggerEvent("protectedSessionStarted", {});
|
2017-09-17 12:46:14 -04:00
|
|
|
|
2022-06-16 15:28:51 +02:00
|
|
|
appContext.triggerCommand("closeProtectedSessionPasswordDialog");
|
2018-09-01 13:18:55 +02:00
|
|
|
|
2022-06-16 15:28:51 +02:00
|
|
|
if (protectedSessionDeferred !== null) {
|
2021-05-07 22:23:49 +02:00
|
|
|
protectedSessionDeferred.resolve(true);
|
|
|
|
|
protectedSessionDeferred = null;
|
|
|
|
|
}
|
2017-09-06 22:06:43 -04:00
|
|
|
|
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`);
|
2021-05-07 22:23:49 +02:00
|
|
|
}
|
|
|
|
|
});
|
2017-11-10 22:55:19 -05:00
|
|
|
|
2024-12-19 22:25:48 +02:00
|
|
|
async function protectNote(noteId: string, protect: boolean, includingSubtree: boolean) {
|
2019-05-05 20:45:07 +02:00
|
|
|
await enterProtectedSession();
|
2017-11-22 20:46:42 -05:00
|
|
|
|
2020-02-26 16:37:17 +01:00
|
|
|
await server.put(`notes/${noteId}/protect/${protect ? 1 : 0}?subtree=${includingSubtree ? 1 : 0}`);
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-11-15 00:04:26 -05:00
|
|
|
|
2024-12-19 22:25:48 +02:00
|
|
|
function makeToast(message: Message, title: string, text: string): ToastOptions {
|
2019-10-19 09:58:18 +02:00
|
|
|
return {
|
|
|
|
|
id: message.taskId,
|
2024-10-20 02:34:33 +03:00
|
|
|
title,
|
2019-10-19 09:58:18 +02:00
|
|
|
message: text,
|
2019-11-09 13:32:06 +01:00
|
|
|
icon: message.data.protect ? "check-shield" : "shield"
|
2019-10-19 09:58:18 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
ws.subscribeToMessages(async (message) => {
|
2025-09-13 12:54:53 +03:00
|
|
|
if (!("taskType" in message) || message.taskType !== "protectNotes") {
|
2019-10-19 09:58:18 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-13 13:44:23 +03:00
|
|
|
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);
|
2019-10-19 09:58:18 +02:00
|
|
|
toast.closeAfter = 3000;
|
|
|
|
|
|
2019-10-20 10:00:18 +02:00
|
|
|
toastService.showPersistent(toast);
|
2019-10-19 09:58:18 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
export default {
|
2020-02-26 16:37:17 +01:00
|
|
|
protectNote,
|
2018-05-31 20:00:39 -04:00
|
|
|
enterProtectedSession,
|
2019-01-01 15:39:13 +01:00
|
|
|
leaveProtectedSession,
|
2019-05-05 19:48:30 +02:00
|
|
|
setupProtectedSession
|
2020-06-03 11:47:30 +02:00
|
|
|
};
|