Add isChildOf

This commit is contained in:
Jon Staab
2024-03-06 12:46:35 -08:00
parent 5f5e46f603
commit 446e2bc52e
3 changed files with 25 additions and 17 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "paravel", "name": "paravel",
"version": "0.5.0", "version": "0.5.1",
"description": "Yet another toolkit for nostr", "description": "Yet another toolkit for nostr",
"author": "hodlbod", "author": "hodlbod",
"license": "MIT", "license": "MIT",
+8
View File
@@ -2,6 +2,7 @@ import type {Event, EventTemplate, UnsignedEvent} from 'nostr-tools'
import {verifyEvent, getEventHash} from 'nostr-tools' import {verifyEvent, getEventHash} from 'nostr-tools'
import {cached} from "./LRUCache" import {cached} from "./LRUCache"
import {now} from './Tools' import {now} from './Tools'
import {Tags} from './Tags'
import {addressFromEvent, encodeAddress} from './Address' import {addressFromEvent, encodeAddress} from './Address'
import {isEphemeralKind, isReplaceableKind, isPlainReplaceableKind, isParameterizedReplaceableKind} from './Kinds' import {isEphemeralKind, isReplaceableKind, isPlainReplaceableKind, isParameterizedReplaceableKind} from './Kinds'
@@ -60,3 +61,10 @@ export const isPlainReplaceable = (e: EventTemplate) => isPlainReplaceableKind(e
export const isParameterizedReplaceable = (e: EventTemplate) => isParameterizedReplaceableKind(e.kind) export const isParameterizedReplaceable = (e: EventTemplate) => isParameterizedReplaceableKind(e.kind)
export const isChildOf = (child: EventTemplate, parent: Rumor) => {
const {roots, replies} = Tags.fromEvent(child).ancestors()
const parentIds = (replies.exists() ? replies : roots).values().valueOf()
return getIdAndAddress(parent).some(x => parentIds.includes(x))
}
+16 -16
View File
@@ -11,8 +11,8 @@ const isGroupAddress = (a: string) => decodeAddress(a).kind === GROUP_DEFINITION
const isCommunityAddress = (a: string) => decodeAddress(a).kind === COMMUNITY_DEFINITION const isCommunityAddress = (a: string) => decodeAddress(a).kind === COMMUNITY_DEFINITION
export enum RelayMode { export enum RelayMode {
Inbox = "inbox", Read = "read",
Outbox = "outbox", Write = "write",
} }
export type RouterOptions = { export type RouterOptions = {
@@ -87,9 +87,9 @@ export class Router {
User = () => this.scenario([]) User = () => this.scenario([])
Inbox = () => this.scenario([]).mode(RelayMode.Inbox) ReadRelays = () => this.scenario([]).mode(RelayMode.Read)
Outbox = () => this.scenario([]).mode(RelayMode.Outbox) WriteRelays = () => this.scenario([]).mode(RelayMode.Write)
AllMessages = () => this.scenario([this.getUserRelays()]) AllMessages = () => this.scenario([this.getUserRelays()])
@@ -101,19 +101,19 @@ export class Router {
PublishMessage = (pubkey: string) => PublishMessage = (pubkey: string) =>
this.scenario([ this.scenario([
this.getUserRelays(RelayMode.Outbox), this.getUserRelays(RelayMode.Write),
this.options.getPubkeyRelays(pubkey, RelayMode.Inbox) this.options.getPubkeyRelays(pubkey, RelayMode.Read)
]).policy(this.addMinimalFallbacks) ]).policy(this.addMinimalFallbacks)
Event = (event: UnsignedEvent) => Event = (event: UnsignedEvent) =>
this.scenario([ this.scenario([
this.options.getPubkeyRelays(event.pubkey, RelayMode.Outbox), this.options.getPubkeyRelays(event.pubkey, RelayMode.Write),
...this.getContextRelayGroups(event), ...this.getContextRelayGroups(event),
]) ])
EventChildren = (event: UnsignedEvent) => EventChildren = (event: UnsignedEvent) =>
this.scenario([ this.scenario([
this.options.getPubkeyRelays(event.pubkey, RelayMode.Inbox), this.options.getPubkeyRelays(event.pubkey, RelayMode.Read),
...this.getContextRelayGroups(event), ...this.getContextRelayGroups(event),
]) ])
@@ -125,9 +125,9 @@ export class Router {
tags.roots().relays().valueOf(), tags.roots().relays().valueOf(),
...this.getContextRelayGroups(event), ...this.getContextRelayGroups(event),
...tags.whereKey("p").values().valueOf() ...tags.whereKey("p").values().valueOf()
.map(pk => this.options.getPubkeyRelays(pk, RelayMode.Outbox)), .map(pk => this.options.getPubkeyRelays(pk, RelayMode.Write)),
tags.whereKey("p").relays().valueOf(), tags.whereKey("p").relays().valueOf(),
this.options.getPubkeyRelays(event.pubkey, RelayMode.Inbox), this.options.getPubkeyRelays(event.pubkey, RelayMode.Read),
]) ])
} }
@@ -139,9 +139,9 @@ export class Router {
tags.replies().relays().valueOf(), tags.replies().relays().valueOf(),
...this.getContextRelayGroups(event), ...this.getContextRelayGroups(event),
...tags.whereKey("p").values().valueOf() ...tags.whereKey("p").values().valueOf()
.map(pk => this.options.getPubkeyRelays(pk, RelayMode.Outbox)), .map(pk => this.options.getPubkeyRelays(pk, RelayMode.Write)),
tags.whereKey("p").relays().valueOf(), tags.whereKey("p").relays().valueOf(),
this.options.getPubkeyRelays(event.pubkey, RelayMode.Inbox), this.options.getPubkeyRelays(event.pubkey, RelayMode.Read),
]) ])
} }
@@ -159,18 +159,18 @@ export class Router {
} }
return this.scenario([ return this.scenario([
this.options.getPubkeyRelays(event.pubkey, RelayMode.Outbox), this.options.getPubkeyRelays(event.pubkey, RelayMode.Write),
...groupAddresses.map(this.options.getGroupRelays), ...groupAddresses.map(this.options.getGroupRelays),
...communityAddresses.map(this.options.getCommunityRelays), ...communityAddresses.map(this.options.getCommunityRelays),
...mentions.map((pk: string) => this.options.getPubkeyRelays(pk, RelayMode.Inbox)), ...mentions.map((pk: string) => this.options.getPubkeyRelays(pk, RelayMode.Read)),
]) ])
} }
FromPubkeys = (pubkeys: string[]) => FromPubkeys = (pubkeys: string[]) =>
this.scenario(pubkeys.map(pk => this.options.getPubkeyRelays(pk, RelayMode.Outbox))) this.scenario(pubkeys.map(pk => this.options.getPubkeyRelays(pk, RelayMode.Write)))
ForPubkeys = (pubkeys: string[]) => ForPubkeys = (pubkeys: string[]) =>
this.scenario(pubkeys.map(pk => this.options.getPubkeyRelays(pk, RelayMode.Inbox))) this.scenario(pubkeys.map(pk => this.options.getPubkeyRelays(pk, RelayMode.Read)))
WithinGroup = (address: string) => WithinGroup = (address: string) =>
this.scenario([this.options.getGroupRelays(address)]).policy(this.addNoFallbacks) this.scenario([this.options.getGroupRelays(address)]).policy(this.addNoFallbacks)