save tags on blue sky posts

This commit is contained in:
Travis Shears 2025-07-21 23:18:13 +02:00
parent d6c4254e94
commit fd146d622b
2 changed files with 28 additions and 1 deletions

View file

@ -105,5 +105,6 @@
:fullPost % :fullPost %
:remoteId (:cid %) :remoteId (:cid %)
:authorId (get-in % [:author :handle]) :authorId (get-in % [:author :handle])
:tags (extract-tags %)
:posted (get-in % [:record :createdAt]))) :posted (get-in % [:record :createdAt])))
(map pb/save-post)))) (map pb/save-post))))

View file

@ -84,19 +84,45 @@
:as :json}) :as :json})
:body :items count (> 0))) :body :items count (> 0)))
(defn get-tag-id
"returns id of tag in pocketbase, creating it if it does not exist"
[tag]
(let [existing-tag-id
(->
(http-client/get (str (@config :pocket-base-host) "/api/collections/micro_blog_tags/records")
{:headers {"Authorization" (get-login-token-with-cache)}
:query-params {:page 1
"perPage" 1
:filter (str "tag = '" tag "'")
:fields "id"
"skipTotal" true}
:content-type :json
:as :json})
(get-in [:body :items 0 :id]))]
(if (not (nil? existing-tag-id)) existing-tag-id
(->
(http-client/post (str (@config :pocket-base-host) "/api/collections/micro_blog_tags/records")
{:headers {"Authorization" (get-login-token-with-cache)}
:form-params {:tag tag}
:content-type :json
:as :json})
(get-in [:body :id])))))
(defn save-post [post] (defn save-post [post]
(printf "Saving %s post with remoteId: %s\n" (name (:source post)) (:remoteId post)) (printf "Saving %s post with remoteId: %s\n" (name (:source post)) (:remoteId post))
(let [save-post-schema [:map (let [save-post-schema [:map
[:source source-enum] [:source source-enum]
[:fullPost :any] [:fullPost :any]
[:tags [:list :string]]
[:remoteId :string] [:remoteId :string]
[:authorId :string] [:authorId :string]
[:posted :string]]] [:posted :string]]]
(utils/validate-with-throw post save-post-schema) (utils/validate-with-throw post save-post-schema)
(if (post-with-remote-id-already-saved? (:remoteId post)) (if (post-with-remote-id-already-saved? (:remoteId post))
(println "post already saved") (println "post already saved")
(http-client/post (str (@config :pocket-base-host) "/api/collections/micro_blog_posts/records") (http-client/post (str (@config :pocket-base-host) "/api/collections/micro_blog_posts/records")
{:headers {"Authorization" (get-login-token-with-cache)} {:headers {"Authorization" (get-login-token-with-cache)}
:form-params post :form-params (assoc post :tags (map get-tag-id (:tags post)))
:content-type :json :content-type :json
:as :json})))) :as :json}))))