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';
|
2019-10-20 16:00:18 +08:00
|
|
|
import toastService from "./toast.js";
|
2019-10-19 15:58:18 +08:00
|
|
|
import ws from "./ws.js";
|
2020-01-12 19:30:30 +08:00
|
|
|
import appContext from "./app_context.js";
|
2020-04-25 23:15:57 +08:00
|
|
|
import treeCache from "./tree_cache.js";
|
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-09-08 17:25:57 +08:00
|
|
|
import("../dialogs/protected_session.js").then(dialog => dialog.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
|
|
|
|
2020-05-04 04:49:20 +08:00
|
|
|
async function reloadData() {
|
|
|
|
const allNoteIds = Object.keys(treeCache.notes);
|
|
|
|
|
|
|
|
await treeCache.loadInitialTree();
|
|
|
|
|
|
|
|
// make sure that all notes used in the application are loaded, including the ones not shown in the tree
|
|
|
|
await treeCache.reloadNotes(allNoteIds, true);
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-10-20 16:00:18 +08:00
|
|
|
toastService.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);
|
2019-06-20 04:23:55 +08:00
|
|
|
protectedSessionHolder.touchProtectedSession();
|
2017-11-11 11:55:19 +08:00
|
|
|
|
2020-05-04 04:49:20 +08:00
|
|
|
await reloadData();
|
2020-04-25 23:15:57 +08:00
|
|
|
|
|
|
|
await appContext.triggerEvent('treeCacheReloaded');
|
|
|
|
|
2020-02-17 02:21:17 +08:00
|
|
|
appContext.triggerEvent('protectedSessionStarted');
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (protectedSessionDeferred !== null) {
|
2019-09-08 17:25:57 +08:00
|
|
|
import("../dialogs/protected_session.js").then(dialog => dialog.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;
|
2017-11-09 11:33:08 +08:00
|
|
|
}
|
2018-09-01 19:18:55 +08:00
|
|
|
|
2019-10-20 16:00:18 +08:00
|
|
|
toastService.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
|
|
|
|
2020-02-26 23:37:17 +08:00
|
|
|
async function protectNote(noteId, protect, includingSubtree) {
|
2019-05-06 02:45:07 +08:00
|
|
|
await enterProtectedSession();
|
2017-11-23 09:46:42 +08:00
|
|
|
|
2020-02-26 23:37:17 +08:00
|
|
|
await server.put(`notes/${noteId}/protect/${protect ? 1 : 0}?subtree=${includingSubtree ? 1 : 0}`);
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-15 13:04:26 +08:00
|
|
|
|
2019-10-19 15:58:18 +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"
|
2019-10-19 15:58:18 +08: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 16:00:18 +08:00
|
|
|
toastService.closePersistent(message.taskId);
|
|
|
|
toastService.showError(message.message);
|
2019-10-19 15:58:18 +08:00
|
|
|
} 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));
|
2019-10-19 15:58:18 +08:00
|
|
|
} 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);
|
2019-10-19 15:58:18 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
export default {
|
2020-02-26 23:37:17 +08:00
|
|
|
protectNote,
|
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
|
|
|
setupProtectedSession
|
2018-03-25 23:09:17 +08:00
|
|
|
};
|