Start re-working router

This commit is contained in:
Jon Staab
2024-11-02 10:14:06 -07:00
parent 0c4fbf70ad
commit 75aec594e2
5 changed files with 75 additions and 98 deletions
+4 -4
View File
@@ -9,27 +9,27 @@ export const unfollow = async (value: string) => {
const list = get(userFollows) || makeList({kind: FOLLOWS}) const list = get(userFollows) || makeList({kind: FOLLOWS})
const event = await removeFromList(list, value).reconcile(nip44EncryptToSelf) const event = await removeFromList(list, value).reconcile(nip44EncryptToSelf)
return publishThunk({event, relays: ctx.app.router.WriteRelays().getUrls()}) return publishThunk({event, relays: ctx.app.router.FromUser().getUrls()})
} }
export const follow = async (tag: string[]) => { export const follow = async (tag: string[]) => {
const list = get(userFollows) || makeList({kind: FOLLOWS}) const list = get(userFollows) || makeList({kind: FOLLOWS})
const event = await addToListPublicly(list, tag).reconcile(nip44EncryptToSelf) const event = await addToListPublicly(list, tag).reconcile(nip44EncryptToSelf)
return publishThunk({event, relays: ctx.app.router.WriteRelays().getUrls()}) return publishThunk({event, relays: ctx.app.router.FromUser().getUrls()})
} }
export const unmute = async (value: string) => { export const unmute = async (value: string) => {
const list = get(userMutes) || makeList({kind: MUTES}) const list = get(userMutes) || makeList({kind: MUTES})
const event = await removeFromList(list, value).reconcile(nip44EncryptToSelf) const event = await removeFromList(list, value).reconcile(nip44EncryptToSelf)
return publishThunk({event, relays: ctx.app.router.WriteRelays().getUrls()}) return publishThunk({event, relays: ctx.app.router.FromUser().getUrls()})
} }
export const mute = async (tag: string[]) => { export const mute = async (tag: string[]) => {
const list = get(userMutes) || makeList({kind: MUTES}) const list = get(userMutes) || makeList({kind: MUTES})
const event = await addToListPublicly(list, tag).reconcile(nip44EncryptToSelf) const event = await addToListPublicly(list, tag).reconcile(nip44EncryptToSelf)
return publishThunk({event, relays: ctx.app.router.WriteRelays().getUrls()}) return publishThunk({event, relays: ctx.app.router.FromUser().getUrls()})
} }
+1 -1
View File
@@ -25,7 +25,7 @@ export const feedLoader = new FeedLoader({
const event = await $signer.sign(createEvent(kind, {tags})) const event = await $signer.sign(createEvent(kind, {tags}))
const relays = const relays =
request.relays request.relays
? ctx.app.router.fromRelays(request.relays).getUrls() ? ctx.app.router.FromRelays(request.relays).getUrls()
: ctx.app.router.FromPubkeys(getPubkeyTagValues(tags)).getUrls() : ctx.app.router.FromPubkeys(getPubkeyTagValues(tags)).getUrls()
const req = makeDvmRequest({event, relays}) const req = makeDvmRequest({event, relays})
+68 -90
View File
@@ -114,6 +114,7 @@ export type RelayValues = {
export type ValueRelays = { export type ValueRelays = {
value: string value: string
relays: string[] relays: string[]
weight: number
} }
// Fallback policies // Fallback policies
@@ -131,24 +132,22 @@ export class Router {
// Utilities derived from options // Utilities derived from options
getPubkeySelection = (pubkey: string, mode?: RelayMode) => getRelaysForPubkey = (pubkey: string, mode?: RelayMode) =>
this.selection(pubkey, this.options.getPubkeyRelays?.(pubkey, mode) || []) this.options.getPubkeyRelays?.(pubkey, mode) || []
getPubkeySelections = (pubkeys: string[], mode?: RelayMode) => getRelaysForPubkeys = (pubkeys: string[], mode?: RelayMode) =>
pubkeys.map(pubkey => this.getPubkeySelection(pubkey, mode)) pubkeys.map(pubkey => this.getRelaysForPubkey(pubkey, mode))
getUserSelections = (mode?: RelayMode) => getRelaysForUser = (mode?: RelayMode) => {
this.getPubkeySelections([this.options.getUserPubkey?.()].filter(identity) as string[], mode) const pubkey = this.options.getUserPubkey?.()
return pubkey ? this.getRelaysForPubkey(pubkey) : []
}
// Utilities for creating ValueRelays // Utilities for creating ValueRelays
selection = (value: string, relays: Iterable<string>) => ({value, relays: Array.from(relays)}) selection = (value: string, relays: Iterable<string>, weight = 1): ValueRelays =>
({value, relays: Array.from(relays), weight})
selections = (values: string[], relays: string[]) =>
values.map(value => this.selection(value, relays))
forceValue = (value: string, selections: ValueRelays[]) =>
selections.map(({relays}) => this.selection(value, relays))
// Utilities for processing hints // Utilities for processing hints
@@ -161,8 +160,8 @@ export class Router {
})) }))
) )
scoreRelaySelection = ({values, relay}: RelayValues) => scoreRelaySelection = ({values, relay, weight}: RelayValues) =>
values.length * (this.options.getRelayQuality?.(relay) || 1) values.length * (this.options.getRelayQuality?.(relay) || 1) * weight
sortRelaySelections = (relaySelections: RelayValues[]) => { sortRelaySelections = (relaySelections: RelayValues[]) => {
const scores = new Map<string, number>() const scores = new Map<string, number>()
@@ -182,53 +181,63 @@ export class Router {
merge = (scenarios: RouterScenario[]) => merge = (scenarios: RouterScenario[]) =>
this.scenario(scenarios.flatMap((scenario: RouterScenario) => scenario.selections)) this.scenario(scenarios.flatMap((scenario: RouterScenario) => scenario.selections))
product = (values: string[], relays: string[]) => this.scenario(this.selections(values, relays))
fromRelays = (relays: string[]) => this.scenario([this.selection("", relays)])
// Routing scenarios // Routing scenarios
User = () => this.scenario(this.getUserSelections())
ReadRelays = () => this.scenario(this.getUserSelections(RelayMode.Read)) FromRelays = (relays: string[], id = "") =>
this.scenario([this.selection(id, relays)])
WriteRelays = () => this.scenario(this.getUserSelections(RelayMode.Write)) ForPubkey = (pubkey: string) =>
this.FromRelays(this.getRelaysForPubkey(pubkey, RelayMode.Read))
InboxRelays = () => this.scenario(this.getUserSelections(RelayMode.Inbox)).policy(addNoFallbacks) FromPubkey = (pubkey: string) =>
this.FromRelays(this.getRelaysForPubkey(pubkey, RelayMode.Write))
Messages = (pubkeys: string[]) => PubkeyInbox = (pubkey: string) =>
this.scenario([ this.FromRelays(this.getRelaysForPubkey(pubkey, RelayMode.Inbox)).policy(addNoFallbacks)
...this.getUserSelections(RelayMode.Inbox),
...this.getPubkeySelections(pubkeys, RelayMode.Inbox),
])
PublishMessage = (pubkey: string) => ForUser = () =>
this.scenario([ this.FromRelays(this.getRelaysForUser(RelayMode.Read))
...this.getUserSelections(RelayMode.Inbox),
this.getPubkeySelection(pubkey, RelayMode.Inbox), FromUser = () =>
]).policy(addMinimalFallbacks) this.FromRelays(this.getRelaysForUser(RelayMode.Write))
UserInbox = () =>
this.FromRelays(this.getRelaysForUser(RelayMode.Inbox)).policy(addNoFallbacks)
ForPubkeys = (pubkeys: string[]) =>
this.merge(pubkeys.map(pubkey => this.ForPubkey(pubkey)))
FromPubkeys = (pubkeys: string[]) =>
this.merge(pubkeys.map(pubkey => this.FromPubkey(pubkey)))
PubkeyInboxes = (pubkeys: string[]) =>
this.merge(pubkeys.map(pubkey => this.PubkeyInbox(pubkey)))
Event = (event: TrustedEvent) => Event = (event: TrustedEvent) =>
this.scenario( this.FromRelays(this.getRelaysForPubkey(event.pubkey, RelayMode.Write), event.id)
this.forceValue(event.id, [this.getPubkeySelection(event.pubkey, RelayMode.Write)])
)
EventChildren = (event: TrustedEvent) => EventChildren = (event: TrustedEvent) =>
this.scenario( this.FromRelays(this.getRelaysForPubkey(event.pubkey, RelayMode.Read), event.id)
this.forceValue(event.id, [this.getPubkeySelection(event.pubkey, RelayMode.Read)])
)
EventAncestors = (event: TrustedEvent, type: "mentions" | "replies" | "roots") => { EventAncestors = (event: TrustedEvent, type: "mentions" | "replies" | "roots") => {
const tags = Tags.fromEvent(event) return this.scenario(
const ancestors = tags.ancestors()[type] getAncestorTags(event.tags)[type].flatMap(
const pubkeys = tags.values("p").valueOf() ([_, value, relay, pubkey]) => {
const relays = uniq([ const tagScenarios = [this.selection(value, this.ForUser().getUrls(), 0.5)]
...(this.options.getPubkeyRelays?.(event.pubkey, RelayMode.Read) || []),
...pubkeys.flatMap((k: string) => this.options.getPubkeyRelays?.(k, RelayMode.Write) || []),
...ancestors.relays().valueOf(),
])
return this.product(ancestors.values().valueOf(), relays) if (pubkey) {
tagScenarios.push(this.selection(value, this.FromPubkey(pubkey).getUrls()))
}
if (relay) {
tagScenarios.push(this.selection(value, [relay], 0.9))
}
return tagScenarios
}
)
)
} }
EventMentions = (event: TrustedEvent) => this.EventAncestors(event, "mentions") EventMentions = (event: TrustedEvent) => this.EventAncestors(event, "mentions")
@@ -238,25 +247,13 @@ export class Router {
EventRoots = (event: TrustedEvent) => this.EventAncestors(event, "roots") EventRoots = (event: TrustedEvent) => this.EventAncestors(event, "roots")
PublishEvent = (event: TrustedEvent) => { PublishEvent = (event: TrustedEvent) => {
const tags = Tags.fromEvent(event) const pubkeys = getPubkeyTagValues(event.tags)
const mentions = tags.values("p").valueOf()
return this.scenario( return this.scenario([
this.forceValue(event.id, [ this.selection(event.id, this.FromPubkey(event.pubkey).getUrls()),
this.getPubkeySelection(event.pubkey, RelayMode.Write), ...pubkeys.map(pubkey => this.selection(event.id, this.ForPubkey(event.pubkey).getUrls(), 0.5)),
...this.getPubkeySelections(mentions, RelayMode.Read), ])
])
)
} }
FromPubkeys = (pubkeys: string[]) =>
this.scenario(this.getPubkeySelections(pubkeys, RelayMode.Write))
ForPubkeys = (pubkeys: string[]) =>
this.scenario(this.getPubkeySelections(pubkeys, RelayMode.Read))
Search = (term: string, relays: string[] = []) =>
this.product([term], uniq(relays.concat(this.options.getSearchRelays?.() || [])))
} }
// Router Scenario // Router Scenario
@@ -490,7 +487,7 @@ export const makeFilterSelection = (id: string, filter: Filter, scenario: Router
export const getFilterSelectionsForLocalRelay = (state: FilterSelectionRuleState) => { export const getFilterSelectionsForLocalRelay = (state: FilterSelectionRuleState) => {
const id = getFilterId(state.filter) const id = getFilterId(state.filter)
const scenario = ctx.app.router.product([id], [LOCAL_RELAY_URL]) const scenario = ctx.app.router.FromRelays([LOCAL_RELAY_URL], id)
state.selections.push(makeFilterSelection(id, state.filter, scenario)) state.selections.push(makeFilterSelection(id, state.filter, scenario))
@@ -502,36 +499,18 @@ export const getFilterSelectionsForSearch = (state: FilterSelectionRuleState) =>
const id = getFilterId(state.filter) const id = getFilterId(state.filter)
const relays = ctx.app.router.options.getSearchRelays?.() || [] const relays = ctx.app.router.options.getSearchRelays?.() || []
const scenario = ctx.app.router.product([id], relays) const scenario = ctx.app.router.FromRelays(relays, id)
state.selections.push(makeFilterSelection(id, state.filter, scenario)) state.selections.push(makeFilterSelection(id, state.filter, scenario))
return true return true
} }
export const getFilterSelectionsForContext = (state: FilterSelectionRuleState) => {
const contexts = state.filter["#a"]?.filter(isContextAddress) || []
if (contexts.length === 0) return false
const scenario = ctx.app.router.WithinMultipleContexts(contexts)
for (const {relay, values} of scenario.getSelections()) {
const contextFilter = {...state.filter, "#a": Array.from(values)}
const id = getFilterId(contextFilter)
const scenario = ctx.app.router.product([id], [relay])
state.selections.push(makeFilterSelection(id, contextFilter, scenario))
}
return true
}
export const getFilterSelectionsForWraps = (state: FilterSelectionRuleState) => { export const getFilterSelectionsForWraps = (state: FilterSelectionRuleState) => {
if (!state.filter.kinds?.includes(WRAP) || state.filter.authors) return false if (!state.filter.kinds?.includes(WRAP) || state.filter.authors) return false
const id = getFilterId({...state.filter, kinds: [WRAP]}) const id = getFilterId({...state.filter, kinds: [WRAP]})
const scenario = ctx.app.router.InboxRelays().update(assoc("value", id)) const scenario = ctx.app.router.UserInbox().update(assoc('value', id))
state.selections.push(makeFilterSelection(id, state.filter, scenario)) state.selections.push(makeFilterSelection(id, state.filter, scenario))
@@ -545,7 +524,7 @@ export const getFilterSelectionsForIndexedKinds = (state: FilterSelectionRuleSta
const id = getFilterId({...state.filter, kinds}) const id = getFilterId({...state.filter, kinds})
const relays = ctx.app.router.options.getIndexerRelays?.() || [] const relays = ctx.app.router.options.getIndexerRelays?.() || []
const scenario = ctx.app.router.product([id], relays) const scenario = ctx.app.router.FromRelays(relays, id)
state.selections.push(makeFilterSelection(id, state.filter, scenario)) state.selections.push(makeFilterSelection(id, state.filter, scenario))
@@ -567,8 +546,8 @@ export const getFilterSelectionsForAuthors = (state: FilterSelectionRuleState) =
export const getFilterSelectionsForUser = (state: FilterSelectionRuleState) => { export const getFilterSelectionsForUser = (state: FilterSelectionRuleState) => {
const id = getFilterId(state.filter) const id = getFilterId(state.filter)
const relays = ctx.app.router.ReadRelays().getUrls() const relays = ctx.app.router.ForUser().getUrls()
const scenario = ctx.app.router.product([id], relays) const scenario = ctx.app.router.FromRelays(relays, id)
state.selections.push(makeFilterSelection(id, state.filter, scenario)) state.selections.push(makeFilterSelection(id, state.filter, scenario))
@@ -578,7 +557,6 @@ export const getFilterSelectionsForUser = (state: FilterSelectionRuleState) => {
export const defaultFilterSelectionRules = [ export const defaultFilterSelectionRules = [
getFilterSelectionsForLocalRelay, getFilterSelectionsForLocalRelay,
getFilterSelectionsForSearch, getFilterSelectionsForSearch,
getFilterSelectionsForContext,
getFilterSelectionsForWraps, getFilterSelectionsForWraps,
getFilterSelectionsForIndexedKinds, getFilterSelectionsForIndexedKinds,
getFilterSelectionsForAuthors, getFilterSelectionsForAuthors,
+2 -2
View File
@@ -7,14 +7,14 @@ import {pubkey} from './session'
export const tagZapSplit = (pubkey: string, split = 1) => [ export const tagZapSplit = (pubkey: string, split = 1) => [
"zap", "zap",
pubkey, pubkey,
ctx.app.router.FromPubkeys([pubkey]).getUrl(), ctx.app.router.FromPubkey(pubkey).getUrl(),
String(split), String(split),
] ]
export const tagPubkey = (pubkey: string, ...args: unknown[]) => [ export const tagPubkey = (pubkey: string, ...args: unknown[]) => [
"p", "p",
pubkey, pubkey,
ctx.app.router.FromPubkeys([pubkey]).getUrl(), ctx.app.router.FromPubkey(pubkey).getUrl(),
displayProfileByPubkey(pubkey), displayProfileByPubkey(pubkey),
] ]
-1
View File
@@ -1,6 +1,5 @@
{ {
"targets": [ "targets": [
{"extname": ".cjs", "module": "commonjs"},
{"extname": ".mjs", "module": "esnext", "moduleResolution": "node"} {"extname": ".mjs", "module": "esnext", "moduleResolution": "node"}
], ],
"projects": ["tsconfig.json"] "projects": ["tsconfig.json"]