Move loadUserData to requests

This commit is contained in:
Jon Staab
2025-02-14 11:12:19 -08:00
parent e484c3cb00
commit b05c408977
8 changed files with 99 additions and 76 deletions
+3 -52
View File
@@ -1,6 +1,6 @@
import * as nip19 from "nostr-tools/nip19" import * as nip19 from "nostr-tools/nip19"
import {get} from "svelte/store" import {get} from "svelte/store"
import {ctx, sample, uniq, sleep, chunk, equals} from "@welshman/lib" import {ctx, uniq, equals} from "@welshman/lib"
import { import {
DELETE, DELETE,
REPORT, REPORT,
@@ -26,12 +26,10 @@ import {
getTag, getTag,
getListTags, getListTags,
getRelayTags, getRelayTags,
isShareableRelayUrl,
getRelayTagValues, getRelayTagValues,
toNostrURI, toNostrURI,
} from "@welshman/util" } from "@welshman/util"
import type {TrustedEvent, EventContent, EventTemplate, List} from "@welshman/util" import type {TrustedEvent, EventContent, EventTemplate} from "@welshman/util"
import type {SubscribeRequestWithHandlers} from "@welshman/net"
import {PublishStatus, AuthStatus, SocketStatus} from "@welshman/net" import {PublishStatus, AuthStatus, SocketStatus} from "@welshman/net"
import {Nip59, makeSecret, stamp, Nip46Broker} from "@welshman/signer" import {Nip59, makeSecret, stamp, Nip46Broker} from "@welshman/signer"
import { import {
@@ -40,13 +38,9 @@ import {
repository, repository,
publishThunk, publishThunk,
publishThunks, publishThunks,
loadProfile,
loadInboxRelaySelections,
profilesByPubkey, profilesByPubkey,
relaySelectionsByPubkey, relaySelectionsByPubkey,
getWriteRelayUrls, getWriteRelayUrls,
loadFollows,
loadMutes,
tagEvent, tagEvent,
tagEventForReaction, tagEventForReaction,
getRelayUrls, getRelayUrls,
@@ -67,11 +61,9 @@ import {
userMembership, userMembership,
INDEXER_RELAYS, INDEXER_RELAYS,
NIP46_PERMS, NIP46_PERMS,
loadMembership,
loadSettings,
getDefaultPubkeys,
userRoomsByUrl, userRoomsByUrl,
} from "@app/state" } from "@app/state"
import {loadUserData} from "@app/requests"
// Utils // Utils
@@ -161,47 +153,6 @@ export const logout = async () => {
localStorage.clear() localStorage.clear()
} }
// Loaders
export const loadUserData = (
pubkey: string,
request: Partial<SubscribeRequestWithHandlers> = {},
) => {
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 // Synchronization
export const broadcastUserData = async (relays: string[]) => { export const broadcastUserData = async (relays: string[]) => {
+1 -1
View File
@@ -13,7 +13,7 @@
import {pushModal, clearModals} from "@app/modal" import {pushModal, clearModals} from "@app/modal"
import {PLATFORM_NAME, BURROW_URL} from "@app/state" import {PLATFORM_NAME, BURROW_URL} from "@app/state"
import {pushToast} from "@app/toast" import {pushToast} from "@app/toast"
import {loadUserData} from "@app/commands" import {loadUserData} from "@app/requests"
import {setChecked} from "@app/notifications" import {setChecked} from "@app/notifications"
let signers: any[] = $state([]) let signers: any[] = $state([])
+2 -1
View File
@@ -12,7 +12,8 @@
import ModalFooter from "@lib/components/ModalFooter.svelte" import ModalFooter from "@lib/components/ModalFooter.svelte"
import QRCode from "@app/components/QRCode.svelte" import QRCode from "@app/components/QRCode.svelte"
import InfoBunker from "@app/components/InfoBunker.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 {pushModal, clearModals} from "@app/modal"
import {setChecked} from "@app/notifications" import {setChecked} from "@app/notifications"
import {pushToast} from "@app/toast" import {pushToast} from "@app/toast"
+1 -1
View File
@@ -12,7 +12,7 @@
import ModalHeader from "@lib/components/ModalHeader.svelte" import ModalHeader from "@lib/components/ModalHeader.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte" import ModalFooter from "@lib/components/ModalFooter.svelte"
import PasswordResetRequest from "@app/components/PasswordResetRequest.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 {clearModals, pushModal} from "@app/modal"
import {setChecked} from "@app/notifications" import {setChecked} from "@app/notifications"
import {pushToast} from "@app/toast" import {pushToast} from "@app/toast"
+71 -4
View File
@@ -1,5 +1,19 @@
import {get, writable} from "svelte/store" 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 { import {
MESSAGE, MESSAGE,
DELETE, DELETE,
@@ -9,10 +23,11 @@ import {
matchFilters, matchFilters,
getTagValues, getTagValues,
getTagValue, getTagValue,
isShareableRelayUrl,
} from "@welshman/util" } 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 {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 type {AppSyncOpts, Thunk} from "@welshman/app"
import { import {
subscribe, subscribe,
@@ -22,10 +37,23 @@ import {
hasNegentropy, hasNegentropy,
thunkWorker, thunkWorker,
createFeedController, createFeedController,
loadRelay,
loadMutes,
loadFollows,
loadProfile,
loadInboxRelaySelections,
getRelayUrls,
} from "@welshman/app" } from "@welshman/app"
import {createScroller} from "@lib/html" import {createScroller} from "@lib/html"
import {daysBetween} from "@lib/util" import {daysBetween} from "@lib/util"
import {userRoomsByUrl, getUrlsForEvent} from "@app/state" import {
INDEXER_RELAYS,
getDefaultPubkeys,
userRoomsByUrl,
getUrlsForEvent,
loadMembership,
loadSettings,
} from "@app/state"
// Utils // Utils
@@ -317,3 +345,42 @@ export const listenForNotifications = () => {
} }
} }
} }
export const loadUserData = (
pubkey: string,
request: Partial<SubscribeRequestWithHandlers> = {},
) => {
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))
+2 -2
View File
@@ -67,8 +67,8 @@
import {nsecDecode} from "@lib/util" import {nsecDecode} from "@lib/util"
import {theme} from "@app/theme" import {theme} from "@app/theme"
import {INDEXER_RELAYS, userMembership, ensureUnwrapped, canDecrypt} from "@app/state" import {INDEXER_RELAYS, userMembership, ensureUnwrapped, canDecrypt} from "@app/state"
import {loadUserData, loginWithNip46} from "@app/commands" import {loadUserData, listenForNotifications} from "@app/requests"
import {listenForNotifications} from "@app/requests" import {loginWithNip46} from "@app/commands"
import * as commands from "@app/commands" import * as commands from "@app/commands"
import * as requests from "@app/requests" import * as requests from "@app/requests"
import * as notifications from "@app/notifications" import * as notifications from "@app/notifications"
+17 -14
View File
@@ -20,7 +20,7 @@
userRoomsByUrl, userRoomsByUrl,
getDefaultPubkeys, getDefaultPubkeys,
} from "@app/state" } from "@app/state"
import {discoverRelays} from "@app/commands" import {discoverRelays} from "@app/requests"
import {pushModal} from "@app/modal" import {pushModal} from "@app/modal"
const wotGraph = $derived.by(() => { const wotGraph = $derived.by(() => {
@@ -36,20 +36,23 @@
}) })
const relaySearch = $derived( const relaySearch = $derived(
createSearch($relays, { createSearch(
getValue: (relay: Relay) => relay.url, $relays.filter(r => wotGraph.has(r.url)),
sortFn: ({score, item}) => { {
if (score && score > 0.1) return -score! 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}) const openSpace = (url: string) => pushModal(SpaceCheck, {url})
@@ -129,8 +132,8 @@
</Button> </Button>
{/each} {/each}
{#await discoverRelays($memberships)} {#await discoverRelays($memberships)}
<div class="flex justify-center"> <div class="flex justify-center py-20">
<Spinner loading>Loading more relays...</Spinner> <Spinner loading>Loading spaces...</Spinner>
</div> </div>
{/await} {/await}
</div> </div>
+2 -1
View File
@@ -16,7 +16,8 @@
import RelayItem from "@app/components/RelayItem.svelte" import RelayItem from "@app/components/RelayItem.svelte"
import RelayAdd from "@app/components/RelayAdd.svelte" import RelayAdd from "@app/components/RelayAdd.svelte"
import {pushModal} from "@app/modal" 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 readRelayUrls = derived(userRelaySelections, getReadRelayUrls)
const writeRelayUrls = derived(userRelaySelections, getWriteRelayUrls) const writeRelayUrls = derived(userRelaySelections, getWriteRelayUrls)