flotilla/src/app/sync.ts

433 lines
11 KiB
TypeScript
Raw Normal View History

2025-10-06 18:23:19 +00:00
import {page} from "$app/stores"
import type {Unsubscriber} from "svelte/store"
import {ago, assoc, call, noop, MONTH, WEEK} from "@welshman/lib"
2026-07-28 16:16:26 +00:00
import type {Maybe} from "@welshman/lib"
2025-10-06 18:23:19 +00:00
import {
2026-07-28 16:16:26 +00:00
APP_DATA,
DELETE,
2026-07-03 23:36:52 +00:00
FOLLOWS,
2026-07-28 16:16:26 +00:00
MESSAGE,
MUTES,
PINBOARD,
POLL_RESPONSE,
RELAY_ADD_MEMBER,
RELAY_MEMBERS,
RELAY_REMOVE_MEMBER,
RELAY_ROLE,
REPORT,
ROOM_ADD_MEMBER,
2026-07-28 16:16:26 +00:00
ROOM_ADMINS,
ROOM_CREATE_PERMISSION,
ROOM_DELETE,
ROOM_JOIN,
ROOM_LEAVE,
2026-07-28 16:16:26 +00:00
ROOM_MEMBERS,
ROOM_META,
ROOM_PINS,
ROOM_REMOVE_MEMBER,
WRAP,
outbox,
2025-11-04 23:36:20 +00:00
unionFilters,
2025-10-06 18:23:19 +00:00
} from "@welshman/util"
2026-07-28 16:16:26 +00:00
import type {Filter} from "@welshman/util"
import type {FollowListReader} from "@welshman/domain"
import {merged, synced, throttled, withGetter} from "@welshman/store"
import {FollowLists, MessagingRelayLists, RelayLists, SearchRelayLists, Sync} from "@welshman/app"
2026-07-28 16:16:26 +00:00
import {
app,
blockedRelayLists,
blossomServerLists,
deriveUserItem,
followLists,
messagingRelayLists,
muteLists,
network,
profiles,
relayLists,
relays,
roomLists,
rooms,
2026-07-28 16:16:26 +00:00
router,
searchRelayLists,
2026-07-28 16:16:26 +00:00
} from "@app/core"
import {LIVEKIT_PARTICIPANTS} from "@app/call"
import {CONTENT_KINDS, REACTION_KINDS, makeCommentFilter} from "@app/content"
import {INDEXER_RELAYS} from "@app/env"
import {FEATURED_CONTENT_D} from "@app/featured"
2026-07-28 16:16:26 +00:00
import {decodeRelay} from "@app/relays"
import {activeSpaceUrls, userSpaceUrls} from "@app/rooms"
2026-07-28 16:16:26 +00:00
import {Settings} from "@app/settings"
import {kv} from "@app/storage"
import {hasBlossomSupport} from "@app/uploads"
2026-07-28 16:16:26 +00:00
// Unwrapping is unconditional, so this is what keeps a signer from decrypting the whole DM history.
2026-07-28 16:16:26 +00:00
export const shouldUnwrap = withGetter(
synced({key: "shouldUnwrap", storage: kv, defaultValue: false}),
)
2025-10-06 18:23:19 +00:00
// Utils
type SyncOpts = {
url: string
2025-10-06 18:23:19 +00:00
signal: AbortSignal
filters: Filter[]
2025-10-06 18:23:19 +00:00
}
2026-07-28 16:16:26 +00:00
const pullAndListen = ({url, signal, filters}: SyncOpts) => {
2026-09-21 16:48:35 +00:00
if (signal.aborted) {
return
}
2026-07-28 16:16:26 +00:00
app
.get()
.use(Sync)
.pull({relays: [url], filters})
network.get().request({
relays: [url],
signal,
filters: unionFilters(filters.map(assoc("limit", 0))),
})
2026-02-26 22:29:13 +00:00
}
const userRelayList = deriveUserItem(RelayLists)
2025-10-28 21:53:44 +00:00
const userFollowList = deriveUserItem(FollowLists)
const userMessagingRelayList = deriveUserItem(MessagingRelayLists)
const userSearchRelayList = deriveUserItem(SearchRelayLists)
2025-10-06 18:23:19 +00:00
// Relays
2025-10-06 18:23:19 +00:00
const syncRelays = () => {
for (const url of INDEXER_RELAYS) {
2026-07-28 16:16:26 +00:00
relays.get().load(url)
2025-10-06 18:23:19 +00:00
}
const unsubscribePage = page.subscribe($page => {
if ($page.params.relay) {
2025-10-06 18:23:19 +00:00
const url = decodeRelay($page.params.relay)
2026-07-28 16:16:26 +00:00
relays.get().load(url)
2025-10-06 18:23:19 +00:00
hasBlossomSupport(url)
2025-10-06 18:23:19 +00:00
}
})
const unsubscribeSpaceUrls = userSpaceUrls.subscribe($userSpaceUrls => {
for (const url of $userSpaceUrls) {
2026-07-28 16:16:26 +00:00
relays.get().load(url)
2025-10-06 18:23:19 +00:00
}
})
const unsubscribeSearchRelayUrls = userSearchRelayList.subscribe($searchRelayList => {
for (const url of $searchRelayList?.urls() ?? []) {
relays.get().load(url)
}
})
2025-10-06 18:23:19 +00:00
return () => {
unsubscribePage()
unsubscribeSpaceUrls()
unsubscribeSearchRelayUrls()
2025-10-06 18:23:19 +00:00
}
}
2025-10-06 18:23:19 +00:00
// User data
2025-10-28 18:29:59 +00:00
const syncUserSpaceMembership = (url: string) => {
2026-07-28 16:16:26 +00:00
const $pubkey = app.get().user?.pubkey
2025-10-28 18:29:59 +00:00
const controller = new AbortController()
if ($pubkey) {
pullAndListen({
url,
2025-10-28 18:29:59 +00:00
signal: controller.signal,
filters: [
2025-12-01 18:56:37 +00:00
{kinds: [RELAY_ADD_MEMBER], "#p": [$pubkey], limit: 1},
{kinds: [RELAY_REMOVE_MEMBER], "#p": [$pubkey], limit: 1},
{kinds: [ROOM_CREATE_PERMISSION], "#p": [$pubkey], limit: 1},
{kinds: [ROOM_ADD_MEMBER, ROOM_REMOVE_MEMBER], "#p": [$pubkey]},
2025-10-28 18:29:59 +00:00
],
})
}
return () => controller.abort()
}
2025-10-06 18:23:19 +00:00
const syncUserData = () => {
const unsubscribersByUrl = new Map<string, Unsubscriber>()
2026-09-01 17:40:28 +00:00
const $pubkey = app.get().user?.pubkey
2025-10-28 18:29:59 +00:00
const syncSpaceMembership = ($activeSpaceUrls: string[]) => {
const urls = new Set($activeSpaceUrls)
2025-10-28 18:29:59 +00:00
for (const url of urls) {
if (!unsubscribersByUrl.has(url)) {
unsubscribersByUrl.set(url, syncUserSpaceMembership(url))
2025-10-28 18:29:59 +00:00
}
}
2025-10-28 18:29:59 +00:00
for (const [url, unsubscribe] of unsubscribersByUrl.entries()) {
if (!urls.has(url)) {
unsubscribersByUrl.delete(url)
unsubscribe()
2025-10-28 18:29:59 +00:00
}
2025-10-06 18:23:19 +00:00
}
}
2025-10-06 18:23:19 +00:00
2026-09-01 17:40:28 +00:00
const syncUserLists = () => {
if ($pubkey) {
blossomServerLists.get().load($pubkey)
blockedRelayLists.get().load($pubkey)
followLists.get().load($pubkey)
roomLists.get().load($pubkey)
muteLists.get().load($pubkey)
profiles.get().load($pubkey)
searchRelayLists.get().load($pubkey)
2026-09-01 17:40:28 +00:00
app.get().use(Settings).load($pubkey)
2026-07-03 23:36:52 +00:00
}
}
2025-10-06 18:23:19 +00:00
2026-07-28 16:16:26 +00:00
const syncFollowNetwork = async ($followList: Maybe<FollowListReader>) => {
const authors = $followList?.pubkeys() ?? []
2026-07-28 16:16:26 +00:00
if (authors.length > 0) {
const scenario = await router.get().resolve(authors.map(author => outbox(author)))
2026-09-21 17:58:14 +00:00
network.get().loadLenient({
2026-07-03 23:36:52 +00:00
filters: [{kinds: [FOLLOWS, MUTES], authors}],
2026-07-28 16:16:26 +00:00
relays: scenario.limit(8).getUrls(),
2026-07-03 23:36:52 +00:00
})
}
}
2026-09-01 17:40:28 +00:00
if ($pubkey) {
relayLists.get().load($pubkey)
}
const unsubscribeSpaceMembership = activeSpaceUrls.subscribe(syncSpaceMembership)
2026-09-01 17:40:28 +00:00
const unsubscribeRelayList = userRelayList.subscribe(syncUserLists)
const unsubscribeFollowList = userFollowList.subscribe(syncFollowNetwork)
2025-10-06 18:23:19 +00:00
return () => {
unsubscribersByUrl.forEach(call)
unsubscribeSpaceMembership()
2025-12-01 18:56:37 +00:00
unsubscribeRelayList()
unsubscribeFollowList()
2025-10-06 18:23:19 +00:00
}
}
2025-10-28 18:29:59 +00:00
// Spaces
2025-10-06 18:23:19 +00:00
const syncSpaceActivity = (url: string) => {
2025-10-06 18:23:19 +00:00
const controller = new AbortController()
const {signal} = controller
const asked = new Set<string>()
2026-03-25 21:36:48 +00:00
network.get().request({
relays: [url],
signal,
2026-03-25 21:36:48 +00:00
filters: [
{kinds: [MESSAGE], limit: 1},
{kinds: CONTENT_KINDS, since: ago(WEEK)},
makeCommentFilter(CONTENT_KINDS, {since: ago(WEEK)}),
{kinds: [DELETE, REPORT, POLL_RESPONSE], since: ago(WEEK)},
2026-07-02 23:36:58 +00:00
],
})
pullAndListen({url, signal, filters: [{kinds: [ROOM_META, ROOM_DELETE, RELAY_MEMBERS]}]})
const unsubscribeRooms = throttled(300, rooms.get().forUrl(url).$).subscribe($rooms => {
const hs = $rooms.map(room => room.h).filter(h => !asked.has(h))
if (hs.length > 0) {
for (const h of hs) {
asked.add(h)
}
network.get().request({
relays: [url],
signal,
autoClose: true,
filters: hs.map(h => ({kinds: [MESSAGE], "#h": [h], limit: 1})),
})
}
2026-07-02 23:36:58 +00:00
})
return () => {
unsubscribeRooms()
controller.abort()
}
}
const syncSpaceDetail = (url: string) => {
const controller = new AbortController()
2026-07-02 23:36:58 +00:00
pullAndListen({
url,
signal: controller.signal,
filters: [
{kinds: [RELAY_ROLE]},
{kinds: [APP_DATA], "#d": [FEATURED_CONTENT_D]},
{kinds: [ROOM_ADMINS, ROOM_MEMBERS, LIVEKIT_PARTICIPANTS, PINBOARD, ROOM_PINS]},
{kinds: [ROOM_JOIN, ROOM_LEAVE], since: ago(MONTH)},
{kinds: REACTION_KINDS, since: ago(WEEK)},
2026-03-25 21:36:48 +00:00
],
})
// Which sections a space offers is a question about its whole history, and one event per kind answers it.
2026-09-21 17:58:14 +00:00
network.get().loadLenient({
relays: [url],
signal: controller.signal,
filters: CONTENT_KINDS.map(kind => ({kinds: [kind], limit: 1})),
})
2025-10-06 18:23:19 +00:00
return () => controller.abort()
}
2025-10-28 18:29:59 +00:00
const syncSpaces = () => {
2025-12-01 18:56:37 +00:00
const unsubscribersByUrl = new Map<string, Unsubscriber>()
2026-02-26 22:29:13 +00:00
let detailUrl: Maybe<string>
let unsubscribeDetail: Unsubscriber = noop
const unsubscribe = merged([activeSpaceUrls, page]).subscribe(([$activeSpaceUrls, $page]) => {
const urls = new Set($activeSpaceUrls)
const currentUrl = $page.params.relay ? decodeRelay($page.params.relay) : undefined
2025-12-01 18:56:37 +00:00
if (currentUrl) {
urls.add(currentUrl)
2025-12-01 18:56:37 +00:00
}
2025-10-06 18:23:19 +00:00
2026-02-26 22:29:13 +00:00
// Stop syncing removed spaces
2025-12-01 18:56:37 +00:00
for (const [url, unsubscribe] of unsubscribersByUrl.entries()) {
2026-02-26 22:29:13 +00:00
if (!urls.has(url)) {
2025-12-01 18:56:37 +00:00
unsubscribersByUrl.delete(url)
2025-10-06 18:23:19 +00:00
unsubscribe()
}
}
2025-10-06 18:23:19 +00:00
2026-04-11 17:20:50 +00:00
// Start syncing for new spaces
2025-10-06 18:23:19 +00:00
for (const url of urls) {
2026-04-11 17:20:50 +00:00
if (!unsubscribersByUrl.has(url)) {
unsubscribersByUrl.set(url, syncSpaceActivity(url))
}
2025-10-06 18:23:19 +00:00
}
if (detailUrl !== currentUrl) {
detailUrl = currentUrl
unsubscribeDetail()
unsubscribeDetail = currentUrl ? syncSpaceDetail(currentUrl) : noop
}
2025-10-06 18:23:19 +00:00
})
2025-12-01 18:56:37 +00:00
return () => {
for (const unsubscriber of unsubscribersByUrl.values()) {
unsubscriber()
2025-10-28 18:29:59 +00:00
}
unsubscribeDetail()
2025-12-01 18:56:37 +00:00
unsubscribe()
2025-10-06 18:23:19 +00:00
}
}
2025-10-06 18:23:19 +00:00
// DMs
2025-10-06 18:23:19 +00:00
const syncDMRelay = (url: string, pubkey: string) => {
const controller = new AbortController()
pullAndListen({
url,
2025-10-06 18:23:19 +00:00
signal: controller.signal,
filters: [{kinds: [WRAP], "#p": [pubkey]}],
2025-10-06 18:23:19 +00:00
})
return () => controller.abort()
}
const syncDMs = () => {
const unsubscribersByUrl = new Map<string, Unsubscriber>()
let currentPubkey: string | undefined
let currentShouldUnwrap = false
2025-10-06 18:23:19 +00:00
const unsubscribeAll = () => {
for (const [url, unsubscribe] of unsubscribersByUrl.entries()) {
unsubscribersByUrl.delete(url)
unsubscribe()
}
}
const subscribeAll = (pubkey: string, urls: string[]) => {
// Start syncing newly added relays
for (const url of urls) {
if (!unsubscribersByUrl.has(url)) {
unsubscribersByUrl.set(url, syncDMRelay(url, pubkey))
}
}
// Stop syncing removed spaces
for (const [url, unsubscribe] of unsubscribersByUrl.entries()) {
if (!urls.includes(url)) {
unsubscribersByUrl.delete(url)
unsubscribe()
}
}
}
const syncPubkey = async ($pubkey: Maybe<string>, $shouldUnwrap: boolean) => {
2026-07-28 16:16:26 +00:00
if ($pubkey !== currentPubkey) {
unsubscribeAll()
}
currentPubkey = $pubkey
currentShouldUnwrap = $shouldUnwrap
if ($pubkey && $shouldUnwrap) {
await relayLists.get().load($pubkey).catch(noop)
await messagingRelayLists.get().load($pubkey).catch(noop)
if (currentPubkey === $pubkey && currentShouldUnwrap === $shouldUnwrap) {
subscribeAll($pubkey, messagingRelayLists.get().urls($pubkey).get())
}
}
2026-07-28 16:16:26 +00:00
}
const syncList = () => {
2026-07-28 16:16:26 +00:00
const $pubkey = app.get().user?.pubkey
if ($pubkey && shouldUnwrap.get()) {
subscribeAll($pubkey, messagingRelayLists.get().urls($pubkey).get())
2026-07-28 16:16:26 +00:00
}
}
const unsubscribeUser = merged([app, shouldUnwrap]).subscribe(([$app, $shouldUnwrap]) => {
syncPubkey($app.user?.pubkey, $shouldUnwrap)
})
2025-10-06 18:23:19 +00:00
2026-07-28 16:16:26 +00:00
const unsubscribeList = userMessagingRelayList.subscribe(syncList)
2025-10-06 18:23:19 +00:00
return () => {
unsubscribeAll()
2026-07-28 16:16:26 +00:00
unsubscribeUser()
2025-11-20 23:12:16 +00:00
unsubscribeList()
2025-10-06 18:23:19 +00:00
}
}
2025-10-06 18:23:19 +00:00
// Merge all synchronization functions
let unsubscribe: Unsubscriber | undefined
2026-07-06 20:25:20 +00:00
2025-10-06 18:23:19 +00:00
export const syncApplicationData = () => {
2026-07-27 23:20:27 +00:00
unsubscribe?.()
2025-11-11 00:19:50 +00:00
const unsubscribers = [syncRelays(), syncUserData(), syncSpaces(), syncDMs()]
2025-10-06 18:23:19 +00:00
2026-07-06 20:25:20 +00:00
unsubscribe = () => unsubscribers.forEach(call)
2026-07-27 23:20:27 +00:00
return () => {
unsubscribe?.()
unsubscribe = undefined
}
2025-10-06 18:23:19 +00:00
}