Work on making the Journal Entry saving better.

This commit is contained in:
Scott Idem
2025-05-08 19:37:15 -04:00
parent 0e249b2e6d
commit 8c81f6f65e
5 changed files with 437 additions and 133 deletions

View File

@@ -411,7 +411,9 @@ export async function update_ae_obj__event_presentation(
if (event_presentation_obj_update_result) { if (event_presentation_obj_update_result) {
if (try_cache) { if (try_cache) {
db_save_ae_obj_li__event_presentation({ db_save_ae_obj_li__event_presentation({
obj_type: 'event_presentation', obj_li: [event_presentation_obj_update_result] obj_type: 'event_presentation',
obj_li: [event_presentation_obj_update_result],
log_lvl: log_lvl
}); });
} }
return event_presentation_obj_update_result; return event_presentation_obj_update_result;

View File

@@ -261,27 +261,90 @@ export async function delete_ae_obj_id__journal_entry(
// Updated 2025-03-15 // Updated 2025-03-15
// export async function update_ae_obj__journal_entry(
// {
// api_cfg,
// journal_entry_id,
// data_kv,
// params = {},
// try_cache = true,
// log_lvl = 0
// }: {
// api_cfg: any,
// journal_entry_id: string,
// data_kv: key_val,
// params?: key_val,
// try_cache?: boolean,
// log_lvl?: number
// }
// ) {
// if (log_lvl) {
// console.log(`*** update_ae_obj__journal_entry() *** journal_entry_id=${journal_entry_id}`, data_kv);
// }
// ae_promises.update__journal_entry_obj = api.update_ae_obj_id_crud({
// api_cfg: api_cfg,
// obj_type: 'journal_entry',
// obj_id: journal_entry_id,
// fields: data_kv,
// key: api_cfg.api_crud_super_key,
// params: params,
// return_obj: true,
// log_lvl: log_lvl
// })
// .then(async function (journal_entry_obj_update_result) {
// // if (journal_entry_obj_update_result) {
// if (try_cache) {
// db_save_ae_obj_li__journal_entry({
// obj_type: 'journal_entry',
// obj_li: [journal_entry_obj_update_result],
// log_lvl: log_lvl
// })
// .then(function (result) {
// return result;
// })
// .finally(function () {
// return journal_entry_obj_update_result;
// });
// } else {
// return journal_entry_obj_update_result;
// }
// // } else {
// // return null;
// // }
// })
// .catch(function (error) {
// console.log('No results returned or failed.', error);
// })
// .finally(function () {
// });
// if (log_lvl) {
// console.log('ae_promises.update__journal_entry_obj:', ae_promises.update__journal_entry_obj);
// }
// return await ae_promises.update__journal_entry_obj;
// }
export async function update_ae_obj__journal_entry( export async function update_ae_obj__journal_entry(
{ {
api_cfg, api_cfg,
journal_entry_id, journal_entry_id,
data_kv, data_kv,
params = {}, params = {},
try_cache = true, try_cache = true,
log_lvl = 0 log_lvl = 0,
}: { }: {
api_cfg: any, api_cfg: any;
journal_entry_id: string, journal_entry_id: string;
data_kv: key_val, data_kv: key_val;
params?: key_val, params?: key_val,
try_cache?: boolean, try_cache?: boolean,
log_lvl?: number log_lvl?: number;
} }) {
) {
if (log_lvl) { if (log_lvl) {
console.log(`*** update_ae_obj__journal_entry() *** journal_entry_id=${journal_entry_id}`, data_kv); console.log(`*** update_ae_obj__journal_entry() *** journal_entry_id=${journal_entry_id}`, data_kv);
} }
ae_promises.update__journal_entry_obj = await api.update_ae_obj_id_crud({
const result = await api.update_ae_obj_id_crud({
api_cfg: api_cfg, api_cfg: api_cfg,
obj_type: 'journal_entry', obj_type: 'journal_entry',
obj_id: journal_entry_id, obj_id: journal_entry_id,
@@ -289,31 +352,24 @@ export async function update_ae_obj__journal_entry(
key: api_cfg.api_crud_super_key, key: api_cfg.api_crud_super_key,
params: params, params: params,
return_obj: true, return_obj: true,
log_lvl: log_lvl log_lvl: log_lvl,
})
.then(async function (journal_entry_obj_update_result) {
if (journal_entry_obj_update_result) {
if (try_cache) {
await db_save_ae_obj_li__journal_entry({
obj_type: 'journal_entry', obj_li: [journal_entry_obj_update_result],
log_lvl: log_lvl
});
}
return journal_entry_obj_update_result;
} else {
return null;
}
})
.catch(function (error) {
console.log('No results returned or failed.', error);
})
.finally(function () {
}); });
if (log_lvl) { if (result) {
console.log('ae_promises.update__journal_entry_obj:', ae_promises.update__journal_entry_obj); if (try_cache) {
console.log('Saving to DB...');
await db_save_ae_obj_li__journal_entry({
obj_type: 'journal_entry',
obj_li: [result],
log_lvl: log_lvl,
});
console.log('DB save completed.');
}
return result;
} else {
console.error('Failed to update journal entry.');
return null;
} }
return ae_promises.update__journal_entry_obj;
} }
@@ -331,11 +387,14 @@ export async function db_save_ae_obj_li__journal_entry(
} }
) { ) {
if (log_lvl) { if (log_lvl) {
console.log(`*** db_save_ae_obj_li__journal_entry() ***`, log_lvl); console.log(`*** db_save_ae_obj_li__journal_entry() *** obj_type=${obj_type}`, obj_li);
} }
if (obj_li && obj_li.length) { if (obj_li && obj_li.length) {
obj_li.forEach(async function (obj: any) { // let obj_li_id = obj_li.map((obj: any) => obj.journal_entry_id_random);
let obj_li_id: string[] = [];
for (const obj of obj_li) {
// obj_li.forEach(async function (obj: any) {
if (log_lvl) { if (log_lvl) {
console.log(`ae_obj ${obj_type}:`, obj); console.log(`ae_obj ${obj_type}:`, obj);
} }
@@ -501,6 +560,7 @@ export async function db_save_ae_obj_li__journal_entry(
if (log_lvl) { if (log_lvl) {
console.log(`Updated record with ID: ${obj_record.id}`); console.log(`Updated record with ID: ${obj_record.id}`);
} }
obj_li_id.push(obj_record.id);
} }
if (!id_random) { if (!id_random) {
console.log(`Failed to save record with ID: ${obj_record.id}`); console.log(`Failed to save record with ID: ${obj_record.id}`);
@@ -509,8 +569,9 @@ export async function db_save_ae_obj_li__journal_entry(
console.log(`Saved record with ID: ${obj_record.id}`); console.log(`Saved record with ID: ${obj_record.id}`);
} }
} }
}); // });
}
return true; return obj_li_id;
} }
} }

View File

@@ -88,6 +88,7 @@ let journals_session_data_struct: key_val = {
tmp_obj: {}, tmp_obj: {},
}, },
entry: { entry: {
decrypt_kv: {}, // Essentially flag that the entry (content and history) can be decrypted.
edit: false, edit: false,
edit_kv: {}, edit_kv: {},

View File

@@ -1,12 +1,13 @@
let log_lvl = 1; // 0 = no logging, 1 = some logging, 2 = all logging let log_lvl = 0; // 0 = no logging, 1 = some logging, 2 = all logging
// Updated 2025-05-08
async function generate_iv() { async function generate_iv() {
const data = new Uint8Array(16); const data = new Uint8Array(16);
crypto.getRandomValues(data); crypto.getRandomValues(data);
return data; return data;
} }
// Updated 2025-05-08
export let encrypt_content = async function encrypt_content( export let encrypt_content = async function encrypt_content(
content: string, content: string,
keyData: string keyData: string
@@ -16,36 +17,52 @@ export let encrypt_content = async function encrypt_content(
const key = await crypto.subtle.importKey('raw', keyBytes, { name: 'AES-CBC' }, false, ['encrypt']); const key = await crypto.subtle.importKey('raw', keyBytes, { name: 'AES-CBC' }, false, ['encrypt']);
const encodedContent = await crypto.subtle.encrypt({ name: 'AES-CBC', iv }, key, new TextEncoder().encode(content)); const encodedContent = await crypto.subtle.encrypt({ name: 'AES-CBC', iv }, key, new TextEncoder().encode(content));
const base64 = btoa(String.fromCharCode(...new Uint8Array(encodedContent))); const base64 = btoa(String.fromCharCode(...new Uint8Array(encodedContent)));
console.log('Base64 Encoded Content:', base64); if (log_lvl) {
console.log(`IV: ${iv}; Encrypted:`, base64);
}
return { base64, iv }; return { base64, iv };
} }
// Updated 2025-05-08
export let combine_iv_and_base64 = function combine_iv_and_base64( export let combine_iv_and_base64 = function combine_iv_and_base64(
base64: string, base64: string,
iv: Uint8Array iv: Uint8Array
) { ) {
console.log(`IV: ${iv}; Encrypted: ${base64}`); if (log_lvl) {
console.log(`IV: ${iv}; Encrypted:`, base64);
}
// Combine the IV and encrypted content // Combine the IV and encrypted content
const combined = Array.from(iv).map(byte => byte.toString(16).padStart(2, '0')).join('') + ':' + base64; const combined = Array.from(iv).map(byte => byte.toString(16).padStart(2, '0')).join('') + ':' + base64;
console.log('Combined Data v1:', combined); if (log_lvl) {
console.log('Combined IV and Base64:', combined);
}
// const ivBase64 = btoa(String.fromCharCode(...iv)); // const ivBase64 = btoa(String.fromCharCode(...iv));
// const combined = `${ivBase64}:${base64}`; // const combined = `${ivBase64}:${base64}`;
// console.log('Combined IV and Base64 v2:', combined); // console.log('Combined IV and Base64 v2:', combined);
return combined; return combined;
} }
// Updated 2025-05-08
export let encrypt_wrapper = async function encrypt_wrapper( export let encrypt_wrapper = async function encrypt_wrapper(
content: string, content: string,
keyData: string keyData: string
) { ) {
if (!content) {
console.error('No content provided. Returning empty string.');
return '';
}
if (!keyData) {
console.error('No keyData provided. Returning empty string.');
return '';
}
const { base64, iv } = await encrypt_content(content, keyData); const { base64, iv } = await encrypt_content(content, keyData);
const combined = combine_iv_and_base64(base64, iv); const combined = combine_iv_and_base64(base64, iv);
return combined; return combined;
} }
// This does not handle errors (invalid key/password) well. // This does not handle errors (invalid key/password) well.
// Updated 2025-05-08
export let decrypt_content = async function decrypt_content( export let decrypt_content = async function decrypt_content(
base64Content: string, base64Content: string,
iv: Uint8Array, iv: Uint8Array,
@@ -60,14 +77,19 @@ export let decrypt_content = async function decrypt_content(
return decodedContent; return decodedContent;
} }
// Updated 2025-05-08
export let split_iv_and_base64 = function split_iv_and_base64( export let split_iv_and_base64 = function split_iv_and_base64(
combined: string combined: string
) { ) {
if (!combined) {
console.error('No combined string provided. Returning empty object.');
return { iv: new Uint8Array(), base64: '' };
}
let [iv_hex, encrypted_base64_string] = combined.split(':'); let [iv_hex, encrypted_base64_string] = combined.split(':');
let base64 = encrypted_base64_string let base64 = encrypted_base64_string
let iv = new Uint8Array(iv_hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); let iv = new Uint8Array(iv_hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
if (log_lvl) { if (log_lvl) {
console.log(`IV: ${iv}; Encrypted: ${base64}`); console.log(`IV: ${iv}; Encrypted:`, base64);
} }
// const [ivBase64, base64] = combined.split(':'); // const [ivBase64, base64] = combined.split(':');
@@ -78,14 +100,21 @@ export let split_iv_and_base64 = function split_iv_and_base64(
return { iv, base64 }; return { iv, base64 };
} }
// Updated 2025-05-08
export let decrypt_wrapper = async function decrypt_wrapper( export let decrypt_wrapper = async function decrypt_wrapper(
combined: string, combined: string,
keyData: string keyData: string
) { ) {
if (!combined) {
console.error('No combined string provided. Returning empty string.');
return '';
}
const { iv, base64 } = split_iv_and_base64(combined); const { iv, base64 } = split_iv_and_base64(combined);
const decrypted = await decrypt_content(base64, iv, keyData); const decrypted = await decrypt_content(base64, iv, keyData);
if (log_lvl) { if (log_lvl > 1) {
console.log(`IV: ${iv}; Decrypted:`, decrypted); console.log(`IV: ${iv}; Decrypted:`, decrypted);
} else if (log_lvl) {
console.log(`IV: ${iv}`);
} }
return decrypted; return decrypted;
} }

View File

@@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
// *** Import Svelte specific // *** Import Svelte specific
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { tick } from 'svelte';
// import { clipboard } from '@skeletonlabs/skeleton'; // import { clipboard } from '@skeletonlabs/skeleton';
import { marked } from 'marked'; import { marked } from 'marked';
import { import {
@@ -71,6 +72,11 @@ let updated_idb: boolean = $state(true); // Updated in a separate browser sessio
// } // }
// ); // );
if (!$journals_loc?.entry?.decrypt_kv) {
$journals_loc.entry.decrypt_kv = {};
$journals_loc.entry.decrypt_kv[$lq__journal_entry_obj?.journal_entry_id] = null;
}
function not_obj(obj) { function not_obj(obj) {
if (obj == null) { if (obj == null) {
// null or undefined // null or undefined
@@ -82,27 +88,119 @@ function not_obj(obj) {
return Object.keys(obj).length === 0; return Object.keys(obj).length === 0;
} }
$effect(() => { $effect(async () => {
// if (not_obj(orig_entry_obj) && not_obj(tmp_entry_obj) && $lq__journal_entry_obj && $lq__journal_entry_obj?.journal_entry_id) { // if (not_obj(orig_entry_obj) && not_obj(tmp_entry_obj) && $lq__journal_entry_obj && $lq__journal_entry_obj?.journal_entry_id) {
if (updated_obj && JSON.stringify(orig_entry_obj) === JSON.stringify($lq__journal_entry_obj)) { // if (updated_obj && JSON.stringify(orig_entry_obj) === JSON.stringify($lq__journal_entry_obj)) {
console.log(`TEST: updated_obj but orig_entry_obj and $lq__journal_entry_obj still the same`, $lq__journal_entry_obj); // console.log(`TEST: updated_obj but orig_entry_obj and $lq__journal_entry_obj still the same`, $lq__journal_entry_obj);
// orig_entry_obj = { ...$lq__journal_entry_obj }; // // orig_entry_obj = { ...$lq__journal_entry_obj };
// tmp_entry_obj = { ...$lq__journal_entry_obj }; // // tmp_entry_obj = { ...$lq__journal_entry_obj };
// tmp_entry_obj_changed = false; // // tmp_entry_obj_changed = false;
// // updated_obj = false;
// // } else if (not_obj(orig_entry_obj) && $lq__journal_entry_obj) {
// // console.log('TEST: orig_entry_obj?', orig_entry_obj);
// // console.log('TEST: LQ Journal Entry object available', $lq__journal_entry_obj);
// // orig_entry_obj = { ...$lq__journal_entry_obj };
// // tmp_entry_obj = { ...$lq__journal_entry_obj };
// // tmp_entry_obj_changed = false;
// } else
// || (updated_obj && await $lq__journal_entry_obj.updated_on > orig_entry_obj?.updated_on)
// if (updated_obj && $lq__journal_entry_obj && JSON.stringify(orig_entry_obj) !== JSON.stringify($lq__journal_entry_obj)) {
// if (updated_obj && $lq__journal_entry_obj && $lq__journal_entry_obj?.updated_on > orig_entry_obj?.updated_on) {
if ($lq__journal_entry_obj && $lq__journal_entry_obj?.updated_on) {
console.log(`TEST: Journal Entry updated datetime changed`, $lq__journal_entry_obj);
// updated_obj = false; // updated_obj = false;
// } else if (not_obj(orig_entry_obj) && $lq__journal_entry_obj) {
// console.log('TEST: orig_entry_obj?', orig_entry_obj);
// console.log('TEST: LQ Journal Entry object available', $lq__journal_entry_obj);
// orig_entry_obj = { ...$lq__journal_entry_obj };
// tmp_entry_obj = { ...$lq__journal_entry_obj };
// tmp_entry_obj_changed = false;
} else if (updated_obj && $lq__journal_entry_obj) {
console.log(`TEST: updated_obj and $lq__journal_entry_obj; setting orig_entry_obj and tmp_entry_obj`, $lq__journal_entry_obj);
orig_entry_obj = { ...$lq__journal_entry_obj }; orig_entry_obj = { ...$lq__journal_entry_obj };
tmp_entry_obj = { ...$lq__journal_entry_obj }; tmp_entry_obj = { ...$lq__journal_entry_obj };
// if ($journals_loc?.entry?.decrypt_kv[$lq__journal_entry_obj?.journal_entry_id]) {
// if (!tmp_entry_obj?.content && tmp_entry_obj?.content_encrypted) {
// console.log('TEST: Decrypting the content...');
// tmp_entry_obj.content = await ae_util.decrypt_wrapper(tmp_entry_obj?.content_encrypted, journal_key);
// tmp_entry_obj.content_md_html = handle_marked(tmp_entry_obj?.content);
// orig_entry_obj.content = await ae_util.decrypt_wrapper(orig_entry_obj?.content_encrypted, journal_key);
// orig_entry_obj.content_md_html = handle_marked(orig_entry_obj?.content);
// }
// if (!tmp_entry_obj?.history && tmp_entry_obj?.history_encrypted) {
// console.log('TEST: Decrypting the history...');
// tmp_entry_obj.history = await ae_util.decrypt_wrapper(tmp_entry_obj?.history_encrypted, journal_key);
// tmp_entry_obj.history_md_html = handle_marked(tmp_entry_obj?.history);
// orig_entry_obj.history = await ae_util.decrypt_wrapper(orig_entry_obj?.history_encrypted, journal_key);
// orig_entry_obj.history_md_html = handle_marked(orig_entry_obj?.history);
// }
// }
tmp_entry_obj_changed = false; tmp_entry_obj_changed = false;
updated_obj = false; // updated_obj = false;
updated_idb = false; updated_idb = false;
} else if ((updated_obj && await $lq__journal_entry_obj) ) {
console.log(`TEST: Journal Entry updated and changed`, $lq__journal_entry_obj);
updated_obj = false;
orig_entry_obj = { ...$lq__journal_entry_obj };
tmp_entry_obj = { ...$lq__journal_entry_obj };
if ($journals_loc?.entry?.decrypt_kv[$lq__journal_entry_obj?.journal_entry_id]) {
if (!tmp_entry_obj?.content && tmp_entry_obj?.content_encrypted) {
console.log('TEST: Decrypting the content...');
tmp_entry_obj.content = await ae_util.decrypt_wrapper(tmp_entry_obj?.content_encrypted, journal_key);
tmp_entry_obj.content_md_html = handle_marked(tmp_entry_obj?.content);
orig_entry_obj.content = await ae_util.decrypt_wrapper(orig_entry_obj?.content_encrypted, journal_key);
orig_entry_obj.content_md_html = handle_marked(orig_entry_obj?.content);
}
if (!tmp_entry_obj?.history && tmp_entry_obj?.history_encrypted) {
console.log('TEST: Decrypting the history...');
tmp_entry_obj.history = await ae_util.decrypt_wrapper(tmp_entry_obj?.history_encrypted, journal_key);
tmp_entry_obj.history_md_html = handle_marked(tmp_entry_obj?.history);
orig_entry_obj.history = await ae_util.decrypt_wrapper(orig_entry_obj?.history_encrypted, journal_key);
orig_entry_obj.history_md_html = handle_marked(orig_entry_obj?.history);
}
}
tmp_entry_obj_changed = false;
// updated_obj = false;
updated_idb = false;
// } else if ((updated_obj && await $lq__journal_entry_obj) || ($lq__journal_entry_obj?.updated_on > orig_entry_obj?.updated_on)) {
// // updated_obj = false;
// console.log(`TEST: updated_obj and $lq__journal_entry_obj; setting orig_entry_obj and tmp_entry_obj`, $lq__journal_entry_obj);
// orig_entry_obj = { ...$lq__journal_entry_obj };
// tmp_entry_obj = { ...$lq__journal_entry_obj };
// if ($journals_loc?.entry?.decrypt_kv[$lq__journal_entry_obj?.journal_entry_id]) {
// if (!tmp_entry_obj?.content && tmp_entry_obj?.content_encrypted) {
// console.log('TEST: Decrypting the content...');
// tmp_entry_obj.content = await ae_util.decrypt_wrapper(tmp_entry_obj?.content_encrypted, journal_key);
// tmp_entry_obj.content_md_html = handle_marked(tmp_entry_obj?.content);
// orig_entry_obj.content = await ae_util.decrypt_wrapper(orig_entry_obj?.content_encrypted, journal_key);
// orig_entry_obj.content_md_html = handle_marked(orig_entry_obj?.content);
// }
// if (!tmp_entry_obj?.history && tmp_entry_obj?.history_encrypted) {
// console.log('TEST: Decrypting the history...');
// tmp_entry_obj.history = await ae_util.decrypt_wrapper(tmp_entry_obj?.history_encrypted, journal_key);
// tmp_entry_obj.history_md_html = handle_marked(tmp_entry_obj?.history);
// orig_entry_obj.history = await ae_util.decrypt_wrapper(orig_entry_obj?.history_encrypted, journal_key);
// orig_entry_obj.history_md_html = handle_marked(orig_entry_obj?.history);
// }
// }
// console.log('TEST: tmp_entry_obj:', tmp_entry_obj);
// tmp_entry_obj_changed = false;
// updated_obj = false;
// updated_idb = false;
} else if ( } else if (
!updated_obj !updated_obj
&& JSON.stringify(orig_entry_obj) !== JSON.stringify($lq__journal_entry_obj) && JSON.stringify(orig_entry_obj) !== JSON.stringify($lq__journal_entry_obj)
@@ -132,9 +230,15 @@ $effect(() => {
console.log('TEST: tmp_entry_obj and orig_entry_obj available; marking tmp_entry_obj as changed'); console.log('TEST: tmp_entry_obj and orig_entry_obj available; marking tmp_entry_obj as changed');
// tmp_entry_obj_changed = JSON.stringify(tmp_entry_obj) != JSON.stringify($lq__journal_entry_obj); // tmp_entry_obj_changed = JSON.stringify(tmp_entry_obj) != JSON.stringify($lq__journal_entry_obj);
// tmp_entry_obj_changed = JSON.stringify(tmp_entry_obj) != JSON.stringify(orig_entry_obj); // tmp_entry_obj_changed = JSON.stringify(tmp_entry_obj) != JSON.stringify(orig_entry_obj);
// if (!decrypted_content) { // if (!tmp_entry_obj?.private ) {
tmp_entry_obj_changed = true; // tmp_entry_obj_changed = true;
// } else
// if (tmp_entry_obj?.private && tmp_entry_obj?.content) {
// tmp_entry_obj_changed = false;
// } else {
// tmp_entry_obj_changed = true;
// } // }
tmp_entry_obj_changed = true;
} else { } else {
// console.log('TEST: tmp_entry_obj == orig_entry_obj'); // console.log('TEST: tmp_entry_obj == orig_entry_obj');
tmp_entry_obj_changed = false; tmp_entry_obj_changed = false;
@@ -149,7 +253,7 @@ async function update_journal_entry() {
return; return;
} }
// log_lvl = 1; log_lvl = 1;
let data_kv: key_val = { let data_kv: key_val = {
alert: tmp_entry_obj?.alert, alert: tmp_entry_obj?.alert,
@@ -204,15 +308,21 @@ async function update_journal_entry() {
let { left_over_string, cut_out_string } = handle_cut_string(tmp_entry_obj?.content); let { left_over_string, cut_out_string } = handle_cut_string(tmp_entry_obj?.content);
// Need to decrypt the current content and current history. Assume the history has not been decrypted yet. // Need to decrypt the current content and current history. Assume the history has not been decrypted yet.
let content_enc_combined_data = await handle_encrypt_string(left_over_string, journal_key) let content_enc_combined_data = await ae_util.encrypt_wrapper(left_over_string, journal_key);
if (log_lvl) {
console.log('TEST: Encrypted string:', content_enc_combined_data);
}
// return content_enc_combined_data;
// let content_enc_combined_data = await handle_encrypt_string(left_over_string, journal_key)
data_kv.content_encrypted = content_enc_combined_data; data_kv.content_encrypted = content_enc_combined_data;
data_kv.content = null; data_kv.content = null;
decrypted_content = ''; // decrypted_content = '';
if (log_lvl) { if (log_lvl) {
console.log('TEST: Decrypting the history before saving it...'); console.log('TEST: Decrypting the history before saving it...');
} }
decrypted_history = await handle_decrypt_string(tmp_entry_obj?.history_encrypted, journal_key); decrypted_history = await ae_util.decrypt_wrapper(tmp_entry_obj?.history_encrypted, journal_key);
if (tmp_entry_obj?.history) { if (tmp_entry_obj?.history) {
data_kv.history = tmp_entry_obj?.history ?? '' + '\n' + decrypted_history + '\n' + cut_out_string; data_kv.history = tmp_entry_obj?.history ?? '' + '\n' + decrypted_history + '\n' + cut_out_string;
@@ -224,7 +334,7 @@ async function update_journal_entry() {
if (log_lvl) { if (log_lvl) {
console.log('TEST: Encrypting the history before saving it...'); console.log('TEST: Encrypting the history before saving it...');
} }
let history_enc_combined_data = await handle_encrypt_string(data_kv.history, journal_key); let history_enc_combined_data = await ae_util.encrypt_wrapper(data_kv.history, journal_key);
data_kv.history_encrypted = history_enc_combined_data; data_kv.history_encrypted = history_enc_combined_data;
data_kv.history = null; data_kv.history = null;
@@ -281,56 +391,102 @@ async function update_journal_entry() {
// } // }
// } // }
if (tmp_entry_obj?.content && tmp_entry_obj?.private) { // if (tmp_entry_obj?.content && tmp_entry_obj?.private) {
// // console.log('TEST: Saving encrypted content', tmp_entry_obj?.content); // // // console.log('TEST: Saving encrypted content', tmp_entry_obj?.content);
// content = tmp_entry_obj?.content; // // content = tmp_entry_obj?.content;
// // console.log('TEST: journal_key', journal_key); // // // console.log('TEST: journal_key', journal_key);
// // // Encrypt the content // // // // Encrypt the content
// // let encrypted_base64 = await ae_util.encrypt_content(content, journal_key); // // // let encrypted_base64 = await ae_util.encrypt_content(content, journal_key);
// // encrypted_base64_string = encrypted_base64.base64; // // // encrypted_base64_string = encrypted_base64.base64;
// // encryption_iv = encrypted_base64.iv; // // // encryption_iv = encrypted_base64.iv;
// // console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_string}`); // // // console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_string}`);
// // // Combine the IV and encrypted content // // // // Combine the IV and encrypted content
// // const combined_data = Array.from(encryption_iv).map(byte => byte.toString(16).padStart(2, '0')).join('') + ':' + encrypted_base64_string; // // // const combined_data = Array.from(encryption_iv).map(byte => byte.toString(16).padStart(2, '0')).join('') + ':' + encrypted_base64_string;
// let combined_data = await handle_encrypt_string(content, journal_key); // // let combined_data = await handle_encrypt_string(content, journal_key);
// data_kv.content_encrypted = combined_data; // // data_kv.content_encrypted = combined_data;
// data_kv.content = null; // // data_kv.content = null;
// decrypted_content = ''; // // decrypted_content = '';
} else if (decrypted_content && !tmp_entry_obj?.private) { // } else if (decrypted_content && !tmp_entry_obj?.private) {
// console.log('TEST: Saving decrypted content', decrypted_content); // // console.log('TEST: Saving decrypted content', decrypted_content);
content = decrypted_content; // content = decrypted_content;
data_kv.content = content; // data_kv.content = content;
decrypted_content = ''; // decrypted_content = '';
} else if (tmp_entry_obj?.content_encrypted && !tmp_entry_obj?.private) { // } else if (tmp_entry_obj?.content_encrypted && !tmp_entry_obj?.private) {
console.log('TEST: Decrypting the content before saving it...'); // console.log('TEST: Decrypting the content before saving it...');
decrypted_content = await handle_decrypt_string(tmp_entry_obj?.content_encrypted, journal_key); // decrypted_content = await handle_decrypt_string(tmp_entry_obj?.content_encrypted, journal_key);
data_kv.content = decrypted_content; // tmp_entry_obj.content // data_kv.content = decrypted_content; // tmp_entry_obj.content
decrypted_content = ''; // decrypted_content = '';
// return false; // // return false;
} else { // } else {
console.log('TEST: Saving content without encryption', tmp_entry_obj?.content); // console.log('TEST: Saving content without encryption', tmp_entry_obj?.content);
// Clear content_encrypted again. Just in case. // // Clear content_encrypted again. Just in case.
// data_kv.content_encrypted = null; // // data_kv.content_encrypted = null;
decrypted_content = ''; // decrypted_content = '';
// tmp_entry_obj.content_encrypted = null; // // tmp_entry_obj.content_encrypted = null;
} // }
// Call API to save the content // Call API to save the content
try { try {
await journals_func.update_ae_obj__journal_entry({ const response = await journals_func.update_ae_obj__journal_entry({
api_cfg: $ae_api, api_cfg: $ae_api,
journal_entry_id: $lq__journal_entry_obj?.journal_entry_id, journal_entry_id: $lq__journal_entry_obj?.journal_entry_id,
data_kv: data_kv, data_kv: data_kv,
log_lvl: 1, log_lvl: 1,
}); });
console.log('Journal entry updated successfully:', response);
// tick();
updated_obj = true; updated_obj = true;
updated_idb = false;
console.log('Journal entry updated successfully!'); // tick();
// orig_entry_obj = { ...await $lq__journal_entry_obj };
// tmp_entry_obj = { ...await $lq__journal_entry_obj };
// if ($journals_loc?.entry?.decrypt_kv[$lq__journal_entry_obj?.journal_entry_id]) {
// if (!tmp_entry_obj?.content && tmp_entry_obj?.content_encrypted) {
// console.log('TEST: Decrypting the content...');
// tmp_entry_obj.content = await ae_util.decrypt_wrapper(tmp_entry_obj?.content_encrypted, journal_key);
// tmp_entry_obj.content_md_html = handle_marked(tmp_entry_obj?.content);
// orig_entry_obj.content = await ae_util.decrypt_wrapper(orig_entry_obj?.content_encrypted, journal_key);
// orig_entry_obj.content_md_html = handle_marked(orig_entry_obj?.content);
// }
// if (!tmp_entry_obj?.history && tmp_entry_obj?.history_encrypted) {
// console.log('TEST: Decrypting the history...');
// tmp_entry_obj.history = await ae_util.decrypt_wrapper(tmp_entry_obj?.history_encrypted, journal_key);
// tmp_entry_obj.history_md_html = handle_marked(tmp_entry_obj?.history);
// orig_entry_obj.history = await ae_util.decrypt_wrapper(orig_entry_obj?.history_encrypted, journal_key);
// orig_entry_obj.history_md_html = handle_marked(orig_entry_obj?.history);
// }
// }
// console.log('TEST: tmp_entry_obj:', tmp_entry_obj);
// updated_obj = true;
// updated_idb = false;
// journals_func.update_ae_obj__journal_entry({
// api_cfg: $ae_api,
// journal_entry_id: $lq__journal_entry_obj?.journal_entry_id,
// data_kv: data_kv,
// log_lvl: 1,
// })
// .then((response) => {
// console.log('HERE: response', response);
// updated_obj = true;
// updated_idb = false;
// console.log('Journal entry updated successfully!');
// })
// tick();
} catch (error) { } catch (error) {
console.error('Error updating journal entry:', error); console.error('Error updating journal entry:', error);
alert('Failed to update journal entry.'); alert('Failed to update journal entry.');
@@ -620,7 +776,23 @@ function handle_cut_string(old_string: string) {
return { left_over_string, cut_out_string }; return { left_over_string, cut_out_string };
} }
function handle_marked(text_string: string) {
if (!text_string) {
console.log('TEST: No text string provided');
return '';
}
// let cleaned_string = text_string.replace(/<[^>]*>/g, ''); // Remove HTML tags
let cleaned_string = text_string.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/,"");
// let marked_string = marked.parse(text_string);
let marked_string = marked.parse(cleaned_string, {
// gfm: true,
// breaks: true,
// headerIds: false,
// mangle: false,
// xhtml: true,
});
return marked_string;
}
</script> </script>
@@ -1270,6 +1442,7 @@ function handle_cut_string(old_string: string) {
// Setting private to false will cause the update_journal_entry() function to decrypt the content. This will also copy it to tmp_entry_obj.content. // Setting private to false will cause the update_journal_entry() function to decrypt the content. This will also copy it to tmp_entry_obj.content.
tmp_entry_obj.private = false; tmp_entry_obj.private = false;
// tmp_entry_obj.
// handle_decrypt_content(); // handle_decrypt_content();
update_journal_entry(); update_journal_entry();
} else { } else {
@@ -1299,28 +1472,50 @@ function handle_cut_string(old_string: string) {
decrypted_content = ''; decrypted_content = '';
// tmp_entry_obj.content = null; // tmp_entry_obj.content = null;
} else if (tmp_entry_obj?.private && tmp_entry_obj?.content_encrypted) { } else if (tmp_entry_obj?.private && tmp_entry_obj?.content_encrypted) {
if (confirm('Are you sure you want to decrypt the content to view/edit?')) { if (!$journals_loc?.entry?.decrypt_kv[$lq__journal_entry_obj?.journal_entry_id]) {
// trigger_decrypt = true; if (confirm('Are you sure you want to decrypt the content to view/edit?')) {
// handle_decrypt_content(); $journals_loc.entry.decrypt_kv[$lq__journal_entry_obj?.journal_entry_id] = true;
// handle_decrypt_string(tmp_entry_obj?.content_encrypted, journal_key).then((result) => { $journals_loc.entry.edit_kv[$lq__journal_entry_obj?.journal_entry_id] = 'current';
// decrypted_content = result;
// console.log('TEST: Decrypted content:', decrypted_content);
// });
decrypted_content = await ae_util.decrypt_wrapper(tmp_entry_obj?.content_encrypted, journal_key);
// decrypted_content = await handle_decrypt_string(tmp_entry_obj?.content_encrypted, journal_key); if (!tmp_entry_obj?.content && tmp_entry_obj?.content_encrypted) {
tmp_entry_obj.content = decrypted_content; console.log('TEST: Decrypting the content...');
console.log('TEST: Decrypted content:', decrypted_content); tmp_entry_obj.content = await ae_util.decrypt_wrapper(tmp_entry_obj?.content_encrypted, journal_key);
tmp_entry_obj.content_md_html = handle_marked(tmp_entry_obj?.content);
}
if (!tmp_entry_obj?.history && tmp_entry_obj?.history_encrypted) {
console.log('TEST: Decrypting the history...');
tmp_entry_obj.history = await ae_util.decrypt_wrapper(tmp_entry_obj?.history_encrypted, journal_key);
tmp_entry_obj.history_md_html = handle_marked(tmp_entry_obj?.history);
}
// trigger_decrypt = true;
// handle_decrypt_content();
// handle_decrypt_string(tmp_entry_obj?.content_encrypted, journal_key).then((result) => {
// decrypted_content = result;
// console.log('TEST: Decrypted content:', decrypted_content);
// });
// decrypted_content = await ae_util.decrypt_wrapper(tmp_entry_obj?.content_encrypted, journal_key);
// // decrypted_content = await handle_decrypt_string(tmp_entry_obj?.content_encrypted, journal_key);
// tmp_entry_obj.content = decrypted_content;
// console.log('TEST: Decrypted content:', decrypted_content);
} else {
return false;
}
} else { } else {
return false; $journals_loc.entry.decrypt_kv[$lq__journal_entry_obj?.journal_entry_id] = false;
$journals_loc.entry.edit_kv[$lq__journal_entry_obj?.journal_entry_id] = false;
tmp_entry_obj.content = null;
tmp_entry_obj.content_md_html = null;
tmp_entry_obj.history = null;
tmp_entry_obj.history_md_html = null;
} }
} }
}} }}
class:hidden={!$lq__journal_entry_obj?.private} class:hidden={!$lq__journal_entry_obj?.private || !tmp_entry_obj?.content_encrypted}
class="btn-icon btn-icon-sm variant-soft-secondary hover:variant-filled-secondary transition" class="btn-icon btn-icon-sm variant-soft-secondary hover:variant-filled-secondary transition"
title="Toggle viewing/editing of private encrypted content" title="Toggle viewing/editing of private encrypted content"
> >
{#if $lq__journal_entry_obj?.private && decrypted_content} {#if $lq__journal_entry_obj?.private && tmp_entry_obj?.content}
<LockKeyholeOpen strokeWidth="2.5" color="red" /> <LockKeyholeOpen strokeWidth="2.5" color="red" />
{:else if $lq__journal_entry_obj?.private && tmp_entry_obj?.content_encrypted} {:else if $lq__journal_entry_obj?.private && tmp_entry_obj?.content_encrypted}
<LockKeyhole strokeWidth="2.5" color="green" /> <LockKeyhole strokeWidth="2.5" color="green" />
@@ -1588,7 +1783,19 @@ tabindex={$ae_loc.edit_mode ? 0 : -1} -->
" "
id="rendered_journal_entry_content_{$lq__journal_entry_obj?.journal_entry_id}" id="rendered_journal_entry_content_{$lq__journal_entry_obj?.journal_entry_id}"
> >
{#if decrypted_content?.length} {#if tmp_entry_obj.content_md_html}
{@html tmp_entry_obj.content_md_html}
{:else if tmp_entry_obj?.content_encrypted && tmp_entry_obj?.private}
<div class="text-sm text-gray-500">
<div class=""><LockKeyhole strokeWidth="1.5" color="green" class="inline-block" /> Encrypted:</div>
<div class="max-w-screen-md overflow-auto wrap break-all">
{tmp_entry_obj?.content_encrypted}
</div>
</div>
{/if}
<!-- {#if decrypted_content?.length}
{@html decrypted_content} {@html decrypted_content}
{:else if $lq__journal_entry_obj?.content_encrypted} {:else if $lq__journal_entry_obj?.content_encrypted}
<span class="text-sm text-gray-500 break-all"> <span class="text-sm text-gray-500 break-all">
@@ -1596,7 +1803,7 @@ tabindex={$ae_loc.edit_mode ? 0 : -1} -->
</span> </span>
{:else} {:else}
{@html $lq__journal_entry_obj?.content_md_html} {@html $lq__journal_entry_obj?.content_md_html}
{/if} {/if} -->
</article> </article>
<!-- {@html async () => { <!-- {@html async () => {
const { base64, iv } = await encrypt_content(content, journal_key); const { base64, iv } = await encrypt_content(content, journal_key);
@@ -1615,7 +1822,7 @@ tabindex={$ae_loc.edit_mode ? 0 : -1} -->
<!-- && !($lq__journal_entry_obj?.content_encrypted && decrypted_content)) --> <!-- && !($lq__journal_entry_obj?.content_encrypted && decrypted_content)) -->
<!-- class="flex flex-row flex-wrap gap-1 items-center justify-center w-full max-w-sm" --> <!-- class="flex flex-row flex-wrap gap-1 items-center justify-center w-full max-w-sm" -->
{#if ($lq__journal_entry_obj?.content_encrypted && !decrypted_content)} {#if (!tmp_entry_obj?.content && tmp_entry_obj?.content_encrypted)}
<div <div
class=" class="
flex-grow flex-grow
@@ -1659,6 +1866,7 @@ tabindex={$ae_loc.edit_mode ? 0 : -1} -->
h-full max-h-full h-full max-h-full
w-full min-w-full max-w-6xl" w-full min-w-full max-w-6xl"
> --> > -->
<!-- disabled={tmp_entry_obj?.private && !$journals_loc?.entry?.decrypt_kv[$lq__journal_entry_obj?.journal_entry_id]} -->
<textarea <textarea
bind:value={tmp_entry_obj.content} bind:value={tmp_entry_obj.content}
ondblclick={() => { ondblclick={() => {
@@ -1668,7 +1876,7 @@ tabindex={$ae_loc.edit_mode ? 0 : -1} -->
// $journals_loc.entry.edit_kv[$lq__journal_entry_obj?.journal_entry_id] = $journals_loc.entry.edit; // $journals_loc.entry.edit_kv[$lq__journal_entry_obj?.journal_entry_id] = $journals_loc.entry.edit;
// } // }
}} }}
disabled={tmp_entry_obj.content_encrypted && !decrypted_content}
class=" class="
flex-grow flex-grow
flex-shrink-0 flex-shrink-0
@@ -1782,10 +1990,13 @@ tabindex={$ae_loc.edit_mode ? 0 : -1} -->
{/if} {/if}
<!-- <div class="max-w-screen-md overflow-auto">
aaaa
{tmp_entry_obj?.content ?? '-- not set --'}
<hr>
{tmp_entry_obj?.content_encrypted ?? '-- not set --'}
zzzz
</div> -->