summaryrefslogtreecommitdiff
path: root/backupdesk/app/nostrill.hoon
blob: 4dd6ae57e06f6daa91d9932e67cc0d5e248c2807 (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=nostrill, nsur=nostr
/+  lib=nostrill, nlib=nostr, sr=sortug,
    shim, dbug, muta=nostril-mutations, jsonlib=json-nostrill,
    trill=trill-post
/=  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])
++  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)
  ==
::
++  on-poke
  |~  [=mark =vase]
  ^-  (quip card:agent:gall agent:gall)
  |^
  ?+  mark  `this
    %noun  debug
    %json  on-ui
    %handle-http-request  handle-shim
  ==
  ::  handling shim events
  ++  handle-shim
    =/  order  !<(order:web vase)
    :: ~&  request.req.order
    ?:  .=(url.request.req.order '/nostr-shim')
      =/  msg  (parse-msg:shim order)
      ?~  msg  `this
      (handle-shim-msg:mutat u.msg)

    ::
    :_  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)
      %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 pubkey.poke sp)
        =.  state  (add-to-feed:mutat p)
        `this
      %rt  `this
      %del  `this
    ==
  ++  handle-fols  |=  poke=fols-poke:ui:sur
  `this
  ++  handle-prof  |=  poke=prof-poke:ui:sur
    ?-  -.poke
      %add
        =.  profiles  (~(put by profiles) +<.poke +>.poke)
        `this
      %del
        =.  profiles  (~(del by profiles) +.poke)
        `this
    ==
  ++  handle-rela  |=  poke=relay-poke:ui:sur
    ?-  -.poke
      %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  !<(* vase)
    ?+  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
      %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)
            ==
          =/  sids  (silt (scag 10 ~(tap in +.ids)))
          =^  cards  state  (get-profiles:shimm sids)
          :: (get-profiles:shimm +.ids)
          :: (get-engagement:shimm -.ids)
        [cards this]
    ==
  ::
  --
::
++  on-watch
|=  =(pole knot)  
  ?>  .=(our.bowl src.bowl)
  ?+  pole  !!
  [%ui ~]
    :_  this
    =/  jon  (state:en:jsonlib state)
    [%give %fact ~ [%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 =sign-arvo]
  ^-  (quip card:agent:gall agent:gall)
  `this
::
++  on-fail
  |~  [term tang]
  ^-  (quip card:agent:gall agent:gall)
  `this
--