web_lib

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

parse.ts (1319B)


      1 import type { RadrootsList } 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 { is_nip51_list_kind, type KindRadrootsList } from "./lib.js";
      6 
      7 export type RadrootsListNostrEvent = NostrEventBasis<KindRadrootsList> & { list: RadrootsList };
      8 
      9 const list_entries_from_tags = (tags: NostrEventTags): RadrootsList["entries"] =>
     10     tags
     11         .filter(tag => tag.length >= 2)
     12         .map(tag => ({ tag: tag[0], values: tag.slice(1) }));
     13 
     14 export const parse_nostr_list_event = (
     15     event: NostrEvent,
     16 ): RadrootsListNostrEvent | undefined => {
     17     if (!event || typeof event.kind !== "number") return undefined;
     18     if (!is_nip51_list_kind(event.kind)) return undefined;
     19     if (typeof event.created_at !== "number") return undefined;
     20     try {
     21         const list_raw = {
     22             content: event.content ?? "",
     23             entries: list_entries_from_tags(event.tags),
     24         };
     25         const list = list_raw as RadrootsList;
     26         return {
     27             id: event.id,
     28             published_at: event.created_at,
     29             author: event.pubkey,
     30             kind: event.kind,
     31             list,
     32         };
     33     } catch {
     34         return undefined;
     35     }
     36 };