Load messages more aggressively at the top level for all spaces

This commit is contained in:
Jon Staab
2024-12-30 15:15:22 -08:00
parent 7a17dc772f
commit 0c9109f387
2 changed files with 11 additions and 34 deletions
+11 -28
View File
@@ -1,9 +1,9 @@
import {partition, assoc, now} from "@welshman/lib" import {partition, assoc, now, ago, MONTH} from "@welshman/lib"
import {MESSAGE, REACTION, DELETE, THREAD, COMMENT} from "@welshman/util" import {MESSAGE, DELETE, THREAD, COMMENT} from "@welshman/util"
import type {Subscription} from "@welshman/net" import type {Subscription} from "@welshman/net"
import type {AppSyncOpts} from "@welshman/app" import type {AppSyncOpts} from "@welshman/app"
import {subscribe, repository, load, pull, hasNegentropy} from "@welshman/app" import {subscribe, repository, pull, hasNegentropy} from "@welshman/app"
import {userRoomsByUrl, LEGACY_MESSAGE, GENERAL, getEventsForUrl} from "@app/state" import {userRoomsByUrl, getEventsForUrl} from "@app/state"
// Utils // Utils
@@ -29,16 +29,15 @@ export const pullConservatively = ({relays, filters}: AppSyncOpts) => {
// Application requests // Application requests
export const listenForNotifications = () => { export const listenForNotifications = () => {
const since = now()
const subs: Subscription[] = [] const subs: Subscription[] = []
for (const [url, rooms] of userRoomsByUrl.get()) { for (const [url, rooms] of userRoomsByUrl.get()) {
load({ pullConservatively({
relays: [url], relays: [url],
filters: [ filters: [
{kinds: [THREAD], limit: 1}, {kinds: [THREAD, DELETE], since: ago(MONTH)},
{kinds: [COMMENT], "#K": [String(THREAD)], limit: 1}, {kinds: [COMMENT], "#K": [String(THREAD)], since: ago(MONTH)},
...Array.from(rooms).map(room => ({kinds: [MESSAGE], "#h": [room], limit: 1})), ...Array.from(rooms).map(room => ({kinds: [MESSAGE], "#h": [room], since: ago(MONTH)})),
], ],
}) })
@@ -46,9 +45,9 @@ export const listenForNotifications = () => {
subscribe({ subscribe({
relays: [url], relays: [url],
filters: [ filters: [
{kinds: [THREAD], since}, {kinds: [THREAD], since: now()},
{kinds: [COMMENT], "#K": [String(THREAD)], since}, {kinds: [COMMENT], "#K": [String(THREAD)], since: now()},
{kinds: [MESSAGE], "#h": Array.from(rooms), since}, {kinds: [MESSAGE], "#h": Array.from(rooms), since: now()},
], ],
}), }),
) )
@@ -60,19 +59,3 @@ export const listenForNotifications = () => {
} }
} }
} }
export const listenForChannelMessages = (url: string, room: string) => {
const since = now()
const relays = [url]
const kinds = [MESSAGE, REACTION, DELETE]
const legacyRoom = room === GENERAL ? "general" : room
// Load legacy immediate so our request doesn't get rejected by nip29 relays
load({relays, filters: [{kinds: [LEGACY_MESSAGE], "#~": [legacyRoom]}], delay: 0})
// Load historical state with negentropy if available
pullConservatively({relays, filters: [{kinds, "#h": [room]}]})
// Listen for new messages
return subscribe({relays, filters: [{kinds, "#h": [room], since}]})
}
@@ -9,7 +9,6 @@
import type {TrustedEvent, EventContent} from "@welshman/util" import type {TrustedEvent, EventContent} from "@welshman/util"
import {throttled} from "@welshman/store" import {throttled} from "@welshman/store"
import {createEvent, MESSAGE} from "@welshman/util" import {createEvent, MESSAGE} from "@welshman/util"
import type {Subscription} from "@welshman/net"
import {formatTimestampAsDate, publishThunk, deriveRelay} from "@welshman/app" import {formatTimestampAsDate, publishThunk, deriveRelay} from "@welshman/app"
import {slide} from "@lib/transition" import {slide} from "@lib/transition"
import {createScroller, type Scroller} from "@lib/html" import {createScroller, type Scroller} from "@lib/html"
@@ -34,7 +33,6 @@
} from "@app/state" } from "@app/state"
import {setChecked} from "@app/notifications" import {setChecked} from "@app/notifications"
import {nip29, addRoomMembership, removeRoomMembership, getThunkError} from "@app/commands" import {nip29, addRoomMembership, removeRoomMembership, getThunkError} from "@app/commands"
import {listenForChannelMessages} from "@app/requests"
import {PROTECTED, hasNip29} from "@app/state" import {PROTECTED, hasNip29} from "@app/state"
import {popKey} from "@app/implicit" import {popKey} from "@app/implicit"
import {pushToast} from "@app/toast" import {pushToast} from "@app/toast"
@@ -92,7 +90,6 @@
let limit = 30 let limit = 30
let loading = sleep(5000) let loading = sleep(5000)
let sub: Subscription
let element: HTMLElement let element: HTMLElement
let scroller: Scroller let scroller: Scroller
let editor: Readable<Editor> let editor: Readable<Editor>
@@ -138,14 +135,11 @@
loading = sleep(5000) loading = sleep(5000)
}, },
}) })
sub = listenForChannelMessages(url, room)
}) })
onDestroy(() => { onDestroy(() => {
setChecked($page.url.pathname) setChecked($page.url.pathname)
scroller?.stop() scroller?.stop()
sub?.close()
}) })
</script> </script>