trilium/src/public/javascripts/services/protected_session.js

149 lines
4.6 KiB
JavaScript
Raw Normal View History

import treeService from './tree.js';
import utils from './utils.js';
import server from './server.js';
import protectedSessionHolder from './protected_session_holder.js';
2019-10-20 16:00:18 +08:00
import toastService from "./toast.js";
import ws from "./ws.js";
2020-01-12 19:30:30 +08:00
import appContext from "./app_context.js";
const $enterProtectedSessionButton = $("#enter-protected-session-button");
const $leaveProtectedSessionButton = $("#leave-protected-session-button");
let protectedSessionDeferred = null;
async function leaveProtectedSession() {
if (protectedSessionHolder.isProtectedSessionAvailable()) {
utils.reloadApp();
}
}
/** returned promise resolves with true if new protected session was established, false if no action was necessary */
function enterProtectedSession() {
const dfd = $.Deferred();
if (protectedSessionHolder.isProtectedSessionAvailable()) {
dfd.resolve(false);
}
else {
// using deferred instead of promise because it allows resolving from outside
protectedSessionDeferred = dfd;
2019-09-08 17:25:57 +08:00
import("../dialogs/protected_session.js").then(dialog => dialog.show());
}
return dfd.promise();
}
async function setupProtectedSession(password) {
const response = await enterProtectedSessionOnServer(password);
if (!response.success) {
2019-10-20 16:00:18 +08:00
toastService.showError("Wrong password.", 3000);
return;
2017-11-05 06:18:55 +08:00
}
$("#container").addClass('protected-session-active');
protectedSessionHolder.setProtectedSessionId(response.protectedSessionId);
protectedSessionHolder.touchProtectedSession();
2017-11-11 11:55:19 +08:00
2020-02-17 02:21:17 +08:00
appContext.triggerEvent('protectedSessionStarted');
if (protectedSessionDeferred !== null) {
2019-09-08 17:25:57 +08:00
import("../dialogs/protected_session.js").then(dialog => dialog.close());
protectedSessionDeferred.resolve(true);
protectedSessionDeferred = null;
2017-11-09 11:33:08 +08:00
}
$enterProtectedSessionButton.hide();
$leaveProtectedSessionButton.show();
2019-10-20 16:00:18 +08:00
toastService.showMessage("Protected session has been started.");
}
async function enterProtectedSessionOnServer(password) {
return await server.post('login/protected', {
password: password
});
}
2017-11-11 11:55:19 +08:00
async function protectNoteAndSendToServer() {
if (!appContext.tabManager.getActiveTabNote() || appContext.tabManager.getActiveTabNote().isProtected) {
return;
}
await enterProtectedSession();
const note = appContext.tabManager.getActiveTabNote();
2018-03-26 11:25:17 +08:00
note.isProtected = true;
await appContext.tabManager.getActiveTabContext().saveNote();
}
async function unprotectNoteAndSendToServer() {
const activeNote = appContext.tabManager.getActiveTabNote();
if (!activeNote.isProtected) {
2019-10-20 16:00:18 +08:00
toastService.showAndLogError(`Note ${activeNote.noteId} is not protected`);
return;
}
if (!protectedSessionHolder.isProtectedSessionAvailable()) {
console.log("Unprotecting notes outside of protected session is not allowed.");
// the reason is that it's not easy to handle even with enterProtectedSession,
// because we would first have to make sure the note is loaded and only then unprotect
// we used to have a bug where we would overwrite the previous note with unprotected content.
return;
}
activeNote.isProtected = false;
await appContext.tabManager.getActiveTabContext().saveNote();
}
async function protectSubtree(noteId, protect) {
await enterProtectedSession();
2017-11-23 09:46:42 +08:00
2018-04-02 08:33:10 +08:00
await server.put('notes/' + noteId + "/protect/" + (protect ? 1 : 0));
}
2017-11-15 13:04:26 +08:00
function makeToast(message, protectingLabel, text) {
return {
id: message.taskId,
title: protectingLabel + " status",
message: text,
2019-11-09 20:32:06 +08:00
icon: message.data.protect ? "check-shield" : "shield"
};
}
ws.subscribeToMessages(async message => {
if (message.taskType !== 'protect-notes') {
return;
}
const protectingLabel = message.data.protect ? "Protecting" : "Unprotecting";
if (message.type === 'task-error') {
2019-10-20 16:00:18 +08:00
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
} else if (message.type === 'task-progress-count') {
2019-10-20 16:00:18 +08:00
toastService.showPersistent(makeToast(message, protectingLabel,protectingLabel + " in progress: " + message.progressCount));
} else if (message.type === 'task-succeeded') {
const toast = makeToast(message, protectingLabel, protectingLabel + " finished successfully.");
toast.closeAfter = 3000;
2019-10-20 16:00:18 +08:00
toastService.showPersistent(toast);
}
});
export default {
protectSubtree,
enterProtectedSession,
leaveProtectedSession,
protectNoteAndSendToServer,
unprotectNoteAndSendToServer,
setupProtectedSession
};