diff --git a/package.json b/package.json index 1bde426..6035bb8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "paravel", - "version": "0.5.0", + "version": "0.5.1", "description": "Yet another toolkit for nostr", "author": "hodlbod", "license": "MIT", diff --git a/src/util/Events.ts b/src/util/Events.ts index 7860097..fbfb780 100644 --- a/src/util/Events.ts +++ b/src/util/Events.ts @@ -2,6 +2,7 @@ import type {Event, EventTemplate, UnsignedEvent} from 'nostr-tools' import {verifyEvent, getEventHash} from 'nostr-tools' import {cached} from "./LRUCache" import {now} from './Tools' +import {Tags} from './Tags' import {addressFromEvent, encodeAddress} from './Address' 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 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)) +} + diff --git a/src/util/Router.ts b/src/util/Router.ts index 3a43281..78dceb6 100644 --- a/src/util/Router.ts +++ b/src/util/Router.ts @@ -11,8 +11,8 @@ const isGroupAddress = (a: string) => decodeAddress(a).kind === GROUP_DEFINITION const isCommunityAddress = (a: string) => decodeAddress(a).kind === COMMUNITY_DEFINITION export enum RelayMode { - Inbox = "inbox", - Outbox = "outbox", + Read = "read", + Write = "write", } export type RouterOptions = { @@ -87,9 +87,9 @@ export class Router { 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()]) @@ -101,19 +101,19 @@ export class Router { PublishMessage = (pubkey: string) => this.scenario([ - this.getUserRelays(RelayMode.Outbox), - this.options.getPubkeyRelays(pubkey, RelayMode.Inbox) + this.getUserRelays(RelayMode.Write), + this.options.getPubkeyRelays(pubkey, RelayMode.Read) ]).policy(this.addMinimalFallbacks) Event = (event: UnsignedEvent) => this.scenario([ - this.options.getPubkeyRelays(event.pubkey, RelayMode.Outbox), + this.options.getPubkeyRelays(event.pubkey, RelayMode.Write), ...this.getContextRelayGroups(event), ]) EventChildren = (event: UnsignedEvent) => this.scenario([ - this.options.getPubkeyRelays(event.pubkey, RelayMode.Inbox), + this.options.getPubkeyRelays(event.pubkey, RelayMode.Read), ...this.getContextRelayGroups(event), ]) @@ -125,9 +125,9 @@ export class Router { tags.roots().relays().valueOf(), ...this.getContextRelayGroups(event), ...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(), - 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(), ...this.getContextRelayGroups(event), ...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(), - this.options.getPubkeyRelays(event.pubkey, RelayMode.Inbox), + this.options.getPubkeyRelays(event.pubkey, RelayMode.Read), ]) } @@ -159,18 +159,18 @@ export class Router { } return this.scenario([ - this.options.getPubkeyRelays(event.pubkey, RelayMode.Outbox), + this.options.getPubkeyRelays(event.pubkey, RelayMode.Write), ...groupAddresses.map(this.options.getGroupRelays), ...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[]) => - 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[]) => - 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) => this.scenario([this.options.getGroupRelays(address)]).policy(this.addNoFallbacks)