now it works

use_async
jem 4 years ago
parent c28d74cc5b
commit ca16b645bb

@ -16,6 +16,7 @@
(ns dda.masto-embed.api
(:require
["mastodon-api" :as Mastodon]
[dda.masto-embed.infra :as infra]
[clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as st]
[orchestra.core :refer-macros [defn-spec]]
@ -25,12 +26,8 @@
(s/def ::account-id string?)
(s/def ::host-url string?)
(defn exit-with-error [error]
(js/console.error error)
(js/process.exit 1))
(defn js->edn [data]
(js->clj data :keywordize-keys true))
(defn masto->edn [response]
(-> response .-data infra/js->edn))
(defn-spec mastodon-client any?
[host-url ::host-url]
@ -41,30 +38,13 @@
(defn-spec get-account-statuses any?
[host-url ::host-url
account-id ::account-id
callback fn?]
(.then (.get (mastodon-client host-url)
(str "accounts/" account-id "/statuses")
#js {})
#(let [response (-> % .-data js->edn)]
(if-let [error (:error response)]
(exit-with-error error)
(callback response)))))
(defn-spec get-directory-old any?
[host-url ::host-url
callback fn?]
(.then (.get (mastodon-client host-url)
(str "directory?local=true")
#js {})
#(let [response (-> % .-data js->edn)]
(callback response))))
account-id ::account-id]
(.get (mastodon-client host-url)
(str "accounts/" account-id "/statuses")
#js {}))
(defn-spec get-directory any?
[host-url ::host-url]
(go (let [result (<p! (.get (mastodon-client host-url)
(str "directory?local=true")
#js {}))]
(-> result
.-data
js->edn))))
(.get (mastodon-client host-url)
(str "directory?local=true")
#js {}))

@ -16,6 +16,7 @@
(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!]]
[clojure.pprint :as pprint :refer [pprint]]))
@ -26,8 +27,8 @@
(-> js/document
(.getElementById masto-embed)
(.getAttribute "host_url")))
(defn account-name-from-document []
(defn account-name-from-document []
(-> js/document
(.getElementById masto-embed)
(.getAttribute "account_name")))
@ -44,41 +45,34 @@
(.-innerHTML)
(set! input)))
(defn debug [elem]
(print elem)
elem)
(defn find-account-id [host-url account-name]
(let [in (chan)
out (chan)]
(let [out (chan)]
(go
(->>
(<! in)
(filter #(= account-name (:acct %)))
(map :id)
first
debug
(>! out))
(do (close! in)
(close! out))
)
;(api/get-directory host-url #(go (>! in %)))
(>! out
(->>
(<p! (api/get-directory host-url))
api/masto->edn
(filter #(= account-name (:acct %)))
(map :id)
first
;infra/debug
)))
out))
(defn init []
(go
(let [account-id (or
(let [host-url (host-url-from-document)
account-name (account-name-from-document)
account-id (or
(account-id-from-document)
(<! (find-account-id "https://social.meissa-gmbh.de" "team")))]
(print account-id)
(api/get-account-statuses
(host-url-from-document)
account-id
render-to-document))))
(defn main []
(go
(let [account-id (<! (find-account-id "https://social.meissa-gmbh.de" "team"))]
(<! (find-account-id host-url account-name)))
status (->
(<p! (api/get-account-statuses host-url account-id))
api/masto->edn)
]
(print host-url)
(print account-name)
(print account-id)
(print status)
(render-to-document status)
)))

@ -0,0 +1,30 @@
; 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.infra
(:require
[cljs.core.async :refer [go]]
[cljs.core.async.interop :refer-macros [<p!]]))
(defn exit-with-error [error]
(js/console.error error)
(js/process.exit 1))
(defn js->edn [data]
(js->clj data :keywordize-keys true))
(defn debug [elem]
(print elem)
elem)
Loading…
Cancel
Save