From 183aebf841c0141f6f133a8faa4cc572be308549 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Mon, 10 Nov 2025 16:19:50 -0800 Subject: [PATCH] Improve room syncing --- src/app/core/sync.ts | 70 +++----------------------------------------- 1 file changed, 4 insertions(+), 66 deletions(-) diff --git a/src/app/core/sync.ts b/src/app/core/sync.ts index 4189f7d7..4da1243a 100644 --- a/src/app/core/sync.ts +++ b/src/app/core/sync.ts @@ -6,7 +6,6 @@ import { getListTags, getRelayTagValues, WRAP, - MESSAGE, ROOM_META, ROOM_DELETE, ROOM_ADMINS, @@ -37,9 +36,9 @@ import { repository, shouldUnwrap, hasNegentropy, - relaysByUrl, } from "@welshman/app" import { + REACTION_KINDS, MESSAGE_KINDS, CONTENT_KINDS, INDEXER_RELAYS, @@ -50,7 +49,6 @@ import { bootstrapPubkeys, decodeRelay, getUrlsForEvent, - hasNip29, getSpaceUrlsFromGroupSelections, getSpaceRoomsFromGroupSelections, makeCommentFilter, @@ -267,9 +265,11 @@ const syncSpace = (url: string) => { {kinds: [RELAY_MEMBERS]}, {kinds: [ROOM_META, ROOM_DELETE]}, {kinds: [ROOM_ADMINS, ROOM_MEMBERS]}, + {kinds: [ROOM_ADD_MEMBER, ROOM_REMOVE_MEMBER]}, {kinds: [RELAY_ADD_MEMBER, RELAY_REMOVE_MEMBER]}, ...MESSAGE_KINDS.map(kind => ({kinds: [kind]})), makeCommentFilter(CONTENT_KINDS), + {kinds: REACTION_KINDS, limit: 0}, ], }) @@ -328,68 +328,6 @@ const syncSpaces = () => { } } -// Chat - -const syncRoom = (url: string, h: string) => { - const controller = new AbortController() - - pullAndListen({ - relays: [url], - signal: controller.signal, - filters: [ - {kinds: [ROOM_ADMINS, ROOM_MEMBERS], "#d": [h]}, - {kinds: [ROOM_ADD_MEMBER, ROOM_REMOVE_MEMBER], "#h": [h]}, - {kinds: [MESSAGE], "#h": [h]}, - ], - }) - - return () => controller.abort() -} - -const syncRooms = () => { - const unsubscribersByKey = new Map() - - const unsubscribeSpaceUrls = derived([userGroupSelections, relaysByUrl], identity).subscribe( - ([$l, $relaysByUrl]) => { - const keys = new Set() - const newUnsubscribersByKey = new Map() - - // Add new subscriptions, depending on whether nip 29 is supported - for (const url of getRelayTagValues(getListTags($l))) { - if (hasNip29($relaysByUrl.get(url))) { - for (const h of getSpaceRoomsFromGroupSelections(url, $l)) { - const key = `${url}'${h}` - - if (!unsubscribersByKey.has(key)) { - newUnsubscribersByKey.set(key, syncRoom(url, h)) - } - - keys.add(key) - } - } - } - - // Stop syncing removed selections - for (const [key, unsubscribe] of unsubscribersByKey.entries()) { - if (!keys.has(key)) { - unsubscribersByKey.delete(key) - unsubscribe() - } - } - - // Start syncing newly added spaces - for (const [key, unsubscriber] of newUnsubscribersByKey.entries()) { - unsubscribersByKey.set(key, unsubscriber) - } - }, - ) - - return () => { - Array.from(unsubscribersByKey.values()).forEach(call) - unsubscribeSpaceUrls() - } -} - // DMs const syncDMRelay = (url: string, pubkey: string) => { @@ -479,7 +417,7 @@ const syncDMs = () => { // Merge all synchronization functions export const syncApplicationData = () => { - const unsubscribers = [syncRelays(), syncUserData(), syncSpaces(), syncRooms(), syncDMs()] + const unsubscribers = [syncRelays(), syncUserData(), syncSpaces(), syncDMs()] return () => unsubscribers.forEach(call) }