Add inbox relays scenario

This commit is contained in:
Jon Staab
2024-10-07 10:39:18 -07:00
parent d747424c28
commit eb2b078a39
2 changed files with 29 additions and 12 deletions
+17 -3
View File
@@ -4,13 +4,13 @@ import {
} from '@welshman/lib'
import {
Tags, getFilterId, unionFilters, isShareableRelayUrl, isCommunityAddress, isGroupAddress, isContextAddress,
PROFILE, RELAYS, INBOX_RELAYS, FOLLOWS, LOCAL_RELAY_URL,
PROFILE, RELAYS, INBOX_RELAYS, FOLLOWS, LOCAL_RELAY_URL, WRAP,
} from '@welshman/util'
import type {TrustedEvent, Filter} from '@welshman/util'
import {ConnectionStatus} from '@welshman/net'
import type {RelaysAndFilters} from '@welshman/net'
import {pubkey} from './session'
import {relaySelectionsByPubkey, getReadRelayUrls, getWriteRelayUrls, getRelayUrls} from './relaySelections'
import {relaySelectionsByPubkey, inboxRelaySelectionsByPubkey, getReadRelayUrls, getWriteRelayUrls, getRelayUrls} from './relaySelections'
import {relays, relaysByUrl} from './relays'
export const INDEXED_KINDS = [PROFILE, RELAYS, INBOX_RELAYS, FOLLOWS]
@@ -185,6 +185,8 @@ export class Router {
WriteRelays = () => this.scenario(this.getUserSelections(RelayMode.Write))
InboxRelays = () => this.scenario(this.getUserSelections(RelayMode.Inbox)).policy(addNoFallbacks)
Messages = (pubkeys: string[]) =>
this.scenario([
...this.getUserSelections(),
@@ -426,7 +428,7 @@ export const getRelayQuality = (url: string) => {
export const getPubkeyRelays = (pubkey: string, mode?: string) => {
const $relaySelections = relaySelectionsByPubkey.get()
const $inboxSelections = relaySelectionsByPubkey.get()
const $inboxSelections = inboxRelaySelectionsByPubkey.get()
switch (mode) {
case RelayMode.Read: return getReadRelayUrls($relaySelections.get(pubkey))
@@ -518,6 +520,17 @@ export const getFilterSelectionsForContext = (state: FilterSelectionRuleState) =
return true
}
export const getFilterSelectionsForWraps = (state: FilterSelectionRuleState) => {
if (state.filter.kinds?.includes(WRAP) || !state.filter.authors) return false
const id = getFilterId({...state.filter, kinds: [WRAP]})
const scenario = ctx.app.router.InboxRelays().update(assoc('value', id))
state.selections.push(makeFilterSelection(id, state.filter, scenario))
return false
}
export const getFilterSelectionsForIndexedKinds = (state: FilterSelectionRuleState) => {
const kinds = intersection(INDEXED_KINDS, state.filter.kinds || [])
@@ -558,6 +571,7 @@ export const defaultFilterSelectionRules = [
getFilterSelectionsForLocalRelay,
getFilterSelectionsForSearch,
getFilterSelectionsForContext,
getFilterSelectionsForWraps,
getFilterSelectionsForIndexedKinds,
getFilterSelectionsForAuthors,
getFilterSelectionsForUser,
+12 -9
View File
@@ -18,36 +18,39 @@ export type AppSyncOpts = {
filters: Filter[]
}
export const pull = async ({relays, filters}: AppSyncOpts) =>
export const pull = async ({relays, filters}: AppSyncOpts) => {
const events = repository.query(filters)
await Promise.all(
relays.map(async relay => {
const events = repository.query(filters)
await hasNegentropy(relay)
? basePull({filters, events, relays: [relay]})
: pullWithoutNegentropy({filters, relays: [relay]})
})
)
}
export const push = async ({relays, filters}: AppSyncOpts) => {
const events = repository.query(filters).filter(isSignedEvent)
export const push = async ({relays, filters}: AppSyncOpts) =>
await Promise.all(
relays.map(async relay => {
const events = repository.query(filters).filter(isSignedEvent)
await hasNegentropy(relay)
? basePush({filters, events, relays: [relay]})
: pushWithoutNegentropy({events, relays: [relay]})
})
)
}
export const sync = async ({relays, filters}: AppSyncOpts) => {
const events = repository.query(filters).filter(isSignedEvent)
export const sync = async ({relays, filters}: AppSyncOpts) =>
await Promise.all(
relays.map(async relay => {
const events = repository.query(filters).filter(isSignedEvent)
await hasNegentropy(relay)
? baseSync({filters, events, relays: [relay]})
: syncWithoutNegentropy({filters, events, relays: [relay]})
})
)
}