summaryrefslogtreecommitdiff
path: root/app/lib/nostr
diff options
context:
space:
mode:
authorpolwex <polwex@sortug.com>2025-11-18 16:58:46 +0700
committerpolwex <polwex@sortug.com>2025-11-18 16:58:46 +0700
commit76f99af3c98a689441315b5ed087c4b83c083191 (patch)
treeb51182eda47f4b3311c352a86d8256e417af4db5 /app/lib/nostr
parenta465c73178dc621578d10312f263355f0f2d5309 (diff)
refactooooring
Diffstat (limited to 'app/lib/nostr')
-rw-r--r--app/lib/nostr/client.hoon91
-rw-r--r--app/lib/nostr/events.hoon81
2 files changed, 127 insertions, 45 deletions
diff --git a/app/lib/nostr/client.hoon b/app/lib/nostr/client.hoon
index 9c732bb..259852e 100644
--- a/app/lib/nostr/client.hoon
+++ b/app/lib/nostr/client.hoon
@@ -1,7 +1,7 @@
/- sur=nostrill, nsur=nostr
/+ js=json-nostr, sr=sortug, seq, nostr-keys, constants, server, ws=websockets
/= web /web/router
-|_ [=state:sur =bowl:gall]
+|_ [=state:sur =bowl:gall wid=@ud relay=relay-stats:nsur]
+$ card card:agent:gall
++ parse-msg
@@ -10,6 +10,7 @@
?~ body.request.req ~
=/ jstring q.u.body.request.req
(parse-body jstring)
+
++ parse-body |= jstring=@t
=/ ures (de:json:html jstring)
?~ ures ~
@@ -18,119 +19,123 @@
ur
:: __
-++ get-relay
- =/ rls ~(tap by relays.state) ^- [@ud relay-stats:nsur]
- ?~ rls !!
- :: TODO not how this should work
- =/ wid -.i.rls
- =/ rs=relay-stats:nsur +.i.rls
- [wid rs]
-
++ close-sub |= [sub-id=@t wid=@ud relay=relay-stats:nsur]
- ^- (quip card _state)
+ ^- (quip card _relay)
=. reqs.relay (~(del by reqs.relay) sub-id)
- =. relays.state (~(put by relays.state) wid relay)
=/ req=client-msg:nsur [%close sub-id]
- =/ rl get-relay
- =/ relay +.rl
- =/ url url.relay
- :- :~ (send url req) == state
+ :- :~ (send url.relay req) == relay
-++ send-req |= [fs=(list filter:nsur) ongoing=? chunked=(list filter:nsur)]
- ^- (quip card _state)
+++ send-req |= [fs=(list filter:nsur) ongoing=(unit ?) chunked=(list filter:nsur)]
+ ^- (quip card _relay)
=/ sub-id (gen-sub-id:nostr-keys eny.bowl)
=/ req=client-msg:nsur [%req sub-id fs]
=/ es=event-stats:nsur [fs 0 ongoing chunked]
- =/ rl get-relay
- =/ wid -.rl
- =/ relay +.rl
=/ url url.relay
=. reqs.relay (~(put by reqs.relay) sub-id es)
- =. relays.state (~(put by relays.state) wid relay)
~& > sending-ws-req=sub-id
- :- :~ (send url req) == state
+ :- :~ (send url req) == relay
++ get-posts
- ^- (quip card _state)
=/ kinds (silt ~[1])
:: =/ last-week (sub now.bowl ~d7)
- =/ last-week (sub now.bowl ~m2)
+ =/ last-week (sub now.bowl ~m1)
:: =/ since (to-unix-secs:jikan:sr last-week)
=/ =filter:nsur [~ ~ `kinds ~ `last-week ~ ~]
- (send-req ~[filter] .y ~)
-
+ (send-req ~[filter] `.n ~)
+::
++ get-user-feed
|= pubkey=@ux
- ^- (quip card _state)
=/ kinds (silt ~[1])
:: =/ since (to-unix-secs:jikan:sr last-week)
=/ pubkeys (silt ~[pubkey])
=/ =filter:nsur [~ `pubkeys `kinds ~ ~ ~ ~]
- (send-req ~[filter] .y ~)
+ (send-req ~[filter] `.n ~)
+
+++ get-thread |= id=@ux
+ =/ kinds (silt ~[1])
+ =/ ids (silt :~(id))
+ =/ f1=filter:nsur [`ids ~ `kinds ~ ~ ~ ~]
+ =/ ids=(list @t) :~((crip (scow:parsing:sr %ux id)))
+ =/ tag ['e' ids]
+ =/ tags=(map @t (list @t)) (malt :~(tag))
+ =/ f2=filter:nsur [~ ~ `kinds `tags ~ ~ ~]
+ (send-req ~[f1 f2] `.n ~)
+
+++ get-post |= id=@ux
+ =/ kinds (silt ~[1])
+ =/ ids (silt :~(id))
+ =/ =filter:nsur [`ids ~ `kinds ~ ~ ~ ~]
+ (send-req ~[filter] ~ ~)
+++ get-replies |= id=@ux
+ =/ kinds (silt ~[1])
+ =/ ids=(list @t) :~((crip (scow:parsing:sr %ux id)))
+ =/ tag ['e' ids]
+ =/ tags=(map @t (list @t)) (malt :~(tag))
+ =/ =filter:nsur [~ ~ `kinds `tags ~ ~ ~]
+ (send-req ~[filter] `.n ~)
+::
++ get-profile |= pubkey=@ux
=/ kinds (silt ~[0])
:: =/ since (to-unix-secs:jikan:sr last-week)
=/ pubkeys (silt ~[pubkey])
=/ =filter:nsur [~ `pubkeys `kinds ~ ~ ~ ~]
- (send-req ~[filter] .n ~)
+ (send-req ~[filter] ~ ~)
++ get-profiles
- ^- (quip card _state)
+ ~& >>> "getting profiles"
=/ npoasts (tap:norm:sur nostr-feed.state)
=| missing-profs=(set @ux)
=/ pubkeys=(set @ux)
|- ?~ npoasts missing-profs
=/ poast=event:nsur +.i.npoasts
=/ have (~(has by profiles.state) [%nostr pubkey.poast])
- =. missing-profs ?: have missing-profs (~(put in missing-profs) pubkey.poast)
+ =? missing-profs !have (~(put in missing-profs) pubkey.poast)
$(npoasts t.npoasts)
=/ kinds (silt ~[0])
=/ chunk-size 300
+ ~& >> fetching-profiles=~(wyt in pubkeys)
?. (gth ~(wyt in pubkeys) chunk-size)
=/ =filter:nsur [~ `pubkeys `kinds ~ ~ ~ ~]
- (send-req ~[filter] .n ~)
+ (send-req ~[filter] ~ ~)
::
=/ chunks=(list (list @ux)) (chunk-by-size:seq ~(tap in pubkeys) chunk-size)
- ?~ chunks ~& >>> "error chunking pubkeys" `state
+ ?~ chunks ~& >>> "error chunking pubkeys" `relay
=/ queue=(list filter:nsur)
%+ turn t.chunks |= l=(list @ux) ^- filter:nsur
=/ pubkeys=(set @ux) (silt l)
[~ `pubkeys `kinds ~ ~ ~ ~]
=/ pubkeys=(set @ux) (silt i.chunks)
=/ =filter:nsur [~ `pubkeys `kinds ~ ~ ~ ~]
- (send-req ~[filter] .n queue)
+ (send-req ~[filter] ~ queue)
++ get-engagement
|= post-ids=(set @ux)
- ^- (quip card _state)
- =/ post-strings %- ~(run in post-ids) |= id=@ux (crip (scow:sr %ux id))
+ =/ post-strings %+ turn ~(tap in post-ids) |= id=@ux (crip (scow:sr %ux id))
=/ =filter:nsur
=/ kinds (silt ~[6 7])
=/ tags (malt :~([%e post-strings]))
[~ ~ `kinds `tags ~ ~ ~]
- (send-req ~[filter] .y ~)
+ (send-req ~[filter] `.n ~)
++ get-quotes
|= post-id=@ux
- ^- (quip card _state)
=/ post-string (crip (scow:sr %ux post-id))
=/ kinds (silt ~[1])
- =/ tags (malt :~([%q (silt ~[post-string])]))
+ =/ tags (malt :~([%q ~[post-string]]))
=/ =filter:nsur [~ ~ `kinds `tags ~ ~ ~]
- (send-req ~[filter] .y ~)
+ (send-req ~[filter] `.n ~)
::
++ test-connection
- |= relay-url=@t
=/ kinds (silt ~[1])
=/ since (sub now.bowl ~m10)
=/ =filter:nsur [~ ~ `kinds ~ `since ~ ~]
=/ sub-id (gen-sub-id:nostr-keys eny.bowl)
=/ req=client-msg:nsur [%req sub-id ~[filter]]
- :- :~ (send relay-url req) == state
+ :- :~ (send url.relay req) == relay
++ send
|= [relay-url=@t req=client-msg:nsur] ^- card
@@ -138,10 +143,10 @@
=/ req-body=json (req:en:js req)
=/ octs (json-to-octs:server req-body)
=/ wmsg=websocket-message:eyre [1 `octs]
- ~& >> sup=sup.bowl
=/ conn (check-connected:ws relay-url bowl)
~& >>> send-client-conn=conn
?~ conn :: if no ws connection we start a thread which will connect first, then send the message
+ ~& >>> "no connection!!"
!!
:: =/ =task:iris [%websocket-connect dap.bowl relay-url]
:: [%pass /ws-req/nostrill %arvo %i task]
diff --git a/app/lib/nostr/events.hoon b/app/lib/nostr/events.hoon
index 6f287a7..16beabc 100644
--- a/app/lib/nostr/events.hoon
+++ b/app/lib/nostr/events.hoon
@@ -1,6 +1,7 @@
/- sur=nostrill, nsur=nostr, post=trill-post, gate=trill-gate
/+ js=json-nostr, sr=sortug, trill=trill-post, nostr-keys
|%
+:: filters
++ is-feed |= fs=(list filter:nsur) ^- ?
|- ?~ fs .n
=/ filter i.fs
@@ -13,16 +14,92 @@
== .y
$(fs t.fs)
+++ user-req |= fs=(list filter:nsur) ^- (set @ux)
+ =| pubkeys=(set @ux)
+ |- ?~ fs pubkeys
+ =/ filter i.fs
+ ?~ kinds.filter ~
+ ?~ authors.filter ~
+ ?: (~(has in u.kinds.filter) 0) ~
+ =? pubkeys
+ ?& (~(has in u.kinds.filter) 1)
+ ?=(%~ ids.filter)
+ == (~(uni in pubkeys) u.authors.filter)
+ $(fs t.fs)
+++ posts-req |= fs=(list filter:nsur) ^- (set @ux)
+ =| ids=(set @ux)
+ |- ?~ fs ids
+ =/ filter i.fs
+ ?~ kinds.filter ~
+ ?~ ids.filter ~
+ =? ids (~(has in u.kinds.filter) 1) (~(uni in ids) u.ids.filter)
+ $(fs t.fs)
+
+++ replies-req |= fs=(list filter:nsur) ^- (set @ux)
+ =| ids=(set @ux)
+ |- ?~ fs ids
+ =/ filter i.fs
+ =/ parent (replies-filter filter)
+ =? ids ?=(^ parent) (~(put in ids) u.parent)
+ $(fs t.fs)
+
+++ thread-req |= fs=(list filter:nsur) ^- (unit @ux)
+ =| parent=(unit @ux)
+ |- ?~ fs ~
+ =/ filter i.fs
+ ?~ parent
+ =/ upid (post-filter filter)
+ $(fs t.fs, parent upid)
+ =/ replies-parent (replies-filter i.fs)
+ ?: ?& ?=(^ replies-parent)
+ .=(u.replies-parent u.parent)
+ == parent
+ $(fs t.fs)
+
+++ post-filter |= =filter:nsur ^- (unit @ux)
+ ?~ kinds.filter ~
+ ?~ ids.filter ~
+ =/ post-filter (silt ~[1])
+ ?. .=(u.kinds.filter post-filter) ~
+ =/ idl ~(tap in u.ids.filter)
+ ?~ idl ~
+ ?. .=(1 (lent idl)) ~
+ `i.idl
+
+++ replies-filter |= =filter:nsur ^- (unit @ux)
+ ?~ kinds.filter ~
+ ?~ tags.filter ~
+ =/ post-filter (silt ~[1])
+ ?. .=(u.kinds.filter post-filter) ~
+ =/ tag (~(get by u.tags.filter) 'e')
+ ?~ tag ~
+ ?~ u.tag ~
+ =/ reference (slaw:sr %ux i.u.tag)
+ reference
+
++ is-posts-no-prof |= fs=(list filter:nsur) ^- ?
=/ has-posts .n
|- ?~ fs has-posts
=/ filter i.fs
?~ kinds.filter .n
?: (~(has in u.kinds.filter) 0) .n
- =. has-posts
- ?: (~(has in u.kinds.filter) 1) .y has-posts
+ =? has-posts (~(has in u.kinds.filter) 1) .y
$(fs t.fs)
+
+:: events
+++ get-references |= ev=event:nsur ^- (set @ux)
+ =| ids=(set @ux)
+ =/ tags tags.ev
+ |- ?~ tags ids
+ =/ tag i.tags
+ ?~ tag $(tags t.tags)
+ ?. .=('e' i.tag) $(tags t.tags)
+ ?~ t.tag $(tags t.tags)
+ =/ ref (slaw:sr %ux i.t.tag)
+ =? ids ?=(^ ref) (~(put in ids) u.ref)
+ $(tags t.tags)
+
++ post-to-event |= [=keys:nsur eny=@ p=post:post] ^- event:nsur
=/ cl (latest-post-content:trill contents.p)
=/ string (crip (content-list-to-md:trill cl))