Skip to content

Commit 52fd0fc

Browse files
committed
sort order for recent retweets
1 parent 655ee26 commit 52fd0fc

File tree

8 files changed

+179
-104
lines changed

8 files changed

+179
-104
lines changed

cljs-om/src/cljs_om/core.cljs

+29-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66

77
(enable-console-print!)
88

9+
(defn sort-by [key]
10+
(fn [x y]
11+
(if (not (= (key x) (key y)))
12+
(> (key x) (key y))
13+
(> (:id x) (:id y)))))
14+
915
(def app-state (atom {:count 0
1016
:tweets-map {}
11-
:by-followers (sorted-set-by (fn [x y] (> (:followers_count x) (:followers_count y))))
12-
:by-retweets (sorted-set-by (fn [x y] (> (:retweet_count x) (:retweet_count y))))
13-
:by-favorites (sorted-set-by (fn [x y] (> (:favorite_count x) (:favorite_count y))))
17+
:rt-since-startup {}
18+
:by-followers (sorted-set-by (sort-by :followers_count))
19+
:by-retweets (sorted-set-by (sort-by :retweet_count))
20+
:by-rt-since-startup (sorted-set-by (sort-by :count))
21+
:by-favorites (sorted-set-by (sort-by :favorite_count))
1422
:by-id (sorted-set-by >)
1523
:n 10
1624
:sorted :by-followers}))
@@ -35,15 +43,30 @@
3543

3644
(defn add-rt-status [tweet]
3745
(if (contains? tweet :retweeted_status)
38-
(let [rt (:retweeted_status tweet) prev ((keyword (:id_str rt)) (:tweets-map @app-state))]
46+
(let [rt (:retweeted_status tweet)
47+
prev ((keyword (:id_str rt)) (:tweets-map @app-state))
48+
prev-rt-count ((keyword (:id_str rt)) (:rt-since-startup @app-state))]
3949
(if (not (nil? prev))
4050
(do
4151
(swap! app-state assoc :by-retweets (disj (:by-retweets @app-state)
4252
{:retweet_count (:retweet_count prev)
43-
:id (:id_str rt)})))
53+
:id (:id_str rt)}))
4454
(swap! app-state assoc :by-favorites (disj (:by-favorites @app-state)
4555
{:favorite_count (:favorite_count prev)
46-
:id (:id_str rt)})))
56+
:id (:id_str rt)}))))
57+
(if (not (nil? rt))
58+
(do
59+
(if (not (nil? prev-rt-count))
60+
(swap! app-state assoc :by-rt-since-startup (disj (:by-rt-since-startup @app-state)
61+
{:count prev-rt-count
62+
:id (:id_str rt)})))
63+
64+
(swap! app-state assoc-in [:rt-since-startup (keyword (:id_str rt))]
65+
(inc ((keyword (:id_str rt)) (:rt-since-startup @app-state))))
66+
67+
(swap! app-state assoc :by-rt-since-startup (conj (:by-rt-since-startup @app-state)
68+
{:count ((keyword (:id_str rt)) (:rt-since-startup @app-state))
69+
:id (:id_str rt)}))))
4770
(add-to-tweets-map rt)
4871
(swap! app-state assoc :by-retweets (conj (:by-retweets @app-state)
4972
{:retweet_count (:retweet_count rt)

cljs-om/src/cljs_om/ui.cljs

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818

1919
(defn tweets-by-retweets [app n]
2020
"find top n tweets by retweets in descending order"
21-
(vec (map (fn [m] ((keyword (:id m))(:tweets-map app))) (take n (:by-retweets app)))))
21+
(vec (map (fn [m] ((keyword (:id m)) (:tweets-map app))) (take n (:by-retweets app)))))
22+
23+
(defn tweets-by-rt-since-startup [app n]
24+
"find top n tweets by retweets in descending order"
25+
(vec (map (fn [m] ((keyword (:id m)) (:tweets-map app))) (take n (:by-rt-since-startup app)))))
2226

2327
(defn tweets-by-favorites [app n]
2428
"find top n tweets by retweets in descending order"
@@ -31,7 +35,8 @@
3135
(def find-tweets {:by-id tweets-by-id
3236
:by-followers tweets-by-followers
3337
:by-retweets tweets-by-retweets
34-
:by-favorites tweets-by-favorites})
38+
:by-favorites tweets-by-favorites
39+
:by-rt-since-startup tweets-by-rt-since-startup})
3540

3641
(defn sort-button [app key]
3742
#js {:onClick (fn [e] (om/update! app [:sorted] key))
@@ -47,6 +52,7 @@
4752
(dom/button (sort-button app :by-id) "latest")
4853
(dom/button (sort-button app :by-followers) "followers")
4954
(dom/button (sort-button app :by-retweets) "retweets")
55+
(dom/button (sort-button app :by-rt-since-startup) "retweets2")
5056
(dom/button (sort-button app :by-favorites) "favorites")))))
5157

5258
(defn tweet-view [tweet owner]

public/cljs/out/cljs_om/core.cljs

+29-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66

77
(enable-console-print!)
88

9+
(defn sort-by [key]
10+
(fn [x y]
11+
(if (not (= (key x) (key y)))
12+
(> (key x) (key y))
13+
(> (:id x) (:id y)))))
14+
915
(def app-state (atom {:count 0
1016
:tweets-map {}
11-
:by-followers (sorted-set-by (fn [x y] (> (:followers_count x) (:followers_count y))))
12-
:by-retweets (sorted-set-by (fn [x y] (> (:retweet_count x) (:retweet_count y))))
13-
:by-favorites (sorted-set-by (fn [x y] (> (:favorite_count x) (:favorite_count y))))
17+
:rt-since-startup {}
18+
:by-followers (sorted-set-by (sort-by :followers_count))
19+
:by-retweets (sorted-set-by (sort-by :retweet_count))
20+
:by-rt-since-startup (sorted-set-by (sort-by :count))
21+
:by-favorites (sorted-set-by (sort-by :favorite_count))
1422
:by-id (sorted-set-by >)
1523
:n 10
1624
:sorted :by-followers}))
@@ -35,15 +43,30 @@
3543

3644
(defn add-rt-status [tweet]
3745
(if (contains? tweet :retweeted_status)
38-
(let [rt (:retweeted_status tweet) prev ((keyword (:id_str rt)) (:tweets-map @app-state))]
46+
(let [rt (:retweeted_status tweet)
47+
prev ((keyword (:id_str rt)) (:tweets-map @app-state))
48+
prev-rt-count ((keyword (:id_str rt)) (:rt-since-startup @app-state))]
3949
(if (not (nil? prev))
4050
(do
4151
(swap! app-state assoc :by-retweets (disj (:by-retweets @app-state)
4252
{:retweet_count (:retweet_count prev)
43-
:id (:id_str rt)})))
53+
:id (:id_str rt)}))
4454
(swap! app-state assoc :by-favorites (disj (:by-favorites @app-state)
4555
{:favorite_count (:favorite_count prev)
46-
:id (:id_str rt)})))
56+
:id (:id_str rt)}))))
57+
(if (not (nil? rt))
58+
(do
59+
(if (not (nil? prev-rt-count))
60+
(swap! app-state assoc :by-rt-since-startup (disj (:by-rt-since-startup @app-state)
61+
{:count prev-rt-count
62+
:id (:id_str rt)})))
63+
64+
(swap! app-state assoc-in [:rt-since-startup (keyword (:id_str rt))]
65+
(inc ((keyword (:id_str rt)) (:rt-since-startup @app-state))))
66+
67+
(swap! app-state assoc :by-rt-since-startup (conj (:by-rt-since-startup @app-state)
68+
{:count ((keyword (:id_str rt)) (:rt-since-startup @app-state))
69+
:id (:id_str rt)}))))
4770
(add-to-tweets-map rt)
4871
(swap! app-state assoc :by-retweets (conj (:by-retweets @app-state)
4972
{:retweet_count (:retweet_count rt)

public/cljs/out/cljs_om/core.js

+20-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)