chat: add history functionality. optionally request backlog.

This commit is contained in:
Logan Allen 2019-11-13 16:53:47 -08:00
parent a480e3aafd
commit 9cacb12dbc
9 changed files with 234 additions and 83 deletions

View File

@ -39,12 +39,14 @@
[%say letter] :: send message
[%eval cord hoon] :: send #-message
::
[%create chat-security path (unit glyph)] :: create chat
::
:: create chat
[%create chat-security path (unit glyph) (unit ?)]
[%delete path] :: delete chat
[%invite ?(%r %w %rw) path (set ship)] :: allow
[%banish ?(%r %w %rw) path (set ship)] :: disallow
::
[%join target (unit glyph)] :: join target
[%join target (unit glyph) (unit ?)] :: join target
[%leave target] :: nuke target
::
[%bind glyph target] :: bind glyph
@ -363,13 +365,20 @@
;~ (glue ace)
(tag %create)
security
;~(plug path (punt ;~(pfix ace glyph)))
path
(punt ;~(pfix ace glyph))
(punt ;~(pfix ace ;~(pose (cold %| bar) (cold %& pad))))
==
;~((glue ace) (tag %delete) path)
;~((glue ace) (tag %invite) rw path ships)
;~((glue ace) (tag %banish) rw path ships)
::
;~((glue ace) (tag %join) ;~(plug targ (punt ;~(pfix ace glyph))))
;~ (glue ace)
(tag %join)
targ
(punt ;~(pfix ace glyph))
(punt ;~(pfix ace ;~(pose (cold %| bar) (cold %& pad))))
==
;~((glue ace) (tag %leave) targ)
::
;~((glue ace) (tag %bind) glyph targ)
@ -602,7 +611,7 @@
:: +create: new local mailbox
::
++ create
|= [security=chat-security =path gyf=(unit char)]
|= [security=chat-security =path gyf=(unit char) allow-history=(unit ?)]
^- (quip move _this)
::TODO check if already exists
=/ =target [our-self path]
@ -613,18 +622,22 @@
=- [[- moz] this]
%^ act %do-create %chat-view
:- %chat-view-action
:^ %create path security
:: ensure we can read from/write to our own chats
::
:- :: read
:* %create
path
security
:: ensure we can read from/write to our own chats
::
:: read
?- security
?(%channel %journal) ~
?(%village %mailbox) [our-self ~ ~]
==
:: write
?- security
?(%channel %mailbox) ~
?(%village %journal) [our-self ~ ~]
:: write
?- security
?(%channel %mailbox) ~
?(%village %journal) [our-self ~ ~]
==
?~(allow-history %.y u.allow-history)
==
:: +delete: delete local chats
::
@ -680,7 +693,7 @@
:: +join: sync with remote mailbox
::
++ join
|= [=target gyf=(unit char)]
|= [=target gyf=(unit char) ask-history=(unit ?)]
^- (quip move _this)
=^ moz this
?. ?=(^ gyf) [~ this]
@ -692,7 +705,7 @@
:: gives ugly %chat-hook-reap
%^ act %do-join %chat-view
:- %chat-view-action
[%join target]
[%join ship.target path.target ?~(ask-history %.y u.ask-history)]
:: +leave: unsync & destroy mailbox
::
::TODO allow us to "mute" local chats using this

View File

@ -15,9 +15,10 @@
[%peer wire dock path]
==
::
+$ state-both
+$ versioned-state
$% state-zero
state-one
state-two
==
::
+$ state-zero
@ -33,22 +34,29 @@
invite-created=_|
==
::
+$ state-two
$: %2
synced=(map path ship)
boned=(map wire (list bone))
invite-created=_|
allow-history=(map path ?)
==
::
+$ poke
$% [%chat-action chat-action]
[%permission-action permission-action]
[%invite-action invite-action]
[%chat-view-action chat-view-action]
==
::
--
::
|_ [bol=bowl:gall state-one]
|_ [bol=bowl:gall state-two]
::
++ this .
::
++ prep
|= old=(unit state-both)
^- (quip move _this)
|= old=(unit versioned-state)
|^ ^- (quip move _this)
?~ old
:_ this(invite-created %.y)
:~ (invite-poke [%create /chat])
@ -56,19 +64,32 @@
[ost.bol %peer /permissions [our.bol %permission-store] /updates]
==
?- -.u.old
%1 [~ this(+<+ u.old)]
%2 [~ this(+<+ u.old)]
%1 [~ (migrate-state synced.u.old boned.u.old)]
::
%0
=/ sta *state-one
=: boned.sta boned.u.old
synced.sta synced.u.old
invite-created %.y
==
:_ this(+<+ sta)
:_ (migrate-state synced.u.old boned.u.old)
:~ (invite-poke [%create /chat])
[ost.bol %peer /invites [our.bol %invite-store] /invitatory/chat]
==
==
::
++ migrate-state
|= [synced=(map path ship) boned=(map wire (list bone))]
^- _this
=/ sta *state-two
=: boned.sta boned
synced.sta synced
allow-history.sta (create-allow-history synced)
invite-created %.y
==
this(+<+ sta)
::
++ create-allow-history
|= synced=(map path ship)
^- (map path ?)
(~(run by synced) |=(shp=ship %.n))
--
::
++ poke-json
|= jon=json
@ -110,7 +131,9 @@
=/ chat-path [%mailbox path.act]
?: (~(has by synced) path.act)
[~ this]
=. synced (~(put by synced) path.act our.bol)
=: synced (~(put by synced) path.act our.bol)
allow-history (~(put by allow-history) path.act allow-history.act)
==
:_ (track-bone chat-path)
%+ weld
[ost.bol %peer chat-path [our.bol %chat-store] chat-path]~
@ -118,12 +141,13 @@
::
%add-synced
?> (team:title our.bol src.bol)
=/ chat-path [%mailbox (scot %p ship.act) path.act]
=/ chat-path=path [%mailbox (scot %p ship.act) path.act]
?: (~(has by synced) [(scot %p ship.act) path.act])
[~ this]
=. synced (~(put by synced) [(scot %p ship.act) path.act] ship.act)
=/ history=path ?:(ask-history.act /0 /~)
:_ (track-bone chat-path)
[ost.bol %peer chat-path [ship.act %chat-hook] chat-path]~
[ost.bol %peer chat-path [ship.act %chat-hook] (weld chat-path history)]~
::
%remove
=/ ship (~(get by synced) path.act)
@ -157,6 +181,13 @@
++ peer-mailbox
|= pax=path
^- (quip move _this)
?> ?=(^ pax)
=/ last (dec (lent pax))
=/ backlog-start=(unit @ud)
%+ rush
(snag last `(list @ta)`pax)
dem:ag
=> .(pax `path`(oust [last last] `(list @ta)`pax))
?> ?=([* ^] pax)
?> (~(has by synced) pax)
:: scry permissions to check if read is permitted
@ -164,7 +195,47 @@
=/ box (chat-scry pax)
?~ box !!
:_ this
[ost.bol %diff %chat-update [%create (slav %p i.pax) pax]]~
:- [ost.bol %diff %chat-update [%create (slav %p i.pax) pax]]
?:
?&
?=(^ backlog-start)
(~(got by allow-history) pax)
==
(paginate-messages pax u.box u.backlog-start)
~
::
++ paginate-messages
|= [=path =mailbox start=@ud]
^- (list move)
=/ moves=(list move) ~
=/ end (lent envelopes.mailbox)
?: |((gte start end) =(end 0))
moves
=. envelopes.mailbox (slag start `(list envelope)`envelopes.mailbox)
|- ^- (list move)
?~ envelopes.mailbox
moves
?: (lte end 500)
=. moves
%+ snoc moves
%- messages-move
[path start (lent envelopes.mailbox) envelopes.mailbox]
$(envelopes.mailbox ~)
=. moves
%+ snoc moves
%- messages-move
:^ path start
(add start 500)
(scag 500 `(list envelope)`envelopes.mailbox)
=: start (add start 500)
end (sub end 500)
==
$(envelopes.mailbox (slag 500 `(list envelope)`envelopes.mailbox))
::
++ messages-move
|= [=path start=@ud end=@ud envelopes=(list envelope)]
^- move
[ost.bol %diff %chat-update [%messages path start end envelopes]]
::
++ diff-invite-update
|= [wir=wire diff=invite-update]
@ -173,8 +244,12 @@
[~ this]
::
%accepted
=/ ask-history
?~ (chat-scry [(scot %p ship.invite.diff) path.invite.diff])
%.y
%.n
:_ this
[(chat-view-poke [%join ship.invite.diff path.invite.diff])]~
[(chat-view-poke [%join ship.invite.diff path.invite.diff ask-history])]~
==
::
++ diff-permission-update
@ -222,10 +297,10 @@
|= diff=chat-update
^- (quip move _this)
?- -.diff
%keys [~ this]
%config [~ this]
%create [~ this]
%read [~ this]
%keys [~ this]
%config [~ this]
%create [~ this]
%read [~ this]
%delete
?. (~(has by synced) path.diff)
[~ this]
@ -238,6 +313,13 @@
|= [=bone *]
^- move
[bone %diff [%chat-update diff]]
::
%messages
:_ this
%+ turn (prey:pubsub:userlib [%mailbox path.diff] bol)
|= [=bone *]
^- move
[bone %diff [%chat-update diff]]
==
::
++ handle-foreign
@ -263,7 +345,7 @@
?. =(u.shp src.bol)
[~ this]
:_ this(synced (~(del by synced) path.diff))
:- (chat-poke diff)
:- (chat-poke [%delete path.diff])
[ost.bol %pull [%mailbox path.diff] [src.bol %chat-hook] ~]~
::
%message
@ -272,7 +354,15 @@
=/ shp (~(get by synced) path.diff)
?~ shp ~
?. =(src.bol u.shp) ~
[(chat-poke diff)]~
[(chat-poke [%message path.diff envelope.diff])]~
::
%messages
:_ this
?> ?=([* ^] path.diff)
=/ shp (~(get by synced) path.diff)
?~ shp ~
?. =(src.bol u.shp) ~
[(chat-poke [%messages path.diff envelopes.diff])]~
==
::
++ quit
@ -286,9 +376,13 @@
?. (~(has by synced) t.wir)
:: no-op
[~ this]
=/ mailbox (chat-scry t.wir)
?~ mailbox [~ this]
~& %chat-hook-resubscribe
=/ pax=path (weld wir /(scot %ud (lent envelopes.u.mailbox)))
~& pax
:_ (track-bone wir)
[ost.bol %peer wir [(slav %p i.t.wir) %chat-hook] wir]~
[ost.bol %peer wir [(slav %p i.t.wir) %chat-hook] pax]~
::
++ reap
|= [wir=wire saw=(unit tang)]

View File

@ -154,10 +154,11 @@
^- (quip move _this)
?> (team:title our.bol src.bol)
?- -.action
%create (handle-create action)
%delete (handle-delete action)
%message (handle-message action)
%read (handle-read action)
%create (handle-create action)
%delete (handle-delete action)
%message (handle-message action)
%messages (handle-messages action)
%read (handle-read action)
==
::
++ handle-create
@ -198,6 +199,35 @@
:- (send-diff path.act act)
this(inbox (~(put by inbox) path.act u.mailbox))
::
++ handle-messages
|= act=chat-action
^- (quip move _this)
?> ?=(%messages -.act)
=/ mailbox=(unit mailbox) (~(get by inbox) path.act)
?~ mailbox
[~ this]
=/ diff=chat-update
:* %messages
path.act
+(length.config.u.mailbox)
(add length.config.u.mailbox (lent envelopes.act))
envelopes.act
==
|- ^- (quip move _this)
?~ envelopes.act
[(send-diff path.act diff) this]
=* envelope i.envelopes.act
=* letter letter.envelope
=? letter &(?=(%code -.letter) ?=(~ output.letter))
=/ =hoon (ream expression.letter)
letter(output (eval bol hoon))
=: length.config.u.mailbox +(length.config.u.mailbox)
number.envelope +(length.config.u.mailbox)
envelopes.u.mailbox (snoc envelopes.u.mailbox envelope)
==
=. inbox (~(put by inbox) path.act u.mailbox)
$(envelopes.act t.envelopes.act)
::
++ handle-read
|= act=chat-action
^- (quip move _this)
@ -210,25 +240,25 @@
this(inbox (~(put by inbox) path.act u.mailbox))
::
++ update-subscribers
|= [pax=path act=chat-action]
|= [pax=path upd=chat-update]
^- (list move)
%+ turn (prey:pubsub:userlib pax bol)
|= [=bone *]
[bone %diff %chat-update act]
[bone %diff %chat-update upd]
::
++ send-diff
|= [pax=path act=chat-action]
|= [pax=path upd=chat-update]
^- (list move)
%- zing
:~ (update-subscribers /all act)
(update-subscribers /updates act)
(update-subscribers [%mailbox pax] act)
?. |(=(%read -.act) =(%message -.act))
:~ (update-subscribers /all upd)
(update-subscribers /updates upd)
(update-subscribers [%mailbox pax] upd)
?. |(|(=(%read -.upd) =(%message -.upd)) =(%messages -.upd))
~
(update-subscribers /configs act)
?. |(=(%create -.act) =(%delete -.act))
(update-subscribers /configs upd)
?. |(=(%create -.upd) =(%delete -.upd))
~
(update-subscribers /keys act)
(update-subscribers /keys upd)
==
::
--

View File

@ -129,9 +129,8 @@
%http-response
%- json-response:app
%- json-to-octs
%+ envelopes-update
envelopes
[start end pax]
%- update-to-json
[%messages pax start end envelopes]
==
::
:: inbox page
@ -155,7 +154,6 @@
[~ this]
?- -.act
%create
:: TODO: add invites
=/ pax [(scot %p our.bol) path.act]
=/ group-read=path [%chat (weld pax /read)]
=/ group-write=path [%chat (weld pax /write)]
@ -166,7 +164,7 @@
(group-poke [%add read.act group-read])
(group-poke [%add write.act group-write])
(chat-poke [%create our.bol path.act])
(chat-hook-poke [%add-owned pax security.act])
(chat-hook-poke [%add-owned pax security.act allow-history.act])
==
(create-security [%chat pax] security.act)
:~ (permission-hook-poke [%add-owned group-read group-read])
@ -190,7 +188,7 @@
=/ group-read [%chat (scot %p ship.act) (weld path.act /read)]
=/ group-write [%chat (scot %p ship.act) (weld path.act /write)]
:_ this
:~ (chat-hook-poke [%add-synced ship.act path.act])
:~ (chat-hook-poke [%add-synced ship.act path.act ask-history.act])
(permission-hook-poke [%add-synced ship.act group-write])
(permission-hook-poke [%add-synced ship.act group-read])
==
@ -308,22 +306,6 @@
::
==
::
++ envelopes-update
|= [envelopes=(list envelope) start=@ud end=@ud pax=path]
^- json
=, enjs:format
%+ frond %chat-update
%- pairs
:~
:- %messages
%- pairs
:~ [%path (path pax)]
[%start (numb start)]
[%end (numb end)]
[%envelopes [%a (turn envelopes enve)]]
==
==
::
++ truncate-envelopes
|= envelopes=(list envelope)
^- (list envelope)
@ -340,5 +322,4 @@
^- mailbox
:- config.mail
(truncate-envelopes envelopes.mail)
::
--

View File

@ -132,6 +132,15 @@
:~ [%path (path path.upd)]
[%envelope (enve envelope.upd)]
==
?: =(%messages -.upd)
?> ?=(%messages -.upd)
:- %messages
%- pairs
:~ [%path (path path.upd)]
[%start (numb start.upd)]
[%end (numb end.upd)]
[%envelopes [%a (turn envelopes.upd enve)]]
==
?: =(%read -.upd)
?> ?=(%read -.upd)
[%read (pairs [%path (path path.upd)]~)]
@ -166,6 +175,7 @@
:~ [%create create]
[%delete delete]
[%message message]
[%messages messages]
[%read read]
==
::
@ -184,6 +194,12 @@
[%envelope envelope]
==
::
++ messages
%- ot
:~ [%path pa]
[%envelopes (ar envelope)]
==
::
++ read
(ot [%path pa] ~)
::
@ -225,6 +241,7 @@
[%security sec]
[%read (as (su ;~(pfix sig fed:ag)))]
[%write (as (su ;~(pfix sig fed:ag)))]
[%allow-history bo]
==
::
++ delete
@ -234,6 +251,7 @@
%- ot
:~ [%ship (su ;~(pfix sig fed:ag))]
[%path pa]
[%ask-history bo]
==
::
++ sec

View File

@ -20,12 +20,14 @@
%- ot
:~ [%path pa]
[%security sec]
[%allow-history bo]
==
::
++ add-synced
%- ot
:~ [%ship (su ;~(pfix sig fed:ag))]
[%path pa]
[%ask-history bo]
==
::
++ sec

View File

@ -10,14 +10,13 @@
$% :: %add-owned: make a chatroom accessible to foreign ships
:: specified by the chat-security model
::
[%add-owned =path security=chat-security]
[%add-owned =path security=chat-security allow-history=?]
:: %add-synced: mirror a foreign chatroom to our chat-store
::
[%add-synced =ship =path]
[%add-synced =ship =path ask-history=?]
:: %remove: stop mirroring a foreign chatroom or allowing a local
:: chatroom to be mirrored
::
[%remove =path]
==
--

View File

@ -30,16 +30,24 @@
::
+$ chat-configs (map path config)
::
+$ chat-action
+$ chat-base
$% [%create =ship =path] :: %create: create a mailbox at ~ship/path
[%delete =path] :: %delete: delete a mailbox at path
[%message =path =envelope] :: %message: append a message to mailbox
[%read =path] :: %read: set mailbox to read
==
::
+$ chat-action
$% :: %messages: append a list of messages to mailbox
::
[%messages =path envelopes=(list envelope)]
chat-base
==
::
+$ chat-update
$% [%keys keys=(set path)]
[%config =path =config]
chat-action
[%messages =path start=@ud end=@ud envelopes=(list envelope)]
chat-base
==
--

View File

@ -7,8 +7,14 @@
==
::
+$ chat-view-action
$% [%create =path security=chat-security read=(set ship) write=(set ship)]
$% $: %create
=path
security=chat-security
read=(set ship)
write=(set ship)
allow-history=?
==
[%delete =path]
[%join =ship =path]
[%join =ship =path ask-history=?]
==
--