From 6dab0a4ebd9ef6538ca0a256f5eb858eb0a5008d Mon Sep 17 00:00:00 2001 From: "Sotnikov, Dmitri" Date: Mon, 23 Apr 2018 17:47:41 -0400 Subject: [PATCH] added support for filtering out content --- README.md | 5 ++++- mastodon-bot.cljs | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 85b45b5..4311799 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,10 @@ the bot will post the timeline from the specified Twitter/Tumblr accounts and RS :api_url "https://botsin.space/api/v1/" :max-post-length 300 ;; optional signature for posts - :signature "#newsbot"}} + :signature "#newsbot" + ;; optional content filter regexes + ;; any posts matching the regexes will be filtered out + :content-filters [".*bannedsite.*"]}} ``` * the bot looks for `config.edn` at its relative path by default, an alternative location can be specified either using the `MASTODON_BOT_CONFIG` environment variable or passing the path to config as an argument diff --git a/mastodon-bot.cljs b/mastodon-bot.cljs index 790e750..5afdd54 100755 --- a/mastodon-bot.cljs +++ b/mastodon-bot.cljs @@ -20,6 +20,11 @@ (def config (-> (find-config) fs/readFileSync str edn/read-string)) +(def content-filter-regexes (mapv re-pattern (-> config :mastodon :content-filters))) + +(defn blocked-content? [text] + (boolean (some #(re-matches % text) content-filter-regexes))) + (def max-post-length (or (-> config :mastodon :max-post-length) 300)) (def mastodon-client (or (some-> config :mastodon clj->js mastodon.) @@ -72,7 +77,9 @@ (.then (.get mastodon-client "timelines/home" #js {}) #(-> % .-data js->edn callback))) (defn post-items [last-post-time items] - (doseq [{:keys [text media-links]} (filter #(> (:created-at %) last-post-time) items)] + (doseq [{:keys [text media-links]} (->> items + (remove #(blocked-content? (:text %))) + (filter #(> (:created-at %) last-post-time)))] (if media-links (post-status-with-images text media-links) (post-status text))))