diff --git a/deps.edn b/deps.edn index bbb74f0..47e2f36 100644 --- a/deps.edn +++ b/deps.edn @@ -17,9 +17,7 @@ ;; metosin/muuntaja {:mvn/version "0.6.11"} org.clojure/clojure {:mvn/version "1.12.1"} ;; websockets - stylefruits/gniazdo {:mvn/version "1.2.2"} - ;; hato/hato {:mvn/version "1.0.0"} - } + 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 1a5354a..ad9372b 100644 --- a/src/micro_blog/config.clj +++ b/src/micro_blog/config.clj @@ -9,10 +9,10 @@ :mastodon-account-id "MASTODON_ACCOUNT_ID" :api-host "API_HOST" :api-port "API_PORT" - :nostr-fetcher-npub "NOSTR_FETCHER_NPUB" + ;; :nostr-fetcher-npub "NOSTR_FETCHER_NPUB" + :nostr-fetcher-id "NOSTR_FETCHER_PUB_HEX" :nostr-id "NOSTR_ID" - :nostr-relay "NOSTR_RELAY" - }) + :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 index 1d952b8..ed9adbb 100644 --- a/src/micro_blog/nostr.clj +++ b/src/micro_blog/nostr.clj @@ -1,28 +1,41 @@ (ns micro-blog.nostr (:require - [gniazdo.core :as ws] - [cheshire.core :as json] - [micro-blog.config :refer [config]])) + [micro-blog.pocket-base :as pb] + [hato.websocket :as ws] + [cheshire.core :as json] + [clojure.string :as str] + [micro-blog.config :refer [config]]) + (:import + [java.time Instant OffsetDateTime ZoneOffset] + [java.time.format DateTimeFormatter])) + +(defn pb-date-to-unix-timestamp-seconds [date-str] + (-> date-str + (str/replace " " "T") + (Instant/parse) + (.getEpochSecond))) + +(defn last-post-timestamp [] + (pb-date-to-unix-timestamp-seconds (:posted (pb/get-latest-post-by-source :nostr)))) ;; :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 %)))) + (reset! socket @(ws/websocket (@config :nostr-relay) + {:on-message (fn [_ws msg _last?] + (println "Received message:" msg)) + :on-close (fn [_ws _status _reason] + (println "WebSocket closed!"))}))) -(defn subscribe-to-author [pubkey] - (let [sub-id "sub-1" - filter {:kinds [1] :authors [pubkey] :limit 10} +;; (last-post-timestamp []) + +(defn subscribe-to-author [pubkey since] + (let [sub-id (@config :nostr-fetcher-id) + filter {:kinds [1] :authors [pubkey] :since since} msg (json/generate-string ["REQ" sub-id filter])] - (send-msg msg))) - -(defn send-msg [msg] - (ws/send-msg @socket msg)) + (.get (ws/send! @socket msg)))) (defn close [] - (ws/close @socket)) + (ws/close! @socket))