refactor rendering out

This commit is contained in:
jem 2020-07-08 20:35:01 +02:00
parent ed8f26ec50
commit 431df76787
5 changed files with 65 additions and 83 deletions

View file

@ -27,32 +27,6 @@
(defn mastojs->edn [response]
(-> response .-data infra/js->edn))
(defn mastocard->html [card]
(when (some? card)
(let [{:keys [title description image url]} card]
[:div {:class "card" :url url}
(when (some? image)
[:img {:class "card-img-top" :src image}])
[:h3 {:class "card-title"} title]
[:p {:class "card-body"} description]])))
(defn masto->html [statuses]
[:ul {:class "list-group"}
(map (fn [status]
(let [{:keys [created_at card]} status
date (t/parse created_at)]
[:li {:class "list-group-item, card"}
[:div {:class "card-body"}
[:h2 {:class "card-title"}
[:a {:href (get-in status [:account :url])}
(t/unparse (t/formatters :date) date) " "
(t/unparse (t/formatters :hour-minute-second) date)]]
[:p {:class "card-text"}
(:content status)
(mastocard->html card)]]]))
statuses)])
(defn-spec mastodon-client any?
[host-url ::host-url]
(let [mastodon-config

View file

@ -15,11 +15,13 @@
; limitations under the License.
(ns dda.masto-embed.app
(:require
[dda.masto-embed.api :as api]
[dda.masto-embed.infra :as infra]
[cljs.core.async :refer [go close! put! take! timeout chan <! >!]]
[cljs.core.async.interop :refer-macros [<p!]]
[hiccups.runtime :refer [render-html]]))
[hiccups.runtime :refer [render-html]]
[dda.masto-embed.api :as api]
[dda.masto-embed.infra :as infra]
[dda.masto-embed.render-bootstrap :as rb]
))
(def masto-embed "masto-embed")
@ -71,7 +73,7 @@
]
(->> statuus
(take 4)
(api/masto->html)
(rb/masto->html)
(render-html)
(render-to-document))
)))

View file

@ -1,38 +0,0 @@
; Licensed to the Apache Software Foundation (ASF) under one
; or more contributor license agreements. See the NOTICE file
; distributed with this work for additional information
; regarding copyright ownership. The ASF licenses this file
; to you under the Apache License, Version 2.0 (the
; "License"); you may not use this file except in compliance
; with the License. You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
(ns dda.masto-embed.js-api
(:require
["mastodon-api" :as Mastodon]
[clojure.pprint :as pprint :refer [pprint]]
[cljs.core.async :refer [go]]
[cljs.core.async.interop :refer-macros [<p!]]))
(defn get-content-seq [response]
(map
#(aget % "content")
(array-seq
(aget response "data"))))
(defn luccas-fn []
(let [config (js-obj "api_url" "https://social.meissa-gmbh.de/api/v1/" "access_token" "...")
masto (new Mastodon config)
rest-endpoint "accounts/:id/statuses"
id-config (js-obj "id" "2")
result (go
(let [response (<p! (.get masto rest-endpoint id-config))]
(get-content-seq response)))]
(pprint result)
result))

View file

@ -0,0 +1,45 @@
; Licensed to the Apache Software Foundation (ASF) under one
; or more contributor license agreements. See the NOTICE file
; distributed with this work for additional information
; regarding copyright ownership. The ASF licenses this file
; to you under the Apache License, Version 2.0 (the
; "License"); you may not use this file except in compliance
; with the License. You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
(ns dda.masto-embed.render-bootstrap
(:require
[cljs-time.format :as t]
[clojure.spec.alpha :as s]
[orchestra.core :refer-macros [defn-spec]]))
(defn mastocard->html [card]
(when (some? card)
(let [{:keys [title description image url]} card]
[:div {:class "card" :url url}
(when (some? image)
[:img {:class "card-img-top" :src image}])
[:h3 {:class "card-title"} title]
[:p {:class "card-body"} description]])))
(defn masto->html [statuses]
[:ul {:class "list-group"}
(map (fn [status]
(let [{:keys [created_at card]} status
date (t/parse created_at)]
[:li {:class "list-group-item, card"}
[:div {:class "card-body"}
[:h2 {:class "card-title"}
[:a {:href (get-in status [:account :url])}
(t/unparse (t/formatters :date) date) " "
(t/unparse (t/formatters :hour-minute-second) date)]]
[:p {:class "card-text"}
(:content status)
(mastocard->html card)]]]))
statuses)])

View file

@ -14,10 +14,10 @@
; See the License for the specific language governing permissions and
; limitations under the License.
(ns dda.masto-embed.api-test
(ns dda.masto-embed.render-bootstrap-test
(:require
[cljs.test :refer (deftest is)]
[dda.masto-embed.api :as sut]))
[dda.masto-embed.render-bootstrap :as sut]))
(def statuses [{:mentions []
:emojis []
@ -154,12 +154,12 @@
:spoiler_text ""}])
(deftest test-mastodon->html
(is (= [:ul
'([:li
[:h2 [:a {:href "https://social.meissa-gmbh.de/@team"}
"2020-05-17" "10:12:10"]]
"<p>We&apos;ve a new asciicast ... </p>"
nil])]
(is (= [:ul {:class "list-group"}
'([:li {:class "list-group-item, card"}
[:div {:class "card-body"}
[:h2 {:class "card-title"} [:a {:href "https://social.meissa-gmbh.de/@team"} "2020-05-17" " " "10:12:10"]]
[:p {:class "card-text"} "<p>We&apos;ve a new asciicast ... </p>"
nil]]])]
(sut/masto->html statuses))))
@ -175,10 +175,9 @@
:author_name "", :image nil, :provider_url "", :height 0, :html ""})
(deftest link-card-should-show-desc-and-link
(is (= [:div {:class "card", :url "https://www.ssllabs.com/ssltest/"}
nil
[:h1 "SSL Server Test (Powered by Qualys SSL Labs)"]
[:p "A comprehensive free SSL test for your public web servers."]]
(is (= [:div {:class "card", :url "https://www.ssllabs.com/ssltest/"} nil
[:h3 {:class "card-title"} "SSL Server Test (Powered by Qualys SSL Labs)"]
[:p {:class "card-body"} "A comprehensive free SSL test for your public web servers."]]
(sut/mastocard->html link-card))))
(def link-card-with-image
@ -190,7 +189,7 @@
(deftest link-card-should-show-image-and-desc-and-link
(is (= [:div {:class "card", :url "https://github.com/DomainDrivenArchitecture/cryogen-core"}
[:img {:src "https://cf.mastohost.com/v1/AUTH_91eb37814936490c95da7b85993cc2ff/socialmeissagmbhde/cache/preview_cards/images/000/017/635/original/5634071238f1f91f.png"}]
[:h1 "DomainDrivenArchitecture/cryogen-core"]
[:p "Cryogen's core. Contribute to DomainDrivenArchitecture/cryogen-core development by creating an account on GitHub."]]
[:img {:class "card-img-top", :src "https://cf.mastohost.com/v1/AUTH_91eb37814936490c95da7b85993cc2ff/socialmeissagmbhde/cache/preview_cards/images/000/017/635/original/5634071238f1f91f.png"}]
[:h3 {:class "card-title"} "DomainDrivenArchitecture/cryogen-core"]
[:p {:class "card-body"} "Cryogen's core. Contribute to DomainDrivenArchitecture/cryogen-core development by creating an account on GitHub."]]
(sut/mastocard->html link-card-with-image))))