urbit/pkg/arvo/app/publish.hoon

502 lines
13 KiB
Plaintext
Raw Normal View History

2019-11-21 21:58:15 +03:00
::
/- *publish
/+ *server, *publish, cram, default-agent
2019-11-21 21:58:15 +03:00
::
/= index
/^ $-(json manx)
/: /===/app/publish/index /!noun/
::
/= js
/^ octs
/; as-octs:mimes:html
/| /: /===/app/publish/js/index /js/
/~ ~
==
::
/= css
/^ octs
/; as-octs:mimes:html
/| /: /===/app/publish/css/index /css/
/~ ~
==
::
/= tile-js
/^ octs
/; as-octs:mimes:html
/| /: /===/app/publish/js/tile /js/
/~ ~
==
::
/= images
/^ (map knot @)
/: /===/app/publish/img /_ /png/
::
|%
+$ card card:agent:gall
2019-11-21 21:58:15 +03:00
::
+$ comment
$: author=@p
date-created=@da
last-edit=@da
content=@t
2019-11-21 21:58:15 +03:00
==
::
+$ note
$: author=@p
title=@t
filename=@tas
date-created=@da
last-edit=@da
file=@t
build=(each manx tang)
comments=(map @da comment)
2019-11-21 21:58:15 +03:00
==
::
+$ notebook
$: title=@t
date-created=@da
last-note=@da
notes=(map @tas note)
order=(list @tas)
pinned=(set @tas)
==
2019-11-21 21:58:15 +03:00
::
+$ versioned-state
$% [%1 state-one]
==
::
+$ state-one
$: our-paths=(list path)
books=(map @tas notebook)
subs=(map [@p @tas] notebook)
recent=(list [@p @tas @tas])
unread=(set [@p @tas @tas])
==
2019-11-21 21:58:15 +03:00
--
::
=| state-one
2019-11-21 21:58:15 +03:00
=* state -
^- agent:gall
=<
|_ bol=bowl:gall
+* this .
def ~(. (default-agent this %|) bol)
main ~(. +> bol)
2019-11-21 21:58:15 +03:00
::
++ on-init
^- (quip card _this)
=/ lac [%launch-action %publish /publishtile '/~publish/tile.js']
=/ rav [%sing %t [%da now.bol] /app/publish/notebooks]
2019-11-21 21:58:15 +03:00
:_ this
:~ [%pass /bind %arvo %e %connect [~ /'~publish'] %publish]
[%pass /tile %agent [our.bol %launch] %poke %launch-action !>(lac)]
[%pass /read/paths %arvo %c %warp our.bol q.byk.bol `rav]
2019-11-21 21:58:15 +03:00
==
::
2019-11-21 21:58:15 +03:00
++ on-save !>(state)
::
2019-11-21 21:58:15 +03:00
++ on-load
|= old=vase
^- (quip card _this)
:: [~ this(state !<(,[%1 state-one] old))]
[~ this(state *state-one)]
2019-11-21 21:58:15 +03:00
::
++ on-poke
|= [mar=mark vas=vase]
2019-11-21 21:58:15 +03:00
^- (quip card _this)
?+ mar (on-poke:def mar vas)
%noun
~& state
[~ this]
::
%handle-http-request
=+ !<([id=@ta req=inbound-request:eyre] vas)
:_ this
%+ give-simple-payload:app id
%+ require-authorization:app req
|= req=inbound-request:eyre
^- simple-payload:http
not-found:gen
==
2019-11-21 21:58:15 +03:00
::
++ on-watch
|= =path
^- (quip card _this)
?+ path (on-watch:def path)
[%http-response *] [~ this]
:: [%tile ~]
==
2019-11-21 21:58:15 +03:00
::
++ on-leave
|= path
`this
2019-11-21 21:58:15 +03:00
::
++ on-peek
|= =path
~| "unexpected scry into {<dap.bol>} on path {<path>}"
!!
2019-11-21 21:58:15 +03:00
::
++ on-agent
|= [=wire =sign:agent:gall]
^- (quip card _this)
?- -.sign
%poke-ack
?~ p.sign
`this
%- (slog leaf+"poke failed from {<dap.bol>} on wire {<wire>}" u.p.sign)
`this
2019-11-21 21:58:15 +03:00
::
%watch-ack
?~ p.sign
`this
=/ =tank leaf+"subscribe failed from {<dap.bol>} on wire {<wire>}"
%- (slog tank u.p.sign)
`this
2019-11-21 21:58:15 +03:00
::
%kick `this
2019-11-21 21:58:15 +03:00
%fact
~| "unexpected subscription update to {<dap.bol>} on wire {<wire>}"
~| "with mark {<p.cage.sign>}"
!!
2019-11-21 21:58:15 +03:00
==
::
++ on-arvo
|= [wir=wire sin=sign-arvo]
2019-11-21 21:58:15 +03:00
^- (quip card _this)
?+ wir
(on-arvo:def wir sin)
::
[%read %paths ~]
?> ?=([?(%b %c) %writ *] sin)
=/ rot=riot:clay +>.sin
?> ?=(^ rot)
=^ cards state
(read-paths:main u.rot)
[cards this]
2019-11-21 21:58:15 +03:00
::
[%read %note *]
?> ?=([?(%b %c) %writ *] sin)
=/ rot=riot:clay +>.sin
=^ cards state
(read-note:main t.t.wir rot)
[cards this]
2019-11-21 21:58:15 +03:00
::
[%read %comment *]
?> ?=([?(%b %c) %writ *] sin)
=/ rot=riot:clay +>.sin
2019-11-21 21:58:15 +03:00
=^ cards state
(read-comment:main t.t.wir rot)
2019-11-21 21:58:15 +03:00
[cards this]
::
[%bind ~]
[~ this]
2019-11-21 21:58:15 +03:00
==
::
++ on-fail
|= [=term =tang]
%- (slog leaf+"error in {<dap.bol>}" >term< tang)
`this
2019-11-21 21:58:15 +03:00
--
::
|_ bol=bowl:gall
::
++ read-paths
|= ran=rant:clay
^- (quip card _state)
=/ rav [%next %t [%da now.bol] /app/publish/notebooks]
=/ new (filter-and-sort-paths !<((list path) q.r.ran))
=/ dif (diff-paths our-paths new)
=^ del-moves state (del-paths del.dif)
=^ add-moves state (add-paths add.dif)
::
=/ cards=(list card)
;: weld
[%pass /read/paths %arvo %c %warp our.bol q.byk.bol `rav]~
del-moves
add-moves
==
[cards state(our-paths new)]
::
++ read-note
|= [pax=path rot=riot:clay]
^- (quip card _state)
?> ?=([%app %publish %notebooks @ @ %udon ~] pax)
=/ book-name i.t.t.t.pax
=/ note-name i.t.t.t.t.pax
=/ book (~(get by books) book-name)
?~ book
[~ state]
=/ old-note (~(get by notes.u.book) note-name)
?~ old-note
[~ state]
?~ rot
[~ state]
=/ udon !<(@t q.r.u.rot)
=/ new-note=note (form-note note-name udon)
=. date-created.new-note date-created.u.old-note
=. comments.new-note comments.u.old-note
=. notes.u.book (~(put by notes.u.book) note-name new-note)
=/ rif=riff:clay [q.byk.bol `[%next %x [%da now.bol] pax]]
:- [%pass (welp /read/note pax) %arvo %c %warp our.bol rif]~
state(books (~(put by books) book-name u.book))
::
++ read-comment
|= [pax=path rot=riot:clay]
^- (quip card _state)
?> ?=([%app %publish %notebooks @ @ @ %publish-comment ~] pax)
=/ book-name i.t.t.t.pax
=/ note-name i.t.t.t.t.pax
=/ comment-date (slaw %da i.t.t.t.t.t.pax)
?~ comment-date
[~ state]
=/ book (~(get by books) book-name)
?~ book
[~ state]
=/ note (~(get by notes.u.book) note-name)
?~ note
[~ state]
=/ old-comment (~(get by comments.u.note) u.comment-date)
?~ old-comment
[~ state]
?~ rot
[~ state]
=/ new-comment !<(comment q.r.u.rot)
=. comments.u.note (~(put by comments.u.note) u.comment-date new-comment)
=. notes.u.book (~(put by notes.u.book) note-name u.note)
=/ rif=riff:clay [q.byk.bol `[%next %x [%da now.bol] pax]]
:- [%pass (welp /read/comment pax) %arvo %c %warp our.bol rif]~
state(books (~(put by books) book-name u.book))
::
++ filter-and-sort-paths
|= paths=(list path)
^- (list path)
%+ sort
%+ skim paths
|= pax=path
?| ?=([%app %publish %notebooks @ @ %udon ~] pax)
?=([%app %publish %notebooks @ @ @ %publish-comment ~] pax)
==
|= [a=path b=path]
^- ?
(lte (lent a) (lent b))
2019-11-21 21:58:15 +03:00
::
++ diff-paths
|= [old=(list path) new=(list path)]
^- [del=(list path) add=(list path)]
=/ del=(list path) (skim old |=(p=path ?=(~ (find [p]~ new))))
=/ add=(list path) (skim new |=(p=path ?=(~ (find [p]~ old))))
[del add]
2019-11-21 21:58:15 +03:00
::
++ del-paths
|= paths=(list path)
^- (quip card _state)
%+ roll paths
|= [pax=path cad=(list card) sty=_state]
?+ pax !!
[%app %publish %notebooks @ @ %udon ~]
=/ book-name i.t.t.t.pax
=/ note-name i.t.t.t.t.pax
=/ book (~(get by books.sty) book-name)
?~ book
[~ sty]
=. notes.u.book (~(del by notes.u.book) note-name)
:- ~
sty(books (~(put by books) book-name u.book))
::
[%app %publish %notebooks @ @ @ %publish-comment ~]
=/ book-name i.t.t.t.pax
=/ note-name i.t.t.t.t.pax
=/ comment-date (slaw %da i.t.t.t.t.t.pax)
?~ comment-date
[~ sty]
=/ book (~(get by books.sty) book-name)
?~ book
[~ sty]
=/ note (~(get by notes.u.book) note-name)
?~ note
[~ sty]
=. comments.u.note (~(del by comments.u.note) u.comment-date)
=. notes.u.book (~(put by notes.u.book) note-name u.note)
:- ~
sty(books (~(put by books.sty) book-name u.book))
==
2019-11-21 21:58:15 +03:00
::
++ add-paths
|= paths=(list path)
^- (quip card _state)
%+ roll paths
|= [pax=path cad=(list card) sty=_state]
?+ pax !!
[%app %publish %notebooks @ @ %udon ~]
=/ book-name i.t.t.t.pax
=/ note-name i.t.t.t.t.pax
=/ new-note=note (scry-note pax)
=/ book=notebook
%+ fall (~(get by books.sty) book-name)
[book-name now.bol now.bol ~ [note-name]~ ~]
=/ old-note (~(get by notes.book) note-name)
?^ old-note
=. date-created.new-note date-created.u.old-note
=. comments.new-note comments.u.old-note
=. notes.book (~(put by notes.book) note-name new-note)
=/ rif=riff:clay [q.byk.bol `[%next %x [%da now.bol] pax]]
:- [%pass (welp /read/note pax) %arvo %c %warp our.bol rif]~
sty(books (~(put by books.sty) book-name book))
::
=/ comment-dir /app/publish/notebooks/[book-name]/[note-name]
=/ comment-paths .^((list path) %ct (weld our-beak comment-dir))
=+ ^- [cards=(list card) new-comments=(map @da comment)]
%+ roll comment-paths
|= [pax=path cad=(list card) com=(map @da comment)]
?. ?=([%app %publish %notebooks @ @ @ %publish-comment ~] pax)
[cad com]
=/ comment-name (slaw %da i.t.t.t.t.t.pax)
?~ comment-name
[cad com]
=/ new-com .^(comment %cx (welp our-beak pax))
=/ rif=riff:clay [q.byk.bol `[%next %x [%da now.bol] pax]]
:- [[%pass (welp /read/comment pax) %arvo %c %warp our.bol rif] cad]
(~(put by com) u.comment-name new-com)
=. comments.new-note new-comments
=. notes.book (~(put by notes.book) note-name new-note)
=/ rif=riff:clay [q.byk.bol `[%next %x [%da now.bol] pax]]
:- [[%pass (welp /read/note pax) %arvo %c %warp our.bol rif] cards]
sty(books (~(put by books.sty) book-name book))
::
[%app %publish %notebooks @ @ @ %publish-comment ~]
=/ book-name i.t.t.t.pax
=/ note-name i.t.t.t.t.pax
=/ comment-name (slaw %da i.t.t.t.t.t.pax)
=/ book (~(get by books.sty) book-name)
?~ book
[~ sty]
=/ note (~(get by notes.u.book) note-name)
?~ note
[~ sty]
?~ comment-name
[~ sty]
=/ new-com .^(comment %cx (welp our-beak pax))
=. comments.u.note (~(put by comments.u.note) u.comment-name new-com)
=. notes.u.book (~(put by notes.u.book) note-name u.note)
=/ rif=riff:clay [q.byk.bol `[%next %x [%da now.bol] pax]]
:- [%pass (welp /read/comment pax) %arvo %c %warp our.bol rif]~
sty(books (~(put by books.sty) book-name u.book))
==
2019-11-21 21:58:15 +03:00
::
++ scry-note
|= pax=path
^- note
?> ?=([%app %publish %notebooks @ @ %udon ~] pax)
=/ note-name i.t.t.t.t.pax
=/ udon=@t .^(@t %cx (welp our-beak pax))
(form-note note-name udon)
::
++ form-note
|= [note-name=@tas udon=@t]
^- note
=/ build=(each manx tang)
%- mule |.
^- manx
elm:(static:cram (ream udon))
::
=/ meta=(each (map term knot) tang)
%- mule |.
%- ~(run by inf:(static:cram (ream udon)))
|= a=dime ^- cord
?+ (end 3 1 p.a) (scot a)
%t q.a
==
::
=/ author=@p our.bol
=? author ?=(%.y -.meta)
%+ fall
(biff (~(get by p.meta) %author) (slat %p))
our.bol
::
=/ title=@t note-name
=? title ?=(%.y -.meta)
(fall (~(get by p.meta) %title) note-name)
::
:* author
title
note-name
now.bol
now.bol
udon
build
~
==
::
++ our-beak /(scot %p our.bol)/[q.byk.bol]/(scot %da now.bol)
2019-11-21 21:58:15 +03:00
::
++ allowed
|= [who=@p mod=?(%read %write) pax=path]
^- ?
=. pax (weld our-beak pax)
=/ pem=[dict:clay dict:clay] .^([dict:clay dict:clay] %cp pax)
?- mod
%read (allowed-by who -.pem)
%write (allowed-by who +.pem)
==
:: +allowed-by: checks if ship :who is allowed by the permission rules in :dic
::
++ allowed-by
|= [who=@p dic=dict:clay]
^- ?
?: =(who our.bol) &
=/ in-list=?
?| (~(has in p.who.rul.dic) who)
::
%- ~(rep by q.who.rul.dic)
|= [[@ta cru=crew:clay] out=_|]
?: out &
(~(has in cru) who)
==
?: =(%black mod.rul.dic)
!in-list
in-list
::
++ write-file
=, space:userlib
|= [pax=path cay=cage]
2020-01-04 00:06:42 +03:00
^- card
2019-11-21 21:58:15 +03:00
=. pax (weld our-beak pax)
[%pass (weld /write pax) %arvo %c %info (foal pax cay)]
2019-11-21 21:58:15 +03:00
::
++ delete-file
=, space:userlib
|= pax=path
^- card
=. pax (weld our-beak pax)
[%pass (weld /delete pax) %arvo %c %info (fray pax)]
2019-11-21 21:58:15 +03:00
::
++ update-udon-front
|= [fro=(map knot cord) udon=@t]
^- @t
%- of-wain:format
=/ tum (trip udon)
=/ id (find ";>" tum)
?~ id
%+ weld (front-to-wain fro)
(to-wain:format (crip (weld ";>\0a" tum)))
%+ weld (front-to-wain fro)
(to-wain:format (crip (slag u.id tum)))
::
++ front-to-wain
|= a=(map knot cord)
^- wain
=/ entries=wain
%+ turn ~(tap by a)
|= b=[knot cord]
=/ c=[term cord] (,[term cord] b)
(crip " [{<-.c>} {<+.c>}]")
::
?~ entries ~
;: weld
[':- :~' ~]
entries
[' ==' ~]
==
::
--