summaryrefslogtreecommitdiff
path: root/front/src/logic/nostrill.ts
blob: bd5fc9c20a3b172c75f05daf65e62dce1de483b2 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import type { Event } from "@/types/nostr";
import type { Content, FC, Poast } from "@/types/trill";
import { engagementBunt, openLock } from "./bunts";
import type { UserType } from "@/types/nostrill";
import type { Result } from "@/types/ui";
import { isValidPatp } from "urbit-ob";
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;
}

export function userToString(user: UserType): Result<string> {
  if ("urbit" in user) {
    const isValid = isValidPatp(user.urbit);
    if (isValid) return { ok: user.urbit };
    else return { error: "invalid @p" };
  } else if ("nostr" in user) return { ok: user.nostr };
  else return { error: "unknown user" };
}
export function isValidNostrPubkey(pubkey: string): boolean {
  // TODO
  if (pubkey.length !== 64) return false;
  try {
    BigInt("0x" + pubkey);
    return true;
  } catch (_e) {
    return false;
  }
}
// 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;
// }