/- sur=nostrill, nsur=nostr /+ lib=nostrill, nlib=nostr, sr=sortug, shim, dbug, muta=nostrill-mutations, jsonlib=json-nostrill, trill=trill-post, comms=nostrill-comms, followlib=nostrill-follows /= web /web/router |% +$ versioned-state $%(state-0:sur) -- =| versioned-state =* state - %- agent:dbug ^- agent:gall |_ =bowl:gall +* this . rout ~(. router:web [state bowl]) cards ~(. cards:lib bowl) mutat ~(. muta [state bowl]) shimm ~(. shim [state bowl]) coms ~(. comms [state bowl]) fols ~(. followlib [state bowl]) ++ on-init ^- (quip card:agent:gall agent:gall) =/ default (default-state:lib bowl) :_ this(state default) :~ shim-binding:cards == :: ++ on-save ^- vase !>(state) :: ++ on-load |~ old-state=vase ^- (quip card:agent:gall agent:gall) =/ old-state !<(versioned-state old-state) ?- -.old-state %0 `this(state old-state) == :: `this(state (default-state:lib bowl)) :: ++ on-poke |~ [=mark =vase] ^- (quip card:agent:gall agent:gall) |^ ?+ mark `this %noun handle-comms %json on-ui %handle-http-request handle-shim == ++ handle-comms =/ pok (cast-poke:coms q.vase) ?: ?=(%dbug -.pok) (debug +.pok) =^ cs state ?- -.pok %req (handle-req:coms +.pok) %res (handle-res:coms +.pok) == [cs this] ++ handle-shim =/ order !<(order:web vase) :: ~& request.req.order ?: .=(url.request.req.order '/nostr-shim') =/ msg (parse-msg:shim order) ?~ msg `this ?> ?=(%ws -.u.msg) :: =^ cards state (handle-shim-msg:mutat u.msg) =^ cards state (handle-ws:mutat +.u.msg) [cards this] :: :_ this (rout:rout order) :: ++ on-ui =/ jon=json !<(json vase) ~& > on-ui-jon=jon =/ upoke=(unit poke:ui:sur) (ui:de:jsonlib jon) ?~ upoke ~& bad-ui-poke=jon `this ?- -.u.upoke %keys handle-cycle-keys %fols (handle-fols +.u.upoke) %begs (handle-begs +.u.upoke) %prof (handle-prof +.u.upoke) %post (handle-post +.u.upoke) %rela (handle-rela +.u.upoke) == ++ handle-cycle-keys =/ ks (gen-keys:nlib eny.bowl) =. keys [ks keys] :: =/ nkeys keys(i ks, t `(list keys:nsur)`keys) :: :: =. keys nkeys ~& new-keys=keys `this ++ handle-post |= poke=post-poke:ui:sur ?- -.poke %add =/ sp (build-sp:trill our.bowl our.bowl content.poke) =/ p (build-post:trill now.bowl pub.i.keys sp) =. state (add-to-feed:mutat p) =/ profile (~(get by profiles) [%urbit our.bowl]) =/ pw [p (some pub.i.keys) ~ ~ profile] =/ =fact:ui:sur [%post %add pw] =/ card (update-ui:cards fact) :_ this :~(card) %rt `this %del `this == ++ handle-begs |= poke=begs-poke:ui:sur ?- -.poke %feed =/ cs ~ [cs this] %thread =/ cs ~ [cs this] == ++ handle-fols |= poke=fols-poke:ui:sur =^ cs state ?- -.poke %add (handle-add:fols +.poke) %del (handle-del:fols +.poke) == [cs this] ++ handle-prof |= poke=prof-poke:ui:sur ?- -.poke %add =. profiles (~(put by profiles) [%urbit our.bowl] +.poke) `this %del =. profiles (~(del by profiles) [%urbit our.bowl]) `this == ++ handle-rela |= poke=relay-poke:ui:sur ?- -.poke %add =. relays (~(put by relays) +.poke *relay-stats:nsur) `this %del =. relays (~(del by relays) +.poke) `this :: %sync =^ cs state get-posts:shimm [cs this] :: %send =/ upoast (get-poast:mutat host.poke id.poke) ?~ upoast `this =/ event (post-to-event:lib i.keys eny.bowl u.upoast) =/ req=bulk-req:shim:nsur [relays.poke %event event] =/ cards :~((send:shimm req)) [cards this] == :: ++ debug |= noun=* ?+ noun `this %wtf =/ lol=(unit @) ~ =/ l ~| "wtf" (need lol) `this %genkey =/ keys (gen-keys:nlib eny.bowl) ~& pub=(scow:sr %ux -.keys) ~& priv=(scow:sr %ux +.keys) `this %printkey =/ ks `(list keys:nsur)`keys |- ?~ ks `this =/ key i.ks ~& pub=(scow:sr %ux -.key) ~& priv=(scow:sr %ux +.key) $(ks t.ks) %feed =/ lol debug-own-feed:mutat `this %nstats ~& nostr-feed=~(wyt by nostr-feed) ~& profiles=~(wyt in ~(key by profiles)) =/ lol (print-relay-stats:lib relays) `this %http `this %rt :: relay test =^ cards state get-posts:shimm [cards this] %rt0 =/ ids %+ roll (tap:norm:sur nostr-feed) |= [[@ ev=event:nsur] acc=[(set @ux) (set @ux)]] ?. .=(kind.ev 1) acc %= acc - (~(put in -.acc) id.ev) + (~(put in +.acc) pubkey.ev) == =^ cards state (populate-profiles:mutat -.ids) :: (get-profiles:shimm +.ids) :: (get-engagement:shimm -.ids) [cards this] :: %rt1 :: =| cards=(list card:agent:gall) :: |- :: ?~ l :: ~& cards=(lent cards) [cards this] :: =/ [sub-id=@t pf=filter:nsur done=filter:nsur] i.l :: =/ diff (diff-filters:nlib pf done) :: :: ~& > diff=diff :: ?~ authors.pf $(l t.l) :: =^ cs state (populate-profiles:mutat u.authors.pf) :: $(l t.l, cards (weld cards cs)) %rt2 =/ poasts (tap:norm:sur nostr-feed) =/ pcount (lent poasts) =| invalid=(list @t) |- ?~ poasts ~& >>> invalid=invalid `this =/ p=event:nsur +.i.poasts =/ valid (validate-pubkey:nlib pubkey.p) ?. valid =/ ids (crip (scow:sr %ux id.p)) ~& ids ~& content.p $(invalid [ids invalid], poasts t.poasts) $(poasts t.poasts) %rt3 =/ poasts (tap:norm:sur nostr-feed) =| pubkeys=(set @ux) =/ pks=(set @ux) |- ?~ poasts pubkeys =/ p=event:nsur +.i.poasts =/ npks (~(put in pubkeys) pubkey.p) $(poasts t.poasts, pubkeys npks) :: =^ cards state (populate-profiles:mutat pks) [cards this] %ui =/ =fact:ui:sur [%post %add *post-wrapper:sur] =/ card (update-ui:cards fact) :_ this :~(card) %kick :_ this =/ subs ~(tap by sup.bowl) %+ turn subs |= [* p=@p pat=path] [%give %kick ~[pat] ~] == :: -- :: ++ on-watch |= =(pole knot) ~& on-watch=`path`pole ?+ pole !! [%follow ~] :_ this (give-feed:coms pole) [%beg %feed ~] :_ this (give-feed:coms pole) [%beg %thread ids=@t ~] =/ id (slaw:sr %uw ids.pole) ?~ id ~& error-parsing-ted-id=pole `this :_ this (give-ted:coms u.id pole) [%ui ~] ?> .=(our.bowl src.bowl) :_ this =/ jon (state:en:jsonlib state) [%give %fact ~[/ui] [%json !>(jon)]]^~ == :: ++ on-leave |~ =(pole knot) ^- (quip card:agent:gall agent:gall) `this :: ++ on-peek |~ =(pole knot) ^- (unit (unit cage)) ~& > on-peek=pole ?+ pole ~ [%j %feed rest=*] ~ [%j %post rest=*] ~ == :: ++ on-agent |~ [wire=(pole knot) =sign:agent:gall] ^- (quip card:agent:gall agent:gall) ~& on-agent=[wire -.sign] :: if p.sign is not ~ here that means it's intentional ?+ wire `this [%refollow ~] ?. ?=(%watch-ack -.sign) `this ?~ p.sign `this =^ cs state (handle-kick-nack:fols src.bowl) [cs this] [%follow ~] ?: ?=(%kick -.sign) =^ cs state (handle-refollow:fols src.bowl) [cs this] ?. ?=(%fact -.sign) `this =^ cs state (handle-agent-res:fols q.q.cage.sign) [cs this] == :: ++ on-arvo |~ [wire=(pole knot) =sign-arvo] ^- (quip card:agent:gall agent:gall) ?+ wire `this [%http sub-id=@t *] ?> ?=([%khan %arow *] sign-arvo) ?: ?=(%| -.p.sign-arvo) `this =/ jstring !<(@ +>.p.sign-arvo) =/ msg (parse-body:shimm jstring) ?~ msg ~& `@t`jstring `this ?> ?=(%http -.u.msg) =^ cards state (handle-http:mutat sub-id.wire +.u.msg) `this == :: ++ on-fail |~ [term tang] ^- (quip card:agent:gall agent:gall) `this --