mv visibility to transform

This commit is contained in:
jem 2020-05-29 21:13:43 +02:00
parent 64fa3120eb
commit 0c72647d5b
5 changed files with 51 additions and 40 deletions

View file

@ -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"}}]
}

View file

@ -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))

View file

@ -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))))
(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-auth mastodon-auth?
target mastodon-target?
last-post-time any?
items any?]
(doseq [{:keys [text media-links]}
(->> items
(remove #(blocked-content? mastodon-auth (:text %)))
(filter #(> (:created-at %) last-post-time)))]
(if media-links
(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 {})
(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?
[mastodon-config mastodon-config?
last-post-time any?
items any?]
(doseq [{:keys [text media-links]}
(->> items
(remove #(blocked-content? mastodon-config (: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)))))

View file

@ -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?

View file

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