summaryrefslogtreecommitdiff
path: root/desk/lib/json/nostr.hoon
blob: cb10c1da99295cc9cefed6e3589385a55d6d6695 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/-  sur=nostr
/+  common=json-common, sr=sortug
|%
++  en
=,  enjs:format  
  |%
  ::  shim comms
  ++  bulk-req  |=  [relays=(list @t) r=req:shim:sur]  ^-  json
    %+  frond  %ws
    %:  pairs
      relays+a+(turn relays cord:en:common)
      req+(req r)
    ~
    ==
  ++  http-req  |=  [relay=@t delay=@ud sub-id=@t fs=(list filter:sur)]
    %+  frond  %http
    %:  pairs
      relay+s+relay
      delay+(numb delay)
      ['subscription_id' %s sub-id]
      filters+a+(turn fs filter)
    ~
    ==
  ++  req  |=  =req:shim:sur  ^-  json
    =/  en-ev  event
    :-  %a  :-  s+(crip (cuss (trip -.req)))
    ?-  -.req
      %req    (enreq +.req)
      %event  :_  ~  (en-ev(nostr .y) +.req)
      %auth   :_  ~  (en-ev(nostr .y) +.req)
      %close  :_  ~  [%s +.req]
    ==
++  enreq
  |=  [sub-id=@t fs=(list filter:sur)]
  ^-  (list json)
  :-  [%s sub-id]
  %+  turn  fs  filter
  
    ::
  ++  raw-event  |=  raw-event:sur
  :: WTF nostr doesn't want the prefix on the pubkey
    :: =/  scw  scow:sr
    :: =/  pubkeyt  (scw(min-chars 64) %ux pubkey)
    =/  pubkeyt  (scow:sr %ux pubkey)
    ?~  pubkeyt  !!
    =/  pubkeyj  [%s (crip t.pubkeyt)]
    :: =/  pubkeyj  [%s (crip pubkeyt)]
    :-  %a  :~
      [%n '0']
      pubkeyj  
      (numb created-at)
      (numb kind)
      a+(turn tags tag)
      s+content
    ==
  ++  event
  =/  nostr=?  .n
  |=  e=event:sur  ^-  json
    =/  pubkey  ?.  nostr
        (hex:en:common pubkey.e)
      =/  pubkeyt  (scow:sr %ux pubkey.e)
      ?~  pubkeyt  !!
      [%s (crip t.pubkeyt)]
    %:  pairs
      id+(hex:en:common id.e)
      pubkey+pubkey
      sig+(hex:en:common sig.e)
      ['created_at' (numb created-at.e)]
      kind+(numb kind.e)
      content+s+content.e
      tags+a+(turn tags.e tag)
    ~
    ==
  ++  tag
  |=  t=tag:sur  ^-  json  [%a (turn t cord:en:common)]
    :: :-  s+key.t
    :: :-  s+value.t
    :: (turn rest.t |=(tt=@t s+tt))
    :: 
  ++  filter
  |=  f=filter:sur  ^-  json
    =|  l=(list [key=@t jon=json])
    =.  l  ?~  ids.f      l  :_  l  ['ids' %a (turn ~(tap in u.ids.f) hex:en:common)]
    =.  l  ?~  authors.f  l  :_  l  ['authors' %a (turn ~(tap in u.authors.f) hex:en:common)]
    =.  l  ?~  kinds.f    l  :_  l  ['kinds' %a (turn ~(tap in u.kinds.f) numb)]
    =.  l  ?~  tags.f     l  %+  weld  l  (tags u.tags.f)

    =.  l  ?~  since.f    l  :_  l  ['since' (sect u.since.f)]
    =.  l  ?~  until.f    l  :_  l  ['until' (sect u.until.f)]
    =.  l  ?~  limit.f    l  :_  l  ['limit' (numb u.limit.f)]
    ::
    %-  pairs  l


  ++  tags
  |=  tm=(map @t (set @t))  ^-  (list [@t json])  ::  entries to the filter obeject
    %+  turn  ~(tap by tm)    |=  [key=@t values=(set @t)]
      =/  nkey  (cat 3 '#' key)
      [nkey %a (turn ~(tap in values) cord:en:common)]

  ++  user-meta
  |=  meta=user-meta:sur
    %:  pairs
        name+s+name.meta
        picture+s+picture.meta
        about+s+about.meta
        other+o+other.meta
        ~
    ==
  --
++  de
=,  dejs-soft:format
  |%
    :: shim
  ++  shim-res
    %-  of  :~
      http+(ar relay-msg)
      ws+msg
    ==    
  ++  msg
    %-  ot  :~
      relay+so
      msg+relay-msg
    ==
  ++  relay-msg
    %-  of  :~
      event+event-sub
      ok+relay-ok
      eose+so
      closed+closed
      notice+so
      error+so
    ==

    
    :: | { event: { subscription_id: string; event: NostrEvent } }
    :: | { ok: { event_id: string; accepted: boolean; message: string } }
    :: | { eose: string }
    :: | { closed: { subscription_id: string; message: string } }
    :: | { notice: string }
    :: // this is ours
    :: | { error: string };
  ++  event-sub
    %-  ot  :~
      ['subscription_id' so]
      event+event
    ==
  ++  relay-ok
    %-  ot  :~
      ['event_id' hex:de:common]
      accepted+bo
      message+so
    ==
  ++  closed
    %-  ot  :~
      ['subscription_id' so]
      message+so
    ==
  ++  event
    %-  ot  :~
      id+hex:de:common
      pubkey+hex:de:common
      ['created_at' ni]
      kind+ni
      tags+(ar (ar so))
      content+so
      sig+hex:de:common
    ==
  ++  user-meta  |=  jon=json
    ^-  (unit user-meta:sur)
    ?.  ?=(%o -.jon)  ~
    =|  um=user-meta:sur
    =/  fields  ~(tap by p.jon)
    |-  ?~  fields  (some um)
      =/  k  -.i.fields
      =/  jn=json  +.i.fields
      ?+  k
        =/  ot  (~(put by other.um) k jn)
        =.  um  um(other ot)  $(fields t.fields)
      %'name'             
        =/  crd  (so jn)
        ?~  crd  $(fields t.fields)  $(fields t.fields, um um(name u.crd))
      %'display_name'
        =/  crd  (so jn)
        ?~  crd  $(fields t.fields)  $(fields t.fields, um um(name u.crd))
      %'displayName'
        =/  crd  (so jn)
        ?~  crd  $(fields t.fields)  $(fields t.fields, um um(name u.crd))
      %'about'
        =/  crd  (so jn)
        ?~  crd  $(fields t.fields)  $(fields t.fields, um um(picture u.crd))
      %'picture'
        =/  crd  (so jn)
        ?~  crd  $(fields t.fields)  $(fields t.fields, um um(picture u.crd))
      ==
  --
--