diff --git a/src/micro_blog/blue_sky.clj b/src/micro_blog/blue_sky.clj index e350d24..60aa37d 100644 --- a/src/micro_blog/blue_sky.clj +++ b/src/micro_blog/blue_sky.clj @@ -105,5 +105,6 @@ :fullPost % :remoteId (:cid %) :authorId (get-in % [:author :handle]) + :tags (extract-tags %) :posted (get-in % [:record :createdAt]))) (map pb/save-post)))) diff --git a/src/micro_blog/pocket_base.clj b/src/micro_blog/pocket_base.clj index 996b29d..51eabbd 100644 --- a/src/micro_blog/pocket_base.clj +++ b/src/micro_blog/pocket_base.clj @@ -84,19 +84,45 @@ :as :json}) :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] (printf "Saving %s post with remoteId: %s\n" (name (:source post)) (:remoteId post)) (let [save-post-schema [:map [:source source-enum] [:fullPost :any] + [:tags [:list :string]] [: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 + :form-params (assoc post :tags (map get-tag-id (:tags post))) :content-type :json :as :json}))))