shrub/pkg/landscape/app/notify.hoon

529 lines
14 KiB
Plaintext
Raw Normal View History

2021-07-23 02:58:13 +03:00
::
/- *notify, resource, hark-store, post
2021-09-06 06:31:11 +03:00
/+ default-agent, verb, dbug, group, agentio, graphlib=graph
2021-07-23 02:58:13 +03:00
::
|%
+$ card card:agent:gall
::
+$ provider-state (map term provider-entry)
+$ provider-entry
$: notify-endpoint=@t
binding-endpoint=@t
auth-token=@t
2021-07-27 03:19:40 +03:00
clients=(map ship binding=(unit @t))
2021-07-23 02:58:13 +03:00
=whitelist
==
++ clear-interval ~d7
2021-07-23 02:58:13 +03:00
::
+$ client-state
$: providers=(jug @p term)
==
::
2021-09-06 06:31:11 +03:00
+$ base-state-0
$: =provider-state
2021-07-23 02:58:13 +03:00
=client-state
==
2021-12-02 18:19:22 +03:00
+$ base-state-2
2021-11-30 19:31:46 +03:00
$: notifications=(map uid notification)
base-state-0
==
2021-07-23 02:58:13 +03:00
::
2021-09-06 06:31:11 +03:00
+$ state-0
[%0 base-state-0]
::
+$ state-1
[%1 base-state-0]
::
2021-11-30 19:31:46 +03:00
+$ state-2
2021-12-02 18:19:22 +03:00
[%2 base-state-2]
+$ state-3
[%3 base-state-2]
2021-11-30 19:31:46 +03:00
::
2021-07-23 02:58:13 +03:00
+$ versioned-state
$% state-0
2021-09-06 06:31:11 +03:00
state-1
2021-11-30 19:31:46 +03:00
state-2
state-3
2021-07-23 02:58:13 +03:00
==
::
--
::
=| state-3
2021-07-23 02:58:13 +03:00
=* state -
::
%- agent:dbug
%+ verb |
^- agent:gall
::
=<
|_ =bowl:gall
+* this .
2021-07-27 03:19:40 +03:00
def ~(. (default-agent this %|) bowl)
do ~(. +> bowl)
io ~(. agentio bowl)
pass pass:io
2021-07-23 02:58:13 +03:00
::
++ on-init
:_ this
2021-12-02 18:19:22 +03:00
:~ (~(watch-our pass:io /hark/notes) %hark-store /notes)
(~(watch-our pass:io /hark/updates) %hark-store /updates)
(~(wait pass:io /clear) (add now.bowl clear-interval))
2021-12-02 18:19:22 +03:00
==
2021-07-23 02:58:13 +03:00
::
++ on-save !>(state)
++ on-load
|= =old=vase
^- (quip card _this)
=/ old !<(versioned-state old-vase)
2021-09-06 06:31:11 +03:00
=| cards=(list card)
|-
2021-07-23 02:58:13 +03:00
?- -.old
2021-09-06 06:31:11 +03:00
::
%3
=/ upd=wire /hark/updates
=/ not=wire /hark/notes
2021-11-30 19:31:46 +03:00
=/ =dock [our.bowl %hark-store]
2021-12-02 18:19:22 +03:00
=? cards !(~(has by wex.bowl) [upd dock]) :: rewatch updates
:_(cards [%pass upd %agent dock %watch /updates])
2021-12-02 18:19:22 +03:00
=? cards !(~(has by wex.bowl) [not dock]) :: rewatch notes
:_(cards [%pass not %agent dock %watch /notes])
=. notifications.old ~
2021-11-30 19:31:46 +03:00
[(flop cards) this(state old)]
::
%2
=. cards
:_ cards
(~(wait pass:io /clear) (add now.bowl clear-interval))
$(-.old %3)
2021-11-30 19:31:46 +03:00
::
?(%0 %1)
%_ $
-.old %2
+.old [~ +.old]
2021-09-06 06:31:11 +03:00
==
2021-07-23 02:58:13 +03:00
==
::
++ on-poke
|= [=mark =vase]
^- (quip card _this)
|^
=^ cards state
?+ mark (on-poke:def mark vase)
%notify-provider-action (handle-provider-action !<(provider-action vase))
%notify-client-action (handle-client-action !<(client-action vase))
==
[cards this]
::
++ handle-provider-action
|= act=provider-action
^- (quip card _state)
?- -.act
%add
?> (team:title our.bowl src.bowl)
=/ new-entry=provider-entry
:* notify.act
binding.act
auth-token.act
~
whitelist.act
==
[~ state(provider-state (~(put by provider-state) service.act new-entry))]
::
%remove
?> (team:title our.bowl src.bowl)
=/ entry=(unit provider-entry) (~(get by provider-state) service.act)
?~ entry
~|("no such service: {<service.act>}" !!)
:_ state(provider-state (~(del by provider-state) service.act))
%+ turn ~(tap by clients.u.entry)
|= [who=@p *]
^- card
2021-07-27 03:19:40 +03:00
(leave-path:pass [who %notify] /notify/(scot %p who)/[service.act])
2021-07-23 02:58:13 +03:00
::
%client-join
=/ entry=(unit provider-entry) (~(get by provider-state) service.act)
?~ entry
~|("no such service: {<service.act>}" !!)
?. (is-whitelisted:do src.bowl u.entry)
~|("permission denied" !!)
=. clients.u.entry (~(put by clients.u.entry) src.bowl ~)
=/ cards=(list card)
:_ ~
%: register-binding:do
service.act
u.entry
binding-endpoint.u.entry
src.bowl
address.act
==
=/ =wire /agentio-watch/notify/(scot %p src.bowl)/[service.act]
=? cards !(~(has by wex.bowl) wire src.bowl %notify)
:_ cards
%+ watch:pass
[src.bowl %notify]
/notify/(scot %p src.bowl)/[service.act]
:- cards
state(provider-state (~(put by provider-state) service.act u.entry))
2021-07-23 02:58:13 +03:00
::
%client-leave
=/ entry=(unit provider-entry) (~(get by provider-state) service.act)
?~ entry
~|("no such service: {<service.act>}" !!)
?. (is-client:do src.bowl u.entry)
~|("permission denied" !!)
=/ client-info=(unit @t) (~(got by clients.u.entry) src.bowl)
=. clients.u.entry (~(del by clients.u.entry) src.bowl)
:_ state(provider-state (~(put by provider-state) service.act u.entry))
?~ client-info
2021-07-27 03:19:40 +03:00
:_ ~
%+ leave-path:pass
[src.bowl %notify]
/notify/(scot %p src.bowl)/[service.act]
2021-07-23 02:58:13 +03:00
:~ %: remove-binding:do
service.act
u.entry
src.bowl
binding-endpoint.u.entry
u.client-info
==
2021-07-27 03:19:40 +03:00
%+ leave-path:pass
[src.bowl %notify]
/notify/(scot %p src.bowl)/[service.act]
2021-07-23 02:58:13 +03:00
==
==
::
++ handle-client-action
|= act=client-action
^- (quip card _state)
?> (team:title our.bowl src.bowl)
?- -.act
%connect-provider
=. providers.client-state
(~(put ju providers.client-state) who.act service.act)
2021-07-27 03:19:40 +03:00
=/ pact=provider-action [%client-join service.act address.act]
2021-07-23 02:58:13 +03:00
:_ state
2021-07-27 03:19:40 +03:00
[(poke:pass [who.act %notify] %notify-provider-action !>(pact))]~
2021-07-23 02:58:13 +03:00
::
%remove-provider
=. providers.client-state
(~(del ju providers.client-state) who.act service.act)
2021-07-27 03:19:40 +03:00
=/ pact=provider-action [%client-leave service.act]
2021-07-23 02:58:13 +03:00
:_ state
2021-07-27 03:19:40 +03:00
[(poke:pass [who.act %notify] %notify-provider-action !>(pact))]~
2021-07-23 02:58:13 +03:00
==
--
::
++ on-watch
|= =path
^- (quip card _this)
?+ path (on-watch:def path)
2021-07-27 03:19:40 +03:00
[%notify @ @ ~]
=* service i.t.t.path
2021-07-23 02:58:13 +03:00
?. (~(has ju providers.client-state) src.bowl service)
~|("permission denied" !!)
`this
==
::
++ on-leave
|= =path
^- (quip card _this)
`this
::
2021-11-30 19:31:46 +03:00
++ on-peek
|= =path
^- (unit (unit cage))
=/ =(pole knot) path
?+ pole [~ ~]
::
[%x %note uid=@t ~]
=/ =uid (slav %ux uid.pole)
=/ note=notification (~(got by notifications) uid)
``hark-note+!>(note)
==
2021-07-23 02:58:13 +03:00
::
++ on-agent
|= [=wire =sign:agent:gall]
^- (quip card _this)
?+ wire (on-agent:def wire sign)
::
:: subscription from client to their own hark-store
::
[%hark @ ~]
2021-07-23 02:58:13 +03:00
?+ -.sign (on-agent:def wire sign)
%fact
?. ?=(%hark-update p.cage.sign)
2021-11-30 19:31:46 +03:00
`this
2021-07-23 02:58:13 +03:00
=+ !<(hark-update=update:hark-store q.cage.sign)
2021-11-30 19:31:46 +03:00
=^ upds notifications
(filter-notifications:do hark-update)
:_ this
(murn upds |=(=update (fact-all:io %notify-update !>(update))))
2021-07-23 02:58:13 +03:00
::
%kick
:_ this
[%pass wire %agent [our.bowl %hark-store] %watch t.wire]~
2021-07-23 02:58:13 +03:00
==
::
:: subscription from provider to client
::
2021-07-27 03:19:40 +03:00
[%agentio-watch %notify @ @ ~]
=/ who (slav %p i.t.t.wire)
=* service i.t.t.t.wire
2021-07-23 02:58:13 +03:00
?+ -.sign (on-agent:def wire sign)
%fact
?> ?=(%notify-update p.cage.sign)
=+ !<(=update q.cage.sign)
2021-07-27 03:19:40 +03:00
:_ this
2021-11-30 19:31:46 +03:00
=/ entry=(unit provider-entry) (~(get by provider-state) service)
?~ entry
~
[(send-notification:do u.entry who update)]~
2021-07-23 02:58:13 +03:00
::
%kick
:_ this
2021-07-27 03:19:40 +03:00
[(watch:pass [who %notify] /notify/(scot %p who)/[service])]~
2021-07-23 02:58:13 +03:00
::
%watch-ack
?~ p.sign
`this
2021-07-27 03:19:40 +03:00
((slog u.p.sign) `this)
2021-07-23 02:58:13 +03:00
==
==
::
++ on-arvo
|= [=wire =sign-arvo]
^- (quip card _this)
?+ wire (on-arvo:def wire sign-arvo)
[%register-binding @ @ @ ~]
=/ who=@p (slav %p i.t.wire)
2021-07-27 03:19:40 +03:00
=* service i.t.t.wire
2021-07-23 02:58:13 +03:00
::
?> ?=(%iris -.sign-arvo)
?> ?=(%http-response +<.sign-arvo)
?> ?=(%finished -.client-response.sign-arvo)
?> ?=(^ full-file.client-response.sign-arvo)
=/ =mime-data:iris u.full-file.client-response.sign-arvo
?> =('application/json' type.mime-data)
=/ jon=json
(fall (de:json:html (@t q.data.mime-data)) *json)
2021-07-23 02:58:13 +03:00
=/ [sid=@t message=@t]
%. jon
%- ot:dejs:format
:~ sid+so:dejs:format
message+so:dejs:format
==
::
=/ entry=(unit provider-entry) (~(get by provider-state) service)
2021-07-27 03:19:40 +03:00
:- ~
2021-07-23 02:58:13 +03:00
?~ entry
2021-07-27 03:19:40 +03:00
this
2021-07-23 02:58:13 +03:00
=. clients.u.entry (~(put by clients.u.entry) who `sid)
this(provider-state (~(put by provider-state) service u.entry))
::
2021-11-30 19:31:46 +03:00
[%remove-binding *] `this
2021-07-23 02:58:13 +03:00
::
[%send-notification *]
2021-11-30 19:31:46 +03:00
?> ?=(%iris -.sign-arvo)
?> ?=(%http-response +<.sign-arvo)
2021-12-02 18:19:22 +03:00
=* res client-response.sign-arvo
?> ?=(%finished -.res)
%. `this
=* status status-code.response-header.res
?: =(200 status) same
%+ slog
leaf/"Error sending notfication, status: {(scow %ud status)}"
?~ full-file.res ~
~[leaf/(trip `@t`q.data.u.full-file.res)]
::
[%clear ~]
?> ?=([%behn %wake *] sign-arvo)
=. notifications ~
~& "notify/debug: cleared notifications"
:_ this
~[(~(wait pass:io /clear) (add now.bowl clear-interval))]
2021-07-23 02:58:13 +03:00
==
::
++ on-fail on-fail:def
--
|_ bowl=bowl:gall
2021-09-06 06:31:11 +03:00
+* gra ~(. graphlib bowl)
2021-07-23 02:58:13 +03:00
::
++ filter-notifications
2021-11-30 19:31:46 +03:00
|= upd=update:hark-store
^- (quip update _notifications)
?+ -.upd `notifications
::
%more
=| upds=(list update)
|-
?~ more.upd [upds notifications]
=^ us notifications
(filter-notifications i.more.upd)
$(upds (welp upds us), more.upd t.more.upd)
::
%read-count
`notifications
:: TODO: re-enable if/when fixed
::=/ uids ~(tap in (uids-for-place place.upd))
:: =| upds=(list update)
::|-
::?~ uids
:: [upds notifications]
::%_ $
:: notifications (~(del by notifications) i.uids)
::upds :_(upds [i.uids %dismiss])
::uids t.uids
:: ==
2021-11-30 19:31:46 +03:00
::
%add-note
=/ note=notification +.upd
?. (should-notify note) `notifications
=/ =uid (shas %notify-uid eny.bowl)
:_ (~(put by notifications) uid note)
[uid %notify]~
==
::
++ should-notify
|= note=notification
^- ?
?. ?=([%graph @ @ *] path.place.bin.note)
|
=/ s=(unit ship) (slaw %p i.t.path.place.bin.note)
?~ s |
=/ =resource:resource
[u.s i.t.t.path.place.bin.note]
?& ?=(%landscape desk.place.bin.note)
?| ?=([%graph-validator-dm *] link.body.note)
?& (group-is-hidden resource)
?=([%graph-validator-chat *] link.body.note)
==
== ==
::
++ uids-for-place
|= =place:hark
%- ~(gas in *(set uid))
%+ murn ~(tap by notifications)
|= [=uid =notification]
^- (unit ^uid)
?. =(place.bin.notification place) ~
`uid
2021-07-23 02:58:13 +03:00
::
++ group-is-hidden
|= =resource:resource
2021-11-30 19:31:46 +03:00
^- ?
2021-07-23 02:58:13 +03:00
=/ grp=(unit group:group) (~(scry-group group bowl) resource)
2021-11-30 19:31:46 +03:00
?~ grp |
hidden.u.grp
2021-07-23 02:58:13 +03:00
::
++ is-whitelisted
|= [who=@p entry=provider-entry]
^- ?
|^
?| public.whitelist.entry
=(our.bowl who)
is-kid
(~(has in users.whitelist.entry) who)
in-group
==
::
++ is-kid
?& kids.whitelist.entry
=(our.bowl (sein:title our.bowl now.bowl who))
==
::
++ in-group
=/ gs ~(tap in groups.whitelist.entry)
|-
?~ gs %.n
?: (~(is-member group bowl) who i.gs)
%.y
$(gs t.gs)
--
::
++ is-client
|= [who=@p entry=provider-entry]
^- ?
(~(has by clients.entry) who)
::
++ post-form
|= [=wire url=@t auth=@t params=(list [@t @t])]
^- card
2021-11-30 19:31:46 +03:00
=/ esc=$-(@t @t)
|=(t=@t (crip (en-urlt:html (trip t))))
=. params
%+ turn params
|= [p=@t q=@t]
[(esc p) (esc q)]
2021-07-23 02:58:13 +03:00
=/ data
%+ roll
%+ sort params
|= [[p=@t @t] [q=@t @t]]
(aor p q)
|= [[p=@t q=@t] out=_url]
2021-07-27 03:19:40 +03:00
(rap 3 out p q ~)
2021-07-23 02:58:13 +03:00
=/ hmac-sig (hmac-sha1t:hmac:crypto auth data)
=/ b64-sig (en:base64:mimes:html (met 3 hmac-sig) (swp 3 hmac-sig))
=/ headers
:~ ['X-Twilio-Signature' b64-sig]
['Content-Type' 'application/x-www-form-urlencoded']
==
=/ form-data (build-form-data params)
=/ =request:http
[%'POST' url headers `[(met 3 form-data) form-data]]
[%pass wire %arvo %i %request request *outbound-config:iris]
::
++ build-form-data
|= data=(list [@t @t])
^- @t
%+ roll data
|= [[p=@t q=@t] out=@t]
?: =(out '')
2021-07-27 03:19:40 +03:00
(rap 3 p '=' q ~)
(rap 3 out '&' p '=' q ~)
2021-07-23 02:58:13 +03:00
::
++ send-notification
2021-11-30 19:31:46 +03:00
|= [entry=provider-entry who=@p =update]
2021-07-23 02:58:13 +03:00
^- card
=/ params=(list [@t @t])
:~ identity+(rsh [3 1] (scot %p who))
2021-11-30 19:31:46 +03:00
action+`@t`action.update
uid+(scot %ux uid.update)
2021-07-23 02:58:13 +03:00
==
%: post-form
/send-notification/(scot %uv (sham eny.bowl))
notify-endpoint.entry
auth-token.entry
params
==
::
++ register-binding
|= [service=term entry=provider-entry url=@t who=@p address=@t]
^- card
=/ params=(list [@t @t])
:~ identity+(rsh [3 1] (scot %p who))
bindingtype+'apn'
address+address
action+'add'
==
%: post-form
/register-binding/(scot %p who)/[service]/(scot %uv (sham eny.bowl))
binding-endpoint.entry
auth-token.entry
params
==
::
++ remove-binding
|= [service=term entry=provider-entry who=@p url=@t sid=@t]
^- card
=/ params=(list [@t @t])
:~ sid+sid
action+'remove'
==
%: post-form
/remove-binding/(scot %p who)/[service]/(scot %uv (sham eny.bowl))
binding-endpoint.entry
auth-token.entry
params
==
--