-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathheap.hoon
600 lines (600 loc) · 14.6 KB
/
heap.hoon
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/- h=heap, g=groups, ha=hark
/- meta
/+ default-agent, verb, dbug
/+ cur=curios
/+ heap-json
^- agent:gall
=>
|%
+$ card card:agent:gall
+$ state-0
$: %0
=stash:h
==
--
=| state-0
=* state -
=<
%+ verb &
%- agent:dbug
|_ =bowl:gall
+* this .
def ~(. (default-agent this %|) bowl)
cor ~(. +> [bowl ~])
++ on-init
^- (quip card _this)
=^ cards state
abet:init:cor
[cards this]
::
++ on-save !>(state)
++ on-load
|= =vase
^- (quip card _this)
=/ old=(unit state-0)
(mole |.(!<(state-0 vase)))
?^ old `this(state u.old)
~& >>> "Incompatible load, nuking"
=^ cards this on-init
:_ this
=- (welp - cards)
%+ turn ~(tap in ~(key by wex.bowl))
|= [=wire =ship =term]
^- card
[%pass wire %agent [ship term] %leave ~]
::
++ on-poke
|= [=mark =vase]
^- (quip card _this)
=^ cards state
abet:(poke:cor mark vase)
[cards this]
++ on-watch
|= =path
^- (quip card _this)
=^ cards state
abet:(watch:cor path)
[cards this]
::
++ on-peek peek:cor
::
++ on-leave on-leave:def
++ on-fail on-fail:def
::
++ on-agent
|= [=wire =sign:agent:gall]
^- (quip card _this)
=^ cards state
abet:(agent:cor wire sign)
[cards this]
++ on-arvo
|= [=wire sign=sign-arvo]
^- (quip card _this)
=^ cards state
abet:(arvo:cor wire sign)
[cards this]
--
|_ [=bowl:gall cards=(list card)]
++ abet [(flop cards) state]
++ cor .
++ emit |=(=card cor(cards [card cards]))
++ emil |=(caz=(list card) cor(cards (welp (flop caz) cards)))
++ give |=(=gift:agent:gall (emit %give gift))
++ init
^+ cor
watch-groups
::
++ watch-groups
^+ cor
(emit %pass /groups %agent [our.bowl %groups] %watch /groups)
::
++ poke
|= [=mark =vase]
|^ ^+ cor
?+ mark ~|(bad-poke/mark !!)
?(%flag %channel-join)
=+ !<(=flag:h vase)
?< =(our.bowl p.flag)
(join flag)
::
%heap-leave
=+ !<(=leave:h vase)
?< =(our.bowl p.leave) :: cannot leave chat we host
he-abet:he-leave:(he-abed:he-core leave)
::
%heap-create
=+ !<(req=create:h vase)
(create req)
::
?(%heap-action-0 %heap-action)
=+ !<(=action:h vase)
=. p.q.action now.bowl
=/ heap-core (he-abed:he-core p.action)
?: =(p.p.action our.bowl)
he-abet:(he-update:heap-core q.action)
he-abet:(he-proxy:heap-core q.action)
::
%heap-remark-action
=+ !<(act=remark-action:h vase)
he-abet:(he-remark-diff:(he-abed:he-core p.act) q.act)
==
++ join
|= =flag:h
^+ cor
=. stash (~(put by stash) flag *heap:h)
he-abet:(he-join:he-core flag)
::
++ create
|= req=create:h
|^ ^+ cor
~_ leaf+"Create failed: check group permissions"
?> can-nest
?> ((sane %tas) name.req)
=/ =flag:h [our.bowl name.req]
=| =heap:h
=/ =perm:h [writers.req group.req]
=. perm.heap perm
=. net.heap [%pub ~]
=. stash (~(put by stash) flag heap)
he-abet:(he-init:(he-abed:he-core flag) req)
++ can-nest
^- ?
=/ gop (~(got by groups) group.req)
%- ~(any in bloc.gop)
~(has in sects:(~(got by fleet.gop) our.bowl))
::
++ groups
.^ groups:g
%gx
/(scot %p our.bowl)/groups/(scot %da now.bowl)/groups/noun
==
--
--
++ watch
|= =path
^+ cor
?+ path ~|(bad-watch-path/path !!)
[%briefs ~] ?>(from-self cor)
[%ui ~] ?>(from-self cor)
::
[%heap @ @ *]
=/ =ship (slav %p i.t.path)
=* name i.t.t.path
he-abet:(he-watch:(he-abed:he-core ship name) t.t.t.path)
==
::
++ agent
|= [=wire =sign:agent:gall]
^+ cor
?+ wire ~|(bad-agent-wire/wire !!)
::
[%hark ~]
?> ?=(%poke-ack -.sign)
?~ p.sign cor
%- (slog leaf/"Failed to hark" u.p.sign)
cor
::
[%heap @ @ *]
=/ =ship (slav %p i.t.wire)
=* name i.t.t.wire
he-abet:(he-agent:(he-abed:he-core ship name) t.t.t.wire sign)
::
[%groups ~]
?+ -.sign !!
%kick watch-groups
::
%watch-ack
%. cor
?~ p.sign same
=/ =tank
leaf/"Failed groups subscription in {<dap.bowl>}, unexpected"
(slog tank u.p.sign)
::
%fact
?. =(%group-action p.cage.sign) cor
(take-groups !<(=action:g q.cage.sign))
==
==
++ take-groups
|= =action:g
=/ affected=(list flag:h)
%+ murn ~(tap by stash)
|= [=flag:h =heap:h]
?. =(p.action group.perm.heap) ~
`flag
?+ q.q.action cor
[%fleet * %del ~]
~& 'revoke perms for'
%+ roll affected
|= [=flag:h co=_cor]
^+ cor
%+ roll ~(tap in p.q.q.action)
|= [=ship ci=_cor]
^+ cor
=/ he (he-abed:he-core:ci flag)
he-abet:(he-revoke:he ship)
::
[%fleet * %del-sects *]
~& 'recheck permissions'
%+ roll affected
|= [=flag:h co=_cor]
=/ he (he-abed:he-core:co flag)
he-abet:he-recheck:he
::
[%channel * %del-sects *]
~& 'recheck permissions'
%+ roll affected
|= [=flag:h co=_cor]
=/ he (he-abed:he-core:co flag)
he-abet:he-recheck:he
==
::
++ arvo
|= [=wire sign=sign-arvo]
^+ cor
~& arvo/wire
cor
++ peek
|= =path
^- (unit (unit cage))
?+ path [~ ~]
::
[%x %stash ~] ``stash+!>(stash)
::
[%x %heap @ @ *]
=/ =ship (slav %p i.t.t.path)
=* name i.t.t.t.path
(he-peek:(he-abed:he-core ship name) t.t.t.t.path)
::
[%x %briefs ~]
=- ``heap-briefs+!>(-)
^- briefs:h
%- ~(gas by *briefs:h)
%+ turn ~(tap in ~(key by stash))
|= =flag:h
:- flag
he-brief:(he-abed:he-core flag)
==
::
++ give-brief
|= [=flag:h =brief:briefs:h]
(give %fact ~[/briefs] heap-brief-update+!>([flag brief]))
::
++ pass-hark
|= [all=? desk=? =yarn:ha]
^- card
=/ =wire /hark
=/ =dock [our.bowl %hark]
=/ =cage hark-action+!>([%add-yarn all desk yarn])
[%pass wire %agent dock %poke cage]
++ spin
|= [=rope:ha con=(list content:ha) wer=path but=(unit button:ha)]
^- yarn:ha
=/ id (end [7 1] (shax eny.bowl))
[id rope now.bowl con wer but]
++ flatten
|= content=(list inline:h)
^- cord
%- crip
%- zing
%+ turn
content
|= c=inline:h
^- tape
?@ c (trip c)
?- -.c
%break ""
%tag (trip p.c)
%link (trip q.c)
?(%code %inline-code) ""
?(%italics %bold %strike %blockquote) (trip (flatten p.c))
==
++ from-self =(our src):bowl
++ he-core
|_ [=flag:h =heap:h gone=_|]
+* he-curios ~(. cur curios.heap)
++ he-core .
:: TODO: archive??
++ he-abet
%_ cor
stash
?:(gone (~(del by stash) flag) (~(put by stash) flag heap))
==
++ he-abed
|= f=flag:h
he-core(flag f, heap (~(got by stash) f))
++ he-area `path`/heap/(scot %p p.flag)/[q.flag]
++ he-spin
|= [rest=path con=(list content:ha) but=(unit button:ha)]
=* group group.perm.heap
=/ =nest:g [dap.bowl flag]
=/ rope [`group `nest q.byk.bowl (welp /(scot %p p.flag)/[q.flag] rest)]
=/ link
(welp /groups/(scot %p p.group)/[q.group]/channels/heap/(scot %p p.flag)/[q.flag] rest)
(spin rope con link but)
::
++ he-watch
|= =path
^+ he-core
?+ path !!
[%updates *] (he-pub t.path)
[%ui ~] ?>(from-self he-core)
[%ui %curios ~] ?>(from-self he-core)
::
==
++ he-pass
|%
++ add-channel
|= req=create:h
=/ =dock [p.group.req %groups]
=/ =nest:g [dap.bowl flag]
=/ =channel:g
=,(req [[title description '' ''] now.bowl %default | readers])
=/ =action:g [group.req now.bowl %channel nest %add channel]
=/ =cage group-action+!>(action)
=/ =wire (snoc he-area %create)
=/ =card
[%pass he-area %agent dock %poke cage]
=. cor
(emit card)
he-core
--
++ he-init
|= req=create:h
=/ =perm:h [writers.req group.req]
=. cor
(give-brief flag he-brief)
=. he-core (he-update now.bowl %create perm)
(add-channel:he-pass req)
::
++ he-agent
|= [=wire =sign:agent:gall]
^+ he-core
?+ wire !!
~ :: noop wire, should only send pokes
he-core
::
[%updates ~]
(he-take-update sign)
::
[%create ~]
?> ?=(%poke-ack -.sign)
%. he-core :: TODO rollback creation if poke fails?
?~ p.sign same
(slog leaf/"poke failed" u.p.sign)
==
::
++ he-brief (brief:he-curios our.bowl last-read.remark.heap)
::
++ he-peek
|= =(pole knot)
^- (unit (unit cage))
?+ pole [~ ~]
[%curios rest=*] (peek:he-curios rest.pole)
[%perm ~] ``heap-perm+!>(perm.heap)
==
::
++ he-revoke
|= her=ship
%+ roll ~(tap in he-subscriptions)
|= [[=ship =path] he=_he-core]
?. =(ship her) he
he(cor (emit %give %kick ~[path] `ship))
::
++ he-recheck
%+ roll ~(tap in he-subscriptions)
|= [[=ship =path] he=_he-core]
?: (he-can-read:he ship) he
he(cor (emit %give %kick ~[path] `ship))
::
++ he-take-update
|= =sign:agent:gall
^+ he-core
?+ -.sign he-core
%kick he-sub
::
%watch-ack
=. net.heap [%sub src.bowl]
?~ p.sign he-core
%- (slog leaf/"Failed subscription" u.p.sign)
=. gone &
he-core
::
%fact
=* cage cage.sign
?+ p.cage he-core
?(%heap-logs-0 %heap-logs) (he-apply-logs !<(log:h q.cage))
?(%heap-update-0 %heap-update) (he-update !<(update:h q.cage))
==
==
++ he-proxy
|= =update:h
^+ he-core
?> he-can-write
=/ =dock [p.flag dap.bowl]
=/ =cage heap-action+!>([flag update])
=. cor
(emit %pass he-area %agent dock %poke cage)
he-core
::
++ he-groups-scry
=* group group.perm.heap
/(scot %p our.bowl)/groups/(scot %da now.bowl)/groups/(scot %p p.group)/[q.group]
::
++ he-can-write
?: =(p.flag src.bowl) &
=/ =path
%+ welp he-groups-scry
/fleet/(scot %p src.bowl)/vessel/noun
=+ .^(=vessel:fleet:g %gx path)
?: =(~ writers.perm.heap) &
!=(~ (~(int in writers.perm.heap) sects.vessel))
::
++ he-can-read
|= her=ship
=/ =path
%+ welp he-groups-scry
/channel/[dap.bowl]/(scot %p p.flag)/[q.flag]/can-read/(scot %p her)/loob
.^(? %gx path)
::
++ he-pub
|= =path
^+ he-core
?> (he-can-read src.bowl)
=/ =log:h
?~ path log.heap
=/ =time (slav %da i.path)
(lot:log-on:h log.heap `time ~)
=/ =cage heap-logs+!>(log)
=. cor (give %fact ~ cage)
he-core
::
++ he-sub
^+ he-core
=/ tim=(unit time)
(bind (ram:log-on:h log.heap) head)
=/ base=wire (snoc he-area %updates)
=/ =path
%+ weld base
?~ tim ~
/(scot %da u.tim)
=/ =card
[%pass base %agent [p.flag dap.bowl] %watch path]
=. cor (emit card)
he-core
++ he-join
|= f=flag:h
^+ he-core
=. stash (~(put by stash) f *heap:h)
=. he-core (he-abed f)
=. cor (give-brief flag he-brief)
he-sub
::
++ he-leave
=/ =dock [p.flag dap.bowl]
=/ =wire (snoc he-area %updates)
=. cor (emit %pass wire %agent dock %leave ~)
=. cor (emit %give %fact ~[/briefs] heap-leave+!>(flag))
=. gone &
he-core
::
++ he-apply-logs
|= =log:h
^+ he-core
=/ updates=(list update:h)
(tap:log-on:h log)
%+ roll updates
|= [=update:h he=_he-core]
(he-update:he update)
::
++ he-subscriptions
%+ roll ~(val by sup.bowl)
|= [[=ship =path] out=(set [ship path])]
?. =((scag 4 path) (snoc he-area %updates))
out
(~(put in out) [ship path])
::
++ he-give-updates
|= [=time d=diff:h]
^+ he-core
=/ paths=(set path)
%- ~(gas in *(set path))
(turn ~(tap in he-subscriptions) tail)
=. paths (~(put in paths) (snoc he-area %ui))
=/ cag=cage heap-update+!>([time d])
=. cor
(give %fact ~(tap in paths) cag)
=. cor (give %fact ~[/ui] heap-action+!>([flag [time d]]))
=? cor ?=(%curios -.d)
=/ =cage curios-diff+!>(p.d)
(give %fact ~[(welp he-area /ui/curios)] cage)
he-core
::
++ he-remark-diff
|= diff=remark-diff:h
^+ he-core
=. cor
(give %fact ~[(snoc he-area %ui)] heap-remark-action+!>([flag diff]))
=. remark.heap
?- -.diff
%watch remark.heap(watching &)
%unwatch remark.heap(watching |)
%read-at !!
::
%read remark.heap(last-read now.bowl)
==
=. cor
(give-brief flag he-brief)
he-core
::
++ he-update
|= [=time d=diff:h]
^+ he-core
?> he-can-write
=? time =(p.flag our.bowl)
now.bowl
=. log.heap
(put:log-on:h log.heap time d)
=. he-core
(he-give-updates time d)
?- -.d
%curios
=. curios.heap (reduce:he-curios time q.p.d)
=. cor (give-brief flag he-brief)
?- -.q.p.d
?(%edit %del %add-feel %del-feel) he-core
%add
=/ =heart:h p.q.p.d
?~ replying.heart he-core
=/ op (~(get cur curios.heap) u.replying.heart)
?~ op he-core
=/ curio curio.u.op
=/ in-replies
%+ lien
~(tap in replied.curio)
|= =^time
=/ curio (~(get cur curios.heap) time)
?~ curio %.n
=(author.curio.u.curio our.bowl)
?: |(=(author.heart our.bowl) !in-replies) he-core
=/ content (trip (flatten content.curio))
=/ title
?: !=(title.heart ~) (need title.heart)
?: (lte (lent content) 80) (crip content)
(crip (weld (swag [0 77] content) "..."))
=/ yarn
%^ he-spin
/curio/(rsh 4 (scot %ui u.replying.heart))
:~ [%ship author.heart]
' commented on '
[%emph title]
': '
[%ship author.heart]
': '
(flatten content.heart)
==
~
=. cor (emit (pass-hark & & yarn))
he-core
==
::
%add-sects
=* p perm.heap
=. writers.p (~(uni in writers.p) p.d)
he-core
::
%del-sects
=* p perm.heap
=. writers.p (~(dif in writers.p) p.d)
he-core
::
%create
=. perm.heap p.d
he-core
::
%view
=. view.heap p.d
he-core
==
--
--