2018-03-26 01:41:29 +08:00
|
|
|
import treeService from './tree.js';
|
2018-04-08 21:25:35 +08:00
|
|
|
import noteDetailService from './note_detail.js';
|
2018-03-25 23:09:17 +08:00
|
|
|
import utils from './utils.js';
|
|
|
|
import server from './server.js';
|
2018-03-26 09:16:57 +08:00
|
|
|
import protectedSessionHolder from './protected_session_holder.js';
|
2018-03-26 09:29:35 +08:00
|
|
|
import infoService from "./info.js";
|
2019-05-06 02:45:07 +08:00
|
|
|
import protectedSessionDialog from "../dialogs/protected_session.js";
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2018-11-25 21:12:33 +08:00
|
|
|
const $enterProtectedSessionButton = $("#enter-protected-session-button");
|
|
|
|
const $leaveProtectedSessionButton = $("#leave-protected-session-button");
|
2018-03-25 23:09:17 +08:00
|
|
|
|
|
|
|
let protectedSessionDeferred = null;
|
|
|
|
|
2018-06-01 08:00:39 +08:00
|
|
|
async function leaveProtectedSession() {
|
|
|
|
if (protectedSessionHolder.isProtectedSessionAvailable()) {
|
|
|
|
utils.reloadApp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 21:03:23 +08:00
|
|
|
/** returned promise resolves with true if new protected session was established, false if no action was necessary */
|
2019-05-06 02:45:07 +08:00
|
|
|
function enterProtectedSession() {
|
2018-03-25 23:09:17 +08:00
|
|
|
const dfd = $.Deferred();
|
|
|
|
|
2019-05-06 02:45:07 +08:00
|
|
|
if (protectedSessionHolder.isProtectedSessionAvailable()) {
|
|
|
|
dfd.resolve(false);
|
|
|
|
}
|
|
|
|
else {
|
2018-08-17 21:21:59 +08:00
|
|
|
// using deferred instead of promise because it allows resolving from outside
|
2018-03-25 23:09:17 +08:00
|
|
|
protectedSessionDeferred = dfd;
|
|
|
|
|
2019-05-06 02:45:07 +08:00
|
|
|
protectedSessionDialog.show();
|
2017-09-07 10:06:43 +08:00
|
|
|
}
|
2017-11-04 08:01:32 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
return dfd.promise();
|
|
|
|
}
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2018-12-28 03:04:59 +08:00
|
|
|
async function setupProtectedSession(password) {
|
2018-06-01 08:00:39 +08:00
|
|
|
const response = await enterProtectedSessionOnServer(password);
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (!response.success) {
|
2018-11-23 04:19:12 +08:00
|
|
|
infoService.showError("Wrong password.", 3000);
|
2018-03-25 23:09:17 +08:00
|
|
|
return;
|
2017-11-05 06:18:55 +08:00
|
|
|
}
|
2017-09-09 08:55:24 +08:00
|
|
|
|
2018-03-26 09:16:57 +08:00
|
|
|
protectedSessionHolder.setProtectedSessionId(response.protectedSessionId);
|
2017-11-11 11:55:19 +08:00
|
|
|
|
2018-11-30 17:33:40 +08:00
|
|
|
await treeService.reload();
|
2018-11-09 21:36:27 +08:00
|
|
|
|
2019-05-06 02:45:07 +08:00
|
|
|
// 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();
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (protectedSessionDeferred !== null) {
|
2019-05-06 02:45:07 +08:00
|
|
|
protectedSessionDialog.close();
|
2017-09-18 00:46:14 +08:00
|
|
|
|
2018-08-28 21:03:23 +08:00
|
|
|
protectedSessionDeferred.resolve(true);
|
2018-06-04 08:42:25 +08:00
|
|
|
protectedSessionDeferred = null;
|
2018-01-27 11:32:44 +08:00
|
|
|
|
2018-11-25 21:12:33 +08:00
|
|
|
$enterProtectedSessionButton.hide();
|
|
|
|
$leaveProtectedSessionButton.show();
|
2017-11-09 11:33:08 +08:00
|
|
|
}
|
2018-09-01 19:18:55 +08:00
|
|
|
|
|
|
|
infoService.showMessage("Protected session has been started.");
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2018-06-01 08:00:39 +08:00
|
|
|
async function enterProtectedSessionOnServer(password) {
|
2018-03-25 23:09:17 +08:00
|
|
|
return await server.post('login/protected', {
|
|
|
|
password: password
|
|
|
|
});
|
|
|
|
}
|
2017-11-11 11:55:19 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function protectNoteAndSendToServer() {
|
2019-03-15 03:21:27 +08:00
|
|
|
if (noteDetailService.getActiveNote().isProtected) {
|
2018-06-02 23:47:16 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-06 02:45:07 +08:00
|
|
|
await enterProtectedSession();
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
const note = noteDetailService.getActiveNote();
|
2018-03-26 11:25:17 +08:00
|
|
|
note.isProtected = true;
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2019-05-06 02:45:07 +08:00
|
|
|
await noteDetailService.getActiveContext().saveNote();
|
2017-12-25 22:30:37 +08:00
|
|
|
|
2018-03-26 11:25:17 +08:00
|
|
|
treeService.setProtected(note.noteId, note.isProtected);
|
2017-11-03 11:36:58 +08:00
|
|
|
|
2019-05-06 02:45:07 +08:00
|
|
|
await noteDetailService.reload();
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-03 11:55:22 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function unprotectNoteAndSendToServer() {
|
2019-03-15 03:21:27 +08:00
|
|
|
const activeNote = noteDetailService.getActiveNote();
|
2018-11-09 21:36:27 +08:00
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
if (!activeNote.isProtected) {
|
|
|
|
infoService.showAndLogError(`Note ${activeNote.noteId} is not protected`);
|
2018-11-09 21:36:27 +08:00
|
|
|
|
2018-06-02 23:47:16 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-17 21:21:59 +08:00
|
|
|
if (!protectedSessionHolder.isProtectedSessionAvailable()) {
|
|
|
|
console.log("Unprotecting notes outside of protected session is not allowed.");
|
2019-05-06 02:45:07 +08:00
|
|
|
// the reason is that it's not easy to handle even with enterProtectedSession,
|
2018-08-17 21:21:59 +08: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-04 08:01:32 +08:00
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
activeNote.isProtected = false;
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2019-05-06 02:45:07 +08:00
|
|
|
await noteDetailService.getActiveContext().saveNote();
|
2017-12-25 22:30:37 +08:00
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
treeService.setProtected(activeNote.noteId, activeNote.isProtected);
|
2017-09-18 00:46:14 +08:00
|
|
|
|
2019-05-06 02:45:07 +08:00
|
|
|
await noteDetailService.reload();
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
|
2018-09-01 00:22:53 +08:00
|
|
|
async function protectSubtree(noteId, protect) {
|
2019-05-06 02:45:07 +08:00
|
|
|
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
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
treeService.reload();
|
2018-04-08 21:25:35 +08:00
|
|
|
noteDetailService.reload();
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-15 13:04:26 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
export default {
|
2018-09-01 00:22:53 +08:00
|
|
|
protectSubtree,
|
2018-06-01 08:00:39 +08:00
|
|
|
enterProtectedSession,
|
2019-01-01 22:39:13 +08:00
|
|
|
leaveProtectedSession,
|
2019-05-06 01:48:30 +08:00
|
|
|
protectNoteAndSendToServer,
|
|
|
|
unprotectNoteAndSendToServer,
|
|
|
|
setupProtectedSession
|
2018-03-25 23:09:17 +08:00
|
|
|
};
|