shrub/pkg/arvo/app/chat-view.hoon

570 lines
15 KiB
Plaintext
Raw Normal View History

:: chat-view [landscape]:
::
:: sets up chat JS client, paginates data, and combines commands
2019-11-23 02:02:31 +03:00
:: into semantic actions for the UI
::
/- *permission-store,
*permission-hook,
2020-05-25 08:52:54 +03:00
*group,
*invite-store,
*metadata-store,
2020-05-25 08:52:54 +03:00
group-hook,
2019-11-23 02:02:31 +03:00
*permission-group-hook,
*chat-hook,
2020-06-01 21:34:32 +03:00
*metadata-hook,
2020-05-27 05:22:51 +03:00
hook=chat-hook,
contact-view,
pull-hook
2020-06-01 21:34:32 +03:00
/+ *server, default-agent, verb, dbug,
store=chat-store,
2020-05-25 08:52:54 +03:00
view=chat-view,
2020-05-27 05:22:51 +03:00
group-store,
grpl=group,
resource,
mdl=metadata
2019-11-23 02:02:31 +03:00
::
2020-04-03 00:43:03 +03:00
~% %chat-view-top ..is ~
2019-11-23 02:02:31 +03:00
|%
+$ versioned-state
$% state-0
==
::
+$ state-0
$: %0
~
2020-06-02 09:24:37 +03:00
==
2020-05-25 08:52:54 +03:00
+$ poke
2020-06-02 09:24:37 +03:00
$% [%chat-action action:store]
2020-05-25 08:52:54 +03:00
[%group-action action:group-store]
2020-06-02 09:24:37 +03:00
[%chat-hook-action action:hook]
2020-05-25 08:52:54 +03:00
[%permission-hook-action permission-hook-action]
[%permission-group-hook-action permission-group-hook-action]
==
::
2019-11-23 02:02:31 +03:00
+$ card card:agent:gall
--
::
=| state-0
=* state -
::
%+ verb |
%- agent:dbug
2019-11-23 02:02:31 +03:00
^- agent:gall
=<
2020-04-03 00:43:03 +03:00
~% %chat-view-agent-core ..poke-handle-http-request ~
|_ bol=bowl:gall
2019-11-23 02:02:31 +03:00
+* this .
chat-core +>
cc ~(. chat-core bol)
def ~(. (default-agent this %|) bol)
2019-11-23 02:02:31 +03:00
::
++ on-init
^- (quip card _this)
:_ this
:~ :* %pass /srv %agent [our.bol %file-server]
%poke %file-server-action
!>([%serve-dir /'~chat' /app/landscape %.n %.y])
==
[%pass / %arvo %e %connect [~ /'chat-view'] %chat-view]
[%pass /updates %agent [our.bol %chat-store] %watch /updates]
2019-11-23 02:02:31 +03:00
==
::
2019-11-23 02:02:31 +03:00
++ on-poke
2020-04-03 00:43:03 +03:00
~/ %chat-view-poke
2019-11-23 02:02:31 +03:00
|= [=mark =vase]
^- (quip card _this)
?> (team:title our.bol src.bol)
2019-11-23 02:02:31 +03:00
?+ mark (on-poke:def mark vase)
%handle-http-request
=+ !<([eyre-id=@ta =inbound-request:eyre] vase)
:_ this
%+ give-simple-payload:app eyre-id
%+ require-authorization:app inbound-request
poke-handle-http-request:cc
::
%json
:_ this
2020-04-30 02:04:29 +03:00
(poke-chat-view-action:cc (action:dejs:view !<(json vase)))
2019-11-23 02:02:31 +03:00
::
%chat-view-action
:_ this
(poke-chat-view-action:cc !<(action:view vase))
2019-11-23 02:02:31 +03:00
==
::
++ on-watch
2020-04-03 00:43:03 +03:00
~/ %chat-view-watch
2019-11-23 02:02:31 +03:00
|= =path
^- (quip card _this)
?> (team:title our.bol src.bol)
|^
2019-11-23 02:02:31 +03:00
?: ?=([%http-response *] path)
[~ this]
?: =(/primary path)
:: create inbox with 20 messages max per mailbox and send that along
2019-11-23 02:02:31 +03:00
:: then quit the subscription
:_ this
2020-06-01 21:34:32 +03:00
[%give %fact ~ %json !>((update:enjs:store [%initial truncated-inbox]))]~
2019-11-23 02:02:31 +03:00
(on-watch:def path)
::
++ message-limit 20
::
++ truncated-inbox
2020-06-01 21:34:32 +03:00
^- inbox:store
=/ =inbox:store
2020-06-03 08:48:57 +03:00
=/ our (scot %p our.bol)
=/ now (scot %da now.bol)
.^(inbox:store %gx /[our]/chat-store/[now]/all/noun)
%- ~(run by inbox)
|= =mailbox:store
^- mailbox:store
[config.mailbox (scag message-limit envelopes.mailbox)]
--
2019-11-23 02:02:31 +03:00
::
++ on-agent
2020-04-03 00:43:03 +03:00
~/ %chat-view-agent
2019-11-23 02:02:31 +03:00
|= [=wire =sign:agent:gall]
^- (quip card _this)
?+ -.sign (on-agent:def wire sign)
%poke-ack
?. ?=([%join-group @ @ @ @ @ ~] wire)
(on-agent:def wire sign)
?^ p.sign
(on-agent:def wire sign)
=/ =ship
(slav %p i.t.wire)
=/ ask-history=?
=('y' i.t.t.wire)
=/ rid=resource
(de-path:resource t.t.t.wire)
:_ this
(joined-group:cc rid ship ask-history)
::
2019-11-23 02:02:31 +03:00
%kick
:_ this
[%pass / %agent [our.bol %chat-store] %watch /updates]~
::
%fact
?+ p.cage.sign (on-agent:def wire sign)
%chat-update
:_ this
(diff-chat-update:cc !<(update:store q.cage.sign))
2019-11-23 02:02:31 +03:00
==
==
::
2020-01-04 00:06:42 +03:00
++ on-arvo
2020-04-03 00:43:03 +03:00
~/ %chat-view-arvo
2019-11-23 02:02:31 +03:00
|= [=wire =sign-arvo]
^- (quip card _this)
?: ?=(%bound +<.sign-arvo) [~ this]
(on-arvo:def wire sign-arvo)
2019-11-23 02:02:31 +03:00
::
++ on-save !>(state)
++ on-load
|= old-vase=vase
^- (quip card _this)
=/ old ((soft state-0) q.old-vase)
?^ old [~ this]
:_ this(state [%0 ~])
:~ [%pass / %arvo %e %disconnect [~ /'~chat']]
[%pass / %arvo %e %connect [~ /'chat-view'] %chat-view]
:* %pass /srv %agent [our.bol %file-server]
%poke %file-server-action
!>([%serve-dir /'~chat' /app/landscape %.n %.y])
==
==
::
2019-11-23 02:02:31 +03:00
++ on-leave on-leave:def
++ on-peek on-peek:def
++ on-fail on-fail:def
--
::
::
2020-04-03 00:43:03 +03:00
~% %chat-view-library ..card ~
2019-11-23 02:02:31 +03:00
|_ bol=bowl:gall
2020-05-27 05:22:51 +03:00
++ grp ~(. grpl bol)
++ md ~(. mdl bol)
2019-11-23 02:02:31 +03:00
::
++ poke-handle-http-request
|= =inbound-request:eyre
^- simple-payload:http
=+ url=(parse-request-line url.request.inbound-request)
?+ site.url not-found:gen
[%'chat-view' %paginate @t @t *]
2019-11-23 02:02:31 +03:00
=/ start (need (rush i.t.t.site.url dem))
=/ end (need (rush i.t.t.t.site.url dem))
=/ pax t.t.t.t.site.url
=/ envelopes (envelope-scry [(scot %ud start) (scot %ud end) pax])
%- json-response:gen
2020-04-30 02:04:29 +03:00
%- update:enjs:store
2019-11-23 02:02:31 +03:00
[%messages pax start end envelopes]
==
::
++ poke-json
|= jon=json
^- (list card)
?> (team:title our.bol src.bol)
2020-04-30 02:04:29 +03:00
(poke-chat-view-action (action:dejs:view jon))
2019-11-23 02:02:31 +03:00
::
++ poke-chat-view-action
|= act=action:view
2019-11-23 02:02:31 +03:00
^- (list card)
|^
?> (team:title our.bol src.bol)
2019-11-23 02:02:31 +03:00
?- -.act
%create
?> ?=(^ app-path.act)
?> ?| =(+:group-path.act app-path.act)
=(~(tap in members.act) ~)
==
?^ (chat-scry app-path.act)
~& %chat-already-exists
~
2019-11-23 02:02:31 +03:00
%- zing
:~ (create-chat app-path.act allow-history.act)
%- create-group
:* group-path.act
app-path.act
2020-05-25 08:52:54 +03:00
policy.act
members.act
title.act
description.act
managed.act
==
(create-metadata title.act description.act group-path.act app-path.act)
2019-11-23 02:02:31 +03:00
==
::
%delete
?> ?=(^ app-path.act)
:: always just delete the chat from chat-store
::
:+ (chat-hook-poke [%remove app-path.act])
(chat-poke [%delete app-path.act])
:: if we still have metadata for the chat, remove it, and the associated
:: group if it's unmanaged.
::
:: we aren't guaranteed to have metadata: the chat might have been
:: deleted by the host, which pushes metadata deletion down to us.
::
=/ maybe-group-path
(maybe-group-from-chat app-path.act)
?~ maybe-group-path
~
=* group-path u.maybe-group-path
=/ rid=resource
(de-path:resource group-path)
=/ maybe-group
(scry-group:grp rid)
=/ hidden
?~ maybe-group
%.n
hidden.u.maybe-group
%- zing
:~ ?. (is-creator group-path %chat app-path.act)
~
[(metadata-poke [%remove group-path [%chat app-path.act]])]~
::
?. hidden
~
:~ (group-proxy-poke %remove-members rid (sy our.bol ~))
(group-poke [%remove-group rid ~])
(metadata-hook-poke [%remove group-path])
(metadata-store-poke [%remove group-path [%chat app-path.act]])
==
2019-11-23 02:02:31 +03:00
==
::
%invite
=/ =group-path
(need (maybe-group-from-chat app-path.act))
=/ rid=resource
(de-path:resource group-path)
=/ =group
(need (scry-group:grp rid))
?> ?=(%invite -.policy.group)
:- (group-poke %change-policy rid %invite %add-invites ships.act)
%+ turn
~(tap in ships.act)
|= =ship
(send-invite group-path app-path.act ship)
2019-11-23 02:02:31 +03:00
::
%join
=/ group-path
(maybe-group-from-chat app-path.act)
2020-06-24 03:28:57 +03:00
=/ group
?~ group-path
~
(scry-group-path:grp u.group-path)
?: &(?=(^ group) =(hidden.u.group %.n))
~[(chat-hook-poke %add-synced ship.act app-path.act ask-history.act)]
=/ rid=resource
(de-path:resource ship+app-path.act)
?: =(our.bol entity.rid) ~
=/ =cage
:- %group-update
!> ^- action:group-store
[%add-members rid (sy our.bol ~)]
:: we need this info in the wire to continue the flow after the
:: poke ack
=/ =wire
:- %join-group
[(scot %p ship.act) ?:(ask-history.act %y %n) ship+app-path.act]
[%pass wire %agent [entity.rid %group-push-hook] %poke cage]~
::
%groupify
2020-05-27 05:22:51 +03:00
=* app-path app-path.act
=/ group-path
(snag 0 (groups-from-resource:md %chat app-path))
2020-05-27 05:22:51 +03:00
=/ scry-pax=path
/metadata/[(scot %t (spat group-path))]/chat/[(scot %t (spat app-path))]
2020-05-27 05:22:51 +03:00
=/ =metadata
(need (scry-for (unit metadata) %metadata-store scry-pax))
=/ old-rid=resource
(de-path:resource group-path)
?< (is-managed:grp old-rid)
2020-05-27 05:22:51 +03:00
?~ existing.act
:: just create contacts object for group
~[(contact-view-poke %groupify old-rid title.metadata description.metadata)]
2020-05-27 05:22:51 +03:00
:: change associations
=* group-path group-path.u.existing.act
=/ rid=resource
(de-path:resource group-path)
2020-05-27 05:22:51 +03:00
=/ old-group=group
(need (scry-group:grp old-rid))
2020-05-27 05:22:51 +03:00
=/ =group
(need (scry-group:grp rid))
2020-05-27 05:22:51 +03:00
=/ ships=(set ship)
(~(dif in members.old-group) members.group)
:* (metadata-store-poke %remove ship+app-path %chat app-path)
2020-05-27 05:22:51 +03:00
(metadata-store-poke %add group-path [%chat app-path] metadata)
(group-poke %remove-group old-rid ~)
2020-05-27 05:22:51 +03:00
?. inclusive.u.existing.act
~
:- (group-poke %add-members rid ships)
2020-05-27 05:22:51 +03:00
%+ turn
~(tap in ships)
|= =ship
(send-invite group-path app-path ship)
==
2019-11-23 02:02:31 +03:00
==
::
++ create-chat
|= [=path history=?]
^- (list card)
:~ (chat-poke [%create path])
(chat-hook-poke [%add-owned path history])
==
::
++ create-group
|= [=path app-path=path =policy ships=(set ship) title=@t desc=@t managed=?]
^- (list card)
?^ (scry-group-path:grp path) ~
=/ rid=resource
(de-path:resource path)
?> =(our.bol entity.rid)
2020-05-25 08:52:54 +03:00
:: do not create a contacts object if this is unmanaged
::
2020-05-25 08:52:54 +03:00
:-
?. managed
(group-poke %add-group rid policy %.y)
(contact-view-poke %create name.rid policy title desc)
2020-05-25 08:52:54 +03:00
%+ murn ~(tap in ships)
|= =ship
2020-05-25 08:52:54 +03:00
^- (unit card)
?: =(ship our.bol) ~
2020-05-25 08:52:54 +03:00
`(send-invite path app-path ship)
::
++ create-metadata
|= [title=@t description=@t group-path=path app-path=path]
^- (list card)
=/ =metadata
%* . *metadata
title title
description description
date-created now.bol
creator
(slav %p (snag 0 app-path))
==
:~ (metadata-poke [%add group-path [%chat app-path] metadata])
(metadata-hook-poke [%add-owned group-path])
==
::
++ contact-view-poke
2020-05-27 05:22:51 +03:00
|= act=contact-view-action:contact-view
^- card
[%pass / %agent [our.bol %contact-view] %poke %contact-view-action !>(act)]
::
++ metadata-poke
|= act=metadata-action
^- card
[%pass / %agent [our.bol %metadata-hook] %poke %metadata-action !>(act)]
::
++ metadata-store-poke
|= act=metadata-action
^- card
[%pass / %agent [our.bol %metadata-store] %poke %metadata-action !>(act)]
::
++ send-invite
2020-05-25 08:52:54 +03:00
|= [group-path=path app-path=path =ship]
^- card
2020-05-25 08:52:54 +03:00
=/ managed=?
!=(ship+app-path group-path)
=/ =invite
2020-05-25 08:52:54 +03:00
:* our.bol
2020-05-27 05:22:51 +03:00
?:(managed %contact-hook %chat-hook)
2020-05-25 08:52:54 +03:00
?:(managed group-path app-path)
ship ''
==
2020-05-27 05:22:51 +03:00
=/ act=invite-action [%invite ?:(managed /contacts /chat) (shaf %msg-uid eny.bol) invite]
[%pass / %agent [our.bol %invite-hook] %poke %invite-action !>(act)]
::
++ chat-scry
|= pax=path
^- (unit mailbox:store)
2020-05-19 23:53:55 +03:00
=. pax
;: weld
2020-05-20 10:28:14 +03:00
/(scot %p our.bol)/chat-store/(scot %da now.bol)/mailbox
2020-05-19 23:53:55 +03:00
pax
/noun
==
.^((unit mailbox:store) %gx pax)
::
++ maybe-group-from-chat
|= app-path=path
^- (unit path)
?. .^(? %gu (scot %p our.bol) %metadata-store (scot %da now.bol) ~)
?: ?=([@ ^] app-path)
~& [%assuming-ported-legacy-chat app-path]
`[%'~' app-path]
~& [%weird-chat app-path]
!!
=/ resource-indices
.^ (jug md-resource group-path)
%gy
(scot %p our.bol)
%metadata-store
(scot %da now.bol)
/resource-indices
==
=/ groups=(set path)
%+ fall
(~(get by resource-indices) [%chat app-path])
*(set path)
?~ groups ~
`n.groups
::
++ group-from-chat
(cork maybe-group-from-chat need)
::
++ is-managed
|= =path
^- ?
?> ?=(^ path)
!=(i.path '~')
::
++ is-creator
|= [group-path=path app-name=@ta app-path=path]
^- ?
2020-03-04 05:04:00 +03:00
=/ meta=(unit metadata)
.^ (unit metadata)
%gx
(scot %p our.bol)
%metadata-store
(scot %da now.bol)
%metadata
(scot %t (spat group-path))
app-name
(scot %t (spat app-path))
/noun
==
2020-03-04 05:04:00 +03:00
?~ meta !!
=(our.bol creator.u.meta)
--
:: +joined-group: Successfully joined unmanaged group, continue flow
::
++ joined-group
|= [rid=resource =ship ask-history=?]
^- (list card)
=/ =path
(en-path:resource rid)
?> ?=(^ path)
:~ (group-pull-hook-poke %add ship rid)
(chat-hook-poke %add-synced ship t.path ask-history)
(metadata-hook-poke %add-synced ship path)
==
2019-11-23 02:02:31 +03:00
::
++ diff-chat-update
|= upd=update:store
2019-11-23 02:02:31 +03:00
^- (list card)
2020-06-01 21:34:32 +03:00
[%give %fact ~[/primary] %json !>((update:enjs:store upd))]~
2019-11-23 02:02:31 +03:00
::
:: +utilities
::
++ chat-poke
|= act=action:store
2019-11-23 02:02:31 +03:00
^- card
[%pass / %agent [our.bol %chat-store] %poke %chat-action !>(act)]
::
++ group-poke
|= upd=update:group-store
2019-11-23 02:02:31 +03:00
^- card
[%pass / %agent [our.bol %group-store] %poke %group-update !>(upd)]
++ group-pull-hook-poke
|= act=action:pull-hook
2020-05-25 08:52:54 +03:00
^- card
[%pass / %agent [our.bol %group-pull-hook] %poke %pull-hook-action !>(act)]
2020-05-25 08:52:54 +03:00
::
++ group-proxy-poke
|= act=action:group-store
^- card
[%pass / %agent [entity.resource.act %group-push-hook] %poke %group-update !>(act)]
2019-11-23 02:02:31 +03:00
::
++ permission-poke
|= act=permission-action
^- card
[%pass / %agent [our.bol %permission-store] %poke %permission-action !>(act)]
::
2019-11-23 02:02:31 +03:00
++ chat-hook-poke
|= act=action:hook
2019-11-23 02:02:31 +03:00
^- card
[%pass / %agent [our.bol %chat-hook] %poke %chat-hook-action !>(act)]
::
++ permission-hook-poke
|= act=permission-hook-action
^- card
:* %pass / %agent [our.bol %permission-hook]
%poke %permission-hook-action !>(act)
==
::
++ perm-group-hook-poke
|= act=permission-group-hook-action
^- card
:* %pass / %agent [our.bol %permission-group-hook]
%poke %permission-group-hook-action !>(act)
==
::
++ metadata-hook-poke
|= act=metadata-hook-action
^- card
:* %pass / %agent
[our.bol %metadata-hook]
%poke %metadata-hook-action
!>(act)
==
::
2019-11-23 02:02:31 +03:00
++ envelope-scry
|= pax=path
^- (list envelope:store)
(scry-for (list envelope:store) %chat-store [%envelopes pax])
2019-11-23 02:02:31 +03:00
::
2020-01-11 20:44:35 +03:00
::
2020-03-13 20:12:57 +03:00
++ scry-for
|* [=mold app=term =path]
.^ mold
%gx
(scot %p our.bol)
app
(scot %da now.bol)
(snoc `^path`path %noun)
==
2019-11-23 02:02:31 +03:00
--