mv visibility to transform

master
jem 4 years ago
parent 64fa3120eb
commit 0c72647d5b

@ -55,9 +55,6 @@ with later timestamps to avoid duplicate posts. On the first run the timestamp w
;; optional boolean defaults to false ;; optional boolean defaults to false
;; only sources containing media will be posted when set to true ;; only sources containing media will be posted when set to true
:media-only? true :media-only? true
;; optional visibility flag: direct, private, unlisted, public
;; defaults to public
:visibility "unlisted"
;; optional limit for the post length ;; optional limit for the post length
:max-post-length 300 :max-post-length 300
;; optionally try to resolve URLs in posts to skip URL shorteners ;; 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 ;; optional flag specifying wether the name of the account
;; will be appended in the post, defaults to false ;; will be appended in the post, defaults to false
:append-screen-name? false :append-screen-name? false
;; optional visibility flag: direct, private, unlisted, public
;; defaults to public
:visibility "unlisted"
;; optional signature for posts ;; optional signature for posts
:signature "#newsbot"}}] :signature "#newsbot"}}]
} }

@ -48,12 +48,14 @@
(:target (first (transform config))) %)) (:target (first (transform config))) %))
(masto/post-items (masto/post-items
(mastodon-auth config) (mastodon-auth config)
(:target (first (transform config)))
last-post-time)))) last-post-time))))
(defn parse-feed [last-post-time parser [title url]] (defn parse-feed [last-post-time parser [title url]]
(-> (.parseURL parser url) (-> (.parseURL parser url)
(.then #(masto/post-items (.then #(masto/post-items
(mastodon-auth config) (mastodon-auth config)
(:target (first (transform config)))
last-post-time last-post-time
(for [{:keys [title isoDate pubDate content link]} (-> % infra/js->edn :items)] (for [{:keys [title isoDate pubDate content link]} (-> % infra/js->edn :items)]
{:created-at (js/Date. (or isoDate pubDate)) {:created-at (js/Date. (or isoDate pubDate))

@ -19,7 +19,7 @@
(s/def ::sensitive? boolean?) (s/def ::sensitive? boolean?)
(s/def ::resolve-urls? boolean?) (s/def ::resolve-urls? boolean?)
(s/def ::nitter-urls? boolean?) (s/def ::nitter-urls? boolean?)
(s/def ::visibility string?) (s/def ::visibility #{"direct" "private" "unlisted" "public"})
(s/def ::replacements string?) (s/def ::replacements string?)
(s/def ::max-post-length (fn [n] (and (s/def ::max-post-length (fn [n] (and
(int? n) (int? n)
@ -33,7 +33,7 @@
;::content-filters ::keyword-filters ;::content-filters ::keyword-filters
;::max-post-length ;::max-post-length
::signature ::signature
;::visibility ::visibility
::append-screen-name? ::append-screen-name?
;::sensitive? ::resolve-urls? ;::sensitive? ::resolve-urls?
;::nitter-urls? ::replacements ;::nitter-urls? ::replacements
@ -106,64 +106,72 @@
(string/replace #"https://twitter.com" "https://nitter.net"))) (string/replace #"https://twitter.com" "https://nitter.net")))
(defn post-status (defn post-status
([mastodon-config status-text] ([mastodon-auth target status-text]
(post-status mastodon-config status-text nil print)) (post-status mastodon-auth target status-text nil print))
([mastodon-config status-text media-ids] ([mastodon-auth target status-text media-ids]
(post-status mastodon-config status-text media-ids print)) (post-status mastodon-auth target status-text media-ids print))
([mastodon-config status-text media-ids callback] ([mastodon-auth target status-text media-ids callback]
(let [{:keys [sensitive? signature visibility]} mastodon-config] (let [{:keys [sensitive?]} mastodon-auth
(-> (.post (mastodon-client mastodon-config) "statuses" {:keys [visibility]} target]
(-> (.post (mastodon-client mastodon-auth) "statuses"
(clj->js (merge {:status (->> status-text (clj->js (merge {:status (->> status-text
(resolve-urls mastodon-config) (resolve-urls mastodon-auth)
(perform-replacements mastodon-config))} (perform-replacements mastodon-auth))}
(when media-ids {:media_ids media-ids}) (when media-ids {:media_ids media-ids})
(when sensitive? {:sensitive sensitive?}) (when sensitive? {:sensitive sensitive?})
(when visibility {:visibility visibility})))) (when visibility {:visibility visibility}))))
(.then #(-> % callback)))))) (.then #(-> % callback))))))
(defn-spec post-image any? (defn-spec post-image any?
[mastodon-config mastodon-config? [mastodon-auth mastodon-auth?
target mastodon-target?
image-stream any? image-stream any?
description string? description string?
callback fn?] callback fn?]
(-> (.post (mastodon-client mastodon-config) "media" (-> (.post (mastodon-client mastodon-auth) "media"
#js {:file image-stream :description description}) #js {:file image-stream :description description})
(.then #(-> % .-data .-id callback)))) (.then #(-> % .-data .-id callback))))
(defn post-status-with-images (defn post-status-with-images
([mastodon-config status-text urls] ([mastodon-auth target status-text urls]
(post-status-with-images mastodon-config status-text urls [] print)) (post-status-with-images mastodon-auth target status-text urls [] print))
([mastodon-config status-text urls ids] ([mastodon-auth target status-text urls ids]
(post-status-with-images mastodon-config status-text urls ids print)) (post-status-with-images mastodon-auth target status-text urls ids print))
([mastodon-config status-text [url & urls] ids callback] ([mastodon-auth target status-text [url & urls] ids callback]
(if url (if url
(-> request (-> request
(.get url) (.get url)
(.on "response" (.on "response"
(fn [image-stream] (fn [image-stream]
(post-image mastodon-config image-stream status-text (post-image mastodon-auth target image-stream status-text
#(post-status-with-images mastodon-config status-text urls (conj ids %) callback))))) #(post-status-with-images mastodon-auth
(post-status mastodon-config status-text (not-empty ids) callback)))) target
status-text
(defn-spec get-mastodon-timeline any? urls
[mastodon-auth mastodon-auth? (conj ids %)
callback fn?] callback)))))
(.then (.get (mastodon-client mastodon-auth) (post-status mastodon-auth target status-text (not-empty ids) callback))))
(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)))))
(defn-spec post-items any? (defn-spec post-items any?
[mastodon-config mastodon-config? [mastodon-auth mastodon-auth?
target mastodon-target?
last-post-time any? last-post-time any?
items any?] items any?]
(doseq [{:keys [text media-links]} (doseq [{:keys [text media-links]}
(->> items (->> items
(remove #(blocked-content? mastodon-config (:text %))) (remove #(blocked-content? mastodon-auth (:text %)))
(filter #(> (:created-at %) last-post-time)))] (filter #(> (:created-at %) last-post-time)))]
(if media-links (if media-links
(post-status-with-images mastodon-config text media-links) (post-status-with-images mastodon-auth target text media-links)
(when-not (::media-only? mastodon-config) (when-not (::media-only? mastodon-auth)
(post-status mastodon-config text))))) (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)))))

@ -82,7 +82,7 @@
(->> (infra/js->edn tweets) (->> (infra/js->edn tweets)
(map twitter/parse-tweet) (map twitter/parse-tweet)
(map #(intermediate-to-mastodon mastodon-auth target %)) (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? (defn-spec tweets-to-mastodon any?
[mastodon-auth masto/mastodon-auth? [mastodon-auth masto/mastodon-auth?

@ -15,4 +15,5 @@
:accounts ["an-twitter-account"]} :accounts ["an-twitter-account"]}
:target {:type :mastodon-target :target {:type :mastodon-target
:append-screen-name? true :append-screen-name? true
:visibility "unlisted"
:signature "my-bot"}}]))) :signature "my-bot"}}])))