urbit/pkg/garden/app/hark-store.hoon

542 lines
13 KiB
Plaintext
Raw Normal View History

:: hark-store: notifications and unread counts [landscape]
2020-10-21 08:54:59 +03:00
::
:: hark-store can store unread counts differently, depending on the
:: resource.
:: - last seen. This way, hark-store simply stores an index into
:: graph-store, which represents the last "seen" item, useful for
:: high-volume applications which are intrinsically time-ordered. i.e.
:: chats, comments
:: - each. Hark-store will store an index for each item that is unread.
:: Usefull for non-linear, low-volume applications, i.e. blogs,
:: collections
::
/- store=hark-store
/+ verb, dbug, default-agent, re=hark-unreads, agentio
2020-10-21 08:54:59 +03:00
::
::
~% %hark-store-top ..part ~
2020-10-21 08:54:59 +03:00
|%
+$ card card:agent:gall
+$ versioned-state
$% state-2
state-3
state-4
2021-03-02 05:43:51 +03:00
state-5
2021-03-18 10:02:39 +03:00
state-6
state-7
state-8
state-9
2020-10-21 08:54:59 +03:00
==
::
2021-03-18 10:02:39 +03:00
+$ base-state
$: places=(map place:store stats:store)
2021-09-17 03:44:35 +03:00
seen=timebox:store
unseen=timebox:store
=archive:store
half-open=(map bin:store @da)
2020-10-21 08:54:59 +03:00
==
2020-12-16 07:22:23 +03:00
::
+$ state-2
[%2 *]
::
+$ state-3
[%3 *]
::
+$ state-4
[%4 *]
::
2021-03-02 05:43:51 +03:00
+$ state-5
[%5 *]
2021-03-18 10:02:39 +03:00
::
+$ state-6
[%6 *]
::
+$ state-7
[%7 *]
2021-03-02 05:43:51 +03:00
::
+$ state-8
[%8 base-state]
::
+$ state-9
[%9 base-state]
::
::
+$ cached-state
2021-09-17 03:44:35 +03:00
$: by-place=(jug place:store [=lid:store =path])
~
==
+$ inflated-state
[state-9 cached-state]
2020-10-21 08:54:59 +03:00
::
2020-11-16 06:52:45 +03:00
++ orm ((ordered-map @da timebox:store) gth)
2020-10-21 08:54:59 +03:00
--
::
=| inflated-state
2020-10-21 08:54:59 +03:00
=* state -
::
=<
2021-09-29 07:48:27 +03:00
%+ verb |
2020-10-21 08:54:59 +03:00
%- agent:dbug
^- agent:gall
~% %hark-store-agent ..card ~
|_ =bowl:gall
+* this .
ha ~(. +> bowl)
def ~(. (default-agent this %|) bowl)
io ~(. agentio bowl)
pass pass:io
2020-10-21 08:54:59 +03:00
::
++ on-init
`this
2020-10-21 08:54:59 +03:00
::
++ on-save !>(-.state)
2020-10-21 08:54:59 +03:00
++ on-load
|= =old=vase
=/ old
!<(versioned-state old-vase)
=| cards=(list card)
|- ^- (quip card _this)
?+ -.old
:: pre-dist migration
:_ this
(poke-our:pass %hark-graph-hook hark-graph-migrate+old-vase)^~
::
%9
=. -.state old
2021-10-13 19:54:32 +03:00
=. +.state inflate:ha
:_(this (flop cards))
::
%8
$(-.old %9, archive.old *archive:store)
==
::
++ on-watch
|= =path
^- (quip card _this)
?> (team:title [src our]:bowl)
|^
?+ path (on-watch:def path)
[%notes ~] `this
::
[%updates ~]
:_ this
[%give %fact ~ hark-update+!>(initial-updates)]~
::
==
2020-11-10 06:32:48 +03:00
::
++ initial-updates
^- update:store
:- %more
^- (list update:store)
2021-09-17 03:44:35 +03:00
:~ [%timebox unseen+~ ~(val by unseen)]
[%timebox seen+~ ~(val by seen)]
[%all-stats places]
==
--
2020-10-21 08:54:59 +03:00
::
++ on-peek
|= =path
^- (unit (unit cage))
?+ path (on-peek:def path)
::
[%x %recent %inbox @ @ ~]
=/ date=@da
(slav %ud i.t.t.t.path)
2020-11-16 06:53:18 +03:00
=/ length=@ud
(slav %ud i.t.t.t.t.path)
:^ ~ ~ %hark-update
!> ^- update:store
:- %more
2021-09-17 03:44:35 +03:00
%+ turn (tab:orm archive `date length)
|= [time=@da =timebox:store]
^- update:store
2021-09-17 03:44:35 +03:00
[%timebox archive+time ~(val by timebox)]
==
2020-11-10 06:32:48 +03:00
::
2020-10-21 08:54:59 +03:00
++ on-poke
~/ %hark-store-poke
|= [=mark =vase]
^- (quip card _this)
|^
?> (team:title our.bowl src.bowl)
=^ cards state
?+ mark (on-poke:def mark vase)
%hark-action (hark-action !<(action:store vase))
%noun (poke-noun !<(* vase))
2020-10-21 08:54:59 +03:00
==
[cards this]
::
++ poke-noun
|= val=*
?+ val ~|(%bad-noun-poke !!)
%print ~&(+.state [~ state])
2021-09-17 03:44:35 +03:00
%clear [~ state(. *inflated-state)]
%sane
2021-10-13 19:54:32 +03:00
~& +.state
~& inflate
?>(=(+.state inflate) `state)
==
::
++ poke-us
|= =action:store
^- card
[%pass / %agent [our dap]:bowl %poke hark-action+!>(action)]
::
2020-10-21 08:54:59 +03:00
++ hark-action
|= =action:store
^- (quip card _state)
abet:(abed:poke-engine:ha action)
2020-10-21 08:54:59 +03:00
--
::
++ on-agent on-agent:def
::
++ on-leave on-leave:def
++ on-arvo
|= [=wire =sign-arvo]
^- (quip card _this)
?. ?=([%autoseen ~] wire)
(on-arvo:def wire sign-arvo)
2021-05-17 06:35:00 +03:00
`this
2020-10-21 08:54:59 +03:00
::
++ on-fail on-fail:def
--
|_ =bowl:gall
+* met ~(. metadata bowl)
io ~(. agentio bowl)
pass pass:io
++ poke-engine
|_ [out=(list update:store) cards=(list card)]
++ poke-core .
::
++ abed
|= in=action:store
2021-09-17 03:44:35 +03:00
^+ poke-core
?- -.in
::
%add-note (add-note +.in)
%del-place (del-place +.in)
%archive (do-archive +.in)
::
%unread-count (unread-count +.in)
%read-count (read-count +.in)
::
%read-each (read-each +.in)
%unread-each (unread-each +.in)
::
%read-note (read-note +.in)
::
2021-09-17 03:44:35 +03:00
%saw-place (saw-place +.in)
::
2021-09-17 03:44:35 +03:00
%opened opened
%archive-all archive-all
::
==
::
++ abet
^- (quip card _state)
:_ state
%+ snoc (flop cards)
[%give %fact ~[/updates] %hark-update !>([%more (flop out)])]
::
++ give |=(=update:store poke-core(out [update out]))
++ emit |=(=card poke-core(cards [card cards]))
::
::
:: +| %note
::
:: notification tracking
++ put-notifs
|= [time=@da =timebox:store]
2021-09-17 03:44:35 +03:00
poke-core(archive (put:orm archive time timebox))
::
++ put-lid
|= [=lid:store =bin:store =notification:store]
^+ poke-core
=. by-place (~(put ju by-place) place.bin [lid path.bin])
?- -.lid
%seen
poke-core(seen (~(put by seen) bin notification))
::
%unseen
poke-core(unseen (~(put by unseen) bin notification))
::
%archive
poke-core(archive (~(put re archive) time.lid bin notification))
==
::
++ del-lid
|= [=lid:store =bin:store]
=. by-place (~(del ju by-place) place.bin [lid path.bin])
?- -.lid
%seen poke-core(seen (~(del by seen) bin))
%unseen poke-core(unseen (~(del by unseen) bin))
%archive poke-core(archive (~(del re archive) time.lid bin))
==
::
++ add-note
|= [=bin:store =body:store]
^+ poke-core
=. poke-core
(emit (fact:io hark-update+!>([%add-note bin body]) /notes ~))
=/ existing-notif
2021-09-17 03:44:35 +03:00
(~(gut by unseen) bin *notification:store)
=/ new=notification:store
2021-09-17 03:44:35 +03:00
[now.bowl bin [body body.existing-notif]]
=. poke-core
(put-lid unseen/~ bin new)
(give %added new)
::
++ del-place
|= =place:store
2021-09-17 03:44:35 +03:00
=. poke-core (give %del-place place)
=/ notes=(list [=lid:store =path])
~(tap in (~(get ju by-place) place))
|- ^+ poke-core
?~ notes poke-core
2021-09-17 03:44:35 +03:00
=, i.notes
=. poke-core
(del-lid lid path place)
$(notes t.notes)
::
++ do-archive
2021-09-17 03:44:35 +03:00
|= [=lid:store =bin:store]
^+ poke-core
2021-09-17 03:44:35 +03:00
~| %already-archived
?< ?=(%time -.lid)
~| %non-existent
=/ =notification:store (need (get-lid lid bin))
=. poke-core (del-lid lid bin)
=. poke-core (put-lid archive+now.bowl bin notification)
(give %archived now.bowl lid notification)
2021-05-17 06:35:00 +03:00
::
++ read-note
|= =bin:store
2021-05-17 06:35:00 +03:00
=/ =notification:store
2021-09-17 03:44:35 +03:00
(~(got by unseen) bin)
=. poke-core
(del-lid unseen/~ bin)
2021-05-17 06:35:00 +03:00
=/ =time
(fall timebox:(gut-place place.bin) now.bowl)
=. date.notification time
=. poke-core
(put-lid archive/time bin notification)
(give %note-read time bin)
::
::
:: +| %each
::
:: each unread tracking
::
++ unread-each
|= [=place:store =path]
2021-09-17 03:44:35 +03:00
=. poke-core (saw-place place ~)
=. poke-core (give %unread-each place path)
%+ jub-place place
|=(=stats:store stats(each (~(put in each.stats) path)))
::
++ read-index-each
|= [=place:store =path]
%- read-bins
%+ skim
2021-09-17 03:44:35 +03:00
~(tap in ~(key by unseen))
|= =bin:store
?. =(place place.bin) %.n
=/ not=notification:store
2021-09-17 03:44:35 +03:00
(~(got by unseen) bin)
(lien body.not |=(=body:store =(binned.body path)))
::
++ read-each
|= [=place:store =path]
=. poke-core (read-index-each place path)
=. poke-core (give %read-each place path)
%+ jub-place place
|= =stats:store
%_ stats
timebox `now.bowl
each (~(del in each.stats) path)
==
::
++ gut-place
|= =place:store
?: (~(has by places) place) (~(got by places) place)
=| def=stats:store
def(timebox ~, last now.bowl)
::
++ jub-place
|= $: =place:store
f=$-(stats:store stats:store)
==
^+ poke-core
=/ =stats:store
(gut-place place)
poke-core(places (~(put by places) place (f stats)))
::
++ unread-count
|= [=place:store inc=? count=@ud]
=. poke-core
(give %unread-count place inc count)
2021-09-17 03:44:35 +03:00
=. poke-core (saw-place place ~)
=/ f
?: inc (cury add count)
(curr sub count)
%+ jub-place place
|= =stats:store
stats(count (f count.stats))
::
2021-09-17 03:44:35 +03:00
++ half-archive
|= =place:store
=/ bins=(list [=lid:store =path])
~(tap in (~(get ju by-place) place))
|-
?~ bins poke-core
=/ =bin:store
[path.i.bins place]
=* lid lid.i.bins
?: ?=(%archive -.lid)
$(bins t.bins)
=/ seen-place (~(get by seen) bin)
=/ n=(unit notification:store) (get-lid lid bin)
?~ n $(bins t.bins)
=* note u.n
=/ =time (~(gut by half-open) bin now.bowl)
=? half-open !(~(has by half-open) bin)
(~(put by half-open) bin now.bowl)
2021-10-13 19:54:32 +03:00
=/ existing (get-lid archive/time bin)
=/ new (merge-notification existing note)
=. poke-core
(put-lid archive/time bin new)
=. poke-core (del-lid lid bin)
2021-09-17 03:44:35 +03:00
=. poke-core (give %archived now.bowl unseen+~ (~(got re archive) time bin))
=. poke-core (give %archived now.bowl seen+~ (~(got re archive) time bin))
$(bins t.bins)
::
++ read-count
|= =place:store
=. poke-core (give %read-count place)
2021-09-17 03:44:35 +03:00
=. poke-core (half-archive place)
%+ jub-place place
|= =stats:store
stats(count 0, timebox `now.bowl)
::
++ read-bins
|= bins=(list bin:store)
2020-12-16 10:37:10 +03:00
|-
?~ bins poke-core
2020-12-16 10:37:10 +03:00
=/ core
(read-note i.bins)
$(poke-core core, bins t.bins)
::
2021-09-17 03:44:35 +03:00
++ saw-place
|= [=place:store time=(unit time)]
2021-09-17 03:44:35 +03:00
=. poke-core (give %saw-place place time)
%+ jub-place place
|=(=stats:store stats(last (fall time now.bowl)))
2020-12-17 09:10:26 +03:00
::
2021-09-17 03:44:35 +03:00
++ archive-seen
=/ seen=(list [=bin:store =notification:store]) ~(tap by seen)
poke-core
::
++ opened
=. poke-core (turn-places |=(=stats:store stats(timebox ~)))
=. poke-core (give %opened ~)
%+ roll ~(tap in ~(key by unseen))
|= [=bin:store out=_poke-core]
(opened-note:out bin)
::
++ opened-note
|= =bin:store
^+ poke-core
=/ old
(~(got by unseen) bin)
=. poke-core
(del-lid unseen/~ bin)
=/ se (~(get by seen) bin)
%^ put-lid seen/~ bin
(merge-notification se old)
2021-09-17 03:44:35 +03:00
::
++ archive-all
2021-09-20 07:15:19 +03:00
|^
=. poke-core (archive-lid unseen/~ unseen)
(archive-lid seen/~ seen)
++ archive-lid
|= [=lid:store =timebox:store]
2021-09-20 07:51:01 +03:00
%+ roll ~(tap in ~(key by timebox))
2021-09-20 07:15:19 +03:00
|= [=bin:store out=_poke-core]
(do-archive:out lid bin)
--
::
++ turn-places
|= f=$-(stats:store stats:store)
=/ places ~(tap in ~(key by places))
|- ^+ poke-core
?~ places poke-core
=/ core=_poke-core (jub-place i.places f)
$(poke-core core, places t.places)
--
2020-10-21 08:54:59 +03:00
::
2021-09-17 03:44:35 +03:00
++ get-lid
|= [=lid:store =bin:store]
2021-10-13 19:54:32 +03:00
=; =timebox:store
(~(get by timebox) bin)
?- -.lid
%unseen unseen
%seen seen
%archive (fall (get:orm archive time.lid) *timebox:store)
==
2021-09-17 03:44:35 +03:00
::
2020-10-21 08:54:59 +03:00
++ merge-notification
|= [existing=(unit notification:store) new=notification:store]
2020-10-21 08:54:59 +03:00
^- notification:store
?~ existing new
2021-09-17 03:44:35 +03:00
[(max date.u.existing date.new) bin.new (welp body.new body.u.existing)]
2020-10-21 08:54:59 +03:00
::
:: +key-orm: +key:by for ordered maps
++ key-orm
2021-09-17 03:44:35 +03:00
|= =archive:store
^- (list @da)
2021-09-17 03:44:35 +03:00
(turn (tap:orm archive) |=([@da *] +<-))
::
2020-10-22 04:30:37 +03:00
:: +gut-orm: +gut:by for ordered maps
:: TODO: move to zuse.hoon
2020-10-21 08:54:59 +03:00
++ gut-orm
2021-09-17 03:44:35 +03:00
|= [=archive:store time=@da]
2020-10-21 08:54:59 +03:00
^- timebox:store
2021-09-17 03:44:35 +03:00
(fall (get:orm archive time) ~)
2020-10-21 08:54:59 +03:00
::
::
++ scry
|* [=mold p=path]
?> ?=(^ p)
?> ?=(^ t.p)
.^(mold i.p (scot %p our.bowl) i.t.p (scot %da now.bowl) t.t.p)
::
2020-10-21 08:54:59 +03:00
++ give
|= [paths=(list path) update=update:store]
^- (list card)
[%give %fact paths [%hark-update !>(update)]]~
::
++ tap-nonempty
2021-09-17 03:44:35 +03:00
|= =archive:store
^- (list [@da timebox:store])
2021-09-17 03:44:35 +03:00
%+ skim (tap:orm archive)
|=([@da =timebox:store] !=(~(wyt by timebox) 0))
2021-10-13 19:54:32 +03:00
::
++ inflate
=. by-place ~
=. by-place (index-timebox seen+~ seen by-place)
=. by-place (index-timebox unseen+~ unseen by-place)
=. by-place
%+ roll (tap:orm archive)
|= [[=time =timebox:store] out=_by-place]
(index-timebox archive/time timebox out)
+.state
::
++ index-timebox
|= [=lid:store =timebox:store out=_by-place]
^+ by-place
%+ roll ~(tap by timebox)
|= [[=bin:store =notification:store] out=_out]
(~(put ju out) place.bin [lid path.bin])
2020-10-21 08:54:59 +03:00
--