diff --git a/src/app/commands.ts b/src/app/commands.ts index 2ad7b460..344fe4ff 100644 --- a/src/app/commands.ts +++ b/src/app/commands.ts @@ -1,6 +1,6 @@ import * as nip19 from "nostr-tools/nip19" import {get} from "svelte/store" -import {ctx, sample, uniq, sleep, chunk, equals} from "@welshman/lib" +import {ctx, uniq, equals} from "@welshman/lib" import { DELETE, REPORT, @@ -26,12 +26,10 @@ import { getTag, getListTags, getRelayTags, - isShareableRelayUrl, getRelayTagValues, toNostrURI, } from "@welshman/util" -import type {TrustedEvent, EventContent, EventTemplate, List} from "@welshman/util" -import type {SubscribeRequestWithHandlers} from "@welshman/net" +import type {TrustedEvent, EventContent, EventTemplate} from "@welshman/util" import {PublishStatus, AuthStatus, SocketStatus} from "@welshman/net" import {Nip59, makeSecret, stamp, Nip46Broker} from "@welshman/signer" import { @@ -40,13 +38,9 @@ import { repository, publishThunk, publishThunks, - loadProfile, - loadInboxRelaySelections, profilesByPubkey, relaySelectionsByPubkey, getWriteRelayUrls, - loadFollows, - loadMutes, tagEvent, tagEventForReaction, getRelayUrls, @@ -67,11 +61,9 @@ import { userMembership, INDEXER_RELAYS, NIP46_PERMS, - loadMembership, - loadSettings, - getDefaultPubkeys, userRoomsByUrl, } from "@app/state" +import {loadUserData} from "@app/requests" // Utils @@ -161,47 +153,6 @@ export const logout = async () => { localStorage.clear() } -// Loaders - -export const loadUserData = ( - pubkey: string, - request: Partial = {}, -) => { - const promise = Promise.race([ - sleep(3000), - Promise.all([ - loadInboxRelaySelections(pubkey, request), - loadMembership(pubkey, request), - loadSettings(pubkey, request), - loadProfile(pubkey, request), - loadFollows(pubkey, request), - loadMutes(pubkey, request), - ]), - ]) - - // Load followed profiles slowly in the background without clogging other stuff up. Only use a single - // indexer relay to avoid too many redundant validations, which slow things down and eat bandwidth - promise.then(async () => { - for (const pubkeys of chunk(50, getDefaultPubkeys())) { - const relays = sample(1, INDEXER_RELAYS) - - await sleep(1000) - - for (const pubkey of pubkeys) { - loadMembership(pubkey, {relays}) - loadProfile(pubkey, {relays}) - loadFollows(pubkey, {relays}) - loadMutes(pubkey, {relays}) - } - } - }) - - return promise -} - -export const discoverRelays = (lists: List[]) => - Promise.all(uniq(lists.flatMap(getRelayUrls)).filter(isShareableRelayUrl).map(loadRelay)) - // Synchronization export const broadcastUserData = async (relays: string[]) => { diff --git a/src/app/components/LogIn.svelte b/src/app/components/LogIn.svelte index 1998b3e1..13e9be59 100644 --- a/src/app/components/LogIn.svelte +++ b/src/app/components/LogIn.svelte @@ -13,7 +13,7 @@ import {pushModal, clearModals} from "@app/modal" import {PLATFORM_NAME, BURROW_URL} from "@app/state" import {pushToast} from "@app/toast" - import {loadUserData} from "@app/commands" + import {loadUserData} from "@app/requests" import {setChecked} from "@app/notifications" let signers: any[] = $state([]) diff --git a/src/app/components/LogInBunker.svelte b/src/app/components/LogInBunker.svelte index c44db935..365f9b80 100644 --- a/src/app/components/LogInBunker.svelte +++ b/src/app/components/LogInBunker.svelte @@ -12,7 +12,8 @@ import ModalFooter from "@lib/components/ModalFooter.svelte" import QRCode from "@app/components/QRCode.svelte" import InfoBunker from "@app/components/InfoBunker.svelte" - import {loginWithNip46, loadUserData} from "@app/commands" + import {loginWithNip46} from "@app/commands" + import {loadUserData} from "@app/requests" import {pushModal, clearModals} from "@app/modal" import {setChecked} from "@app/notifications" import {pushToast} from "@app/toast" diff --git a/src/app/components/LogInPassword.svelte b/src/app/components/LogInPassword.svelte index 748c731b..cd768327 100644 --- a/src/app/components/LogInPassword.svelte +++ b/src/app/components/LogInPassword.svelte @@ -12,7 +12,7 @@ import ModalHeader from "@lib/components/ModalHeader.svelte" import ModalFooter from "@lib/components/ModalFooter.svelte" import PasswordResetRequest from "@app/components/PasswordResetRequest.svelte" - import {loadUserData} from "@app/commands" + import {loadUserData} from "@app/requests" import {clearModals, pushModal} from "@app/modal" import {setChecked} from "@app/notifications" import {pushToast} from "@app/toast" diff --git a/src/app/requests.ts b/src/app/requests.ts index abc569be..3737b43b 100644 --- a/src/app/requests.ts +++ b/src/app/requests.ts @@ -1,5 +1,19 @@ import {get, writable} from "svelte/store" -import {partition, shuffle, int, YEAR, MONTH, insert, sortBy, assoc, now} from "@welshman/lib" +import { + partition, + chunk, + sample, + sleep, + shuffle, + uniq, + int, + YEAR, + MONTH, + insert, + sortBy, + assoc, + now, +} from "@welshman/lib" import { MESSAGE, DELETE, @@ -9,10 +23,11 @@ import { matchFilters, getTagValues, getTagValue, + isShareableRelayUrl, } from "@welshman/util" -import type {TrustedEvent, Filter} from "@welshman/util" +import type {TrustedEvent, Filter, List} from "@welshman/util" import {feedFromFilters, makeRelayFeed, makeIntersectionFeed} from "@welshman/feeds" -import type {Subscription} from "@welshman/net" +import type {Subscription, SubscribeRequestWithHandlers} from "@welshman/net" import type {AppSyncOpts, Thunk} from "@welshman/app" import { subscribe, @@ -22,10 +37,23 @@ import { hasNegentropy, thunkWorker, createFeedController, + loadRelay, + loadMutes, + loadFollows, + loadProfile, + loadInboxRelaySelections, + getRelayUrls, } from "@welshman/app" import {createScroller} from "@lib/html" import {daysBetween} from "@lib/util" -import {userRoomsByUrl, getUrlsForEvent} from "@app/state" +import { + INDEXER_RELAYS, + getDefaultPubkeys, + userRoomsByUrl, + getUrlsForEvent, + loadMembership, + loadSettings, +} from "@app/state" // Utils @@ -317,3 +345,42 @@ export const listenForNotifications = () => { } } } + +export const loadUserData = ( + pubkey: string, + request: Partial = {}, +) => { + const promise = Promise.race([ + sleep(3000), + Promise.all([ + loadInboxRelaySelections(pubkey, request), + loadMembership(pubkey, request), + loadSettings(pubkey, request), + loadProfile(pubkey, request), + loadFollows(pubkey, request), + loadMutes(pubkey, request), + ]), + ]) + + // Load followed profiles slowly in the background without clogging other stuff up. Only use a single + // indexer relay to avoid too many redundant validations, which slow things down and eat bandwidth + promise.then(async () => { + for (const pubkeys of chunk(50, getDefaultPubkeys())) { + const relays = sample(1, INDEXER_RELAYS) + + await sleep(1000) + + for (const pubkey of pubkeys) { + loadMembership(pubkey, {relays}) + loadProfile(pubkey, {relays}) + loadFollows(pubkey, {relays}) + loadMutes(pubkey, {relays}) + } + } + }) + + return promise +} + +export const discoverRelays = (lists: List[]) => + Promise.all(uniq(lists.flatMap(getRelayUrls)).filter(isShareableRelayUrl).map(loadRelay)) diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 8387b411..2827476e 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -67,8 +67,8 @@ import {nsecDecode} from "@lib/util" import {theme} from "@app/theme" import {INDEXER_RELAYS, userMembership, ensureUnwrapped, canDecrypt} from "@app/state" - import {loadUserData, loginWithNip46} from "@app/commands" - import {listenForNotifications} from "@app/requests" + import {loadUserData, listenForNotifications} from "@app/requests" + import {loginWithNip46} from "@app/commands" import * as commands from "@app/commands" import * as requests from "@app/requests" import * as notifications from "@app/notifications" diff --git a/src/routes/discover/+page.svelte b/src/routes/discover/+page.svelte index 63559f44..d2e3f8bc 100644 --- a/src/routes/discover/+page.svelte +++ b/src/routes/discover/+page.svelte @@ -20,7 +20,7 @@ userRoomsByUrl, getDefaultPubkeys, } from "@app/state" - import {discoverRelays} from "@app/commands" + import {discoverRelays} from "@app/requests" import {pushModal} from "@app/modal" const wotGraph = $derived.by(() => { @@ -36,20 +36,23 @@ }) const relaySearch = $derived( - createSearch($relays, { - getValue: (relay: Relay) => relay.url, - sortFn: ({score, item}) => { - if (score && score > 0.1) return -score! + createSearch( + $relays.filter(r => wotGraph.has(r.url)), + { + getValue: (relay: Relay) => relay.url, + sortFn: ({score, item}) => { + if (score && score > 0.1) return -score! - const wotScore = wotGraph.get(item.url)?.size || 0 + const wotScore = wotGraph.get(item.url)?.size || 0 - return score ? dec(score) * wotScore : -wotScore + return score ? dec(score) * wotScore : -wotScore + }, + fuseOptions: { + keys: ["url", "name", {name: "description", weight: 0.3}], + shouldSort: false, + }, }, - fuseOptions: { - keys: ["url", "name", {name: "description", weight: 0.3}], - shouldSort: false, - }, - }), + ), ) const openSpace = (url: string) => pushModal(SpaceCheck, {url}) @@ -129,8 +132,8 @@ {/each} {#await discoverRelays($memberships)} -
- Loading more relays... +
+ Loading spaces...
{/await}
diff --git a/src/routes/settings/relays/+page.svelte b/src/routes/settings/relays/+page.svelte index dfa9d1d7..a807b3f9 100644 --- a/src/routes/settings/relays/+page.svelte +++ b/src/routes/settings/relays/+page.svelte @@ -16,7 +16,8 @@ import RelayItem from "@app/components/RelayItem.svelte" import RelayAdd from "@app/components/RelayAdd.svelte" import {pushModal} from "@app/modal" - import {setRelayPolicy, discoverRelays, setInboxRelayPolicy} from "@app/commands" + import {discoverRelays} from "@app/requests" + import {setRelayPolicy, setInboxRelayPolicy} from "@app/commands" const readRelayUrls = derived(userRelaySelections, getReadRelayUrls) const writeRelayUrls = derived(userRelaySelections, getWriteRelayUrls)