Add search relays

This commit is contained in:
Jon Staab
2026-03-11 11:36:05 -07:00
parent 54ac2274d3
commit 5dfcabce67
5 changed files with 81 additions and 1 deletions
+30
View File
@@ -28,6 +28,7 @@ import {
makeEvent, makeEvent,
MESSAGING_RELAYS, MESSAGING_RELAYS,
BLOCKED_RELAYS, BLOCKED_RELAYS,
SEARCH_RELAYS,
FOLLOWS, FOLLOWS,
RELAYS, RELAYS,
MUTES, MUTES,
@@ -43,6 +44,8 @@ import {
forceLoadUserMessagingRelayList, forceLoadUserMessagingRelayList,
userBlockedRelayList, userBlockedRelayList,
forceLoadUserBlockedRelayList, forceLoadUserBlockedRelayList,
userSearchRelayList,
forceLoadUserSearchRelayList,
userFollowList, userFollowList,
forceLoadUserFollowList, forceLoadUserFollowList,
userMuteList, userMuteList,
@@ -159,6 +162,33 @@ export const setBlockedRelays = async (urls: string[]) => {
return publishThunk({event, relays}) return publishThunk({event, relays})
} }
export const removeSearchRelay = async (url: string) => {
await forceLoadUserSearchRelayList([])
const list = get(userSearchRelayList) || makeList({kind: SEARCH_RELAYS})
const event = await removeFromList(list, url).reconcile(nip44EncryptToSelf)
const relays = Router.get().FromUser().policy(addMaximalFallbacks).getUrls()
return publishThunk({event, relays})
}
export const addSearchRelay = async (url: string) => {
await forceLoadUserSearchRelayList([])
const list = get(userSearchRelayList) || makeList({kind: SEARCH_RELAYS})
const event = await addToListPublicly(list, ["relay", url]).reconcile(nip44EncryptToSelf)
const relays = Router.get().FromUser().policy(addMaximalFallbacks).getUrls()
return publishThunk({event, relays})
}
export const setSearchRelays = async (urls: string[]) => {
const event = makeEvent(SEARCH_RELAYS, {tags: urls.map(url => ["relay", url])})
const relays = Router.get().FromUser().getUrls()
return publishThunk({event, relays})
}
// NIP 01 // NIP 01
export const setProfile = (profile: Profile) => { export const setProfile = (profile: Profile) => {
+4
View File
@@ -41,6 +41,7 @@ import {repository, tracker} from "./core.js"
import {getRelays, loadRelay} from "./relays.js" import {getRelays, loadRelay} from "./relays.js"
import {trackRelayStats, getRelayQuality} from "./relayStats.js" import {trackRelayStats, getRelayQuality} from "./relayStats.js"
import {deriveRelayList, getRelayList} from "./relayLists.js" import {deriveRelayList, getRelayList} from "./relayLists.js"
import {deriveSearchRelayList, getSearchRelayList} from "./searchRelayLists.js"
import {deriveBlockedRelayList, getBlockedRelayList} from "./blockedRelayLists.js" import {deriveBlockedRelayList, getBlockedRelayList} from "./blockedRelayLists.js"
import {deriveMessagingRelayList, getMessagingRelayList} from "./messagingRelayLists.js" import {deriveMessagingRelayList, getMessagingRelayList} from "./messagingRelayLists.js"
@@ -87,12 +88,15 @@ const _relayGetter = (fn?: (relay: RelayProfile) => any) =>
}) })
export const getPubkeyRelays = (pubkey: string, mode?: RelayMode) => { export const getPubkeyRelays = (pubkey: string, mode?: RelayMode) => {
if (mode === RelayMode.Search) return getRelaysFromList(getSearchRelayList(pubkey))
if (mode === RelayMode.Blocked) return getRelaysFromList(getBlockedRelayList(pubkey)) if (mode === RelayMode.Blocked) return getRelaysFromList(getBlockedRelayList(pubkey))
if (mode === RelayMode.Messaging) return getRelaysFromList(getMessagingRelayList(pubkey)) if (mode === RelayMode.Messaging) return getRelaysFromList(getMessagingRelayList(pubkey))
return getRelaysFromList(getRelayList(pubkey), mode) return getRelaysFromList(getRelayList(pubkey), mode)
} }
export const derivePubkeyRelays = (pubkey: string, mode?: RelayMode) => { export const derivePubkeyRelays = (pubkey: string, mode?: RelayMode) => {
if (mode === RelayMode.Search)
return derived(deriveSearchRelayList(pubkey), list => getRelaysFromList(list))
if (mode === RelayMode.Blocked) if (mode === RelayMode.Blocked)
return derived(deriveBlockedRelayList(pubkey), list => getRelaysFromList(list)) return derived(deriveBlockedRelayList(pubkey), list => getRelaysFromList(list))
if (mode === RelayMode.Messaging) if (mode === RelayMode.Messaging)
+36
View File
@@ -0,0 +1,36 @@
import {SEARCH_RELAYS, asDecryptedEvent, readList} from "@welshman/util"
import {TrustedEvent} from "@welshman/util"
import {
deriveItemsByKey,
deriveItems,
makeForceLoadItem,
makeLoadItem,
makeDeriveItem,
getter,
} from "@welshman/store"
import {repository} from "./core.js"
import {makeOutboxLoader} from "./relayLists.js"
export const searchRelayListsByPubkey = deriveItemsByKey({
repository,
eventToItem: (event: TrustedEvent) => readList(asDecryptedEvent(event)),
filters: [{kinds: [SEARCH_RELAYS]}],
getKey: searchRelayLists => searchRelayLists.event.pubkey,
})
export const searchRelayLists = deriveItems(searchRelayListsByPubkey)
export const getSearchRelayListsByPubkey = getter(searchRelayListsByPubkey)
export const getSearchRelayLists = getter(searchRelayLists)
export const getSearchRelayList = (pubkey: string) => getSearchRelayListsByPubkey().get(pubkey)
export const forceLoadSearchRelayList = makeForceLoadItem(
makeOutboxLoader(SEARCH_RELAYS),
getSearchRelayList,
)
export const loadSearchRelayList = makeLoadItem(makeOutboxLoader(SEARCH_RELAYS), getSearchRelayList)
export const deriveSearchRelayList = makeDeriveItem(searchRelayListsByPubkey, loadSearchRelayList)
+9
View File
@@ -21,6 +21,11 @@ import {
forceLoadBlockedRelayList, forceLoadBlockedRelayList,
loadBlockedRelayList, loadBlockedRelayList,
} from "./blockedRelayLists.js" } from "./blockedRelayLists.js"
import {
searchRelayListsByPubkey,
forceLoadSearchRelayList,
loadSearchRelayList,
} from "./searchRelayLists.js"
import {wotGraph, getWotGraph} from "./wot.js" import {wotGraph, getWotGraph} from "./wot.js"
export const makeUserData = <T>( export const makeUserData = <T>(
@@ -72,6 +77,10 @@ export const userMessagingRelayList = makeUserData(
export const forceLoadUserMessagingRelayList = makeUserLoader(forceLoadMessagingRelayList) export const forceLoadUserMessagingRelayList = makeUserLoader(forceLoadMessagingRelayList)
export const loadUserMessagingRelayList = makeUserLoader(loadMessagingRelayList) export const loadUserMessagingRelayList = makeUserLoader(loadMessagingRelayList)
export const userSearchRelayList = makeUserData(searchRelayListsByPubkey, loadSearchRelayList)
export const forceLoadUserSearchRelayList = makeUserLoader(forceLoadSearchRelayList)
export const loadUserSearchRelayList = makeUserLoader(loadSearchRelayList)
export const userBlockedRelayList = makeUserData(blockedRelayListsByPubkey, loadBlockedRelayList) export const userBlockedRelayList = makeUserData(blockedRelayListsByPubkey, loadBlockedRelayList)
export const forceLoadUserBlockedRelayList = makeUserLoader(forceLoadBlockedRelayList) export const forceLoadUserBlockedRelayList = makeUserLoader(forceLoadBlockedRelayList)
export const loadUserBlockedRelayList = makeUserLoader(loadBlockedRelayList) export const loadUserBlockedRelayList = makeUserLoader(loadBlockedRelayList)
+2 -1
View File
@@ -258,7 +258,8 @@ export class Repository extends Emitter {
} }
} }
if (shouldNotify) { // Notify, but only if the event hasn't been deleted
if (shouldNotify && !this.isDeleted(event)) {
this.emit("update", {added: [event], removed}) this.emit("update", {added: [event], removed})
} }