Notes/src/public/app/services/protected_session.js

128 lines
4.0 KiB
JavaScript
Raw Normal View History

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 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";
2024-10-20 02:34:33 +03:00
import { t } from './i18n.js';
let protectedSessionDeferred = null;
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);
}
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
2021-04-16 22:57:37 +02:00
await froca.reloadNotes(allNoteIds, true);
}
async function setupProtectedSession(password) {
const response = await server.post('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
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"));
}
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, protect, includingSubtree) {
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
2024-10-20 02:34:33 +03:00
function makeToast(message, title, text) {
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"
};
}
ws.subscribeToMessages(async message => {
2021-04-24 11:39:44 +02:00
if (message.taskType !== 'protectNotes') {
return;
}
2024-10-20 02:34:33 +03:00
const isProtecting = message.data.protect;
const title = isProtecting ? t("protected_session.protecting-title") : t("protected_session.unprotecting-title");
2021-04-24 11:39:44 +02:00
if (message.type === 'taskError') {
2019-10-20 10:00:18 +02:00
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
2021-04-24 11:39:44 +02:00
} else if (message.type === 'taskProgressCount') {
2024-10-20 02:34:33 +03:00
const count = message.progressCount;
const text = ( isProtecting ? t("protected_session.protecting-in-progress", { count }) : t("protected_session.unprotecting-in-progress-count", { count }));
toastService.showPersistent(makeToast(message, title, text));
2021-04-24 11:39:44 +02:00
} else if (message.type === 'taskSucceeded') {
2024-10-20 02:34:33 +03:00
const text = (isProtecting ? t("protected_session.protecting-finished-successfully") : t("protected_session.unprotecting-finished-successfully"))
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
};