From 117b0c51b64aedb7a7177e1ad4f784a976b3a462 Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 26 Jun 2020 17:15:33 +0200 Subject: [PATCH] fix screen-name-append --- src/main/mastodon_bot/transform.cljs | 17 ++++++----- src/test/mastodon_bot/transform_rss_test.cljs | 29 +++++++++++++++++-- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/main/mastodon_bot/transform.cljs b/src/main/mastodon_bot/transform.cljs index 08df70e..d6dfa67 100644 --- a/src/main/mastodon_bot/transform.cljs +++ b/src/main/mastodon_bot/transform.cljs @@ -17,8 +17,9 @@ (s/def ::untrimmed-text string?) (s/def ::media-links string?) (s/def ::screen_name string?) -(def input? (s/keys :req-un [::created-at ::text ::screen_name] +(def intermediate? (s/keys :req-un [::created-at ::text ::screen_name] :opt-un [::media-links ::untrimmed-text])) + (def mastodon-output? (s/keys :req-un [::created-at ::text] :opt-un [::media-links])) (s/def ::source-type #{:twitter :rss :tumblr}) @@ -79,9 +80,9 @@ (def shortened-url-pattern #"(https?://)?(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?") -(defn-spec intermediate-resolve-urls string? +(defn-spec intermediate-resolve-urls intermediate? [resolve-urls? ::resolve-urls? - input input?] + input intermediate?] (if resolve-urls? (update input :text #(string/replace % shortened-url-pattern resolve-url)) input)) @@ -102,22 +103,22 @@ (when (not-empty (keyword-filter-regexes transformation)) (empty? (some #(re-find % text) (keyword-filter-regexes transformation))))))) -(defn-spec perform-replacements string? +(defn-spec perform-replacements intermediate? [transformation ::transformation - input input?] + input intermediate?] (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? [target masto/mastodon-target? - input input?] + input intermediate?] (let [{:keys [created-at text media-links screen_name untrimmed-text]} input {:keys [signature append-screen-name?]} target untrimmed (if (some? untrimmed-text) (str " " untrimmed-text) "") - sname (if (some? append-screen-name?) - (str "\n#" screen_name) "") + sname (if append-screen-name? + (str "\n#" screen_name) "") signature_text (if (some? signature) (str "\n" signature) "") diff --git a/src/test/mastodon_bot/transform_rss_test.cljs b/src/test/mastodon_bot/transform_rss_test.cljs index 5f0f48e..fc6e9f8 100755 --- a/src/test/mastodon_bot/transform_rss_test.cljs +++ b/src/test/mastodon_bot/transform_rss_test.cljs @@ -13,12 +13,37 @@ :text "Taking Theatre Online with WebGL and WebRTC\n\nhttps://chrisuehlinger.com/blog/2020/06/16/unshattering-the-audience-building-theatre-on-the-web-in-2020/"} (sut/intermediate-resolve-urls false intermediate-rss-item)))) -(deftest should-not-trim +(deftest should-not-append-screen-name (is (= {:created-at #inst "2020-06-26T12:17:33.000-00:00", - :text "Taking Theatre Online with WebGL and WebRTC\n\nhttps://chrisuehlinger.com/blog/2020/06/16/unshattering-the-audience-building-theatre-on-the-web-in-2020/\n#\n#rssbot", + :text "Taking Theatre Online with WebGL and WebRTC + +https://chrisuehlinger.com/blog/2020/06/16/unshattering-the-audience-building-theatre-on-the-web-in-2020/ +#rssbot", :reblogged true, :media-links nil} (sut/intermediate-to-mastodon {:target-type :mastodon :append-screen-name? false :max-post-length 500 :signature "#rssbot"} + intermediate-rss-item))) + (is (= {:created-at #inst "2020-06-26T12:17:33.000-00:00" + :text "Taking Theatre Online with WebGL and WebRTC + +https://chrisuehlinger.com/blog/2020/06/16/unshattering-the-audience-building-theatre-on-the-web-in-2020/ +#rssbot" + :reblogged true, :media-links nil} + (sut/intermediate-to-mastodon {:target-type :mastodon + :max-post-length 500 + :signature "#rssbot"} intermediate-rss-item)))) + +(deftest should-not-append-signature + (is (= {:created-at #inst "2020-06-26T12:17:33.000-00:00" + :text "Taking Theatre Online with WebGL and WebRTC + +https://chrisuehlinger.com/blog/2020/06/16/unshattering-the-audience-building-theatre-on-the-web-in-2020/" + :reblogged true, :media-links nil} + (sut/intermediate-to-mastodon {:target-type :mastodon + :append-screen-name? false + :max-post-length 500} + intermediate-rss-item))) + )