micro_blog_repo_fetchers/src/micro_blog/nostr.clj

41 lines
1.3 KiB
Clojure

(ns micro-blog.nostr
(:require
[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/websocket (@config :nostr-relay)
{:on-message (fn [_ws msg _last?]
(println "Received message:" msg))
:on-close (fn [_ws _status _reason]
(println "WebSocket closed!"))})))
;; (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])]
(.get (ws/send! @socket msg))))
(defn close []
(ws/close! @socket))