From 3f9cd958f5b5335d7ba1a3c9c41a894d32b947b4 Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Sat, 11 Apr 2020 12:15:31 +0200 Subject: [PATCH] Allow configuring 'replacements' --- mastodon_bot/core.cljs | 7 ++++++- mastodon_bot/core_test.cljs | 6 ++++++ test.edn | 14 ++++++++++++++ testdata/twitter/tweet-mentions.edn | 1 + 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 testdata/twitter/tweet-mentions.edn diff --git a/mastodon_bot/core.cljs b/mastodon_bot/core.cljs index 4b25f45..9e02d6a 100755 --- a/mastodon_bot/core.cljs +++ b/mastodon_bot/core.cljs @@ -131,10 +131,15 @@ (exit-with-error error) (callback response))))) +(defn perform-replacements [post] + (assoc post :text (reduce-kv string/replace (:text post) (:replacements mastodon-config))) + ) + (defn post-items [last-post-time items] (doseq [{:keys [text media-links]} (->> items (remove #(blocked-content? (:text %))) - (filter #(> (:created-at %) last-post-time)))] + (filter #(> (:created-at %) last-post-time)) + (map perform-replacements))] (if media-links (post-status-with-images text media-links) (when-not (:media-only? mastodon-config) diff --git a/mastodon_bot/core_test.cljs b/mastodon_bot/core_test.cljs index fdc5e91..016f741 100755 --- a/mastodon_bot/core_test.cljs +++ b/mastodon_bot/core_test.cljs @@ -26,6 +26,12 @@ (:text (core/parse-tweet (readfile "testdata/twitter/normal-tweet.edn"))) ))) +(deftest test-replacements + (is (= + "πŸ’  Check out what has been going on during March in the world of @ReproBuilds! πŸ’  https://t.co/k6NsSO115z @opensuse@fosstodon.org @conservancy@mastodon.technology @PrototypeFund@mastodon.social @debian@fosstodon.org " + (:text (core/perform-replacements (core/parse-tweet (readfile "testdata/twitter/tweet-mentions.edn")))) + ))) + (cljs.test/run-tests) ; Don't run core's -main when testing diff --git a/test.edn b/test.edn index 6d30eaf..1bf349b 100644 --- a/test.edn +++ b/test.edn @@ -51,5 +51,19 @@ ;; optional keyword filter regexes ;; any posts not matching the regexes will be filtered out :keyword-filters [".*clojure.*"] + ;; optional replacements + :replacements {"@openSUSE " "@opensuse@fosstodon.org ", + "@archlinux " "", + "@lolamby " "", + "@conservancy " "@conservancy@mastodon.technology ", + "@prototypefund " "@PrototypeFund@mastodon.social ", + "@coreboot_org " "", + "@OpenTechFund " "", + "@OpenWrtSummit " "", + "@OpenMirage " "", + "@debian " "@debian@fosstodon.org ", + "@nixos_org " "", + "@lwnnet " "", + "@guixhpc" ""} ;; Replace Twitter links by Nitter :nitter-urls? false}} diff --git a/testdata/twitter/tweet-mentions.edn b/testdata/twitter/tweet-mentions.edn new file mode 100644 index 0000000..e7533d6 --- /dev/null +++ b/testdata/twitter/tweet-mentions.edn @@ -0,0 +1 @@ +{:in_reply_to_screen_name nil, :is_quote_status false, :coordinates nil, :in_reply_to_status_id_str nil, :place nil, :possibly_sensitive false, :geo nil, :in_reply_to_status_id nil, :entities {:hashtags [], :symbols [], :user_mentions [{:screen_name "ReproBuilds", :name "Reproducible Builds", :id 4061230191, :id_str "4061230191", :indices [64 76]} {:screen_name "openSUSE", :name "openSUSE Linux", :id 15134408, :id_str "15134408", :indices [104 113]} {:screen_name "archlinux", :name "archlinux", :id 15752204, :id_str "15752204", :indices [114 124]} {:screen_name "lolamby", :name "𝓡π“ͺ𝓢𝓫𝔂", :id 17737876, :id_str "17737876", :indices [125 133]} {:screen_name "conservancy", :name "Software Freedom Conservancy", :id 17518412, :id_str "17518412", :indices [134 146]} {:screen_name "prototypefund", :name "Prototype Fund", :id 720574992249921500, :id_str "720574992249921536", :indices [147 161]} {:screen_name "coreboot_org", :name "coreboot", :id 344207792, :id_str "344207792", :indices [162 175]} {:screen_name "OpenTechFund", :name "Open Technology Fund", :id 472177521, :id_str "472177521", :indices [176 189]} {:screen_name "OpenWrtSummit", :name "OpenWrt Summit", :id 975426387220811800, :id_str "975426387220811776", :indices [190 204]} {:screen_name "OpenMirage", :name "MirageOS", :id 356106530, :id_str "356106530", :indices [205 216]} {:screen_name "nixos_org", :name "NixOS", :id 271858833, :id_str "271858833", :indices [217 227]} {:screen_name "debian", :name "The Debian Project", :id 24253645, :id_str "24253645", :indices [228 235]} {:screen_name "lwnnet", :name "LWN.net", :id 34670660, :id_str "34670660", :indices [236 243]} {:screen_name "GuixHpc", :name "Guix HPC Project", :id 960571477467844600, :id_str "960571477467844614", :indices [244 252]}], :urls [{:url "https://t.co/k6NsSO115z", :expanded_url "https://reproducible-builds.org/reports/2020-03/", :display_url "reproducible-builds.org/reports/2020-0…", :indices [80 103]}], :media [{:sizes {:thumb {:w 150, :h 150, :resize "crop"}, :medium {:w 1046, :h 767, :resize "fit"}, :large {:w 1046, :h 767, :resize "fit"}, :small {:w 680, :h 499, :resize "fit"}}, :media_url_https "https://pbs.twimg.com/media/EU_cBTHWsAEPFed.jpg", :type "photo", :media_url "http://pbs.twimg.com/media/EU_cBTHWsAEPFed.jpg", :id 1247457536674017300, :expanded_url "https://twitter.com/ReproBuilds/status/1247457901637140480/photo/1", :url "https://t.co/t4q9PeczpO", :display_url "pic.twitter.com/t4q9PeczpO", :indices [253 276], :id_str "1247457536674017281"}]}, :source "TweetDeck", :lang "en", :in_reply_to_user_id_str nil, :full_text "πŸ’  Check out what has been going on during March in the world of @ReproBuilds! πŸ’  https://t.co/k6NsSO115z @openSUSE @archlinux @lolamby @conservancy @prototypefund @coreboot_org @OpenTechFund @OpenWrtSummit @OpenMirage @nixos_org @debian @lwnnet @guixhpc https://t.co/t4q9PeczpO", :id 1247457901637140500, :contributors nil, :extended_entities {:media [{:sizes {:thumb {:w 150, :h 150, :resize "crop"}, :medium {:w 1046, :h 767, :resize "fit"}, :large {:w 1046, :h 767, :resize "fit"}, :small {:w 680, :h 499, :resize "fit"}}, :media_url_https "https://pbs.twimg.com/media/EU_cBTHWsAEPFed.jpg", :type "photo", :media_url "http://pbs.twimg.com/media/EU_cBTHWsAEPFed.jpg", :id 1247457536674017300, :expanded_url "https://twitter.com/ReproBuilds/status/1247457901637140480/photo/1", :url "https://t.co/t4q9PeczpO", :display_url "pic.twitter.com/t4q9PeczpO", :indices [253 276], :id_str "1247457536674017281"}]}, :display_text_range [0 252], :truncated false, :retweeted false, :in_reply_to_user_id nil, :id_str "1247457901637140480", :favorited false, :user {:description "A set of software development practices that create an independently-verifiable path from source code to the binary code used by computers.", :profile_link_color "4682B4", :profile_sidebar_border_color "000000", :is_translation_enabled false, :profile_image_url "http://pbs.twimg.com/profile_images/1001801313070534656/gOSrgWid_normal.jpg", :profile_use_background_image false, :default_profile false, :profile_background_image_url "http://abs.twimg.com/images/themes/theme1/bg.png", :is_translator false, :profile_text_color "000000", :name "Reproducible Builds", :profile_background_image_url_https "https://abs.twimg.com/images/themes/theme1/bg.png", :favourites_count 141, :screen_name "ReproBuilds", :entities {:url {:urls [{:url "https://t.co/5Kl43N6n2C", :expanded_url "https://reproducible-builds.org/", :display_url "reproducible-builds.org", :indices [0 23]}]}, :description {:urls []}}, :listed_count 36, :profile_image_url_https "https://pbs.twimg.com/profile_images/1001801313070534656/gOSrgWid_normal.jpg", :statuses_count 703, :has_extended_profile false, :contributors_enabled false, :following true, :lang nil, :utc_offset nil, :notifications false, :default_profile_image false, :profile_background_color "000000", :id 4061230191, :follow_request_sent false, :url "https://t.co/5Kl43N6n2C", :translator_type "none", :time_zone nil, :profile_sidebar_fill_color "000000", :protected false, :profile_background_tile false, :id_str "4061230191", :geo_enabled false, :location "", :followers_count 1424, :friends_count 37, :verified false, :created_at "Wed Oct 28 14:52:10 +0000 2015"}, :retweet_count 5, :favorite_count 15, :created_at "Tue Apr 07 09:35:15 +0000 2020"}