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

134 lines
4 KiB
JavaScript
Raw Normal View History

import treeService from './tree.js';
2018-04-08 21:25:35 +08:00
import noteDetailService from './note_detail.js';
import utils from './utils.js';
import server from './server.js';
import protectedSessionHolder from './protected_session_holder.js';
2018-03-26 09:29:35 +08:00
import infoService from "./info.js";
import protectedSessionDialog from "../dialogs/protected_session.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;
protectedSessionDialog.show();
}
return dfd.promise();
}
async function setupProtectedSession(password) {
const response = await enterProtectedSessionOnServer(password);
if (!response.success) {
2018-11-23 04:19:12 +08:00
infoService.showError("Wrong password.", 3000);
return;
2017-11-05 06:18:55 +08:00
}
protectedSessionHolder.setProtectedSessionId(response.protectedSessionId);
2017-11-11 11:55:19 +08:00
await treeService.reload();
// it's important that tree has been already reloaded at this point since detail also uses tree cache (for children overview)
// children overview is the reason why we need to reload all tabs
await noteDetailService.reloadAllTabs();
if (protectedSessionDeferred !== null) {
protectedSessionDialog.close();
protectedSessionDeferred.resolve(true);
protectedSessionDeferred = null;
$enterProtectedSessionButton.hide();
$leaveProtectedSessionButton.show();
2017-11-09 11:33:08 +08:00
}
infoService.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 (noteDetailService.getActiveNote().isProtected) {
return;
}
await enterProtectedSession();
const note = noteDetailService.getActiveNote();
2018-03-26 11:25:17 +08:00
note.isProtected = true;
await noteDetailService.getActiveContext().saveNote();
2018-03-26 11:25:17 +08:00
treeService.setProtected(note.noteId, note.isProtected);
await noteDetailService.reload();
}
async function unprotectNoteAndSendToServer() {
const activeNote = noteDetailService.getActiveNote();
if (!activeNote.isProtected) {
infoService.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 noteDetailService.getActiveContext().saveNote();
treeService.setProtected(activeNote.noteId, activeNote.isProtected);
await noteDetailService.reload();
}
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
2018-03-26 09:29:35 +08:00
infoService.showMessage("Request to un/protect sub tree has finished successfully");
2017-11-15 13:04:26 +08:00
treeService.reload();
2018-04-08 21:25:35 +08:00
noteDetailService.reload();
}
2017-11-15 13:04:26 +08:00
export default {
protectSubtree,
enterProtectedSession,
leaveProtectedSession,
protectNoteAndSendToServer,
unprotectNoteAndSendToServer,
setupProtectedSession
};