summaryrefslogtreecommitdiff
path: root/front/src/logic/nostrill.ts
blob: bf9212d4c768ec8115d7785a9805273a9f666250 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import type { Event } from "@/types/nostr";
import type { Content, FC, Poast } from "@/types/trill";
import { engagementBunt, openLock } from "./bunts";
export function eventsToFc(postEvents: Event[]): FC {
  const fc = postEvents.reduce(
    (acc: FC, event: Event) => {
      const p = eventToPoast(event);
      if (!p) return acc;
      acc.feed[p.id] = p;
      if (!acc.start || event.created_at < Number(acc.start)) acc.start = p.id;
      if (!acc.end || event.created_at > Number(acc.end)) acc.end = p.id;
      return acc;
    },
    { feed: {}, start: null, end: null } as FC,
  );
  return fc;
}
export function eventToPoast(event: Event): Poast | null {
  if (event.kind !== 1) return null;
  const contents: Content = [{ paragraph: [{ text: event.content }] }];
  const ts = event.created_at * 1000;
  const id = `${ts}`;
  const poast: Poast = {
    id,
    host: event.pubkey,
    author: event.pubkey,
    contents,
    thread: id,
    parent: null,
    read: openLock,
    write: openLock,
    tags: [],
    time: ts,
    engagement: engagementBunt,
    children: [],
  };
  for (const tag of event.tags) {
    const f = tag[0];
    if (!f) continue;
    const ff = f.toLowerCase();
    console.log("tag", ff);
    if (ff === "e") {
      const [, eventId, _relayURL, marker, _pubkey, ..._] = tag;
      // TODO
      if (marker === "root") poast.thread = eventId;
      else if (marker === "reply") poast.parent = eventId;
    }
    //
    if (ff === "r")
      contents.push({
        paragraph: [{ link: { show: tag[1]!, href: tag[1]! } }],
      });
    if (ff === "p")
      contents.push({
        paragraph: [{ ship: tag[1]! }],
      });
    if (ff === "q")
      contents.push({
        ref: {
          type: "nostr",
          ship: tag[1]!,
          path: tag[2] || "" + `/${tag[3] || ""}`,
        },
      });
  }
  return poast;
}

// NOTE common tags:
// imeta
// client
// nonce
// proxy

// export function parseEventTags(event: Event) {
//   const effects: any[] = [];
//   for (const tag of event.tags) {
//     const f = tag[0];
//     if (!f) continue;
//     const ff = f.toLowerCase();
//     switch (ff) {
//       case "p": {
//         const [, pubkey, relayURL, ..._] = tag;
//         // people mention
//         break;
//       }
//       case "e": {
//         // marker to be "root" or "reply"
//         // event mention
//         break;
//       }
//       case "q": {
//         const [, eventId, relayURL, pubkey, ..._] = tag;
//         // event mention
//         break;
//       }
//       case "t": {
//         const [, hashtag, ..._] = tag;
//         // event mention
//         break;
//       }
//       case "r": {
//         const [, url, ..._] = tag;
//         // event mention
//         break;
//       }
//       case "alt": {
//         const [, summary, ..._] = tag;
//         // event mention
//         break;
//       }
//       default: {
//         break;
//       }
//     }
//   }
//   return effects;
// }