From 911f2f03307c9aed9aa7f6cfb0ab176b53c73f92 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Mon, 18 Aug 2025 09:39:23 +0200 Subject: [PATCH] init nostr with gniazdo --- deps.edn | 6 +++++- src/micro_blog/config.clj | 6 +++++- src/micro_blog/nostr.clj | 28 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/micro_blog/nostr.clj diff --git a/deps.edn b/deps.edn index 0a35e3d..bbb74f0 100644 --- a/deps.edn +++ b/deps.edn @@ -15,7 +15,11 @@ ;; json cheshire/cheshire {:mvn/version "6.0.0"} ;; metosin/muuntaja {:mvn/version "0.6.11"} - org.clojure/clojure {:mvn/version "1.12.1"}} + org.clojure/clojure {:mvn/version "1.12.1"} + ;; websockets + stylefruits/gniazdo {:mvn/version "1.2.2"} + ;; hato/hato {:mvn/version "1.0.0"} + } :aliases {;; Run with clj -T:build function-in-build diff --git a/src/micro_blog/config.clj b/src/micro_blog/config.clj index c12eaef..1a5354a 100644 --- a/src/micro_blog/config.clj +++ b/src/micro_blog/config.clj @@ -8,7 +8,11 @@ :mastodon-host "MASTODON_BASE_URL" :mastodon-account-id "MASTODON_ACCOUNT_ID" :api-host "API_HOST" - :api-port "API_PORT"}) + :api-port "API_PORT" + :nostr-fetcher-npub "NOSTR_FETCHER_NPUB" + :nostr-id "NOSTR_ID" + :nostr-relay "NOSTR_RELAY" + }) (defn- load-config [] (merge (read-string (slurp "config.edn")) diff --git a/src/micro_blog/nostr.clj b/src/micro_blog/nostr.clj new file mode 100644 index 0000000..1d952b8 --- /dev/null +++ b/src/micro_blog/nostr.clj @@ -0,0 +1,28 @@ +(ns micro-blog.nostr + (:require + [gniazdo.core :as ws] + [cheshire.core :as json] + [micro-blog.config :refer [config]])) + +;; :nostr-fetcher-npub "NOSTR_FETCHER_NPUB" +;; :nostr-id "NOSTR_ID" +;; :nostr-relay "NOSTR_RELAY" +(def socket (atom nil)) + +(defn connect [] + (reset! socket + (ws/connect + (@config :nostr-relay) + :on-receive #(prn 'received %)))) + +(defn subscribe-to-author [pubkey] + (let [sub-id "sub-1" + filter {:kinds [1] :authors [pubkey] :limit 10} + msg (json/generate-string ["REQ" sub-id filter])] + (send-msg msg))) + +(defn send-msg [msg] + (ws/send-msg @socket msg)) + +(defn close [] + (ws/close @socket))