switch to hato for websockets
This commit is contained in:
parent
911f2f0330
commit
33a6ccd33b
3 changed files with 33 additions and 22 deletions
4
deps.edn
4
deps.edn
|
|
@ -17,9 +17,7 @@
|
||||||
;; metosin/muuntaja {:mvn/version "0.6.11"}
|
;; metosin/muuntaja {:mvn/version "0.6.11"}
|
||||||
org.clojure/clojure {:mvn/version "1.12.1"}
|
org.clojure/clojure {:mvn/version "1.12.1"}
|
||||||
;; websockets
|
;; websockets
|
||||||
stylefruits/gniazdo {:mvn/version "1.2.2"}
|
hato/hato {:mvn/version "1.0.0"}}
|
||||||
;; hato/hato {:mvn/version "1.0.0"}
|
|
||||||
}
|
|
||||||
|
|
||||||
:aliases
|
:aliases
|
||||||
{;; Run with clj -T:build function-in-build
|
{;; Run with clj -T:build function-in-build
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,10 @@
|
||||||
:mastodon-account-id "MASTODON_ACCOUNT_ID"
|
:mastodon-account-id "MASTODON_ACCOUNT_ID"
|
||||||
:api-host "API_HOST"
|
:api-host "API_HOST"
|
||||||
:api-port "API_PORT"
|
: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-id "NOSTR_ID"
|
||||||
:nostr-relay "NOSTR_RELAY"
|
:nostr-relay "NOSTR_RELAY"})
|
||||||
})
|
|
||||||
|
|
||||||
(defn- load-config []
|
(defn- load-config []
|
||||||
(merge (read-string (slurp "config.edn"))
|
(merge (read-string (slurp "config.edn"))
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,41 @@
|
||||||
(ns micro-blog.nostr
|
(ns micro-blog.nostr
|
||||||
(:require
|
(:require
|
||||||
[gniazdo.core :as ws]
|
[micro-blog.pocket-base :as pb]
|
||||||
[cheshire.core :as json]
|
[hato.websocket :as ws]
|
||||||
[micro-blog.config :refer [config]]))
|
[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-fetcher-npub "NOSTR_FETCHER_NPUB"
|
||||||
;; :nostr-id "NOSTR_ID"
|
;; :nostr-id "NOSTR_ID"
|
||||||
;; :nostr-relay "NOSTR_RELAY"
|
;; :nostr-relay "NOSTR_RELAY"
|
||||||
(def socket (atom nil))
|
(def socket (atom nil))
|
||||||
|
|
||||||
(defn connect []
|
(defn connect []
|
||||||
(reset! socket
|
(reset! socket @(ws/websocket (@config :nostr-relay)
|
||||||
(ws/connect
|
{:on-message (fn [_ws msg _last?]
|
||||||
(@config :nostr-relay)
|
(println "Received message:" msg))
|
||||||
:on-receive #(prn 'received %))))
|
:on-close (fn [_ws _status _reason]
|
||||||
|
(println "WebSocket closed!"))})))
|
||||||
|
|
||||||
(defn subscribe-to-author [pubkey]
|
;; (last-post-timestamp [])
|
||||||
(let [sub-id "sub-1"
|
|
||||||
filter {:kinds [1] :authors [pubkey] :limit 10}
|
(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])]
|
msg (json/generate-string ["REQ" sub-id filter])]
|
||||||
(send-msg msg)))
|
(.get (ws/send! @socket msg))))
|
||||||
|
|
||||||
(defn send-msg [msg]
|
|
||||||
(ws/send-msg @socket msg))
|
|
||||||
|
|
||||||
(defn close []
|
(defn close []
|
||||||
(ws/close @socket))
|
(ws/close! @socket))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue