get saving of blue sky posts working

TODO: blue sky tags
TODO: blue sky images
This commit is contained in:
Travis Shears 2025-07-20 13:12:12 +02:00
parent c247774256
commit d6c4254e94
3 changed files with 65 additions and 6 deletions

View file

@ -4,6 +4,7 @@
[clojure.string :as str]
[clj-http.client :as http-client]
[malli.core :as m]
[micro-blog.utils :as utils]
[micro-blog.config :refer [config]]))
(defonce token-cache ^:private (atom {:token nil :fetched-at nil}))
@ -37,9 +38,9 @@
(reset! token-cache {:token new-token :fetched-at (now)})
new-token))))
(def sources #{:pleroma :blue_sky :mastodon :pixelfed :nostr})
(def source-enum [:enum :pleroma :blue_sky :mastodon :pixelfed :nostr])
(defn valid-source? [source]
(contains? sources source))
(m/validate source-enum source))
(defn get-latest-post-remote-id-by-source [source]
(let [res-schema
@ -69,3 +70,33 @@
(m/explain res-schema x)
(throw (ex-info "Res does not follow schema" {:res x}))))
(-> x :items first :remoteId))))
(defn post-with-remote-id-already-saved? [remote-id]
(->
(http-client/get (str (@config :pocket-base-host) "/api/collections/micro_blog_posts/records")
{:headers {"Authorization" (get-login-token-with-cache)}
:query-params {:page 1
"perPage" 1
:filter (str "remoteId = '" remote-id "'")
:fields "id"
"skipTotal" true}
:content-type :json
:as :json})
:body :items count (> 0)))
(defn save-post [post]
(printf "Saving %s post with remoteId: %s\n" (name (:source post)) (:remoteId post))
(let [save-post-schema [:map
[:source source-enum]
[:fullPost :any]
[:remoteId :string]
[:authorId :string]
[:posted :string]]]
(utils/validate-with-throw post save-post-schema)
(if (post-with-remote-id-already-saved? (:remoteId post))
(println "post already saved")
(http-client/post (str (@config :pocket-base-host) "/api/collections/micro_blog_posts/records")
{:headers {"Authorization" (get-login-token-with-cache)}
:form-params post
:content-type :json
:as :json}))))