fix screen-name-append

This commit is contained in:
jem 2020-06-26 17:15:33 +02:00
parent 7089abf0f9
commit 117b0c51b6
2 changed files with 36 additions and 10 deletions

View file

@ -17,8 +17,9 @@
(s/def ::untrimmed-text string?) (s/def ::untrimmed-text string?)
(s/def ::media-links string?) (s/def ::media-links string?)
(s/def ::screen_name 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])) :opt-un [::media-links ::untrimmed-text]))
(def mastodon-output? (s/keys :req-un [::created-at ::text] (def mastodon-output? (s/keys :req-un [::created-at ::text]
:opt-un [::media-links])) :opt-un [::media-links]))
(s/def ::source-type #{:twitter :rss :tumblr}) (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*)?") (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? [resolve-urls? ::resolve-urls?
input input?] input intermediate?]
(if resolve-urls? (if resolve-urls?
(update input :text #(string/replace % shortened-url-pattern resolve-url)) (update input :text #(string/replace % shortened-url-pattern resolve-url))
input)) input))
@ -102,22 +103,22 @@
(when (not-empty (keyword-filter-regexes transformation)) (when (not-empty (keyword-filter-regexes transformation))
(empty? (some #(re-find % text) (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 [transformation ::transformation
input input?] input intermediate?]
(update input :text #(reduce-kv string/replace % (:replacements transformation)))) (update input :text #(reduce-kv string/replace % (:replacements transformation))))
; TODO: move this to mastodon-api - seems to belong strongly to mastodon ; TODO: move this to mastodon-api - seems to belong strongly to mastodon
(defn-spec intermediate-to-mastodon mastodon-output? (defn-spec intermediate-to-mastodon mastodon-output?
[target masto/mastodon-target? [target masto/mastodon-target?
input input?] input intermediate?]
(let [{:keys [created-at text media-links screen_name untrimmed-text]} input (let [{:keys [created-at text media-links screen_name untrimmed-text]} input
{:keys [signature append-screen-name?]} target {:keys [signature append-screen-name?]} target
untrimmed (if (some? untrimmed-text) untrimmed (if (some? untrimmed-text)
(str " " untrimmed-text) "") (str " " untrimmed-text) "")
sname (if (some? append-screen-name?) sname (if append-screen-name?
(str "\n#" screen_name) "") (str "\n#" screen_name) "")
signature_text (if (some? signature) signature_text (if (some? signature)
(str "\n" signature) (str "\n" signature)
"") "")

View file

@ -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/"} :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)))) (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", (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} :reblogged true, :media-links nil}
(sut/intermediate-to-mastodon {:target-type :mastodon (sut/intermediate-to-mastodon {:target-type :mastodon
:append-screen-name? false :append-screen-name? false
:max-post-length 500 :max-post-length 500
:signature "#rssbot"} :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)))) 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)))
)