From 9c525deb9d44228370f78bfb998155224de2945f Mon Sep 17 00:00:00 2001 From: jem Date: Sat, 30 May 2020 15:07:11 +0200 Subject: [PATCH] mv replacements to transform --- README.md | 4 +++- src/main/mastodon_bot/mastodon_api.cljs | 10 +--------- src/main/mastodon_bot/transform.cljs | 9 ++++++++- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5bd038f..473cfb8 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,9 @@ with later timestamps to avoid duplicate posts. On the first run the timestamp w :content-filters [".*bannedsite.*"] ;; optional keyword filter regexes ;; any posts not matching the regexes will be filtered out - :keyword-filters [".*clojure.*"]}] + :keyword-filters [".*clojure.*"] + ;; TODO: Description & example missing here + :replacements nil}] } ``` diff --git a/src/main/mastodon_bot/mastodon_api.cljs b/src/main/mastodon_bot/mastodon_api.cljs index 6deed6e..0678460 100755 --- a/src/main/mastodon_bot/mastodon_api.cljs +++ b/src/main/mastodon_bot/mastodon_api.cljs @@ -16,7 +16,6 @@ (s/def ::sensitive? boolean?) (s/def ::media-only? boolean?) (s/def ::visibility #{"direct" "private" "unlisted" "public"}) -(s/def ::replacements string?) (s/def ::max-post-length (fn [n] (and (int? n) (<= n 500) @@ -30,7 +29,6 @@ ::append-screen-name? ::sensitive? ::media-only? - ;::replacements ])) (def mastodon-config? (s/merge mastodon-auth? mastodon-target?)) @@ -39,11 +37,6 @@ [target mastodon-target?] (:max-post-length target)) -(defn-spec perform-replacements string? - [mastodon-config mastodon-config? - text string?] - (reduce-kv string/replace text (:replacements mastodon-config))) - (defn-spec mastodon-client any? [mastodon-auth mastodon-auth?] (or (some-> mastodon-auth @@ -64,8 +57,7 @@ ([mastodon-auth target status-text media-ids callback] (let [{:keys [visibility sensitive?]} target] (-> (.post (mastodon-client mastodon-auth) "statuses" - (clj->js (merge {:status (->> status-text - (perform-replacements mastodon-auth))} + (clj->js (merge {:status status-text} (when media-ids {:media_ids media-ids}) (when sensitive? {:sensitive sensitive?}) (when visibility {:visibility visibility})))) diff --git a/src/main/mastodon_bot/transform.cljs b/src/main/mastodon_bot/transform.cljs index 8cff295..365bcbf 100644 --- a/src/main/mastodon_bot/transform.cljs +++ b/src/main/mastodon_bot/transform.cljs @@ -26,6 +26,7 @@ (s/def ::content-filters (s/* ::content-filter)) (s/def ::keyword-filter string?) (s/def ::keyword-filters (s/* ::keyword-filter)) +(s/def ::replacements any?) (defmulti source-type :type) (defmethod source-type :twitter-source [_] (s/merge (s/keys :req-un[::type]) twitter/twitter-source?)) @@ -36,7 +37,7 @@ (s/def ::target (s/multi-spec target-type ::type)) (s/def ::transformation (s/keys :req-un [::source ::target] - :opt-un [::resolve-urls? ::content-filters ::keyword-filters])) + :opt-un [::resolve-urls? ::content-filters ::keyword-filters ::replacements])) (def transformations? (s/* ::transformation)) (defn trim-text [text max-post-length] @@ -93,6 +94,11 @@ (when (not-empty (keyword-filter-regexes transformation)) (empty? (some #(re-find % text) (keyword-filter-regexes transformation))))))) +(defn-spec perform-replacements string? + [transformation ::transformation + input input?] + (update input :text #(reduce-kv string/replace % (:replacements transformation)))) + ; TODO: move this to mastodon-api - seems to belong strongly to mastodon (defn-spec intermediate-to-mastodon mastodon-output? @@ -132,6 +138,7 @@ (remove #(blocked-content? transformation (:text %))) (map #(intermediate-resolve-urls resolve-urls? %)) (map #(twitter/nitter-url source %)) + (map #(perform-replacements transformation %)) (map #(intermediate-to-mastodon mastodon-auth target %)) (masto/post-items mastodon-auth target last-post-time))))))