contacts: implement v1 json conversions

This commit is contained in:
Mikołaj Paraniak 2024-09-11 19:00:19 +08:00
parent 7229969871
commit c4efedc1d4
14 changed files with 409 additions and 11 deletions

View File

@ -2,7 +2,7 @@
/+ default-agent, dbug, verb
/+ *contacts
:: performance, keep warm
/+ contacts-json
/+ j0=contacts-json-0, j1=contacts-json-1
::
|%
:: conventions
@ -621,11 +621,11 @@
::
:: v1 scries
::
:: /x/v1/self -> $@(~ $profile-1)
:: /x/v1/self -> $contact-1
:: /x/v1/book -> $book
:: /x/v1/book/her=@p -> $page
:: /x/v1/book/id/cid=@uv -> $page
:: /x/v1/all -> (map ship contact-1)
:: /x/v1/all -> $directory
:: /x/v1/contact/her=@p -> $contact-1
:: /x/v1/peer/her=@p -> $contact-1
::

View File

@ -0,0 +1,131 @@
/- c=contacts, g=groups
/+ gj=groups-json
|%
++ enjs
=, enjs:format
|%
:: XX shadowed for compat, +ship:enjs removes the ~
::
++ ship
|=(her=@p n+(rap 3 '"' (scot %p her) '"' ~))
::
++ action
|= a=action-0:c
^- json
%+ frond -.a
?- -.a
%anon ~
%edit a+(turn p.a field)
%meet a+(turn p.a ship)
%heed a+(turn p.a ship)
%drop a+(turn p.a ship)
%snub a+(turn p.a ship)
==
::
++ contact
|= c=contact-0:c
^- json
%- pairs
:~ nickname+s+nickname.c
bio+s+bio.c
status+s+status.c
color+s+(scot %ux color.c)
avatar+?~(avatar.c ~ s+u.avatar.c)
cover+?~(cover.c ~ s+u.cover.c)
::
=- groups+a+-
%- ~(rep in groups.c)
|=([f=flag:g j=(list json)] [s+(flag:enjs:gj f) j])
==
::
++ field
|= f=field-0:c
^- json
%+ frond -.f
?- -.f
%nickname s+nickname.f
%bio s+bio.f
%status s+status.f
%color s+(rsh 3^2 (scot %ux color.f)) :: XX confirm
%avatar ?~(avatar.f ~ s+u.avatar.f)
%cover ?~(cover.f ~ s+u.cover.f)
%add-group s+(flag:enjs:gj flag.f)
%del-group s+(flag:enjs:gj flag.f)
==
::
++ rolodex
|= r=rolodex-0:c
^- json
%- pairs
%- ~(rep by r)
|= [[who=@p foreign-0:c] j=(list [@t json])]
[[(scot %p who) ?.(?=([@ ^] for) ~ (contact con.for))] j] :: XX stale flag per sub state?
::
++ news
|= n=news-0:c
^- json
%- pairs
:~ who+(ship who.n)
con+?~(con.n ~ (contact con.n))
==
--
::
++ dejs
=, dejs:format
|%
:: for performance, @p is serialized above to json %n (no escape)
:: for mark roundtrips, ships are parsed from either %s or %n
:: XX do this elsewhere in groups?
::
++ ship (se-ne %p)
++ se-ne
|= aur=@tas
|= jon=json
?+ jon !!
[%s *] (slav aur p.jon)
::
[%n *] ~| bad-n+p.jon
=/ wyd (met 3 p.jon)
?> ?& =('"' (end 3 p.jon))
=('"' (cut 3 [(dec wyd) 1] p.jon))
==
(slav aur (cut 3 [1 (sub wyd 2)] p.jon))
==
::
++ action
^- $-(json action-0:c)
%- of
:~ anon+ul
edit+(ar field)
meet+(ar ship)
heed+(ar ship)
drop+(ar ship)
snub+(ar ship)
==
::
++ contact
^- $-(json contact-0:c)
%- ot
:~ nickname+so
bio+so
status+so
color+nu
avatar+(mu so)
cover+(mu so)
groups+(as flag:dejs:gj)
==
::
++ field
^- $-(json field-0:c)
%- of
:~ nickname+so
bio+so
status+so
color+nu
avatar+(mu so)
cover+(mu so)
add-group+flag:dejs:gj
del-group+flag:dejs:gj
==
--
--

View File

@ -0,0 +1,152 @@
/- c=contacts, g=groups
/+ gj=groups-json
|%
++ enjs
=, enjs:format
|%
:: XX shadowed for compat, +ship:enjs removes the ~
::
++ ship
|=(her=@p n+(rap 3 '"' (scot %p her) '"' ~))
::
++ cid
|= =cid:c
^- json
s+(scot %uv cid)
::
++ kip
|= =kip:c
^- json
?@ kip
(ship kip)
(cid +.kip)
::
++ value
|= val=value-1:c
^- json
?- -.val
%text (pairs type+s/%text value+s/p.val ~)
%date (pairs type+s/%date value+s/(scot %da p.val) ~)
%tint (pairs type+s/%tint value+s/(rsh 3^2 (scot %ux p.val)) ~)
%ship (pairs type+s/%ship value+(ship p.val) ~)
%look (pairs type+s/%look value+s/p.val ~)
%cult (pairs type+s/%cult value+s/(flag:enjs:gj p.val) ~)
%set (pairs type+s/%set value+a/(turn ~(tap in p.val) value) ~)
==
::
++ contact
|= c=contact-1:c
^- json
o+(~(run by c) value)
::
++ page
|= =page:c
^- json
a+[(contact p.page) (contact q.page) ~]
:: +$ kip $@(@p [%id cid])
:: +$ book (map kip page)
++ book
|= =book:c
^- json
=| kob=(map @ta json)
:- %o
%- ~(rep by book)
|= [[=kip:c =page:c] acc=_kob]
?^ kip
(~(put by acc) (scot %uv +.kip) (^page page))
(~(put by acc) (scot %p kip) (^page page))
::
++ directory
|= =directory:c
^- json
=| dir=(map @ta json)
:- %o
%- ~(rep by directory)
|= [[who=@p con=contact-1:c] acc=_dir]
(~(put by acc) (scot %p who) (contact con))
::
++ news
|= n=news-1:c
^- json
?- -.n
%self (frond self+(contact con.n))
%page %- pairs
:~ kip+(kip kip.n)
con+(contact con.n)
mod+(contact mod.n)
==
%wipe (frond kip+(kip kip.n))
%peer %- pairs
:~ who+(ship who.n)
con+(contact con.n)
==
==
--
::
++ dejs
=, dejs:format
|%
::
++ ship (se %p)
::
++ cid
|= jon=json
^- cid:c
?> ?=(%s -.jon)
(slav %uv p.jon)
::
++ kip
|= jon=json
^- kip:c
?> ?=(%s -.jon)
?: =('~' (end [3 1] p.jon))
(ship jon)
id+(cid jon)
::
++ ta
|* [mas=@tas wit=fist]
|= jon=json
[mas (wit jon)]
::
++ value
^- $-(json value-1:c)
|= jon=json
:: XX is there a way to do it in one go?
::
=/ [type=@tas val=json]
%. jon
(ot text+(se %tas) value+json ~)
?+ type !!
%text %. val (ta %text so)
%date %. val (ta %date (se %da))
:: XX invert arguments in +cu: arguments likely
:: to be heavy should always be at the back
::
%tint %. val
%+ ta %tint
%+ cu
|=(s=@t (slav %ux (cat 3 '0x' s)))
so
%ship %. val (ta %ship ship)
%look %. val (ta %look so)
%cult %. val (ta %cult flag:dejs:gj)
%set %. val (ta %set (as value))
==
++ contact
^- $-(json contact-1:c)
(om value)
++ action
^- $-(json action-1:c)
%- of
:~ anon+ul
self+contact
page+(ot cid+cid contact+contact ~)
spot+(ot ship+ship contact+contact ~)
edit+(ot kip+kip contact+contact ~)
wipe+(ar kip)
meet+(ar ship)
drop+(ar ship)
snub+(ar ship)
==
--
--

View File

@ -1,9 +1,15 @@
/$ rolo %contact-rolodex %json
/$ contact %contact %json
/$ contact %contact-0 %json
/$ contact-1 %contact-1 %json
/$ page-1 %contact-page-1 %json
/$ book-1 %contact-book %json
/$ dir-1 %contact-directory %json
/$ news-1 %contact-news-1 %json
/$ skeins %hark-skeins %json
/$ carpet %hark-carpet %json
/$ blanket %hark-blanket %json
/$ settings %settings-data %json
/$ creds %update %json
:: XX defunct?
:: /$ creds %update %json
/$ storage %storage-update %json
~

14
desk/mar/contact-0.hoon Normal file
View File

@ -0,0 +1,14 @@
/- c=contacts
/+ j=contacts-json-0
|_ =contact-0:c
++ grad %noun
++ grow
|%
++ noun contact-0
++ json (contact:enjs:j contact-0)
--
++ grab
|%
++ noun contact-0:c
--
--

15
desk/mar/contact-1.hoon Normal file
View File

@ -0,0 +1,15 @@
/- c=contacts
/+ j=contacts-json-1
|_ contact=contact-1:c
++ grad %noun
++ grow
|%
++ noun contact
++ json (contact:enjs:j contact)
--
++ grab
|%
++ noun contact-1:c
++ json contact:dejs:j
--
--

View File

@ -1,2 +1,14 @@
/= mark /mar/dummy
mark
/- c=contacts
/+ j=contacts-json-1
|_ action=action-1:c
++ grad %noun
++ grow
|%
++ noun action
--
++ grab
|%
++ noun action-1:c
++ json action:dejs:j
--
--

View File

@ -0,0 +1,14 @@
/- c=contacts
/+ j=contacts-json-1
|_ book=book:c
++ grad %noun
++ grow
|%
++ noun book
++ json (book:enjs:j book)
--
++ grab
|%
++ noun book:c
--
--

View File

@ -0,0 +1,14 @@
/- c=contacts
/+ j=contacts-json-1
|_ directory=directory:c
++ grad %noun
++ grow
|%
++ noun directory
++ json (directory:enjs:j directory)
--
++ grab
|%
++ noun directory:c
--
--

View File

@ -0,0 +1,14 @@
/- c=contacts
/+ j=contacts-json-1
|_ news=news-1:c
++ grad %noun
++ grow
|%
++ noun news
++ json (news:enjs:j news)
--
++ grab
|%
++ noun news-1:c
--
--

View File

@ -0,0 +1,14 @@
/- c=contacts
/+ j=contacts-json-1
|_ =page:c
++ grad %noun
++ grow
|%
++ noun page
++ json (page:enjs:j page)
--
++ grab
|%
++ noun page:c
--
--

View File

@ -1,5 +1,5 @@
/- c=contacts
|_ =update:c
|_ update=update-0:c
++ grad %noun
++ grow
|%
@ -7,6 +7,7 @@
--
++ grab
|%
++ noun update:c
++ noun update-0:c
--
--

View File

@ -1,2 +1,12 @@
/= mark /mar/dummy
mark
/- c=contacts
|_ update=update-1:c
++ grad %noun
++ grow
|%
++ noun update
--
++ grab
|%
++ noun update-1:c
--
--

View File

@ -90,6 +90,7 @@
::
:: uniform set
[%set $|(p=(set value-1) unis)]
:: [%set p=(set value-1)]
==
:: $contact-1: contact data
::