diff --git a/js/contacts.js b/js/contacts.js index 09f4e6e..f2857af 100644 --- a/js/contacts.js +++ b/js/contacts.js @@ -15,12 +15,6 @@ function contacts_process_event(contacts, our_pubkey, ev) { } } -/* contacts_push_relay sends your contact list to the desired relay. - */ -function contacts_push_relay(contacts, relay) { - log_warn("contacts_push_relay not implemented"); -} - /* contacts_save commits the contacts data to storage. */ async function contacts_save(contacts) { diff --git a/js/core.js b/js/core.js index 65c46fc..2574065 100644 --- a/js/core.js +++ b/js/core.js @@ -75,6 +75,25 @@ async function update_profile(profile={}) { return ev; } +async function update_contacts() { + const model = DAMUS; + const contacts = Array.from(model.contacts.friends); + const tags = contacts.map((pubkey) => { + return ["p", pubkey] + }); + let ev = { + kind: KIND_CONTACT, + created_at: new_creation_time(), + pubkey: model.pubkey, + content: "", + tags: tags, + } + ev.id = await nostrjs.calculate_id(ev); + ev = await sign_event(ev); + broadcast_event(ev); + return ev; +} + async function sign_event(ev) { if (window.nostr && window.nostr.signEvent) { const signed = await window.nostr.signEvent(ev) diff --git a/js/main.js b/js/main.js index 5d061ec..53fbb34 100644 --- a/js/main.js +++ b/js/main.js @@ -17,6 +17,7 @@ const SID_NOTIFICATIONS = "notifications"; const SID_EXPLORE = "explore"; const SID_PROFILES = "profiles"; const SID_THREAD = "thread"; +const SID_FRIENDS = "friends"; // This is our main entry. // https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event @@ -160,13 +161,18 @@ function on_pool_open(relay) { limit: 5000, }]); - // Get the latest history as a prefetch + /*// Get the latest history as a prefetch relay.subscribe(SID_HISTORY, [{ kinds: STANDARD_KINDS, limit: 500, - }]); + }]);*/ - // TODO perhaps get our following list's history too + // Grab our friends history so our default timeline looks loaded + relay.subscribe(SID_FRIENDS, [{ + kinds: STANDARD_KINDS, + authors: Array.from(model.contacts.friends), + limit: 500, + }]); } function on_pool_notice(relay, notice) { @@ -183,10 +189,15 @@ async function on_pool_eose(relay, sub_id) { const index = sub_id.indexOf(":"); const sid = sub_id.slice(0, index >= 0 ? index : sub_id.length); switch (sid) { - case SID_THREAD: - case SID_PROFILES: - case SID_META: case SID_HISTORY: + case SID_THREAD: + view_timeline_refresh(model); + pool.unsubscribe(sub_id, relay); + break + case SID_FRIENDS: + view_timeline_refresh(model); + case SID_META: + case SID_PROFILES: pool.unsubscribe(sub_id, relay); break; } diff --git a/js/ui/state.js b/js/ui/state.js index 79511ba..e5f0d97 100644 --- a/js/ui/state.js +++ b/js/ui/state.js @@ -12,16 +12,35 @@ function view_timeline_apply_mode(model, mode, opts={}) { let xs; const { pubkey, thread_id } = opts; const el = view_get_timeline_el(); + const now = new Date().getTime(); + + // Don't do anything if we are already here + if (el.dataset.mode == mode && (el.dataset.pubkey == opts.pubkey || + el.dataset.threadId == thread_id)) + return; + + // Set the last time we were actively viewing explore to correctly fetch + // the history in the future + if (el.dataset.mode == VM_EXPLORE && mode != VM_EXPLORE) { + model.seen_explore = now; + } if (mode == VM_EXPLORE) { - subscribe_explore(100); + // If the time between the last explore and now is less than a minute + // simply only fetch a few + subscribe_explore((now - model.seen_explore) / 1000 < 60 ? 100 : 500); } else { unsubscribe_explore(); } - if (mode == VM_THREAD) { + if (mode == VM_THREAD || mode == VM_USER) { fetch_thread_history(thread_id, model.pool); + view_show_spinner(true); } + // Request the background info for this user + if (pubkey) + fetch_profile(pubkey, model.pool); + el.dataset.mode = mode; switch(mode) { case VM_THREAD: @@ -49,6 +68,20 @@ function view_timeline_apply_mode(model, mode, opts={}) { find_node("#newpost").classList.toggle("hide", mode != VM_FRIENDS); find_node("#timeline").classList.toggle("reverse", mode == VM_THREAD); + view_timeline_refresh(model, mode, opts); + + return mode; +} + +/* view_timeline_refresh is a hack for redrawing the events in order + */ +function view_timeline_refresh(model, mode, opts={}) { + const el = view_get_timeline_el(); + if (!mode) { + mode = el.dataset.mode; + opts.thread_id = el.dataset.threadId; + opts.pubkey = el.dataset.pubkey; + } // Remove all // This is faster than removing one by one el.innerHTML = ""; @@ -72,12 +105,6 @@ function view_timeline_apply_mode(model, mode, opts={}) { view_timeline_update_timestamps(); view_show_spinner(false); } - - // Request the background info for this user - if (pubkey) - fetch_profile(pubkey, model.pool); - - return mode; } function view_show_spinner(show=true) {