2018-03-25 13:41:29 -04:00
|
|
|
import treeService from './tree.js';
|
2018-04-08 09:25:35 -04:00
|
|
|
import noteDetailService from './note_detail.js';
|
2018-03-25 11:09:17 -04:00
|
|
|
import utils from './utils.js';
|
|
|
|
import server from './server.js';
|
2018-03-25 21:16:57 -04:00
|
|
|
import protectedSessionHolder from './protected_session_holder.js';
|
2019-10-20 10:00:18 +02:00
|
|
|
import toastService from "./toast.js";
|
2019-10-19 09:58:18 +02:00
|
|
|
import ws from "./ws.js";
|
2020-01-12 12:30:30 +01:00
|
|
|
import appContext from "./app_context.js";
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2018-11-25 14:12:33 +01:00
|
|
|
const $enterProtectedSessionButton = $("#enter-protected-session-button");
|
|
|
|
const $leaveProtectedSessionButton = $("#leave-protected-session-button");
|
2018-03-25 11:09:17 -04:00
|
|
|
|
|
|
|
let protectedSessionDeferred = null;
|
|
|
|
|
2018-05-31 20:00:39 -04:00
|
|
|
async function leaveProtectedSession() {
|
|
|
|
if (protectedSessionHolder.isProtectedSessionAvailable()) {
|
|
|
|
utils.reloadApp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2019-05-05 20:45:07 +02:00
|
|
|
if (protectedSessionHolder.isProtectedSessionAvailable()) {
|
|
|
|
dfd.resolve(false);
|
|
|
|
}
|
|
|
|
else {
|
2018-08-17 15:21:59 +02:00
|
|
|
// using deferred instead of promise because it allows resolving from outside
|
2018-03-25 11:09:17 -04:00
|
|
|
protectedSessionDeferred = dfd;
|
|
|
|
|
2019-09-08 11:25:57 +02:00
|
|
|
import("../dialogs/protected_session.js").then(dialog => dialog.show());
|
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
|
|
|
|
2018-12-27 20:04:59 +01:00
|
|
|
async function setupProtectedSession(password) {
|
2018-05-31 20:00:39 -04:00
|
|
|
const response = await enterProtectedSessionOnServer(password);
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (!response.success) {
|
2019-10-20 10:00:18 +02:00
|
|
|
toastService.showError("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
|
|
|
|
2020-01-08 19:28:22 +01:00
|
|
|
$("#container").addClass('protected-session-active');
|
|
|
|
|
2018-03-25 21:16:57 -04:00
|
|
|
protectedSessionHolder.setProtectedSessionId(response.protectedSessionId);
|
2019-06-19 22:23:55 +02:00
|
|
|
protectedSessionHolder.touchProtectedSession();
|
2017-11-10 22:55:19 -05:00
|
|
|
|
2020-01-24 15:44:24 +01:00
|
|
|
appContext.trigger('protectedSessionStarted');
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (protectedSessionDeferred !== null) {
|
2019-09-08 11:25:57 +02:00
|
|
|
import("../dialogs/protected_session.js").then(dialog => dialog.close());
|
2017-09-17 12:46:14 -04:00
|
|
|
|
2018-08-28 15:03:23 +02:00
|
|
|
protectedSessionDeferred.resolve(true);
|
2018-06-03 20:42:25 -04:00
|
|
|
protectedSessionDeferred = null;
|
2017-11-08 22:33:08 -05:00
|
|
|
}
|
2018-09-01 13:18:55 +02:00
|
|
|
|
2019-06-03 20:36:37 +02:00
|
|
|
$enterProtectedSessionButton.hide();
|
|
|
|
$leaveProtectedSessionButton.show();
|
|
|
|
|
2019-10-20 10:00:18 +02:00
|
|
|
toastService.showMessage("Protected session has been started.");
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-05-31 20:00:39 -04:00
|
|
|
async function enterProtectedSessionOnServer(password) {
|
2018-03-25 11:09:17 -04:00
|
|
|
return await server.post('login/protected', {
|
|
|
|
password: password
|
|
|
|
});
|
|
|
|
}
|
2017-11-10 22:55:19 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
async function protectNoteAndSendToServer() {
|
2020-01-12 12:30:30 +01:00
|
|
|
if (!appContext.getActiveTabNote() || appContext.getActiveTabNote().isProtected) {
|
2018-06-02 11:47:16 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-05 20:45:07 +02:00
|
|
|
await enterProtectedSession();
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2020-01-12 12:30:30 +01:00
|
|
|
const note = appContext.getActiveTabNote();
|
2018-03-25 23:25:17 -04:00
|
|
|
note.isProtected = true;
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2020-01-12 12:30:30 +01:00
|
|
|
await appContext.getActiveTabContext().saveNote();
|
2017-12-25 09:30:37 -05:00
|
|
|
|
2018-03-25 23:25:17 -04:00
|
|
|
treeService.setProtected(note.noteId, note.isProtected);
|
2017-11-02 23:36:58 -04:00
|
|
|
|
2019-05-05 20:45:07 +02:00
|
|
|
await noteDetailService.reload();
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-11-02 23:55:22 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
async function unprotectNoteAndSendToServer() {
|
2020-01-12 12:30:30 +01:00
|
|
|
const activeNote = appContext.getActiveTabNote();
|
2018-11-09 14:36:27 +01:00
|
|
|
|
2019-03-14 20:21:27 +01:00
|
|
|
if (!activeNote.isProtected) {
|
2019-10-20 10:00:18 +02:00
|
|
|
toastService.showAndLogError(`Note ${activeNote.noteId} is not protected`);
|
2018-11-09 14:36:27 +01:00
|
|
|
|
2018-06-02 11:47:16 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-17 15:21:59 +02:00
|
|
|
if (!protectedSessionHolder.isProtectedSessionAvailable()) {
|
|
|
|
console.log("Unprotecting notes outside of protected session is not allowed.");
|
2019-05-05 20:45:07 +02:00
|
|
|
// the reason is that it's not easy to handle even with enterProtectedSession,
|
2018-08-17 15:21:59 +02:00
|
|
|
// 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;
|
|
|
|
}
|
2017-11-03 20:01:32 -04:00
|
|
|
|
2019-03-14 20:21:27 +01:00
|
|
|
activeNote.isProtected = false;
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2020-01-12 12:30:30 +01:00
|
|
|
await appContext.getActiveTabContext().saveNote();
|
2017-12-25 09:30:37 -05:00
|
|
|
|
2019-03-14 20:21:27 +01:00
|
|
|
treeService.setProtected(activeNote.noteId, activeNote.isProtected);
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
|
2018-08-31 18:22:53 +02:00
|
|
|
async function protectSubtree(noteId, protect) {
|
2019-05-05 20:45:07 +02:00
|
|
|
await enterProtectedSession();
|
2017-11-22 20:46:42 -05:00
|
|
|
|
2018-04-01 20:33:10 -04:00
|
|
|
await server.put('notes/' + noteId + "/protect/" + (protect ? 1 : 0));
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-11-15 00:04:26 -05:00
|
|
|
|
2019-10-19 09:58:18 +02:00
|
|
|
function makeToast(message, protectingLabel, text) {
|
|
|
|
return {
|
|
|
|
id: message.taskId,
|
|
|
|
title: protectingLabel + " status",
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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 10:00:18 +02:00
|
|
|
toastService.closePersistent(message.taskId);
|
|
|
|
toastService.showError(message.message);
|
2019-10-19 09:58:18 +02:00
|
|
|
} else if (message.type === 'task-progress-count') {
|
2019-10-20 10:00:18 +02:00
|
|
|
toastService.showPersistent(makeToast(message, protectingLabel,protectingLabel + " in progress: " + message.progressCount));
|
2019-10-19 09:58:18 +02:00
|
|
|
} else if (message.type === 'task-succeeded') {
|
|
|
|
const toast = makeToast(message, protectingLabel, protectingLabel + " finished successfully.");
|
|
|
|
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 {
|
2018-08-31 18:22:53 +02:00
|
|
|
protectSubtree,
|
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
|
|
|
protectNoteAndSendToServer,
|
|
|
|
unprotectNoteAndSendToServer,
|
|
|
|
setupProtectedSession
|
2018-03-25 11:09:17 -04:00
|
|
|
};
|