Fix conversation sync

This commit is contained in:
Jon Staab
2024-11-13 16:14:05 -08:00
parent da3db67aa6
commit 2d89c152b1
6 changed files with 64 additions and 47 deletions
+42 -30
View File
@@ -1,9 +1,9 @@
<script lang="ts">
import "@src/app.css"
import {onMount} from "svelte"
import {get} from "svelte/store"
import {get, derived} from "svelte/store"
import {dev} from "$app/environment"
import {ctx, uniq, sleep, take, sortBy, ago, now, HOUR, WEEK} from "@welshman/lib"
import {identity, uniq, sleep, take, sortBy, ago, now, HOUR, WEEK, Worker} from "@welshman/lib"
import type {TrustedEvent} from "@welshman/util"
import {
PROFILE,
@@ -33,9 +33,12 @@
session,
signer,
dropSession,
getRelayUrls,
userInboxRelaySelections,
} from "@welshman/app"
import * as lib from "@welshman/lib"
import * as util from "@welshman/util"
import * as net from "@welshman/net"
import * as app from "@welshman/app"
import AppContainer from "@app/components/AppContainer.svelte"
import ModalContainer from "@app/components/ModalContainer.svelte"
@@ -47,6 +50,7 @@
getMembershipUrls,
getMembershipRooms,
userMembership,
ensureUnwrapped,
MEMBERSHIPS,
MESSAGE,
COMMENT,
@@ -62,8 +66,8 @@
let ready: Promise<unknown> = Promise.resolve()
onMount(() => {
Object.assign(window, {get, ...lib, ...util, ...app, ...state})
onMount(async () => {
Object.assign(window, {get, ...lib, ...util, ...net, ...app, ...state})
const getScoreEvent = () => {
const ALWAYS_KEEP = Infinity
@@ -140,6 +144,29 @@
tracker: storageAdapters.fromTracker(tracker, {throttle: 1000}),
}).then(() => sleep(300))
// Unwrap gift wraps as they come in, but throttled
const unwrapper = new Worker<TrustedEvent>({chunkSize: 10})
unwrapper.addGlobalHandler(ensureUnwrapped)
repository.on("update", ({added}) => {
for (const event of added) {
if (event.kind === WRAP) {
unwrapper.push(event)
}
}
})
// Load relay info
for (const url of INDEXER_RELAYS) {
loadRelay(url)
}
// Load user data
if ($pubkey) {
await loadUserData($pubkey)
}
let unsubRooms: any
userMembership.subscribe($membership => {
@@ -165,33 +192,18 @@
let unsubChats: any
pubkey.subscribe($pubkey => {
unsubChats?.()
derived([pubkey, userInboxRelaySelections], identity).subscribe(
([$pubkey, $userInboxRelaySelections]) => {
unsubChats?.()
if ($pubkey) {
unsubChats = subscribePersistent({
filters: [{kinds: [WRAP], "#p": [$pubkey], since: ago(WEEK)}],
relays: ctx.app.router.UserInbox().getUrls(),
})
}
})
// Unwrap gift wraps as they come in
repository.on("update", ({added}) => {
for (const event of added) {
state.ensureUnwrapped(event)
}
})
// Load relay info
for (const url of INDEXER_RELAYS) {
loadRelay(url)
}
// Load user data
if ($pubkey) {
loadUserData($pubkey)
}
if ($pubkey) {
unsubChats = subscribePersistent({
filters: [{kinds: [WRAP], "#p": [$pubkey], since: ago(WEEK, 2)}],
relays: getRelayUrls($userInboxRelaySelections),
})
}
},
)
}
})
</script>
+2 -2
View File
@@ -1,7 +1,7 @@
<script lang="ts">
import {onMount} from "svelte"
import {page} from "$app/stores"
import {WEEK, ctx, ago} from "@welshman/lib"
import {ctx} from "@welshman/lib"
import {WRAP} from "@welshman/util"
import type {TrustedEvent} from "@welshman/util"
import {pubkey, repository} from "@welshman/app"
@@ -20,7 +20,7 @@
const startChat = () => pushModal(ChatStart)
const promise = pullConservatively({
filters: [{kinds: [WRAP], "#p": [$pubkey!], until: ago(WEEK)}],
filters: [{kinds: [WRAP], "#p": [$pubkey!]}],
relays: ctx.app.router.UserInbox().getUrls(),
})