flotilla/src/app/sync.ts

436 lines
10 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"
2026-07-28 16:16:26 +00:00
import {ago, assoc, call, MONTH, WEEK} from "@welshman/lib"
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,
2026-07-03 23:36:52 +00:00
FOLLOWS,
2026-07-28 16:16:26 +00:00
MESSAGE,
MUTES,
PIN,
PINBOARD,
POLL_RESPONSE,
RELAY_ADD_MEMBER,
RELAY_MEMBERS,
RELAY_REMOVE_MEMBER,
RELAY_ROLE,
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,
MessagingRelayListReader,
RelayListReader,
RoomListReader,
} from "@welshman/domain"
import {merged, synced, withGetter} from "@welshman/store"
2025-10-06 18:23:19 +00:00
import {
2026-07-28 16:16:26 +00:00
FollowLists,
MessagingRelayLists,
RelayLists,
RoomLists,
Sync,
makeRoomKey,
2025-10-06 18:23:19 +00:00
} from "@welshman/app"
2026-07-28 16:16:26 +00:00
import {
app,
blockedRelayLists,
blossomServerLists,
deriveUserItem,
followLists,
messagingRelayLists,
muteLists,
network,
profiles,
relayLists,
relays,
roomLists,
router,
} from "@app/core"
import {LIVEKIT_PARTICIPANTS} from "@app/call"
import {REACTION_KINDS, CONTENT_KINDS, makeCommentFilter} from "@app/content"
2026-07-01 17:23:23 +00:00
import {INDEXER_RELAYS, PLATFORM_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 {Settings} from "@app/settings"
import {kv} from "@app/storage"
import {hasBlossomSupport} from "@app/uploads"
2026-07-28 16:16:26 +00:00
// Whether to sync gift wraps. Unwrapping itself is unconditional, so this is the only
// thing keeping a signer from being asked to decrypt the user's entire DM history.
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) => {
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 userRoomList = deriveUserItem(RoomLists)
2025-10-06 18:23:19 +00:00
const userRelayList = deriveUserItem(RelayLists)
2025-10-28 21:53:44 +00:00
const userFollowList = deriveUserItem(FollowLists)
const userMessagingRelayList = deriveUserItem(MessagingRelayLists)
2026-07-28 16:16:26 +00:00
const getSpaceUrls = ($roomList: Maybe<RoomListReader>) =>
PLATFORM_RELAYS.length > 0 ? PLATFORM_RELAYS : ($roomList?.urls() ?? [])
2025-10-28 18:29:59 +00:00
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
}
})
2026-07-28 16:16:26 +00:00
const unsubscribeSpaceUrls = userRoomList.subscribe($roomList => {
for (const url of $roomList?.urls() ?? []) {
relays.get().load(url)
2025-10-06 18:23:19 +00:00
}
})
return () => {
unsubscribePage()
unsubscribeSpaceUrls()
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},
2025-10-28 18:29:59 +00:00
],
})
}
return () => controller.abort()
}
2025-10-30 22:33:34 +00:00
const syncUserRoomMembership = (url: string, h: 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: [ROOM_ADD_MEMBER], "#p": [$pubkey], "#h": [h], limit: 1},
{kinds: [ROOM_REMOVE_MEMBER], "#p": [$pubkey], "#h": [h], limit: 1},
2025-10-28 18:29:59 +00:00
],
})
}
return () => controller.abort()
}
2025-10-06 18:23:19 +00:00
const syncUserData = () => {
2025-10-28 18:29:59 +00:00
const unsubscribersByKey = new Map<string, Unsubscriber>()
2026-07-28 16:16:26 +00:00
const syncRoomList = ($roomList: Maybe<RoomListReader>) => {
if ($roomList) {
2025-10-28 18:29:59 +00:00
const keys = new Set<string>()
2026-07-28 16:16:26 +00:00
for (const url of getSpaceUrls($roomList)) {
2025-10-28 18:29:59 +00:00
if (!unsubscribersByKey.has(url)) {
unsubscribersByKey.set(url, syncUserSpaceMembership(url))
}
keys.add(url)
2026-07-28 16:16:26 +00:00
for (const h of $roomList.roomsForUrl(url)) {
const key = makeRoomKey(url, h)
2025-10-28 18:29:59 +00:00
if (!unsubscribersByKey.has(key)) {
2025-10-30 22:33:34 +00:00
unsubscribersByKey.set(key, syncUserRoomMembership(url, h))
2025-10-28 18:29:59 +00:00
}
keys.add(key)
}
}
for (const [key, unsubscribe] of unsubscribersByKey.entries()) {
if (!keys.has(key)) {
unsubscribersByKey.delete(key)
unsubscribe()
}
}
2025-10-06 18:23:19 +00:00
}
}
2025-10-06 18:23:19 +00:00
2026-07-28 16:16:26 +00:00
const syncRelayList = ($relayList: Maybe<RelayListReader>) => {
const author = $relayList?.author()
if (author) {
blossomServerLists.get().load(author)
blockedRelayLists.get().load(author)
followLists.get().load(author)
roomLists.get().load(author)
muteLists.get().load(author)
profiles.get().load(author)
app.get().use(Settings).load(author)
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-07-28 16:16:26 +00:00
network.get().load({
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-07-28 16:16:26 +00:00
const unsubscribeRoomList = userRoomList.subscribe(syncRoomList)
const unsubscribeRelayList = userRelayList.subscribe(syncRelayList)
const unsubscribeFollowList = userFollowList.subscribe(syncFollowNetwork)
2025-10-06 18:23:19 +00:00
return () => {
2025-10-28 18:29:59 +00:00
unsubscribersByKey.forEach(call)
2026-07-28 16:16:26 +00:00
unsubscribeRoomList()
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
2026-04-11 17:20:50 +00:00
const syncSpace = (url: string) => {
2025-10-06 18:23:19 +00:00
const controller = new AbortController()
2026-03-25 21:36:48 +00:00
2026-07-02 23:36:58 +00:00
// Low cardinality we want everything for
pullAndListen({
url,
signal: controller.signal,
2026-03-25 21:36:48 +00:00
filters: [
2026-07-02 23:36:58 +00:00
{kinds: [RELAY_MEMBERS, RELAY_ROLE]},
{kinds: [APP_DATA], "#d": [FEATURED_CONTENT_D]},
2026-07-02 23:36:58 +00:00
],
})
// Higher cardinality stuff we want as much as we can get
pullAndListen({
url,
signal: controller.signal,
filters: [
{
kinds: [
ROOM_META,
ROOM_ADMINS,
ROOM_MEMBERS,
ROOM_DELETE,
LIVEKIT_PARTICIPANTS,
2026-07-28 16:16:26 +00:00
PINBOARD,
ROOM_PINS,
],
},
2026-07-02 23:36:58 +00:00
],
})
// Recent stuff, best effort
pullAndListen({
url,
signal: controller.signal,
filters: [
{kinds: [...CONTENT_KINDS, MESSAGE, PIN, ROOM_JOIN, ROOM_LEAVE], since: ago(MONTH)},
{kinds: [...REACTION_KINDS, POLL_RESPONSE], since: ago(WEEK)},
makeCommentFilter(CONTENT_KINDS, {since: ago(WEEK)}),
2026-03-25 21:36:48 +00:00
],
})
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
2026-07-28 16:16:26 +00:00
const unsubscribe = merged([userRoomList, page]).subscribe(([$roomList, $page]) => {
const urls = new Set(getSpaceUrls($roomList))
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, syncSpace(url))
}
2025-10-06 18:23:19 +00:00
}
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
}
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()
}
}
}
2026-07-28 16:16:26 +00:00
const syncPubkey = ($pubkey: Maybe<string>, $shouldUnwrap: boolean) => {
if ($pubkey !== currentPubkey) {
unsubscribeAll()
}
if ($pubkey && $shouldUnwrap) {
relayLists
.get()
.load($pubkey)
.then(() => messagingRelayLists.get().load($pubkey))
.then($list => {
if ($list && currentPubkey === $pubkey && currentShouldUnwrap === $shouldUnwrap) {
subscribeAll($pubkey, $list.urls())
}
})
}
currentPubkey = $pubkey
currentShouldUnwrap = $shouldUnwrap
}
const syncList = ($list: Maybe<MessagingRelayListReader>) => {
const $pubkey = app.get().user?.pubkey
if ($pubkey && shouldUnwrap.get()) {
subscribeAll($pubkey, $list?.urls() ?? [])
}
}
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
}