mirror of
https://github.com/zadam/trilium.git
synced 2025-11-02 11:26:15 +01:00
server side WIP - saving encrypted note now works, changing terminology of "encrypted note" to "protected note"
This commit is contained in:
@@ -57,9 +57,9 @@ const contextMenu = (function() {
|
||||
|
||||
if (ui.cmd === "insertNoteHere") {
|
||||
const parentKey = treeUtils.getParentKey(node);
|
||||
const encryption = treeUtils.getParentEncryption(node);
|
||||
const isProtected = treeUtils.getParentEncryption(node);
|
||||
|
||||
noteEditor.createNote(node, parentKey, 'after', encryption);
|
||||
noteEditor.createNote(node, parentKey, 'after', isProtected);
|
||||
}
|
||||
else if (ui.cmd === "insertChildNote") {
|
||||
noteEditor.createNote(node, node.key, 'into');
|
||||
|
||||
@@ -58,7 +58,7 @@ const noteHistory = (function() {
|
||||
let noteTitle = historyItem.note_title;
|
||||
let noteText = historyItem.note_text;
|
||||
|
||||
if (historyItem.encryption > 0) {
|
||||
if (historyItem.is_protected) {
|
||||
noteTitle = encryption.decryptString(noteTitle);
|
||||
noteText = encryption.decryptString(noteText);
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ const encryption = (function() {
|
||||
encryptionSessionTimeout = encSessTimeout;
|
||||
}
|
||||
|
||||
function ensureEncryptionIsAvailable(requireEncryption, modal) {
|
||||
function ensureProtectedSession(requireEncryption, modal) {
|
||||
const dfd = $.Deferred();
|
||||
|
||||
if (requireEncryption && !isEncryptionAvailable()) {
|
||||
@@ -116,15 +116,6 @@ const encryption = (function() {
|
||||
return new aesjs.ModeOfOperation.ctr(dataKey, new aesjs.Counter(5));
|
||||
}
|
||||
|
||||
function encryptNoteIfNecessary(note) {
|
||||
if (note.detail.encryption === 0) {
|
||||
return note;
|
||||
}
|
||||
else {
|
||||
return encryptNote(note);
|
||||
}
|
||||
}
|
||||
|
||||
function encryptString(str) {
|
||||
return encrypt(getDataAes(), str);
|
||||
}
|
||||
@@ -184,36 +175,34 @@ const encryption = (function() {
|
||||
note.detail.note_title = encryptString(note.detail.note_title);
|
||||
note.detail.note_text = encryptString(note.detail.note_text);
|
||||
|
||||
note.detail.encryption = 1;
|
||||
note.detail.is_protected = true;
|
||||
|
||||
return note;
|
||||
}
|
||||
|
||||
async function encryptNoteAndSendToServer() {
|
||||
await ensureEncryptionIsAvailable(true, true);
|
||||
async function protectNoteAndSendToServer() {
|
||||
await ensureProtectedSession(true, true);
|
||||
|
||||
const note = noteEditor.getCurrentNote();
|
||||
|
||||
noteEditor.updateNoteFromInputs(note);
|
||||
|
||||
encryptNote(note);
|
||||
note.detail.is_protected = true;
|
||||
|
||||
await noteEditor.saveNoteToServer(note);
|
||||
|
||||
await changeEncryptionOnNoteHistory(note.detail.note_id, true);
|
||||
|
||||
noteEditor.setNoteBackgroundIfEncrypted(note);
|
||||
}
|
||||
|
||||
async function changeEncryptionOnNoteHistory(noteId, encrypt) {
|
||||
async function changeEncryptionOnNoteHistory(noteId, protect) {
|
||||
const result = await $.ajax({
|
||||
url: baseApiUrl + 'notes-history/' + noteId + "?encryption=" + (encrypt ? 0 : 1),
|
||||
url: baseApiUrl + 'notes-history/' + noteId + "?encryption=" + (protect ? 0 : 1),
|
||||
type: 'GET',
|
||||
error: () => showError("Error getting note history.")
|
||||
});
|
||||
|
||||
for (const row of result) {
|
||||
if (encrypt) {
|
||||
if (protect) {
|
||||
row.note_title = encryptString(row.note_title);
|
||||
row.note_text = encryptString(row.note_text);
|
||||
}
|
||||
@@ -222,7 +211,7 @@ const encryption = (function() {
|
||||
row.note_text = decryptString(row.note_text);
|
||||
}
|
||||
|
||||
row.encryption = encrypt ? 1 : 0;
|
||||
row.is_protected = protect;
|
||||
|
||||
await $.ajax({
|
||||
url: baseApiUrl + 'notes-history',
|
||||
@@ -236,14 +225,14 @@ const encryption = (function() {
|
||||
}
|
||||
}
|
||||
|
||||
async function decryptNoteAndSendToServer() {
|
||||
await ensureEncryptionIsAvailable(true, true);
|
||||
async function unprotectNoteAndSendToServer() {
|
||||
await ensureProtectedSession(true, true);
|
||||
|
||||
const note = noteEditor.getCurrentNote();
|
||||
|
||||
noteEditor.updateNoteFromInputs(note);
|
||||
|
||||
note.detail.encryption = 0;
|
||||
note.detail.is_protected = false;
|
||||
|
||||
await noteEditor.saveNoteToServer(note);
|
||||
|
||||
@@ -253,13 +242,13 @@ const encryption = (function() {
|
||||
}
|
||||
|
||||
async function encryptSubTree(noteId) {
|
||||
await ensureEncryptionIsAvailable(true, true);
|
||||
await ensureProtectedSession(true, true);
|
||||
|
||||
updateSubTreeRecursively(noteId, note => {
|
||||
if (note.detail.encryption === null || note.detail.encryption === 0) {
|
||||
if (!note.detail.is_protected) {
|
||||
encryptNote(note);
|
||||
|
||||
note.detail.encryption = 1;
|
||||
note.detail.is_protected = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -280,13 +269,13 @@ const encryption = (function() {
|
||||
}
|
||||
|
||||
async function decryptSubTree(noteId) {
|
||||
await ensureEncryptionIsAvailable(true, true);
|
||||
await ensureProtectedSession(true, true);
|
||||
|
||||
updateSubTreeRecursively(noteId, note => {
|
||||
if (note.detail.encryption === 1) {
|
||||
if (note.detail.is_protected) {
|
||||
decryptNote(note);
|
||||
|
||||
note.detail.encryption = 0;
|
||||
note.detail.is_protected = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -368,14 +357,13 @@ const encryption = (function() {
|
||||
|
||||
return {
|
||||
setEncryptionSessionTimeout,
|
||||
ensureEncryptionIsAvailable,
|
||||
ensureProtectedSession,
|
||||
resetEncryptionSession,
|
||||
isEncryptionAvailable,
|
||||
encryptNoteIfNecessary,
|
||||
encryptString,
|
||||
decryptString,
|
||||
encryptNoteAndSendToServer,
|
||||
decryptNoteAndSendToServer,
|
||||
protectNoteAndSendToServer,
|
||||
unprotectNoteAndSendToServer,
|
||||
encryptSubTree,
|
||||
decryptSubTree,
|
||||
getProtectedSessionId
|
||||
|
||||
@@ -59,8 +59,6 @@ const noteEditor = (function() {
|
||||
|
||||
updateNoteFromInputs(note);
|
||||
|
||||
encryption.encryptNoteIfNecessary(note);
|
||||
|
||||
await saveNoteToServer(note);
|
||||
}
|
||||
|
||||
@@ -70,7 +68,7 @@ const noteEditor = (function() {
|
||||
|
||||
note.detail.note_text = contents;
|
||||
|
||||
if (!note.detail.encryption) {
|
||||
if (!note.detail.is_protected) {
|
||||
const linkRegexp = /<a[^>]+?href="[^"]*app#([A-Za-z0-9]{22})"[^>]*?>[^<]+?<\/a>/g;
|
||||
let match;
|
||||
|
||||
@@ -170,11 +168,11 @@ const noteEditor = (function() {
|
||||
|
||||
function setTreeBasedOnEncryption(note) {
|
||||
const node = treeUtils.getNodeByKey(note.detail.note_id);
|
||||
node.toggleClass("encrypted", note.detail.encryption > 0);
|
||||
node.toggleClass("encrypted", note.detail.is_protected);
|
||||
}
|
||||
|
||||
function setNoteBackgroundIfEncrypted(note) {
|
||||
if (note.detail.encryption > 0) {
|
||||
if (note.detail.is_protected) {
|
||||
$(".note-editable").addClass("encrypted");
|
||||
encryptButton.hide();
|
||||
decryptButton.show();
|
||||
@@ -197,7 +195,7 @@ const noteEditor = (function() {
|
||||
noteTitleEl.focus().select();
|
||||
}
|
||||
|
||||
await encryption.ensureEncryptionIsAvailable(currentNote.detail.encryption > 0, false);
|
||||
await encryption.ensureProtectedSession(currentNote.detail.is_protected, false);
|
||||
|
||||
noteDetailWrapperEl.show();
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ const noteTree = (function() {
|
||||
|
||||
note.title = note.note_title;
|
||||
|
||||
if (note.encryption > 0) {
|
||||
if (note.is_protected) {
|
||||
note.extraClasses = "encrypted";
|
||||
}
|
||||
else {
|
||||
@@ -63,7 +63,7 @@ const noteTree = (function() {
|
||||
noteEditor.createNote(node, parentKey, 'after', encryption);
|
||||
},
|
||||
"ctrl+insert": node => {
|
||||
noteEditor.createNote(node, node.key, 'into', node.data.encryption);
|
||||
noteEditor.createNote(node, node.key, 'into', node.data.is_protected);
|
||||
},
|
||||
"del": node => {
|
||||
treeChanges.deleteNode(node);
|
||||
|
||||
@@ -8,7 +8,7 @@ const treeUtils = (function() {
|
||||
}
|
||||
|
||||
function getParentEncryption(node) {
|
||||
return node.getParent() === null ? 0 : node.getParent().data.encryption;
|
||||
return node.getParent() === null ? 0 : node.getParent().data.is_protected;
|
||||
}
|
||||
|
||||
function getNodeByKey(noteId) {
|
||||
|
||||
Reference in New Issue
Block a user