From ac0e5ada6e9a258574c82417131e845bac1b29c8 Mon Sep 17 00:00:00 2001 From: azivner Date: Thu, 2 Nov 2017 23:55:22 -0400 Subject: [PATCH] also de-encryption of note history plus bug fixes --- public/javascripts/encryption.js | 26 ++++++++++++++++++-------- routes/api/note_history.js | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/public/javascripts/encryption.js b/public/javascripts/encryption.js index 499d5f0f6..2303cdcf8 100644 --- a/public/javascripts/encryption.js +++ b/public/javascripts/encryption.js @@ -235,31 +235,39 @@ function encryptNoteAndSendToServer() { saveNoteToServer(note); - encryptNoteHistory(note.detail.note_id); + changeEncryptionOnNoteHistory(note.detail.note_id, true); setNoteBackgroundIfEncrypted(note); }); } -function encryptNoteHistory(noteId) { +function changeEncryptionOnNoteHistory(noteId, encrypt) { $.ajax({ - url: baseApiUrl + 'notes-history/' + noteId + "?encryption=0", + url: baseApiUrl + 'notes-history/' + noteId + "?encryption=" + (encrypt ? 0 : 1), type: 'GET', success: result => { for (const row of result) { - row.note_title = encryptString(row.note_title); - row.note_text = encryptString(row.note_text); + if (encrypt) { + row.note_title = encryptString(row.note_title); + row.note_text = encryptString(row.note_text); + } + else { + row.note_title = decryptString(row.note_title); + row.note_text = decryptString(row.note_text); + } - row.encryption = 1; + row.encryption = encrypt ? 1 : 0; $.ajax({ url: baseApiUrl + 'notes-history', type: 'PUT', contentType: 'application/json', data: JSON.stringify(row), - success: result => console.log('Note history ' + row.note_history_id + ' encrypted'), - error: () => alert("Error encrypting note history.") + success: result => console.log('Note history ' + row.note_history_id + ' de/encrypted'), + error: () => alert("Error de/encrypting note history.") }); + + break; } }, error: () => alert("Error getting note history.") @@ -276,6 +284,8 @@ function decryptNoteAndSendToServer() { saveNoteToServer(note); + changeEncryptionOnNoteHistory(note.detail.note_id, false); + setNoteBackgroundIfEncrypted(note); }); } diff --git a/routes/api/note_history.js b/routes/api/note_history.js index 192754824..cfe7477ea 100644 --- a/routes/api/note_history.js +++ b/routes/api/note_history.js @@ -21,7 +21,7 @@ router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => { res.send(history); }); -router.put('/', auth.checkApiAuth, async (req, res, next) => { +router.put('', auth.checkApiAuth, async (req, res, next) => { await sql.doInTransaction(async () => { await sql.replace("notes_history", req.body);