switch to hato for websockets

This commit is contained in:
Travis Shears 2025-08-18 10:05:16 +02:00
parent 911f2f0330
commit 33a6ccd33b
3 changed files with 33 additions and 22 deletions

View file

@ -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

View file

@ -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"))

View file

@ -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))