From 8230d7a9f6b1b1263f3fc7af4653c1cf1836f09c Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Thu, 19 Sep 2024 09:13:58 -0700 Subject: [PATCH] Add a few utilities --- package-lock.json | 16 ++++++++-------- packages/app/package.json | 2 +- packages/dvm/package.json | 2 +- packages/feeds/package.json | 2 +- packages/net/package.json | 2 +- packages/signer/package.json | 2 +- packages/store/package.json | 2 +- packages/util/package.json | 2 +- packages/util/src/List.ts | 11 ++++++++--- packages/util/src/Repository.ts | 6 ++++++ packages/util/src/Tags.ts | 5 ++++- 11 files changed, 33 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9e9d784..ab17a44 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3287,14 +3287,14 @@ }, "packages/app": { "name": "@welshman/app", - "version": "0.0.6", + "version": "0.0.7", "license": "MIT", "dependencies": { "@welshman/lib": "0.0.17", "@welshman/net": "0.0.22", "@welshman/signer": "0.0.5", "@welshman/store": "0.0.7", - "@welshman/util": "0.0.31", + "@welshman/util": "0.0.32", "fuse.js": "^7.0.0", "idb": "^8.0.0", "svelte": "^4.2.18", @@ -3343,7 +3343,7 @@ "dependencies": { "@welshman/lib": "0.0.17", "@welshman/net": "0.0.22", - "@welshman/util": "0.0.31", + "@welshman/util": "0.0.32", "nostr-tools": "^2.7.2" }, "devDependencies": { @@ -3381,7 +3381,7 @@ "license": "MIT", "dependencies": { "@welshman/lib": "0.0.17", - "@welshman/util": "0.0.31" + "@welshman/util": "0.0.32" }, "devDependencies": { "gts": "^5.0.1", @@ -3419,7 +3419,7 @@ "license": "MIT", "dependencies": { "@welshman/lib": "0.0.17", - "@welshman/util": "0.0.31", + "@welshman/util": "0.0.32", "isomorphic-ws": "^5.0.0", "ws": "^8.16.0" }, @@ -3436,7 +3436,7 @@ "dependencies": { "@welshman/lib": "0.0.17", "@welshman/net": "0.0.22", - "@welshman/util": "0.0.31", + "@welshman/util": "0.0.32", "nostr-tools": "^2.7.2" }, "devDependencies": { @@ -3451,7 +3451,7 @@ "license": "MIT", "dependencies": { "@welshman/lib": "0.0.17", - "@welshman/util": "0.0.31", + "@welshman/util": "0.0.32", "svelte": "^4.2.18" }, "devDependencies": { @@ -3462,7 +3462,7 @@ }, "packages/util": { "name": "@welshman/util", - "version": "0.0.31", + "version": "0.0.32", "license": "MIT", "dependencies": { "@welshman/lib": "0.0.17", diff --git a/packages/app/package.json b/packages/app/package.json index 8088c92..0a6a9a5 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -35,7 +35,7 @@ "@welshman/net": "0.0.22", "@welshman/signer": "0.0.5", "@welshman/store": "0.0.7", - "@welshman/util": "0.0.31", + "@welshman/util": "0.0.32", "fuse.js": "^7.0.0", "idb": "^8.0.0", "svelte": "^4.2.18", diff --git a/packages/dvm/package.json b/packages/dvm/package.json index 93a8dc9..0116cc9 100644 --- a/packages/dvm/package.json +++ b/packages/dvm/package.json @@ -33,7 +33,7 @@ "dependencies": { "@welshman/lib": "0.0.17", "@welshman/net": "0.0.22", - "@welshman/util": "0.0.31", + "@welshman/util": "0.0.32", "nostr-tools": "^2.7.2" } } diff --git a/packages/feeds/package.json b/packages/feeds/package.json index 2dceddc..c606d16 100644 --- a/packages/feeds/package.json +++ b/packages/feeds/package.json @@ -32,6 +32,6 @@ }, "dependencies": { "@welshman/lib": "0.0.17", - "@welshman/util": "0.0.31" + "@welshman/util": "0.0.32" } } diff --git a/packages/net/package.json b/packages/net/package.json index 2f6b0b5..8a45ce3 100644 --- a/packages/net/package.json +++ b/packages/net/package.json @@ -32,7 +32,7 @@ }, "dependencies": { "@welshman/lib": "0.0.17", - "@welshman/util": "0.0.31", + "@welshman/util": "0.0.32", "isomorphic-ws": "^5.0.0", "ws": "^8.16.0" } diff --git a/packages/signer/package.json b/packages/signer/package.json index 4ba246d..920aa27 100644 --- a/packages/signer/package.json +++ b/packages/signer/package.json @@ -33,7 +33,7 @@ "dependencies": { "@welshman/lib": "0.0.17", "@welshman/net": "0.0.22", - "@welshman/util": "0.0.31", + "@welshman/util": "0.0.32", "nostr-tools": "^2.7.2" } } diff --git a/packages/store/package.json b/packages/store/package.json index 4d60632..b15fba9 100644 --- a/packages/store/package.json +++ b/packages/store/package.json @@ -32,7 +32,7 @@ }, "dependencies": { "@welshman/lib": "0.0.17", - "@welshman/util": "0.0.31", + "@welshman/util": "0.0.32", "svelte": "^4.2.18" } } diff --git a/packages/util/package.json b/packages/util/package.json index 662c815..99ac1d5 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -1,6 +1,6 @@ { "name": "@welshman/util", - "version": "0.0.31", + "version": "0.0.32", "author": "hodlbod", "license": "MIT", "description": "A collection of nostr-related utilities.", diff --git a/packages/util/src/List.ts b/packages/util/src/List.ts index a31b30b..476f3f4 100644 --- a/packages/util/src/List.ts +++ b/packages/util/src/List.ts @@ -1,4 +1,4 @@ -import {parseJson, nth, nthEq} from "@welshman/lib" +import {parseJson, ensurePlural, nth} from "@welshman/lib" import {Address} from "./Address" import {isShareableRelayUrl} from "./Relay" import {Encryptable, DecryptedEvent} from "./Encryptable" @@ -45,5 +45,10 @@ export const createList = ({kind, publicTags = [], privateTags = []}: List) => export const editList = ({kind, publicTags = [], privateTags = []}: PublishedList) => new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)}) -export const getListValues = (tagName: string, list: List | undefined) => - [...list?.publicTags || [], ...list?.privateTags || []].filter(nthEq(0, tagName)).map(nth(1)) +export const getListValues = (tagName: string | string[], list: List | undefined) => { + const tagNames = ensurePlural(tagName) + const allTags = [...list?.publicTags || [], ...list?.privateTags || []] + const filteredTags = allTags.filter(t => tagNames.includes(t[0])).map(nth(1)) + + return filteredTags +} diff --git a/packages/util/src/Repository.ts b/packages/util/src/Repository.ts index a6615e2..2570620 100644 --- a/packages/util/src/Repository.ts +++ b/packages/util/src/Repository.ts @@ -140,6 +140,12 @@ export class Repository extends Emitter { } publish = (event: E, {shouldNotify = true} = {}): boolean => { + if (!event?.id) { + console.warn("Attempted to publish invalid event to repository", event) + + return false + } + // If we've already seen this event, or it's been deleted, we're done if (this.eventsById.get(event.id) || this.isDeleted(event)) { return false diff --git a/packages/util/src/Tags.ts b/packages/util/src/Tags.ts index d90f46f..6fac671 100644 --- a/packages/util/src/Tags.ts +++ b/packages/util/src/Tags.ts @@ -1,5 +1,5 @@ import type {OmitStatics} from '@welshman/lib' -import {Fluent, nth, nthEq, ensurePlural} from '@welshman/lib' +import {Fluent, mapVals, nth, nthEq, ensurePlural} from '@welshman/lib' import {isRelayUrl, normalizeRelayUrl} from './Relay' import {Address, isContextAddress} from './Address' import {GROUP, COMMUNITY} from './Kinds' @@ -249,3 +249,6 @@ export const getAncestorTags = (tags: string[][]) => { return {roots, replies, mentions} } + +export const getAncestorTagValues = (tags: string[][]) => + mapVals(tags => tags.map(nth(1)), getAncestorTags(tags))