web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

parse.ts (1843B)


      1 import type { RadrootsListSet } from "@radroots/events-bindings";
      2 import type { NostrEventTags } from "../../types/lib.js";
      3 import type { NostrEvent } from "../../types/nostr.js";
      4 import type { NostrEventBasis } from "../subscription.js";
      5 import { get_event_tag } from "../lib.js";
      6 import { is_nip51_list_set_kind, type KindRadrootsListSet } from "./lib.js";
      7 
      8 export type RadrootsListSetNostrEvent =
      9     NostrEventBasis<KindRadrootsListSet> & { list_set: RadrootsListSet };
     10 
     11 const RESERVED_TAGS = new Set(["d", "title", "description", "image"]);
     12 
     13 const list_entries_from_tags = (tags: NostrEventTags): RadrootsListSet["entries"] =>
     14     tags
     15         .filter(tag => tag.length >= 2 && !RESERVED_TAGS.has(tag[0]))
     16         .map(tag => ({ tag: tag[0], values: tag.slice(1) }));
     17 
     18 export const parse_nostr_list_set_event = (
     19     event: NostrEvent,
     20 ): RadrootsListSetNostrEvent | undefined => {
     21     if (!event || typeof event.kind !== "number") return undefined;
     22     if (!is_nip51_list_set_kind(event.kind)) return undefined;
     23     if (typeof event.created_at !== "number") return undefined;
     24     const d_tag = get_event_tag(event.tags, "d");
     25     if (!d_tag) return undefined;
     26     try {
     27         const list_set_raw = {
     28             d_tag,
     29             content: event.content ?? "",
     30             entries: list_entries_from_tags(event.tags),
     31             title: get_event_tag(event.tags, "title") || undefined,
     32             description: get_event_tag(event.tags, "description") || undefined,
     33             image: get_event_tag(event.tags, "image") || undefined,
     34         };
     35         const list_set = list_set_raw as RadrootsListSet;
     36         return {
     37             id: event.id,
     38             published_at: event.created_at,
     39             author: event.pubkey,
     40             kind: event.kind,
     41             list_set,
     42         };
     43     } catch {
     44         return undefined;
     45     }
     46 };