summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpolwex <polwex@sortug.com>2025-10-06 07:58:25 +0700
committerpolwex <polwex@sortug.com>2025-10-06 07:58:25 +0700
commit410cc8e8949c6ab0677daebbad10cea5f1b9686c (patch)
tree2946ca36a81d5642fb8cd53a2268ecca0e89b59e
parentc4b392a179048f936c062f5ffccc2bc25627e500 (diff)
added websockets server logic to nostrill
-rw-r--r--.gitignore2
-rw-r--r--app/app/nostrill.hoon31
-rw-r--r--app/lib/json/common.hoon2
-rw-r--r--app/lib/json/nostr.hoon72
-rw-r--r--app/lib/mutations/nostr.hoon18
-rw-r--r--app/lib/nostr/req.hoon19
-rw-r--r--app/lib/websockets.hoon14
-rw-r--r--app/sur/nostr.hoon14
-rw-r--r--app/sur/nostrill.hoon1
9 files changed, 148 insertions, 25 deletions
diff --git a/.gitignore b/.gitignore
index b8fc47c..0523acd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,7 @@
.claude
.zig-cache
+fakezod
AGENTS.md
CLAUDE.md
NOTES.md
+run.sh
diff --git a/app/app/nostrill.hoon b/app/app/nostrill.hoon
index d584348..7a737ac 100644
--- a/app/app/nostrill.hoon
+++ b/app/app/nostrill.hoon
@@ -1,5 +1,7 @@
/- sur=nostrill, nsur=nostr, tf=trill-feed, comms=nostrill-comms
/+ lib=nostrill, nostr-keys, sr=sortug, scri,
+ ws=websockets,
+ nreq=nostr-req,
shim, dbug,
evlib=nostr-events,
mutations-nostr,
@@ -55,21 +57,13 @@
%handle-http-request handle-shim
%websocket-handshake handle-ws-handshake
%websocket-server-message handle-ws-msg
- ==
- ++ give-ws-payload
- |= [wid=@ event=websocket-event:eyre]
- ^- (list card:agent:gall)
- =/ =cage
- [%websocket-response !>([wid event])]
- =/ wsid (scot %ud wid)
- :~ [%give %fact ~[/websocket-server/[wsid]] cage]
- ==
-
+ ==
++ handle-ws-handshake
=/ order !<([@ inbound-request:eyre] vase)
- =/ response [%accept ~]
:_ this
- (give-ws-payload -.order response)
+ :: TODO refuse if...?
+ (accept-handshake:ws -.order)
+ :: we behave like a Server here, mind you. messages from clients, not relays
++ handle-ws-msg
=/ order !<([wid=@ msg=websocket-message:eyre] vase)
:: ~& opcode=op=opcode.msg.order :: 0 for continuation, 1 for text, 2 for binary, 9 for ping 0xa for pong
@@ -79,11 +73,16 @@
~& >> ws-msg-jsons=jsons
=/ jsonm (de:json:html jsons)
?~ jsonm `this
+ =/ client-msg (parse-client-msg:nreq u.jsonm)
+ ?~ client-msg ~& "wrong nostr ws msg from client" `this
:: TODO de-json thing and handle whatever
- :_ this
- =/ octs (as-octs:mimes:html '"lol-lmao"')
- =/ response [%message 1 `octs]
- (give-ws-payload -.order response)
+ =^ cs state ?- -.u.client-msg
+ %req `state
+ %event (handle-client-event:mutan -.order event.u.client-msg)
+ %auth `state
+ %close `state
+ ==
+ [cs state]
::
::
++ handle-comms
diff --git a/app/lib/json/common.hoon b/app/lib/json/common.hoon
index 0ed54cd..97513cb 100644
--- a/app/lib/json/common.hoon
+++ b/app/lib/json/common.hoon
@@ -22,6 +22,8 @@
=/ atom=(unit @) (slaw:sr %ux p.jon)
?~ atom ~
atom
+ ++ du |= jon=json ^- (unit @da)
+ (cu from-unix:chrono:userlib ni)
++ se |= aur=@tas |= jon=json
?. ?=(%s -.jon) ~
(slaw aur p.jon)
diff --git a/app/lib/json/nostr.hoon b/app/lib/json/nostr.hoon
index cb10c1d..8695a23 100644
--- a/app/lib/json/nostr.hoon
+++ b/app/lib/json/nostr.hoon
@@ -21,7 +21,7 @@
filters+a+(turn fs filter)
~
==
- ++ req |= =req:shim:sur ^- json
+ ++ req |= req=client-msg:sur ^- json
=/ en-ev event
:- %a :- s+(crip (cuss (trip -.req)))
?- -.req
@@ -107,6 +107,18 @@
other+o+other.meta
~
==
+ ++ relay-msg |= msg=relay-msg:sur ^- json
+ =/ head [%s -.msg]
+ :- %a :- head
+ ?- -.msg
+ %event ~[[%s sub-id.msg] (event event.msg)]
+ %ok ~[(hex:en:common id.msg) [%b accepted.msg] [%s msg.msg]]
+ %eose ~[[%s sub-id.msg]]
+ %closed ~[[%s sub-id.msg]]
+ %notice ~[[%s msg.msg]]
+ %auth ~[[%s challenge.msg]]
+ %error ~
+ ==
--
++ de
=, dejs-soft:format
@@ -131,7 +143,63 @@
notice+so
error+so
==
-
+ ++ client-msg
+ |= jon=json ^- (unit client-msg:sur)
+ ?. ?=(%a -.jon) ~
+ ?~ p.jon ~
+ =/ head i.p.jon
+ ?~ t.p.jon ~
+ =/ second i.t.p.jon
+ ?. ?=(%s -.head) ~
+ :: TODO make sure they're always caps
+ ?+ p.head ~
+ %'EVENT' `[%event ((event second))]
+ %'REQ' `[%req (so second) (turn t.t.p.jon filter)]
+ %'CLOSE' `[%close (so second)]
+ %'AUTH' [%auth (event second)]
+ ==
+ ++ filter |= jon=json ^- (unit filter:sur)
+ ?. ?=(%o -.jon) ~
+ =/ f *filter:sur
+ =/ entries ~(tap by p.jon)
+ |- ?~ entries `f
+ =/ entry=[@t json] i.entries
+ =. f
+ ?: .=('ids' -.entry)
+ =/ vl (ar +.entry hex:de:common)
+ ?~ vl f
+ =/ values (silt u.vl)
+ f(ids `values)
+ ?: .=('authors' -.entry)
+ =/ vl (ar +.entry hex:de:common)
+ ?~ vl f
+ =/ values (silt u.vl)
+ f(authors `values)
+ ?: .=('kinds' -.entry)
+ =/ vl (ar +.entry ud:de:common)
+ ?~ vl f
+ =/ values (silt u.vl)
+ f(kinds `values)
+ ?: .=('limit' -.entry)
+ =/ value (ni +.entry)
+ f(limit value)
+ ?: .=('since' -.entry)
+ =/ value (du:de:common +.entry)
+ f(since value)
+ ?: .=('until' -.entry)
+ =/ value (du:de:common +.entry)
+ f(until value)
+ :: anything else is a tag
+ =/ ctags ?~ tags.f *(map @t (set @t)) u.tags.f
+ =/ vl (ar +.entry so)
+ ?~ vl f
+ =/ values (silt u.vl)
+ =/ ntags (~(put by ctags) -.entry values)
+ f(tags ntags)
+ $(entries t.entries)
+
+
+
:: | { event: { subscription_id: string; event: NostrEvent } }
:: | { ok: { event_id: string; accepted: boolean; message: string } }
diff --git a/app/lib/mutations/nostr.hoon b/app/lib/mutations/nostr.hoon
index 2c6acd5..96ea6c6 100644
--- a/app/lib/mutations/nostr.hoon
+++ b/app/lib/mutations/nostr.hoon
@@ -3,6 +3,9 @@
/+ appjs=json-nostrill,
lib=nostrill,
+ nlib=nostr,
+ nreq=nostr-req,
+ server,
njs=json-nostr,
postlib=trill-post,
shim,
@@ -11,6 +14,21 @@
|_ [=state:sur =bowl:gall]
+$ card card:agent:gall
:: events
+++ handle-client-event |= [wid=@ =event:nsur] ^- (quip card _state)
+ ~& handling-client-event=event
+ =. nostr-feed.state (put:norm:sur nostr-feed.state created-at.event event)
+ =/ profile (~(get by profiles.state [%nostr author.event]))
+ :: TODO save if we're following?
+ :: =/ pw (event-to-post:nlib event profile)
+ =/ response (ok-client-event event .n 'we\'re full')
+ =/ cs (ws-response:nreq response)
+ [cs state]
+ :: =/ l events.state
+ :: =| cards=(list card:agent:gall)
+ :: |- ?~ l [cards state]
+ :: =/ n (event-parsing i.l)
+ :: $(cards -.n, state +.n, l t.l)
+
++ process-events ^- (quip card _state)
:: =/ l events.state
:: =| cards=(list card:agent:gall)
diff --git a/app/lib/nostr/req.hoon b/app/lib/nostr/req.hoon
new file mode 100644
index 0000000..ce566bc
--- /dev/null
+++ b/app/lib/nostr/req.hoon
@@ -0,0 +1,19 @@
+/- sur=nostr
+/+ js=json-nostr, sr=sortug,
+ lib=nostr,
+ server,
+ ws=websockets
+|%
+++ parse-client-msg
+ |= jon=json ^- (unit client-msg:nsur)
+ (client-msg:de:js jon)
+++ ok-client-event |= [=event:nsur ok=? msg=@t]
+ ^- relay-msg
+ [%ok id.event ok msg]
+++ ws-response
+ |= msg=relay-msg:sur ^- (list card:agent:gall)
+ =/ jon (relay-msg:en:js msg)
+ =/ octs (json-to-octs:server jon)
+ =/ res-event=websocket-event:eyre [%message 1 `octs]
+ (give-ws-payload:ws wid res-event)
+--
diff --git a/app/lib/websockets.hoon b/app/lib/websockets.hoon
new file mode 100644
index 0000000..9faea96
--- /dev/null
+++ b/app/lib/websockets.hoon
@@ -0,0 +1,14 @@
+|%
+ ++ give-ws-payload
+ |= [wid=@ event=websocket-event:eyre]
+ ^- (list card:agent:gall)
+ =/ =cage
+ [%websocket-response !>([wid event])]
+ =/ wsid (scot %ud wid)
+ :~ [%give %fact ~[/websocket-server/[wsid]] cage]
+ ==
+ ++ accept-handshake |= wid=@
+ =/ response [%accept ~]
+ (give-ws-payload wid response)
+
+--
diff --git a/app/sur/nostr.hoon b/app/sur/nostr.hoon
index a1b54d1..0d99dc7 100644
--- a/app/sur/nostr.hoon
+++ b/app/sur/nostr.hoon
@@ -61,16 +61,16 @@ $% [%event sub-id=@t =event]
:: from our shim
[%error msg=@t]
==
++$ client-msg
+$% [%req relay-req]
+ [%event =event]
+ [%auth =event]
+ [%close sub-id=@t]
+==
++ shim
|%
++ url 'http://localhost:8888/shim'
- +$ req
- $% [%req relay-req]
- [%event =event]
- [%auth =event]
- [%close sub-id=@t]
- ==
- +$ bulk-req [relays=(list @t) =req]
+ +$ bulk-req [relays=(list @t) req=client-msg]
+$ http-req [relay=@t delay=@ud sub-id=@t filters=(list filter)]
+$ res $%([%http p=(list relay-msg)] [%ws relay=@t msg=relay-msg])
--
diff --git a/app/sur/nostrill.hoon b/app/sur/nostrill.hoon
index b5bca13..c5c5ae8 100644
--- a/app/sur/nostrill.hoon
+++ b/app/sur/nostrill.hoon
@@ -10,6 +10,7 @@
feed=feed:trill
feed-perms=gate:gate
:: nostr feed from relays
+ :: TODO deprecate and parse properly into a feed:trill
=nostr-feed
:: profiles
profiles=(map user user-meta:nostr)