From 0c72647d5b71fec00b60ecf0364cb8ba2d296c8b Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 29 May 2020 21:13:43 +0200 Subject: [PATCH] mv visibility to transform --- README.md | 6 +- src/main/mastodon_bot/core.cljs | 2 + src/main/mastodon_bot/mastodon_api.cljs | 80 +++++++++++++---------- src/main/mastodon_bot/transform.cljs | 2 +- src/test/mastodon_bot/transform_test.cljs | 1 + 5 files changed, 51 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 76a6692..6335c76 100644 --- a/README.md +++ b/README.md @@ -55,9 +55,6 @@ with later timestamps to avoid duplicate posts. On the first run the timestamp w ;; optional boolean defaults to false ;; only sources containing media will be posted when set to true :media-only? true - ;; optional visibility flag: direct, private, unlisted, public - ;; defaults to public - :visibility "unlisted" ;; optional limit for the post length :max-post-length 300 ;; optionally try to resolve URLs in posts to skip URL shorteners @@ -82,6 +79,9 @@ with later timestamps to avoid duplicate posts. On the first run the timestamp w ;; optional flag specifying wether the name of the account ;; will be appended in the post, defaults to false :append-screen-name? false + ;; optional visibility flag: direct, private, unlisted, public + ;; defaults to public + :visibility "unlisted" ;; optional signature for posts :signature "#newsbot"}}] } diff --git a/src/main/mastodon_bot/core.cljs b/src/main/mastodon_bot/core.cljs index 463e40a..11b5c53 100755 --- a/src/main/mastodon_bot/core.cljs +++ b/src/main/mastodon_bot/core.cljs @@ -48,12 +48,14 @@ (:target (first (transform config))) %)) (masto/post-items (mastodon-auth config) + (:target (first (transform config))) last-post-time)))) (defn parse-feed [last-post-time parser [title url]] (-> (.parseURL parser url) (.then #(masto/post-items (mastodon-auth config) + (:target (first (transform config))) last-post-time (for [{:keys [title isoDate pubDate content link]} (-> % infra/js->edn :items)] {:created-at (js/Date. (or isoDate pubDate)) diff --git a/src/main/mastodon_bot/mastodon_api.cljs b/src/main/mastodon_bot/mastodon_api.cljs index 4308850..b27dc41 100755 --- a/src/main/mastodon_bot/mastodon_api.cljs +++ b/src/main/mastodon_bot/mastodon_api.cljs @@ -19,7 +19,7 @@ (s/def ::sensitive? boolean?) (s/def ::resolve-urls? boolean?) (s/def ::nitter-urls? boolean?) -(s/def ::visibility string?) +(s/def ::visibility #{"direct" "private" "unlisted" "public"}) (s/def ::replacements string?) (s/def ::max-post-length (fn [n] (and (int? n) @@ -33,7 +33,7 @@ ;::content-filters ::keyword-filters ;::max-post-length ::signature - ;::visibility + ::visibility ::append-screen-name? ;::sensitive? ::resolve-urls? ;::nitter-urls? ::replacements @@ -106,64 +106,72 @@ (string/replace #"https://twitter.com" "https://nitter.net"))) (defn post-status - ([mastodon-config status-text] - (post-status mastodon-config status-text nil print)) - ([mastodon-config status-text media-ids] - (post-status mastodon-config status-text media-ids print)) - ([mastodon-config status-text media-ids callback] - (let [{:keys [sensitive? signature visibility]} mastodon-config] - (-> (.post (mastodon-client mastodon-config) "statuses" + ([mastodon-auth target status-text] + (post-status mastodon-auth target status-text nil print)) + ([mastodon-auth target status-text media-ids] + (post-status mastodon-auth target status-text media-ids print)) + ([mastodon-auth target status-text media-ids callback] + (let [{:keys [sensitive?]} mastodon-auth + {:keys [visibility]} target] + (-> (.post (mastodon-client mastodon-auth) "statuses" (clj->js (merge {:status (->> status-text - (resolve-urls mastodon-config) - (perform-replacements mastodon-config))} + (resolve-urls mastodon-auth) + (perform-replacements mastodon-auth))} (when media-ids {:media_ids media-ids}) (when sensitive? {:sensitive sensitive?}) (when visibility {:visibility visibility})))) (.then #(-> % callback)))))) (defn-spec post-image any? - [mastodon-config mastodon-config? + [mastodon-auth mastodon-auth? + target mastodon-target? image-stream any? description string? callback fn?] - (-> (.post (mastodon-client mastodon-config) "media" + (-> (.post (mastodon-client mastodon-auth) "media" #js {:file image-stream :description description}) (.then #(-> % .-data .-id callback)))) (defn post-status-with-images - ([mastodon-config status-text urls] - (post-status-with-images mastodon-config status-text urls [] print)) - ([mastodon-config status-text urls ids] - (post-status-with-images mastodon-config status-text urls ids print)) - ([mastodon-config status-text [url & urls] ids callback] + ([mastodon-auth target status-text urls] + (post-status-with-images mastodon-auth target status-text urls [] print)) + ([mastodon-auth target status-text urls ids] + (post-status-with-images mastodon-auth target status-text urls ids print)) + ([mastodon-auth target status-text [url & urls] ids callback] (if url (-> request (.get url) (.on "response" (fn [image-stream] - (post-image mastodon-config image-stream status-text - #(post-status-with-images mastodon-config status-text urls (conj ids %) callback))))) - (post-status mastodon-config status-text (not-empty ids) callback)))) - -(defn-spec get-mastodon-timeline any? - [mastodon-auth mastodon-auth? - callback fn?] - (.then (.get (mastodon-client mastodon-auth) - (str "accounts/" (:account-id mastodon-auth)"/statuses") #js {}) - #(let [response (-> % .-data infra/js->edn)] - (if-let [error (::error response)] - (infra/exit-with-error error) - (callback response))))) + (post-image mastodon-auth target image-stream status-text + #(post-status-with-images mastodon-auth + target + status-text + urls + (conj ids %) + callback))))) + (post-status mastodon-auth target status-text (not-empty ids) callback)))) (defn-spec post-items any? - [mastodon-config mastodon-config? + [mastodon-auth mastodon-auth? + target mastodon-target? last-post-time any? items any?] (doseq [{:keys [text media-links]} (->> items - (remove #(blocked-content? mastodon-config (:text %))) + (remove #(blocked-content? mastodon-auth (:text %))) (filter #(> (:created-at %) last-post-time)))] (if media-links - (post-status-with-images mastodon-config text media-links) - (when-not (::media-only? mastodon-config) - (post-status mastodon-config text))))) + (post-status-with-images mastodon-auth target text media-links) + (when-not (::media-only? mastodon-auth) + (post-status mastodon-auth target text))))) + +(defn-spec get-mastodon-timeline any? + [mastodon-auth mastodon-auth? + callback fn?] + (.then (.get (mastodon-client mastodon-auth) + (str "accounts/" (:account-id mastodon-auth) "/statuses") #js {}) + #(let [response (-> % .-data infra/js->edn)] + (if-let [error (::error response)] + (infra/exit-with-error error) + (callback response))))) \ No newline at end of file diff --git a/src/main/mastodon_bot/transform.cljs b/src/main/mastodon_bot/transform.cljs index 173b6d7..d2b1c3a 100644 --- a/src/main/mastodon_bot/transform.cljs +++ b/src/main/mastodon_bot/transform.cljs @@ -82,7 +82,7 @@ (->> (infra/js->edn tweets) (map twitter/parse-tweet) (map #(intermediate-to-mastodon mastodon-auth target %)) - (masto/post-items mastodon-auth last-post-time))))) + (masto/post-items mastodon-auth target last-post-time))))) (defn-spec tweets-to-mastodon any? [mastodon-auth masto/mastodon-auth? diff --git a/src/test/mastodon_bot/transform_test.cljs b/src/test/mastodon_bot/transform_test.cljs index 8f181b8..66faf2a 100755 --- a/src/test/mastodon_bot/transform_test.cljs +++ b/src/test/mastodon_bot/transform_test.cljs @@ -15,4 +15,5 @@ :accounts ["an-twitter-account"]} :target {:type :mastodon-target :append-screen-name? true + :visibility "unlisted" :signature "my-bot"}}])))