summaryrefslogtreecommitdiff
path: root/desk/app/nostrill.hoon
blob: 17732e746954e0403e0a064ad8cec20329f85ee1 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/-  sur=nostrill, nsur=nostr
/+  lib=nostrill, nlib=nostr, sr=sortug,
    shim, dbug, muta=nostrill-mutations, jsonlib=json-nostrill,
    trill=trill-post, comms=nostrill-comms
/=  web  /web/router
|%
+$  versioned-state  $%(state-0:sur)
--
=|  versioned-state
=*  state  -
%-  agent:dbug
^-  agent:gall
|_  =bowl:gall
+*  this  .
    rout  ~(. router:web [state bowl])
    cards  ~(. cards:lib bowl)
    mutat  ~(. muta [state bowl])
    shimm  ~(. shim [state bowl])
    coms   ~(. comms [state bowl])
++  on-init
  ^-  (quip card:agent:gall agent:gall)
  =/  default  (default-state:lib bowl)
  :_  this(state default)
  :~  shim-binding:cards
  ==
::
++  on-save
  ^-  vase
  !>(state)
::
++  on-load
  |~  old-state=vase
  ^-  (quip card:agent:gall agent:gall)
  =/  old-state  !<(versioned-state old-state)
  ?-  -.old-state
    %0  `this(state old-state)
  ==
  :: `this(state (default-state:lib bowl))
::
++  on-poke
  |~  [=mark =vase]
  ^-  (quip card:agent:gall agent:gall)
  |^
  ?+  mark  `this
    %noun    handle-comms
    %json    on-ui
    %handle-http-request  handle-shim
  ==
  ++  handle-comms
    =/  pok  (cast-poke:coms q.vase)
    ?:  ?=(%dbug -.pok)  (debug +.pok)
    =^  cs  state
      ?-  -.pok
        %req  (handle-req:coms +.pok)
        %res  (handle-res:coms +.pok)
      ==
    [cs this]
  ++  handle-shim
    =/  order  !<(order:web vase)
    :: ~&  request.req.order
    ?:  .=(url.request.req.order '/nostr-shim')
      =/  msg  (parse-msg:shim order)
      ?~  msg  `this
      ?>  ?=(%ws -.u.msg)
      :: =^  cards  state  (handle-shim-msg:mutat u.msg)
      =^  cards  state  (handle-ws:mutat +.u.msg)
      [cards this]

    ::
    :_  this  (rout:rout order)
  
  ::
  ++  on-ui
    =/  jon=json  !<(json vase)
    ~&  >  on-ui-jon=jon
    =/  upoke=(unit poke:ui:sur)  (ui:de:jsonlib jon)
    ?~  upoke  ~&  bad-ui-poke=jon  `this
    ?-  -.u.upoke
      %keys  handle-cycle-keys
      %fols  (handle-fols +.u.upoke)
      %begs  (handle-begs +.u.upoke)
      %prof  (handle-prof +.u.upoke)
      %post  (handle-post +.u.upoke)
      %rela  (handle-rela +.u.upoke)
    ==
  ++  handle-cycle-keys
        =/  ks  (gen-keys:nlib eny.bowl)
        =.  keys  [ks keys]
        :: =/  nkeys  keys(i ks, t `(list keys:nsur)`keys)
        :: :: =.  keys  nkeys
        ~&  new-keys=keys
        `this

  ++  handle-post  |=  poke=post-poke:ui:sur
    ?-  -.poke
      %add
        =/  sp     (build-sp:trill our.bowl our.bowl content.poke)
        =/  p      (build-post:trill now.bowl pub.i.keys sp)
        =.  state  (add-to-feed:mutat p)
        =/  profile  (~(get by profiles) pub.i.keys)
        =/  pw  [p (some pub.i.keys) ~ ~ profile]
        =/  =fact:ui:sur  [%post %add pw]
        =/  card     (update-ui:cards fact)
        :_  this  :~(card)

      %rt  `this
      %del  `this
    ==
  ++  handle-begs  |=  poke=begs-poke:ui:sur
  ?-  -.poke
    %feed
      =/  cs  ~
      [cs this]
    %thread
      =/  cs  ~
      [cs this]
  ==
  ++  handle-fols  |=  poke=fols-poke:ui:sur
  ?-  -.poke
    %add   `this
    %del   `this
  ==
  ++  handle-prof  |=  poke=prof-poke:ui:sur
    ?-  -.poke
      %add
        =.  profiles  (~(put by profiles) pub.i.keys +.poke)
        `this
      %del
        =.  profiles  (~(del by profiles) pub.i.keys)
        `this
    ==
  ++  handle-rela  |=  poke=relay-poke:ui:sur
    ?-  -.poke
      %add  =.  relays  (~(put by relays) +.poke *relay-stats:nsur)
            `this
      %del  =.  relays  (~(del by relays) +.poke)
            `this
      ::
      %sync  =^  cs  state  get-posts:shimm
             [cs this]
      ::
      %send
          =/  upoast  (get-poast:mutat host.poke id.poke)
          ?~  upoast  `this
          =/  event  (post-to-event:lib i.keys eny.bowl u.upoast)
          =/  req=bulk-req:shim:nsur  [relays.poke %event event]
          =/  cards  :~((send:shimm req))
          [cards this]
    ==

    
  ::
  ++  debug  |=  noun=*
    ?+  noun  `this
      %wtf
        =/  lol=(unit @)  ~
        =/  l  ~|  "wtf"  (need lol)
        `this
      %genkey
        =/  keys  (gen-keys:nlib eny.bowl)
        ~&  keys=keys
        `this
      %printkey
          =/  key  i.keys
          ~&  pub=(scow:sr %ux -.key)
          ~&  priv=(scow:sr %ux +.key)
        `this
      %feed
        =/  lol  debug-own-feed:mutat
        `this
      %nstats
        ~&  nostr-feed=~(wyt by nostr-feed)
        ~&  profiles=~(wyt in ~(key by profiles))
        =/  lol  (print-relay-stats:lib relays)

      `this
      %http
      `this
      %rt  ::  relay test
        =^  cards  state  get-posts:shimm
        [cards this]
      %rt0
        =/  ids
          %+  roll  (tap:norm:sur nostr-feed)  |=  [[@ ev=event:nsur] acc=[(set @ux) (set @ux)]]
            ?.  .=(kind.ev 1)  acc
            %=  acc
              -  (~(put in -.acc) id.ev)
              +  (~(put in +.acc) pubkey.ev)
            ==
          =^  cards  state  (populate-profiles:mutat -.ids)
          :: (get-profiles:shimm +.ids)
          :: (get-engagement:shimm -.ids)
        [cards this]
      :: %rt1
      ::     =|  cards=(list card:agent:gall)
      ::     |-
      ::     ?~  l
      ::         ~&  cards=(lent cards)  [cards this]
      ::       =/  [sub-id=@t pf=filter:nsur done=filter:nsur]  i.l
      ::       =/  diff  (diff-filters:nlib pf done)
      ::       :: ~&  >  diff=diff
      ::       ?~  authors.pf  $(l t.l)
      ::       =^  cs  state  (populate-profiles:mutat u.authors.pf)
            
      ::       $(l t.l, cards (weld cards cs))
    %rt2

      =/  poasts  (tap:norm:sur nostr-feed)
      =/  pcount  (lent poasts)
      =|  invalid=(list @t)
      |-  ?~  poasts
        ~&  >>>  invalid=invalid
        `this
        =/  p=event:nsur  +.i.poasts
        =/  valid  (validate-pubkey:nlib pubkey.p)
        ?.  valid
          =/  ids  (crip (scow:sr %ux id.p))
          ~&  ids
          ~&  content.p
          $(invalid [ids invalid], poasts t.poasts)
        $(poasts t.poasts)
    %rt3
      =/  poasts  (tap:norm:sur nostr-feed)
      =|  pubkeys=(set @ux)
      =/  pks=(set @ux)
        |-  ?~  poasts  pubkeys
          =/  p=event:nsur  +.i.poasts
          =/  npks  (~(put in pubkeys) pubkey.p)
          $(poasts t.poasts, pubkeys npks)
      ::
      =^  cards  state  (populate-profiles:mutat pks)
      [cards this]
    %ui
      =/  =fact:ui:sur  [%post %add *post-wrapper:sur]
      =/  card     (update-ui:cards fact)
      :_  this  :~(card)
    ==
      
  ::
  --
::
++  on-watch
|=  =(pole knot)  
  ~&  on-watch=`path`pole
  ?+  pole  !!
  [%beg %feed ~]
    :_  this  give-feed:coms
  [%beg %thread ids=@t ~]
    =/  id  (slaw:sr %uw ids.pole)
    ?~  id  ~&  error-parsing-ted-id=pole  `this
    :_  this  (give-ted:coms u.id)
  [%ui ~]
    ?>  .=(our.bowl src.bowl)
    :_  this
    =/  jon  (state:en:jsonlib state)
    [%give %fact ~[/ui] [%json !>(jon)]]^~
  ==
::
++  on-leave
  |~  =(pole knot)
  ^-  (quip card:agent:gall agent:gall)
  `this
::
++  on-peek
  |~  =(pole knot)
  ^-  (unit (unit cage))
  ~&  >  on-peek=pole
  ?+  pole  ~
   [%j %feed rest=*]  ~
   [%j %post rest=*]  ~
  ==
::
++  on-agent
  |~  [wire sign:agent:gall]
  ^-  (quip card:agent:gall agent:gall)
  `this
::
++  on-arvo
  |~  [wire=(pole knot) =sign-arvo]
  ^-  (quip card:agent:gall agent:gall)
  ?+  wire  `this
    [%http sub-id=@t *]  
      ?>  ?=([%khan %arow *] sign-arvo)
      ?:  ?=(%| -.p.sign-arvo)  `this
      =/  jstring  !<(@ +>.p.sign-arvo)
      =/  msg  (parse-body:shimm jstring)
      ?~  msg  ~&  `@t`jstring  `this
      ?>  ?=(%http -.u.msg)
      =^  cards  state  (handle-http:mutat sub-id.wire +.u.msg)
      `this
  ==
::
++  on-fail
  |~  [term tang]
  ^-  (quip card:agent:gall agent:gall)
  `this
--