clean up
This commit is contained in:
parent
d430d62c26
commit
eb2b10571d
4 changed files with 44 additions and 44 deletions
|
|
@ -1,8 +1,6 @@
|
||||||
;; shadow-cljs configuration
|
;; shadow-cljs configuration
|
||||||
{:source-paths
|
{:source-paths
|
||||||
["src/main"
|
["src" ]
|
||||||
"src/dev"
|
|
||||||
"src/test"]
|
|
||||||
|
|
||||||
:dependencies
|
:dependencies
|
||||||
[[reagent/reagent "2.0.1"]
|
[[reagent/reagent "2.0.1"]
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,8 @@
|
||||||
[applied-science.js-interop :as j]
|
[applied-science.js-interop :as j]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
[clojure.pprint :as pprint]
|
[clojure.pprint :as pprint]
|
||||||
[computersandblues.lodestone.database :as db]))
|
[computersandblues.lodestone.database :as db]
|
||||||
|
[computersandblues.lodestone.match :refer [query->matching-fn]]))
|
||||||
|
|
||||||
(defonce state (r/atom {:root nil
|
(defonce state (r/atom {:root nil
|
||||||
|
|
||||||
|
|
@ -22,6 +23,14 @@
|
||||||
:total 0
|
:total 0
|
||||||
:displayed-posts []}}))
|
:displayed-posts []}}))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
; TODO Ensure that user's handles are up to date
|
||||||
|
; TODO Manually fetch older / newer favorites
|
||||||
|
; TODO Handle 429
|
||||||
|
; TODO Search for tags (`#foo`) and handles (`@bar`)
|
||||||
|
; TODO Explain which kind of search currently is possible
|
||||||
|
|
||||||
;; Mastodon API helpers
|
;; Mastodon API helpers
|
||||||
|
|
||||||
(defn- link-header
|
(defn- link-header
|
||||||
|
|
@ -133,13 +142,6 @@
|
||||||
(declare fetch-posts!)
|
(declare fetch-posts!)
|
||||||
(declare refresh-displayed-posts!)
|
(declare refresh-displayed-posts!)
|
||||||
|
|
||||||
(comment
|
|
||||||
(-> (db/open-cursor! ::db/application db/all)
|
|
||||||
(db/transduce-cursor (comp (take 1)
|
|
||||||
(map )))
|
|
||||||
(.then db/cursor-value))
|
|
||||||
)
|
|
||||||
|
|
||||||
(defn setup-application!
|
(defn setup-application!
|
||||||
"Handles Mastodon application setup on the client side"
|
"Handles Mastodon application setup on the client side"
|
||||||
[]
|
[]
|
||||||
|
|
@ -233,6 +235,7 @@
|
||||||
(seq (:body response)))
|
(seq (:body response)))
|
||||||
on-response on-response
|
on-response on-response
|
||||||
on-error on-error}}]
|
on-error on-error}}]
|
||||||
|
(js/console.log 'fetch-favorites! instance-url max-id bearer-token)
|
||||||
((fn fetch-favorites' [url]
|
((fn fetch-favorites' [url]
|
||||||
(let [req-id (js/Date.now)]
|
(let [req-id (js/Date.now)]
|
||||||
(println :calling url)
|
(println :calling url)
|
||||||
|
|
@ -243,12 +246,12 @@
|
||||||
(if (continue? response)
|
(if (continue? response)
|
||||||
(js/setTimeout #(fetch-favorites' (link-header "next" (:raw response))) 500)
|
(js/setTimeout #(fetch-favorites' (link-header "next" (:raw response))) 500)
|
||||||
(when (= req-id (-> @state :section/posts :loading))
|
(when (= req-id (-> @state :section/posts :loading))
|
||||||
(swap! state update :section/posts dissoc :loading))))
|
(swap! state update :section/posts dissoc :loading)))))
|
||||||
(.catch (fn [response]
|
(.catch (fn [response]
|
||||||
(when (= req-id (-> @state :section/posts :loading))
|
(when (= req-id (-> @state :section/posts :loading))
|
||||||
(swap! state update :section/posts dissoc :loading))
|
(swap! state update :section/posts dissoc :loading))
|
||||||
(on-error response))))))
|
(on-error response))))))
|
||||||
(favorites-url {:instance-url instance-url :max-id max-id}))))
|
(favorites-url {:instance-url instance-url :max-id max-id})))
|
||||||
|
|
||||||
;;; views
|
;;; views
|
||||||
|
|
||||||
|
|
@ -321,29 +324,17 @@
|
||||||
[:div.content {:dangerouslySetInnerHTML (r/unsafe-html (:content post))}
|
[:div.content {:dangerouslySetInnerHTML (r/unsafe-html (:content post))}
|
||||||
(when (seq (:media_attachments post))
|
(when (seq (:media_attachments post))
|
||||||
[:div.attachments (map-indexed (fn [idx item]
|
[:div.attachments (map-indexed (fn [idx item]
|
||||||
^{:key idx} [attachment {:attachment item}]
|
^{:key idx} [attachment {:attachment item}])
|
||||||
(:media_attachments post)))])]
|
(:media_attachments post))])]
|
||||||
#_[debug post]])
|
#_[debug post]])
|
||||||
|
|
||||||
(defn ->regex [s]
|
|
||||||
(try
|
|
||||||
(js/RegExp. s "i")
|
|
||||||
(catch js/Error _
|
|
||||||
(js/RegExp. (js/RegExp.escape s) "i"))))
|
|
||||||
|
|
||||||
(defn- refresh-displayed-posts!
|
(defn- refresh-displayed-posts!
|
||||||
[posts-section]
|
[posts-section]
|
||||||
(let [{:keys [per-page query]} posts-section
|
(let [{:keys [per-page query]} posts-section
|
||||||
matches? (if query
|
|
||||||
(partial re-find (->regex query))
|
|
||||||
(constantly true))
|
|
||||||
; this `xform` is responsible for filtering and building the final list
|
; this `xform` is responsible for filtering and building the final list
|
||||||
; of results by iterating through the posts in the database.
|
; of results by iterating through the posts in the database.
|
||||||
xform (comp
|
xform (comp
|
||||||
(filter (fn [post]
|
(filter (query->matching-fn query))
|
||||||
(or (matches? (j/get post :content))
|
|
||||||
(matches? (j/get-in post [:account :acct])) ; search for url + username of poster
|
|
||||||
(some #(matches? (j/get % :username)) (j/get post :mentions))))) ; search only for username of mentions
|
|
||||||
(take per-page)
|
(take per-page)
|
||||||
(map #(js->clj % :keywordize-keys true)))
|
(map #(js->clj % :keywordize-keys true)))
|
||||||
refresh-id (js/Date.now)]
|
refresh-id (js/Date.now)]
|
||||||
|
|
@ -352,11 +343,9 @@
|
||||||
(-> (db/open-cursor! ::db/posts db/all "prev")
|
(-> (db/open-cursor! ::db/posts db/all "prev")
|
||||||
(db/transduce-cursor xform))])
|
(db/transduce-cursor xform))])
|
||||||
(.then (fn [[total displayed-posts]]
|
(.then (fn [[total displayed-posts]]
|
||||||
(swap! state update :section/posts #(let [v (-> (assoc % :total total)
|
(swap! state update :section/posts #(-> (assoc % :total total)
|
||||||
(assoc :displayed-posts displayed-posts))]
|
(assoc :displayed-posts displayed-posts)
|
||||||
(if (= refresh-id (:loading %))
|
(cond-> (= refresh-id (:loading %)) (dissoc :loading)))))))))
|
||||||
(dissoc v :loading)
|
|
||||||
v))))))))
|
|
||||||
|
|
||||||
(def debounced-refresh! (debounce 20 (partial refresh-displayed-posts!)))
|
(def debounced-refresh! (debounce 20 (partial refresh-displayed-posts!)))
|
||||||
|
|
||||||
|
|
@ -382,8 +371,8 @@
|
||||||
(when loading " …")
|
(when loading " …")
|
||||||
#_(cond (= api-state :loading) " …"
|
#_(cond (= api-state :loading) " …"
|
||||||
(= api-state :error) " API Error!")]]
|
(= api-state :error) " API Error!")]]
|
||||||
[:ul.results (map-indexed (fn [idx favorite]
|
[:ul.results (map-indexed (fn [idx p]
|
||||||
^{:key idx} [:li [post {:post favorite}]]) displayed-posts)]
|
^{:key idx} [:li [post {:post p}]]) displayed-posts)]
|
||||||
#_[:div.load-buttons
|
#_[:div.load-buttons
|
||||||
[:button
|
[:button
|
||||||
{:on-click (fn [_]
|
{:on-click (fn [_]
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
(ns computersandblues.lodestone.database
|
(ns computersandblues.lodestone.database)
|
||||||
(:require [clojure.string :as str]))
|
|
||||||
|
|
||||||
(defonce +db+ (atom nil))
|
(defonce +db+ (atom nil))
|
||||||
|
|
||||||
|
|
@ -73,9 +72,6 @@
|
||||||
(let [store (open-store @+db+ store-id)]
|
(let [store (open-store @+db+ store-id)]
|
||||||
(.openCursor store key-range direction))))
|
(.openCursor store key-range direction))))
|
||||||
|
|
||||||
(defn cursor-value [cursor]
|
|
||||||
(js->clj (some-> cursor .-value) :keywordize-keys true))
|
|
||||||
|
|
||||||
#_(defn logging [f]
|
#_(defn logging [f]
|
||||||
(let [n (volatile! 0)]
|
(let [n (volatile! 0)]
|
||||||
(fn [& args]
|
(fn [& args]
|
||||||
17
src/computersandblues/lodestone/match.cljs
Normal file
17
src/computersandblues/lodestone/match.cljs
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
(ns computersandblues.lodestone.match
|
||||||
|
(:require [applied-science.js-interop :as j]))
|
||||||
|
|
||||||
|
(defn ->regex [s]
|
||||||
|
(try
|
||||||
|
(js/RegExp. s "i")
|
||||||
|
(catch js/Error _
|
||||||
|
(js/RegExp. (js/RegExp.escape s) "i"))))
|
||||||
|
|
||||||
|
(defn query->matching-fn [query]
|
||||||
|
(let [match? (if query
|
||||||
|
(partial re-find (->regex query))
|
||||||
|
(constantly true))]
|
||||||
|
(fn [post]
|
||||||
|
(or (match? (j/get post :content))
|
||||||
|
(match? (j/get-in post [:account :acct])) ; search for url + username of poster
|
||||||
|
(some #(match? (j/get % :username)) (j/get post :mentions))))))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue