clean up views; set instance and community

master
Clyne 1 year ago
parent 5523960bef
commit b9c39fdc58
Signed by: clyne
GPG Key ID: 1B74EE6C49C96795

2
.gitignore vendored

@ -13,4 +13,4 @@ pom.xml.asc
.lein-failures .lein-failures
.nrepl-port .nrepl-port
.cpcache/ .cpcache/
.*.sw*

@ -2,7 +2,6 @@
(:require [clj-http.client :as client]) (:require [clj-http.client :as client])
(:require [clojure.data.json :as json])) (:require [clojure.data.json :as json]))
(def INSTANCE "lemmy.ml")
(def POST-LIST "/post/list") (def POST-LIST "/post/list")
(def COMMENTS "/comment/list") (def COMMENTS "/comment/list")
@ -15,24 +14,27 @@
(get :body) (get :body)
(json/read-str))) (json/read-str)))
(defn post-list [page] (defn post-list [instance community page]
(-> (make-api-url INSTANCE POST-LIST) (let [query-params (cond-> {:sort "Hot" :page (str page)}
(api-call {:sort "Hot" :page (str page)}) (not (empty? community))
(get "posts"))) (assoc :community_name community))]
(-> (make-api-url instance POST-LIST)
(api-call query-params)
(get "posts"))))
(defn comments-list [post] (defn comments-list [instance post]
(-> (make-api-url INSTANCE COMMENTS) (-> (make-api-url instance COMMENTS)
(api-call {:limit "50" :post_id (str (get-in post ["post" "id"]))}) (api-call {:limit "50" :post_id (str (get-in post ["post" "id"]))})
(get "comments"))) (get "comments")))
(defn show-post-item [post] (defn show-post-item [index post]
(let [items [["creator" "name"] (let [items [["creator" "name"]
["community" "name"] ["community" "name"]
["post" "name"] ["post" "name"]
["post" "published"] ["post" "published"]
["counts" "comments"]] ["counts" "comments"]]
data (mapv (partial get-in post) items)] data (cons (inc index) (mapv (partial get-in post) items))]
(apply (partial printf "%s on %s\n%s\nat %s (%d comments)\n\n") data))) (apply (partial printf "%d. %s on %s\n %s\n at %s (%d comments)\n\n") data)))
(defn show-comment-item [commnt] (defn show-comment-item [commnt]
(println (get-in commnt ["creator" "name"]) "says:") (println (get-in commnt ["creator" "name"]) "says:")
@ -41,11 +43,11 @@
) )
(defn show-posts [posts] (defn show-posts [posts]
(doseq [post (take 5 posts)] (doseq [index (range 0 5)]
(show-post-item post))) (show-post-item index (nth posts index))))
(defn show-comments [comments] (defn show-comments [comments]
(doseq [commnt (take 5 comments)] (doseq [commnt (take 1 comments)]
(show-comment-item commnt))) (show-comment-item commnt)))
(defn show-posts-prompt [] (defn show-posts-prompt []
@ -58,32 +60,50 @@
(flush) (flush)
(first (read-line))) (first (read-line)))
(defn view-post [post] (defn show-community-prompt []
(loop [offset 0 comments (comments-list post)] (print "Enter community name [all]: ")
(show-comments (if (pos? offset) (drop (* 5 offset) comments) comments)) (flush)
(read-line))
(defn show-instance-prompt []
(print "Enter instance name [lemmy.ml]: ")
(flush)
(let [inst (read-line)] (if (empty? inst) "lemmy.ml" inst)))
(defn view-post [instance post]
(loop [offset 0 comments (comments-list instance post)]
(println)
(show-comments (if (pos? offset) (drop (* 1 offset) comments) comments))
(case (show-comments-prompt) (case (show-comments-prompt)
\B (println) \B (println)
\N (recur (inc offset) comments) \N (recur (inc offset) comments)
\P (recur (dec offset) comments) \P (recur (dec offset) comments)
(do (println "Unknown command.") (recur offset comments))))) (do (println "Unknown command.") (recur offset comments)))))
(defn view-page [] (defn view-page [instance init-community]
(loop [top true page 1 posts (post-list page)] (loop [top true community init-community page 1 posts (post-list instance community page)]
(println)
(show-posts (if top posts (drop 5 posts))) (show-posts (if top posts (drop 5 posts)))
(case (show-posts-prompt) (case (show-posts-prompt)
\N (if top (recur false page posts) \N (if top (recur false community page posts)
(recur true (inc page) (post-list (inc page)))) (recur true community (inc page) (post-list instance community (inc page))))
\P (if top (recur false (dec page) (post-list (dec page))) \P (if top (recur false community (dec page) (post-list instance community (dec page)))
(recur true page posts)) (recur true community page posts))
\Q (do) \Q (println)
\1 (do (view-post (nth posts (cond-> 5 top (- 5)))) (recur top page posts)) \1 (do (view-post instance (nth posts (cond-> 5 top (- 5)))) (recur top community page posts))
\2 (do (view-post (nth posts (cond-> 6 top (- 5)))) (recur top page posts)) \2 (do (view-post instance (nth posts (cond-> 6 top (- 5)))) (recur top community page posts))
\3 (do (view-post (nth posts (cond-> 7 top (- 5)))) (recur top page posts)) \3 (do (view-post instance (nth posts (cond-> 7 top (- 5)))) (recur top community page posts))
\4 (do (view-post (nth posts (cond-> 8 top (- 5)))) (recur top page posts)) \4 (do (view-post instance (nth posts (cond-> 8 top (- 5)))) (recur top community page posts))
\5 (do (view-post (nth posts (cond-> 9 top (- 5)))) (recur top page posts)) \5 (do (view-post instance (nth posts (cond-> 9 top (- 5)))) (recur top community page posts))
(do (println "Unknown command.") (recur top page posts))))) \C (let [comm (show-community-prompt)] (recur true comm 1 (post-list instance comm page)))
(do (println "Unknown command.") (recur top community page posts)))))
(defn -main [& args] (defn -main [& args]
(view-page) (println "Welcome to lemmold, your old-school Lemmy browser!")
(println "Goodbye.")) (println)
(let [instance (show-instance-prompt)
community (show-community-prompt)]
(view-page instance community))
(println "Goodbye.")
(println))

Loading…
Cancel
Save