Skip to content

Commit 8a99943

Browse files
authored
Merge pull request #112 from modulr-software/feat/bundle-posts-endpoints
bundle posts endpoints
2 parents c97cf69 + 245f10d commit 8a99943

File tree

11 files changed

+137
-12
lines changed

11 files changed

+137
-12
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(ns source.bundle-migrations.002-outgoing-posts
2+
(:require [source.db.bundle]
3+
[source.db.tables :as tables]))
4+
5+
(defn run-up! [context]
6+
(let [ds-bundle (:db-bundle context)]
7+
(tables/create-tables!
8+
ds-bundle
9+
:source.db.bundle
10+
[:outgoing-posts])))
11+
12+
(defn run-down! [context]
13+
(let [ds-bundle (:db-bundle context)]
14+
(tables/drop-tables!
15+
ds-bundle
16+
[:outgoing-posts])))

src/source/db/bundle.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
[:post-id :text :not nil]
1818
[:feed-id :integer :not nil]
1919
[:title :text :not nil]
20+
[:thumbnail :text]
2021
[:info :text]
2122
[:url :text]
2223
[:stream-url :text]

src/source/db/master.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
[:post-id :text :not nil]
153153
[:feed-id :integer :not nil]
154154
[:title :text :not nil]
155+
[:thumbnail :text]
155156
[:info :text]
156157
[:url :text]
157158
[:stream-url :text]

src/source/jobs/handlers.clj

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@
5454
(fn [{:keys [args ds]}]
5555
(let [{:keys [bundle-id categories]} args
5656
ds-bundle (db.util/conn :bundle bundle-id)
57-
incoming-posts (services/incoming-posts-with-feeds (db.util/conn) {:where [:= :feeds.state "live"]})
57+
incoming-posts (services/incoming-posts-with-feeds ds {:where [:= :feeds.state "live"]})
5858
posts-categories (incoming-posts/categories-by-posts ds {:where [:= :state "live"]})]
5959
(run!
6060
(fn [post]
6161
; calculate score for post
6262
; determine number of categories matched
63-
(let [; get vector of category ids in the given post, e.g. [1 3]
64-
post-categories-vec (reduce (fn [acc {:keys [post-id id]}]
65-
(if (= post-id (:id post))
66-
(conj acc id)
67-
acc)) [] posts-categories)
63+
; get vector of category ids in the given post, e.g. [1 3]
64+
(let [post-categories-vec (->> posts-categories
65+
(mapv (fn [{:keys [post-id id]}]
66+
(when (= post-id (:id post)) id)))
67+
(filterv identity))
6868
; get vector of category ids in categories to match, e.g. [1 2 3 4]
6969
match-categories-vec (reduce (fn [acc {:keys [id]}]
7070
(conj acc id)) [] categories)
@@ -77,13 +77,12 @@
7777
incoming-posts)
7878

7979
; pull highest scored posts by long heuristics into outgoing posts
80-
(let [; top 30 post-heuristics records ordered by long heuristic in descending order
81-
top-by-long-heuristics (services/top-posts-by-heuristic ds-bundle
80+
; top 100 post-heuristics records ordered by long heuristic in descending order
81+
(let [top-by-long-heuristics (services/top-posts-by-heuristic ds-bundle
8282
{:heuristic :long-heuristic
83-
:limit 30})
83+
:limit 100})
8484
; convert into a vector of id numbers
85-
ids (reduce (fn [acc {:keys [id]}]
86-
(conj acc id)) [] top-by-long-heuristics)
85+
ids (mapv :post-id top-by-long-heuristics)
8786

8887
; get all incoming posts with the above id numbers
8988
posts-in (services/incoming-posts ds {:where [:in :id ids]})
@@ -95,4 +94,3 @@
9594
[] posts-in)]
9695
(when (seq posts-in)
9796
(services/upsert-outgoing-posts! ds-bundle {:data outgoing-posts}))))))
98-
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(ns source.migrations.002-incoming-posts
2+
(:require [source.db.master]
3+
[source.db.tables :as tables]))
4+
5+
(defn run-up! [context]
6+
(let [ds-master (:db-master context)]
7+
(tables/create-tables!
8+
ds-master
9+
:source.db.master
10+
[:incoming-posts])))
11+
12+
(defn run-down! [context]
13+
(let [ds-master (:db-master context)]
14+
(tables/drop-table! ds-master :incoming-posts)))

src/source/routes/bundle_post.clj

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(ns source.routes.bundle-post
2+
(:require [source.services.interface :as services]
3+
[source.db.util :as db.util]
4+
[ring.util.response :as res]))
5+
6+
(defn get
7+
{:summary "get a single outgoing post in the uuid-authorized bundle by post id"
8+
:parameters {:path [:map [:id {:title "id"
9+
:description "post id"} :int]]}
10+
:responses {200 {:body [:map
11+
[:id :int]
12+
[:post-id :string]
13+
[:feed-id :int]
14+
[:creator-id :int]
15+
[:content-type-id :int]
16+
[:title :string]
17+
[:thumbnail [:maybe :string]]
18+
[:info [:maybe :string]]
19+
[:url [:maybe :string]]
20+
[:stream-url [:maybe :string]]
21+
[:season [:maybe :int]]
22+
[:episode [:maybe :int]]
23+
[:redacted [:maybe :int]]
24+
[:posted-at [:maybe :string]]]}
25+
404 {:body [:map [:message :string]]}}}
26+
27+
[{:keys [bundle-id path-params] :as _request}]
28+
(let [bundle-ds (db.util/conn :bundle bundle-id)
29+
id (:id path-params)]
30+
(res/response (services/outgoing-post bundle-ds {:id id}))))
31+

src/source/routes/bundle_posts.clj

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(ns source.routes.bundle-posts
2+
(:require [source.services.interface :as services]
3+
[source.db.util :as db.util]
4+
[clojure.walk :as walk]
5+
[ring.util.response :as res]))
6+
7+
(defn get
8+
{:summary "get all outgoing posts in the uuid-authorized bundle"
9+
:parameters {:query [:map
10+
[:uuid :string]
11+
[:limit :int]
12+
[:start :int]]}
13+
:responses {200 {:body [:vector
14+
[:map
15+
[:id :int]
16+
[:post-id :string]
17+
[:feed-id :int]
18+
[:creator-id :int]
19+
[:content-type-id :int]
20+
[:title :string]
21+
[:thumbnail [:maybe :string]]
22+
[:info [:maybe :string]]
23+
[:url [:maybe :string]]
24+
[:stream-url [:maybe :string]]
25+
[:season [:maybe :int]]
26+
[:episode [:maybe :int]]
27+
[:redacted [:maybe :int]]
28+
[:posted-at [:maybe :string]]]]}
29+
404 {:body [:map [:message :string]]}}}
30+
31+
[{:keys [bundle-id query-params] :as _request}]
32+
(let [bundle-ds (db.util/conn :bundle bundle-id)
33+
{:keys [limit start]} (walk/keywordize-keys query-params)]
34+
(res/response (services/outgoing-posts bundle-ds {:where (when start [:>= :id start])
35+
:limit limit
36+
:order-by [[:id :asc]]}))))
37+

src/source/routes/posts.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
[:creator-id :int]
1515
[:content-type-id :int]
1616
[:title :string]
17+
[:thumbnail [:maybe :string]]
1718
[:info [:maybe :string]]
1819
[:url [:maybe :string]]
1920
[:stream-url [:maybe :string]]

src/source/routes/reitit.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
[source.routes.integrations :as integrations]
4242
[source.routes.integration :as integration]
4343
[source.routes.integration-categories :as integration-categories]
44+
[source.routes.bundle-posts :as bundle-posts]
45+
[source.routes.bundle-post :as bundle-post]
4446
[source.routes.posts :as posts]
4547
[source.routes.admin-feeds :as admin-feeds]
4648
[source.routes.xml :as xml]
@@ -177,6 +179,12 @@
177179
["/categories" (route {:get feed-categories/get
178180
:post feed-categories/post})]]]
179181

182+
["/bundles" {:middleware [[mw/apply-bundle]]
183+
:tags #{"bundles"}}
184+
["/posts"
185+
["" (route {:get bundle-posts/get})]
186+
["/:id" (route {:get bundle-post/get})]]]
187+
180188
["/admin" {:middleware [[mw/apply-auth {:required-type :admin}]]
181189
:tags #{"admin"}
182190
:swagger {:security [{"auth" []}]}

src/source/services/interface.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@
106106
(defn top-posts-by-heuristic [ds {:keys [_select _limit _heuristic] :as opts}]
107107
(post-heuristics/top-posts-by-heuristic ds opts))
108108

109+
(defn outgoing-posts
110+
([ds] (outgoing-posts ds {}))
111+
([ds {:keys [_where] :as opts}]
112+
(outgoing-posts/outgoing-posts ds opts)))
113+
114+
(defn outgoing-post [ds {:keys [_id _where] :as opts}]
115+
(outgoing-posts/outgoing-post ds opts))
116+
109117
(defn upsert-outgoing-posts! [ds {:keys [_data] :as opts}]
110118
(outgoing-posts/upsert-outgoing-posts! ds opts))
111119

0 commit comments

Comments
 (0)