commit e86b22e408fd76ac5a976600af98784ef1130e05 parent 7637e9e8e7083972181db54a67c7432d9fec7a57 Author: triesap <137732411+triesap@users.noreply.github.com> Date: Wed, 13 Aug 2025 18:31:23 -0700 utils-nostr: edit event layers to adopt updated bindings, add explicit event kind constants, edit event subscriber payload variants, edit tags utils Diffstat:
19 files changed, 165 insertions(+), 282 deletions(-)
diff --git a/utils-nostr/src/events/comment/lib.ts b/utils-nostr/src/events/comment/lib.ts @@ -1,10 +1,13 @@ import { NDKEvent, NDKKind } from "@nostr-dev-kit/ndk"; -import { NostrEventComment } from "../../types/lib.js"; +import { type RadrootsComment, } from "@radroots/radroots-common-bindings"; import { NDKEventFigure } from "../../types/ndk.js"; import { tags_comment } from "../../utils/tags.js"; import { ndk_event } from "../lib.js"; -export const ndk_event_comment = async (opts: NDKEventFigure<{ data: NostrEventComment; }>): Promise<NDKEvent | undefined> => { +export const KIND_RADROOTS_COMMENT = 1111; +export type KindRadrootsComment = typeof KIND_RADROOTS_COMMENT; + +export const ndk_event_comment = async (opts: NDKEventFigure<{ data: RadrootsComment; }>): Promise<NDKEvent | undefined> => { const { ndk, ndk_user, data } = opts; return await ndk_event({ ndk, diff --git a/utils-nostr/src/events/comment/parse.ts b/utils-nostr/src/events/comment/parse.ts @@ -1,15 +1,20 @@ import { NDKEvent } from "@nostr-dev-kit/ndk"; -import { nostr_event_comment_schema } from "../../schemas/lib.js"; -import { NostrEventComment } from "../../types/lib.js"; +import { type RadrootsComment, radroots_comment_schema } from "@radroots/radroots-common-bindings"; +import { parse_nostr_event_basis } from "../lib.js"; +import { NdkEventBasis } from "../subscription.js"; +import { KIND_RADROOTS_COMMENT, type KindRadrootsComment } from "./lib.js"; -export const parse_nostr_comment_event = (event: NDKEvent): NostrEventComment | undefined => { - if (!event || typeof event.content !== 'string' || event.kind !== 1111) return undefined; +export type RadrootsCommentNostrEvent = NdkEventBasis<KindRadrootsComment> & { comment: RadrootsComment; } +export const parse_nostr_comment_event = (event: NDKEvent): RadrootsCommentNostrEvent | undefined => { + const ev = parse_nostr_event_basis(event, KIND_RADROOTS_COMMENT); + if (!ev) return undefined; try { const parsed = JSON.parse(event.content); - const result = nostr_event_comment_schema.parse(parsed); - return result; + const comment = radroots_comment_schema.parse(parsed); + return { ...ev, comment }; } catch { return undefined; } }; + diff --git a/utils-nostr/src/events/follow/lib.ts b/utils-nostr/src/events/follow/lib.ts @@ -1,10 +1,13 @@ import { NDKEvent } from "@nostr-dev-kit/ndk"; -import { NostrEventFollow } from "../../types/lib.js"; +import { type RadrootsFollow } from "@radroots/radroots-common-bindings"; import { NDKEventFigure } from "../../types/ndk.js"; import { tags_follow_list } from "../../utils/tags.js"; import { ndk_event } from "../lib.js"; -export const ndk_event_follows = async (opts: NDKEventFigure<{ data: NostrEventFollow; }>): Promise<NDKEvent | undefined> => { +export const KIND_RADROOTS_FOLLOW = 3; +export type KindRadrootsFollow = typeof KIND_RADROOTS_FOLLOW; + +export const ndk_event_follows = async (opts: NDKEventFigure<{ data: RadrootsFollow; }>): Promise<NDKEvent | undefined> => { const { ndk, ndk_user, data } = opts; return await ndk_event({ ndk, diff --git a/utils-nostr/src/events/follow/parse.ts b/utils-nostr/src/events/follow/parse.ts @@ -1,14 +1,18 @@ import { NDKEvent } from "@nostr-dev-kit/ndk"; -import { nostr_event_follow_schema } from "../../schemas/lib.js"; -import { NostrEventFollow } from "../../types/lib.js"; +import { type RadrootsFollow, radroots_follow_schema } from "@radroots/radroots-common-bindings"; +import { parse_nostr_event_basis } from "../lib.js"; +import { NdkEventBasis } from "../subscription.js"; +import { KIND_RADROOTS_FOLLOW, type KindRadrootsFollow } from "./lib.js"; -export const parse_nostr_follow_event = (event: NDKEvent): NostrEventFollow | undefined => { - if (!event || typeof event.content !== 'string' || event.kind !== 3) return undefined; +export type RadrootsFollowNostrEvent = NdkEventBasis<KindRadrootsFollow> & { follow: RadrootsFollow; } +export const parse_nostr_follow_event = (event: NDKEvent): RadrootsFollowNostrEvent | undefined => { + const ev = parse_nostr_event_basis(event, KIND_RADROOTS_FOLLOW); + if (!ev) return undefined; try { const parsed = JSON.parse(event.content); - const result = nostr_event_follow_schema.parse(parsed); - return result; + const follow = radroots_follow_schema.parse(parsed); + return { ...ev, follow }; } catch { return undefined; } diff --git a/utils-nostr/src/events/lib.ts b/utils-nostr/src/events/lib.ts @@ -1,14 +1,21 @@ import { schnorr } from "@noble/curves/secp256k1"; import { hexToBytes } from "@noble/hashes/utils"; import { NDKEvent, NDKTag } from "@nostr-dev-kit/ndk"; -import { time_now_ms, uuidv4 } from "@radroots/utils"; +import { time_now_ms, time_now_s, uuidv4 } from "@radroots/utils"; import { finalizeEvent, getEventHash, nip19, type NostrEvent as NostrToolsEvent } from "nostr-tools"; import { ILibNostrEventSign, ILibNostrNeventEncode, NostrEventTags } from "../types/lib.js"; import { NDKEventFigure } from "../types/ndk.js"; +import { tag_client } from "../utils/tags.js"; +import { NdkEventBasis } from "./subscription.js"; export const get_event_tag = (tags: NDKTag[], key: string): string => tags.find(t => t[0] === key)?.[1] ?? ''; export const get_event_tags = (tags: NDKTag[], key: string): NDKTag[] => tags.filter(t => t[0] === key); +export const parse_nostr_event_basis = <T extends number>(event: NDKEvent, kind: T): NdkEventBasis<T> | undefined => { + if (!event || typeof event.created_at !== 'number' || event.kind !== kind) return undefined; + return { id: event.id, published_at: event.created_at, author: event.pubkey, kind: event.kind as T }; +}; + export const lib_nostr_event_verify = (event: NostrToolsEvent): boolean => { const hash = getEventHash(event); if (hash !== event.id) return false @@ -25,7 +32,7 @@ export const lib_nostr_event_sign_attest = (secret_key: string): NostrToolsEvent secret_key, event: { kind: 1, - created_at: Math.floor(Date.now() / 1000), + created_at: time_now_s(), tags: [], content: uuidv4(), }, @@ -66,6 +73,7 @@ export const ndk_event = async (opts: NDKEventFigure<{ ['published_at', published_at], ]; if (basis.tags?.length) tags.push(...basis.tags); + if (opts.client) tags.push(tag_client(opts.client, tags.find(i => i[0] === `d_tag`)?.[1] || undefined)); const ev = new NDKEvent(ndk, { kind: basis.kind, pubkey: ndk_user.pubkey, diff --git a/utils-nostr/src/events/listing/lib.ts b/utils-nostr/src/events/listing/lib.ts @@ -1,10 +1,13 @@ import { NDKEvent, NDKKind } from "@nostr-dev-kit/ndk"; -import { NostrEventListing } from "../../types/lib.js"; +import { type RadrootsListing } from "@radroots/radroots-common-bindings"; import { NDKEventFigure } from "../../types/ndk.js"; import { tags_classified } from "../../utils/tags.js"; import { ndk_event } from "../lib.js"; -export const ndk_event_classified = async (opts: NDKEventFigure<{ data: NostrEventListing; }>): Promise<NDKEvent | undefined> => { +export const KIND_RADROOTS_LISTING = 30402; +export type KindRadrootsListing = typeof KIND_RADROOTS_LISTING; + +export const ndk_event_classified = async (opts: NDKEventFigure<{ data: RadrootsListing; }>): Promise<NDKEvent | undefined> => { const { ndk, ndk_user, data } = opts; return await ndk_event({ ndk, diff --git a/utils-nostr/src/events/listing/parse.ts b/utils-nostr/src/events/listing/parse.ts @@ -1,17 +1,20 @@ import { NDKEvent } from "@nostr-dev-kit/ndk"; -import { nostr_event_listing_schema, nostr_tag_listing_schema, nostr_tag_location_schema, nostr_tag_price_schema, nostr_tag_quantity_schema } from "../../schemas/lib.js"; -import { NostrEventListing } from "../../types/lib.js"; -import { get_event_tag, get_event_tags } from "../lib.js"; +import { type RadrootsListing, radroots_listing_location_schema, radroots_listing_price_schema, radroots_listing_product_schema, radroots_listing_quantity_schema, radroots_listing_schema } from "@radroots/radroots-common-bindings"; +import { get_event_tag, get_event_tags, parse_nostr_event_basis } from "../lib.js"; +import { NdkEventBasis } from "../subscription.js"; +import { KIND_RADROOTS_LISTING, type KindRadrootsListing } from "./lib.js"; -export const parse_nostr_listing_event = (event: NDKEvent): NostrEventListing | undefined => { - if (!event || event.kind !== 30402 || !Array.isArray(event.tags)) return; +export type RadrootsListingNostrEvent = NdkEventBasis<KindRadrootsListing> & { listing: RadrootsListing; } +export const parse_nostr_listing_event = (event: NDKEvent): RadrootsListingNostrEvent | undefined => { + const ev = parse_nostr_event_basis(event, KIND_RADROOTS_LISTING); + if (!ev) return undefined; try { const tags = event.tags; const d_tag = get_event_tag(tags, 'd'); - const listing_raw = { + const product_raw = { key: get_event_tag(tags, 'key'), title: get_event_tag(tags, 'title'), category: get_event_tag(tags, 'category'), @@ -23,12 +26,12 @@ export const parse_nostr_listing_event = (event: NDKEvent): NostrEventListing | year: get_event_tag(tags, 'year') }; - const listing = nostr_tag_listing_schema.parse(listing_raw); + const product = radroots_listing_product_schema.parse(product_raw); const quantities = get_event_tags(tags, 'quantity') .map(q => { if (q.length < 3) return undefined; - return nostr_tag_quantity_schema.parse({ + return radroots_listing_quantity_schema.parse({ amt: q[1], unit: q[2], label: q[3] @@ -39,7 +42,7 @@ export const parse_nostr_listing_event = (event: NDKEvent): NostrEventListing | const prices = get_event_tags(tags, 'price') .map(p => { if (p.length < 6) return undefined; - return nostr_tag_price_schema.parse({ + return radroots_listing_price_schema.parse({ amt: p[1], currency: p[2], qty_amt: p[3], @@ -58,18 +61,17 @@ export const parse_nostr_listing_event = (event: NDKEvent): NostrEventListing | if (location_parts[3]) location_raw.country = location_parts[3]; const location = Object.keys(location_raw).length - ? nostr_tag_location_schema.parse(location_raw) + ? radroots_listing_location_schema.parse(location_raw) : undefined; - const parsed = nostr_event_listing_schema.parse({ + const listing = radroots_listing_schema.parse({ d_tag, - listing, + product, quantities, prices, location }); - - return parsed; + return { ...ev, listing }; } catch { return undefined; } diff --git a/utils-nostr/src/events/metadata/lib.ts b/utils-nostr/src/events/metadata/lib.ts @@ -1,16 +0,0 @@ -import { NDKEvent } from "@nostr-dev-kit/ndk"; -import { NostrEventMetadata } from "../../types/lib.js"; -import { NDKEventFigure } from "../../types/ndk.js"; -import { ndk_event } from "../lib.js"; - -export const ndk_event_metadata = async (opts: NDKEventFigure<{ data: NostrEventMetadata }>): Promise<NDKEvent | undefined> => { - const { ndk, ndk_user, data } = opts; - return await ndk_event({ - ndk, - ndk_user, - basis: { - kind: 0, - content: JSON.stringify(data), - }, - }); -}; -\ No newline at end of file diff --git a/utils-nostr/src/events/metadata/parse.ts b/utils-nostr/src/events/metadata/parse.ts @@ -1,15 +0,0 @@ -import { NDKEvent } from "@nostr-dev-kit/ndk"; -import { nostr_event_metadata_schema } from "../../schemas/lib.js"; -import { type NostrEventMetadata } from "../../types/lib.js"; - -export const parse_nostr_metadata_event = (event: NDKEvent): NostrEventMetadata | undefined => { - if (!event || typeof event.content !== 'string' || event.kind !== 0) return undefined; - - try { - const parsed = JSON.parse(event.content); - const result = nostr_event_metadata_schema.parse(parsed); - return result; - } catch { - return undefined; - } -}; diff --git a/utils-nostr/src/events/profile/lib.ts b/utils-nostr/src/events/profile/lib.ts @@ -0,0 +1,19 @@ +import { NDKEvent } from "@nostr-dev-kit/ndk"; +import { RadrootsProfile } from "@radroots/radroots-common-bindings"; +import { NDKEventFigure } from "../../types/ndk.js"; +import { ndk_event } from "../lib.js"; + +export const KIND_RADROOTS_PROFILE = 0; +export type KindRadrootsProfile = typeof KIND_RADROOTS_PROFILE; + +export const ndk_event_profile = async (opts: NDKEventFigure<{ data: RadrootsProfile }>): Promise<NDKEvent | undefined> => { + const { ndk, ndk_user, data } = opts; + return await ndk_event({ + ndk, + ndk_user, + basis: { + kind: 0, + content: JSON.stringify(data), + }, + }); +}; +\ No newline at end of file diff --git a/utils-nostr/src/events/profile/parse.ts b/utils-nostr/src/events/profile/parse.ts @@ -0,0 +1,19 @@ +import { NDKEvent } from "@nostr-dev-kit/ndk"; +import { type RadrootsProfile, radroots_profile_schema } from "@radroots/radroots-common-bindings"; +import { parse_nostr_event_basis } from "../lib.js"; +import { NdkEventBasis } from "../subscription.js"; +import { KIND_RADROOTS_PROFILE, type KindRadrootsProfile } from "./lib.js"; + +export type RadrootsProfileNostrEvent = NdkEventBasis<KindRadrootsProfile> & { profile: RadrootsProfile; } + +export const parse_nostr_profile_event = (event: NDKEvent): RadrootsProfileNostrEvent | undefined => { + const ev = parse_nostr_event_basis(event, KIND_RADROOTS_PROFILE); + if (!ev) return undefined; + try { + const parsed = JSON.parse(event.content); + const profile = radroots_profile_schema.parse(parsed); + return { ...ev, profile }; + } catch { + return undefined; + } +}; diff --git a/utils-nostr/src/events/reaction/lib.ts b/utils-nostr/src/events/reaction/lib.ts @@ -1,10 +1,13 @@ import { NDKEvent, NDKKind } from "@nostr-dev-kit/ndk"; -import { NostrEventReaction } from "../../types/lib.js"; +import { type RadrootsReaction, } from "@radroots/radroots-common-bindings"; import { NDKEventFigure } from "../../types/ndk.js"; import { tags_reaction } from "../../utils/tags.js"; import { ndk_event } from "../lib.js"; -export const ndk_event_reaction = async (opts: NDKEventFigure<{ data: NostrEventReaction; }>): Promise<NDKEvent | undefined> => { +export const KIND_RADROOTS_REACTION = 7; +export type KindRadrootsReaction = typeof KIND_RADROOTS_REACTION; + +export const ndk_event_reaction = async (opts: NDKEventFigure<{ data: RadrootsReaction; }>): Promise<NDKEvent | undefined> => { const { ndk, ndk_user, data } = opts; return await ndk_event({ ndk, diff --git a/utils-nostr/src/events/reaction/parse.ts b/utils-nostr/src/events/reaction/parse.ts @@ -1,14 +1,18 @@ import { NDKEvent } from "@nostr-dev-kit/ndk"; -import { nostr_event_reaction_schema } from "../../schemas/lib.js"; -import { NostrEventReaction } from "../../types/lib.js"; +import { type RadrootsReaction, radroots_reaction_schema } from "@radroots/radroots-common-bindings"; +import { parse_nostr_event_basis } from "../lib.js"; +import { NdkEventBasis } from "../subscription.js"; +import { KIND_RADROOTS_REACTION, type KindRadrootsReaction } from "./lib.js"; -export const parse_nostr_reaction_event = (event: NDKEvent): NostrEventReaction | undefined => { - if (!event || typeof event.content !== 'string' || event.kind !== 7) return undefined; +export type RadrootsReactionNostrEvent = NdkEventBasis<KindRadrootsReaction> & { reaction: RadrootsReaction; } +export const parse_nostr_reaction_event = (event: NDKEvent): RadrootsReactionNostrEvent | undefined => { + const ev = parse_nostr_event_basis(event, KIND_RADROOTS_REACTION); + if (!ev) return undefined; try { const parsed = JSON.parse(event.content); - const result = nostr_event_reaction_schema.parse(parsed); - return result; + const reaction = radroots_reaction_schema.parse(parsed); + return { ...ev, reaction }; } catch { return undefined; } diff --git a/utils-nostr/src/events/subscription.ts b/utils-nostr/src/events/subscription.ts @@ -1,48 +1,33 @@ import { NDKEvent } from "@nostr-dev-kit/ndk"; -import { NostrEventComment, NostrEventFollow, NostrEventListing, NostrEventReaction, type NostrEventMetadata } from "../types/lib.js"; -import { parse_nostr_comment_event } from "./comment/parse.js"; -import { parse_nostr_follow_event } from "./follow/parse.js"; -import { parse_nostr_listing_event } from "./listing/parse.js"; -import { parse_nostr_metadata_event } from "./metadata/parse.js"; -import { parse_nostr_reaction_event } from "./reaction/parse.js"; +import { parse_nostr_comment_event, RadrootsCommentNostrEvent } from "./comment/parse.js"; +import { parse_nostr_follow_event, RadrootsFollowNostrEvent } from "./follow/parse.js"; +import { parse_nostr_listing_event, RadrootsListingNostrEvent } from "./listing/parse.js"; +import { parse_nostr_profile_event, RadrootsProfileNostrEvent } from "./profile/parse.js"; +import { parse_nostr_reaction_event, RadrootsReactionNostrEvent } from "./reaction/parse.js"; -export type NdkEventPayload = - | { kind: 0; metadata: NostrEventMetadata; } - | { kind: 30402; listing: NostrEventListing; } - | { kind: 1111; comment: NostrEventComment; } - | { kind: 7; reaction: NostrEventReaction; } - | { kind: 3; follow: NostrEventFollow; } +export type NdkEventBasis<T extends number> = { + id: string; + published_at: number; + author: string; + kind: T; +}; +export type NdkEventPayload = + | RadrootsProfileNostrEvent + | RadrootsListingNostrEvent + | RadrootsCommentNostrEvent + | RadrootsReactionNostrEvent + | RadrootsFollowNostrEvent export const on_ndk_event = (event: NDKEvent): NdkEventPayload | undefined => { if (!event || typeof event.kind !== 'number') return undefined; switch (event.kind) { - case 0: { - const data = parse_nostr_metadata_event(event); - if (!data) return; - return { kind: event.kind, metadata: data }; - }; - case 30402: { - const data = parse_nostr_listing_event(event); - if (!data) return; - return { kind: event.kind, listing: data }; - }; - case 1111: { - const data = parse_nostr_comment_event(event); - if (!data) return; - return { kind: event.kind, comment: data }; - }; - case 7: { - const data = parse_nostr_reaction_event(event); - if (!data) return; - return { kind: event.kind, reaction: data }; - }; - case 3: { - const data = parse_nostr_follow_event(event); - if (!data) return; - return { kind: event.kind, follow: data }; - }; + case 0: return parse_nostr_profile_event(event); + case 30402: return parse_nostr_listing_event(event); + case 1111: return parse_nostr_comment_event(event); + case 7: return parse_nostr_reaction_event(event); + case 3: return parse_nostr_follow_event(event); default: return undefined; } diff --git a/utils-nostr/src/index.ts b/utils-nostr/src/index.ts @@ -5,8 +5,8 @@ export * from "./events/follow/parse.js" export * from "./events/lib.js" export * from "./events/listing/lib.js" export * from "./events/listing/parse.js" -export * from "./events/metadata/lib.js" -export * from "./events/metadata/parse.js" +export * from "./events/profile/lib.js" +export * from "./events/profile/parse.js" export * from "./events/reaction/lib.js" export * from "./events/reaction/parse.js" export * from "./events/subscription.js" diff --git a/utils-nostr/src/schemas/lib.ts b/utils-nostr/src/schemas/lib.ts @@ -1,140 +1,7 @@ import { z } from 'zod'; -export const nostr_event_metadata_schema = z.object({ - name: z.string(), - display_name: z.string().optional(), - about: z.string().optional(), - website: z.url().optional(), - picture: z.url().optional(), - banner: z.url().optional(), - nip05: z.string().optional(), - lud06: z.string().optional(), - lud16: z.string().optional(), - bot: z.boolean().optional(), -}); - -export const nostr_tag_listing_schema = z.object({ - key: z.string(), - title: z.string(), - category: z.string(), - summary: z.string().optional(), - process: z.string().optional(), - lot: z.string().optional(), - location: z.string().optional(), - profile: z.string().optional(), - year: z.string().optional() -}); - -export const nostr_tag_quantity_schema = z.object({ - amt: z.string(), - unit: z.string(), - label: z.string().optional() -}); - -export const nostr_tag_price_schema = z.object({ - amt: z.string(), - currency: z.string(), - qty_amt: z.string(), - qty_unit: z.string(), - qty_key: z.string() -}); - -export const nostr_tag_discount_schema = z.union([ - z.object({ - quantity: z.object({ - ref_quantity: z.string(), - threshold: z.string(), - value: z.string(), - currency: z.string() - }) - }), - z.object({ - mass: z.object({ - unit: z.string(), - threshold: z.string(), - threshold_unit: z.string(), - value: z.string(), - currency: z.string() - }) - }), - z.object({ - subtotal: z.object({ - threshold: z.string(), - currency: z.string(), - value: z.string(), - measure: z.string() - }) - }), - z.object({ - total: z.object({ - total_min: z.string(), - value: z.string(), - measure: z.string() - }) - }) -]); - -export const nostr_tag_location_schema = z.object({ - primary: z.string(), - city: z.string().optional(), - region: z.string().optional(), - country: z.string().optional(), - lat: z.number().optional(), - lng: z.number().optional() -}); - -export const nostr_tag_image_schema = z.object({ - url: z.string(), - size: z - .object({ - w: z.number(), - h: z.number() - }) - .optional() -}); - export const nostr_tag_client_schema = z.object({ name: z.string(), pubkey: z.string(), relay: z.string() -}); - -export const nostr_event_listing_schema = z.object({ - d_tag: z.string(), - listing: nostr_tag_listing_schema, - quantities: z.array(nostr_tag_quantity_schema), - prices: z.array(nostr_tag_price_schema), - discounts: z.array(nostr_tag_discount_schema).optional(), - location: nostr_tag_location_schema.optional(), - images: z.array(nostr_tag_image_schema).optional(), - client: nostr_tag_client_schema.optional() -}); - -export const nostr_event_referenced_schema = z.object({ - id: z.string().regex(/^[0-9a-f]{64}$/i, "expected 64-char hex id"), - kind: z.number().int().nonnegative(), - author: z.string().regex(/^[0-9a-f]{64}$/i, "expected 64-char hex pubkey"), - relays: z.array(z.url()).nonempty().optional(), - d_tag: z.string().min(1).optional(), -}); - -export const nostr_event_comment_schema = z.object({ - root_event: nostr_event_referenced_schema, - ref_event: nostr_event_referenced_schema.optional(), - content: z.string().min(1), -}); - -export const nostr_event_reaction_schema = z.object({ - ref_event: nostr_event_referenced_schema, - content: z.string().min(1), -}); - -export const nostr_follow_list_schema = z.object({ - public_key: z.string(), - relay_url: z.url().optional(), - contact_name: z.string().optional() -}); - -export const nostr_event_follow_schema = z.object({ - list: z.array(nostr_follow_list_schema) -}); +}); +\ No newline at end of file diff --git a/utils-nostr/src/types/lib.ts b/utils-nostr/src/types/lib.ts @@ -1,22 +1,8 @@ import { type EventTemplate as NostrToolsEventTemplate } from "nostr-tools"; import { z } from 'zod'; -import { nostr_event_comment_schema, nostr_event_follow_schema, nostr_event_listing_schema, nostr_event_metadata_schema, nostr_event_reaction_schema, nostr_event_referenced_schema, nostr_follow_list_schema, nostr_tag_client_schema, nostr_tag_discount_schema, nostr_tag_image_schema, nostr_tag_listing_schema, nostr_tag_location_schema, nostr_tag_price_schema, nostr_tag_quantity_schema } from "../schemas/lib.js"; +import { nostr_tag_client_schema } from "../schemas/lib.js"; -export type NostrEventMetadata = z.infer<typeof nostr_event_metadata_schema>;; -export type NostrEventListing = z.infer<typeof nostr_event_listing_schema>; export type NostrTagClient = z.infer<typeof nostr_tag_client_schema>; -export type NostrTagMediaUpload = z.infer<typeof nostr_tag_image_schema>; -export type NostrTagLocation = z.infer<typeof nostr_tag_location_schema>; -export type NostrTagPriceDiscount = z.infer<typeof nostr_tag_discount_schema>; -export type NostrTagPrice = z.infer<typeof nostr_tag_price_schema>; -export type NostrTagQuantity = z.infer<typeof nostr_tag_quantity_schema>; -export type NostrTagListing = z.infer<typeof nostr_tag_listing_schema>; -export type NostrEventComment = z.infer<typeof nostr_event_comment_schema>; -export type NostrEventReferenced = z.infer<typeof nostr_event_referenced_schema>; -export type NostrEventReaction = z.infer<typeof nostr_event_reaction_schema>; -export type NostrEventFollow = z.infer<typeof nostr_event_follow_schema>; -export type NostrFollowList = z.infer<typeof nostr_follow_list_schema>; - export type NostrEventTag = string[]; export type NostrEventTags = NostrEventTag[]; diff --git a/utils-nostr/src/types/ndk.ts b/utils-nostr/src/types/ndk.ts @@ -1,8 +1,10 @@ import NDK, { NDKUser } from "@nostr-dev-kit/ndk"; +import { type NostrEventTagClient } from "./lib.js"; export type NDKEventFigure<T extends object> = { ndk: NDK; ndk_user: NDKUser; date_published?: Date; + client?: NostrEventTagClient; } & T; diff --git a/utils-nostr/src/utils/tags.ts b/utils-nostr/src/utils/tags.ts @@ -1,5 +1,6 @@ +import { RadrootsComment, RadrootsFollowProfile, RadrootsListing, RadrootsReaction, } from "@radroots/radroots-common-bindings"; import ngeotags, { type InputData as NostrGeotagsInputData } from "nostr-geotags"; -import { NostrEventComment, NostrEventListing, NostrEventReaction, NostrEventTag, NostrEventTagClient, NostrEventTagImage, NostrEventTagLocation, NostrEventTagPrice, NostrEventTagPriceDiscount, NostrEventTagQuantity, NostrEventTags, NostrFollowList } from "../types/lib.js"; +import { NostrEventTag, NostrEventTagClient, NostrEventTagImage, NostrEventTagLocation, NostrEventTagPrice, NostrEventTagPriceDiscount, NostrEventTagQuantity, NostrEventTags } from "../types/lib.js"; export const tag_client = (opts: NostrEventTagClient, d_tag?: string): NostrEventTag => { const tag = [`client`, opts.name]; @@ -52,11 +53,10 @@ export const tag_classified_image = (opts: NostrEventTagImage): NostrEventTag => return tag; }; -export const tags_classified = (opts: NostrEventListing): NostrEventTags => { - const { d_tag, listing, quantities, prices } = opts; +export const tags_classified = (opts: RadrootsListing): NostrEventTags => { + const { d_tag, product, quantities, prices } = opts; const tags: NostrEventTags = [[`d`, d_tag]]; - if (opts.client) tags.push(tag_client(opts.client, opts.d_tag)); - for (const [k, v] of Object.entries(listing)) if (v) tags.push([k, v]); + for (const [k, v] of Object.entries(product)) if (v) tags.push([k, v]); for (const quantity of quantities) { tags.push(tag_classified_quantity(quantity)); } @@ -74,8 +74,8 @@ export const tags_classified = (opts: NostrEventListing): NostrEventTags => { return tags; }; -export const tags_comment = (opts: NostrEventComment): NostrEventTags => { - const { root_event, ref_event } = opts; +export const tags_comment = (opts: RadrootsComment): NostrEventTags => { + const { root: root_event, parent: parent_event } = opts; const root = { kind: root_event.kind.toString(), @@ -85,13 +85,13 @@ export const tags_comment = (opts: NostrEventComment): NostrEventTags => { relays: root_event.relays || [], }; - const parent = (ref_event && ref_event.id) + const parent = (parent_event && parent_event.id) ? { - kind: ref_event.kind.toString(), - author: ref_event.author, - id: ref_event.id, - d_tag: ref_event.d_tag, - relays: ref_event.relays || [], + kind: parent_event.kind.toString(), + author: parent_event.author, + id: parent_event.id, + d_tag: parent_event.d_tag, + relays: parent_event.relays || [], } : root; @@ -109,20 +109,20 @@ export const tags_comment = (opts: NostrEventComment): NostrEventTags => { return tags; }; -export const tags_reaction = (opts: NostrEventReaction): NostrEventTags => { - const { ref_event } = opts; - const ref_kind = ref_event.kind.toString(); - const ref_author = ref_event.author; +export const tags_reaction = (opts: RadrootsReaction): NostrEventTags => { + const { root } = opts; + const ref_kind = root.kind.toString(); + const ref_author = root.author; const tags: NostrEventTags = [ - [`e`, ref_event.id, ...ref_event.relays || ``], + [`e`, root.id, ...root.relays || ``], [`p`, ref_author], [`k`, ref_kind], ]; - if (ref_event.d_tag) tags.push([`a`, `${ref_kind}:${ref_author}:${ref_event.d_tag}`, ...ref_event.relays || ``]) + if (root.d_tag) tags.push([`a`, `${ref_kind}:${ref_author}:${root.d_tag}`, ...root.relays || ``]) return tags; }; -export const tags_follow_list = (list: NostrFollowList[]): NostrEventTags => { +export const tags_follow_list = (list: RadrootsFollowProfile[]): NostrEventTags => { return list.map(({ public_key, relay_url, contact_name }) => { const entry = [`p`, public_key]; if (relay_url) entry.push(relay_url);