From 643add9177d64e54907b321a2a40eb5b49b5807c Mon Sep 17 00:00:00 2001 From: Thomas Mathews Date: Mon, 19 Dec 2022 22:14:15 -0800 Subject: [PATCH] web: save contacts works --- web/index.html | 1 - web/js/cache.js | 1 - web/js/contacts.js | 67 ++++++++++++++++++++++++++++++++++++++++++++-- web/js/damus.js | 7 +++++ web/js/ui/util.js | 2 +- 5 files changed, 73 insertions(+), 5 deletions(-) delete mode 100644 web/js/cache.js diff --git a/web/index.html b/web/index.html index 60ef930..afde7ef 100644 --- a/web/index.html +++ b/web/index.html @@ -17,7 +17,6 @@ - diff --git a/web/js/cache.js b/web/js/cache.js deleted file mode 100644 index 8b13789..0000000 --- a/web/js/cache.js +++ /dev/null @@ -1 +0,0 @@ - diff --git a/web/js/contacts.js b/web/js/contacts.js index 1df793c..3048f08 100644 --- a/web/js/contacts.js +++ b/web/js/contacts.js @@ -23,6 +23,69 @@ function contacts_push_relay(contacts, relay) { /* contacts_save commits the contacts data to storage. */ -function contacts_save(contacts) { - log_warn("contacts_save not implemented"); +async function contacts_save(contacts) { + function _contacts_save(ev, resolve, reject) { + const db = ev.target.result; + let tx = db.transaction("friends", "readwrite"); + let store = tx.objectStore("friends"); + contacts.friends.forEach((pubkey) => { + //log_debug("storing", pubkey); + store.put({pubkey}); + }); + tx.oncomplete = (ev) => { + db.close(); + resolve(); + log_debug("contacts saved successfully", contacts.friends.size); + } + tx.onerror = (ev) => { + db.close(); + log_error(`tx errorCode: ${ev.request.errorCode}`); + window.alert("An error occured saving contacts. Check console."); + reject(ev); + }; + }; + return dbcall(_contacts_save); +} + +async function contacts_load(model) { + function _contacts_load(ev, resolve, reject) { + const db = ev.target.result; + const tx = db.transaction("friends", "readonly"); + const store = tx.objectStore("friends"); + const cursor = store.openCursor(); + cursor.onsuccess = (ev)=> { + var cursor = ev.target.result; + if (cursor) { + //log_debug("cursor val", cursor.value); + model.contacts.friends.add(cursor.value.pubkey); + cursor.continue(); + } else { + db.close(); + resolve(); + log_debug("contacts loaded successfully"); + } + } + cursor.onerror = (ev) => { + db.close(); + reject(ev); + log_error("Could not load contacts."); + } + } + return dbcall(_contacts_load); +} + +async function dbcall(fn) { + return new Promise((resolve, reject) => { + var open = indexedDB.open("damus", 2); + open.onupgradeneeded = (ev) => { + const db = ev.target.result; + const os = db.createObjectStore("friends", {keyPath: "pubkey"}); + }; + open.onsuccess = (ev) => { + fn(ev, resolve, reject); + } + open.onerror = (ev) => { + reject(err); + }; + }); } diff --git a/web/js/damus.js b/web/js/damus.js index 12fd514..10dcb58 100644 --- a/web/js/damus.js +++ b/web/js/damus.js @@ -35,6 +35,7 @@ async function damus_web_init_ready() { model.pubkey = await get_pubkey() if (!model.pubkey) return + const {RelayPool} = nostrjs const pool = RelayPool(BOOTSTRAP_RELAYS) const ids = { @@ -50,6 +51,12 @@ async function damus_web_init_ready() { dms: "dms", } + let err; + err = await contacts_load(model); + if (err) { + window.alert("Unable to load contacts."); + } + model.ids = ids model.pool = pool model.view_el = document.querySelector("#view") diff --git a/web/js/ui/util.js b/web/js/ui/util.js index 1d0ba4a..1d1e194 100644 --- a/web/js/ui/util.js +++ b/web/js/ui/util.js @@ -112,7 +112,7 @@ function click_toggle_follow_user(el) { contacts.friends.add(pubkey); } el.innerText = is_friend ? "Follow" : "Unfollow"; - contacts_save(); + contacts_save(contacts); } /* click_event opens the thread view from the element's specified element id