more direct

This commit is contained in:
jem 2020-04-26 18:35:09 +02:00
parent de0c47124c
commit 5fde78a343
3 changed files with 33 additions and 26 deletions

View file

@ -5,7 +5,7 @@
<title>masto-embed</title> <title>masto-embed</title>
</head> </head>
<body> <body>
<div id="masto-embed" account_id="2" <div id="masto-embed" account_name="team" account_id="2"
host_url="https://social.meissa-gmbh.de"> host_url="https://social.meissa-gmbh.de">
Here the timeline will appear. Here the timeline will appear.
</div> </div>

View file

@ -19,20 +19,11 @@
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as st] [clojure.spec.test.alpha :as st]
[orchestra.core :refer-macros [defn-spec]] [orchestra.core :refer-macros [defn-spec]]
[clojure.pprint :as pprint :refer [pprint]]
[cljs.core.async :refer [go]] [cljs.core.async :refer [go]]
[cljs.core.async.interop :refer-macros [<p!]])) [cljs.core.async.interop :refer-macros [<p!]]))
(s/def ::account-id string?) (s/def ::account-id string?)
(s/def ::host-url string?) (s/def ::host-url string?)
(defn-spec create-config map?
[account-id ::account-id
host-url ::host-url]
{:access_token "unused"
:account-id account-id
:api_url (str host-url "/api/v1/") })
(defn exit-with-error [error] (defn exit-with-error [error]
(js/console.error error) (js/console.error error)
(js/process.exit 1)) (js/process.exit 1))
@ -40,26 +31,32 @@
(defn js->edn [data] (defn js->edn [data]
(js->clj data :keywordize-keys true)) (js->clj data :keywordize-keys true))
(defn mastodon-client [mastodon-config] (defn-spec mastodon-client any?
(or (some-> mastodon-config clj->js Mastodon.) [host-url ::host-url]
(exit-with-error "missing Mastodon client configuration!"))) (let [mastodon-config
{:access_token "unused"
:api_url (str host-url "/api/v1/")}]
(some-> mastodon-config clj->js Mastodon.)))
(defn get-account-statuses [mastodon-config callback] (defn-spec get-account-statuses any?
(.then (.get (mastodon-client mastodon-config) [host-url ::host-url
(str "accounts/" (:account-id mastodon-config) "/statuses") account-id ::account-id
callback fn?]
(.then (.get (mastodon-client host-url)
(str "accounts/" account-id "/statuses")
#js {}) #js {})
#(let [response (-> % .-data js->edn)] #(let [response (-> % .-data js->edn)]
(if-let [error (:error response)] (if-let [error (:error response)]
(exit-with-error error) (exit-with-error error)
(callback response))))) (callback response)))))
(defn get-directory [mastodon-config callback] (defn-spec get-directory any?
(.then (.get (mastodon-client mastodon-config) [host-url ::host-url
callback fn?]
(.then (.get (mastodon-client host-url)
(str "directory?local=true") (str "directory?local=true")
#js {}) #js {})
#(let [response (-> % .-data js->edn)] #(let [response (-> % .-data js->edn)]
(if-let [error (:error response)] (if-let [error (:error response)]
(exit-with-error error) (exit-with-error error)
(callback response))))) (callback response)))))
(def my-config (create-config "2" "https://social.meissa-gmbh.de"))

View file

@ -20,11 +20,20 @@
(def masto-embed "masto-embed") (def masto-embed "masto-embed")
(defn mastodon-config-from-document [] (defn host-url-from-document []
(let [masto-embed (.getElementById js/document masto-embed) (-> js/document
host-url (.getAttribute masto-embed "host_url") (.getElementById masto-embed)
account-id (.getAttribute masto-embed "account_id")] (.getAttribute "host_url")))
(api/create-config account-id host-url)))
(defn account-name-from-document []
(-> js/document
(.getElementById masto-embed)
(.getAttribute "account_name")))
(defn account-id-from-document []
(-> js/document
(.getElementById masto-embed)
(.getAttribute "account_id")))
(defn render-to-document (defn render-to-document
[input] [input]
@ -35,5 +44,6 @@
(defn init [] (defn init []
(api/get-account-statuses (api/get-account-statuses
(mastodon-config-from-document) (host-url-from-document)
(account-id-from-document)
render-to-document)) render-to-document))