start nostr websockets on main app start

This commit is contained in:
Travis Shears 2025-08-21 13:04:24 +02:00
parent 33a6ccd33b
commit 0b89cb40ab
3 changed files with 43 additions and 7 deletions

View file

@ -5,6 +5,7 @@
[taoensso.telemere :as tel]
[micro-blog.logging.main :as logging]
micro-blog.api
[micro-blog.nostr :as nostr]
[micro-blog.blue-sky :as blue-sky]
[micro-blog.mastodon :as masto]))
@ -15,6 +16,8 @@
(logging/setup-logging)
(tel/log! :info "Setting up API")
(micro-blog.api/start)
(tel/log! :info "Setting up nostr scraper")
(nostr/start)
(tel/log! :info "Setting up crons")
(doseq [[i cron] (map-indexed vector crons)]
(let [start (.plus (Instant/now) (Duration/ofMinutes (* i 5)))]

View file

@ -1,9 +1,12 @@
(ns micro-blog.nostr
(:require
[micro-blog.pocket-base :as pb]
[micro-blog.is-tech]
[taoensso.telemere :as tel]
[hato.websocket :as ws]
[cheshire.core :as json]
[clojure.string :as str]
[clojure.pprint :refer [pprint]]
[micro-blog.config :refer [config]])
(:import
[java.time Instant OffsetDateTime ZoneOffset]
@ -15,22 +18,49 @@
(Instant/parse)
(.getEpochSecond)))
(defn nostr-date-to-pb [ts]
(let [instant (java.time.Instant/ofEpochSecond ts)
formatter (java.time.format.DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss.SSSX")
zoned (.atZone instant java.time.ZoneOffset/UTC)]
(.format formatter zoned)))
(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"
(defn transform-post [post]
(tel/log! {:level :info :data {:post post}} "Transforming nostr post")
(hash-map :source :nostr
:fullPost post
:remoteId (get post "id")
:isTech (micro-blog.is-tech/is-tech? (get post "content"))
:authorId (get post "pubkey")
:tags (reduce (fn [acc tag]
(let [tag-type (first tag)
tag-value (second tag)]
(if (= tag-type "t")
(conj acc tag-value)
acc))) [] (get post "tags"))
:images []
:posted (nostr-date-to-pb (get post "created_at"))))
(defn process-msg [raw-msg]
(let [msg (json/parse-string (.toString raw-msg))]
(tel/log! {:level :info :data {:msg msg}} "Processing nostr message")
(let [msg-type (first msg) ;; ex: EVENT
event (nth msg 2)]
(when (and (= (get event "kind") 1) (= msg-type "EVENT"))
(-> event
transform-post
pb/save-post)))))
(def socket (atom nil))
(defn connect []
(reset! socket @(ws/websocket (@config :nostr-relay)
{:on-message (fn [_ws msg _last?]
(println "Received message:" msg))
(process-msg 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}
@ -39,3 +69,7 @@
(defn close []
(ws/close! @socket))
(defn start []
(connect)
(subscribe-to-author (@config :nostr-id) (last-post-timestamp)))

View file

@ -1,6 +1,5 @@
(ns micro-blog.pocket-base
(:require
[clojure.pprint :refer [pprint]]
[clojure.string :as str]
[clj-http.client :as http-client]
[malli.core :as m]