diff options
| author | eltrac <hi@eltr.ac> | 2026-10-04 17:06:03 +0800 |
|---|---|---|
| committer | eltrac <hi@eltr.ac> | 2026-10-04 17:07:10 +0800 |
| commit | 61afc1a7cc45a1ab3a5d141c1a490badf3ea03ef (patch) | |
| tree | 7158a08f04af53b6437f54486850f611559a8160 | |
| parent | b13b6ad1c2e9127412583760ff3d75e0e958e207 (diff) | |
API: remove typed API
- /api/likes removed
- /api/replies removed
- /api/reposts removed
| -rw-r--r-- | src/weepinbell/handler.clj | 24 | ||||
| -rw-r--r-- | src/weepinbell/retriever.clj | 19 | ||||
| -rw-r--r-- | test/weepinbell/handler_test.clj | 40 |
3 files changed, 29 insertions, 54 deletions
diff --git a/src/weepinbell/handler.clj b/src/weepinbell/handler.clj index 0c45960..079c1e1 100644 --- a/src/weepinbell/handler.clj +++ b/src/weepinbell/handler.clj @@ -12,26 +12,22 @@ ;; Define API routes -(defmacro defmention-routes [base-path mention-type storage] - `(context ~base-path [] - (GET "/" [~'url ~'page ~'microformats] - (retriever/by-url ~'url ~mention-type ~'page ~storage - {:microformats ~'microformats})) - (GET "/:domain" [~'domain ~'page ~'microformats] - (retriever/by-domain ~'domain ~mention-type ~'page ~storage - {:microformats ~'microformats})))) - (defn make-api [wmc storage config] [;; receiver endpoint (POST (:endpoint config) [source target] (receive {:s source :t target} wmc config)) ;; query API routes (context "/api" [] - ;; query webmention list - (defmention-routes "/mentions" nil storage) - (defmention-routes "/likes" :like storage) - (defmention-routes "/reposts" :repost storage) - (defmention-routes "/replies" :reply storage) + ;; query webmention list + (context "/mentions" [page microformats mftype] + (GET "/" [url] + (retriever/by-url url page storage + {:microformats microformats + :type mftype})) + (GET "/:domain" [domain] + (retriever/by-domain domain page storage + {:microformats microformats + :type mftype}))) ;; query one specific webmention (context "/mention" [microformats] (GET "/:id" [id] (retriever/by-id id storage diff --git a/src/weepinbell/retriever.clj b/src/weepinbell/retriever.clj index e3b288d..864f839 100644 --- a/src/weepinbell/retriever.clj +++ b/src/weepinbell/retriever.clj @@ -54,37 +54,28 @@ (defn- wrap-response-list [opts data] (map #(wrap-response-object % opts) data)) -(defn- get-mftype-key [typ] - (get {:like :like-of - :repost :repost-of - :reply :in-reply-to - :rsvp :rsvp} typ)) - (defn- filter-response-by-mftype [typ data] - (when typ - (filter #(= (-> % :microformats :type) - (get-mftype-key typ)) - data))) + (filter #(= typ (get-in % [:microformats :type])) data)) ;; end utilities (defn by-domain "Get webmentions sent to a particular domain." - [domain typ page storage opts] + [domain page storage opts] (cond->> ((:get-by-domain storage) domain) true (wrap-response-list opts) - typ (filter-response-by-mftype typ) + (:type opts) (filter-response-by-mftype (:type opts)) page (partition-pages page default-page-size) true respond-webmention-list-json)) (defn by-url "Get webmention sent to a particular URL." - [url typ page storage opts] + [url page storage opts] (if url (cond->> (get-domain url) true ((:get-by-domain storage)) true (filter #(= url (get-in % [:webmention :target]))) true (wrap-response-list opts) - typ (filter-response-by-mftype typ) + (:type opts) (filter-response-by-mftype (:type opts)) page (partition-pages page default-page-size) true respond-webmention-list-json) ;; if no url query is passed diff --git a/test/weepinbell/handler_test.clj b/test/weepinbell/handler_test.clj index 5b0d8a6..a59dd5e 100644 --- a/test/weepinbell/handler_test.clj +++ b/test/weepinbell/handler_test.clj @@ -4,45 +4,33 @@ [weepinbell.handler :refer [app]] [cheshire.core :refer [parse-string]])) -(defmacro test-mention-list-by-domain-route [mftype] - `(testing (str "/api/" ~mftype "/:domain route") - (let [~'response ((app nil {:get-by-domain (fn [~'_] +(deftest api-routes-test + (testing "/api/mentions/:domain route" + (let [response ((app nil {:get-by-domain (fn [_] [{:webmention {:source "" :target "" :id "" :modified 0}}])} {}) - (mock/request :get (str "/api/" ~mftype "/www.example")))] - (is (= (:status ~'response) 200)) - (is (some? (:body ~'response)))))) + (mock/request :get "/api/mentions/www.example"))] + (is (= (:status response) 200)) + (is (some? (:body response))))) -(defmacro test-mention-list-by-url-route [mftype] - `(testing (str "/api/" ~mftype "?url= route") - (let [~'response ((app nil + (testing "/api/mentions?url= route" + (let [response ((app nil {:get-by-domain - (fn [~'_] + (fn [_] [{:webmention {:source "https://source.net" :target "http://localhost" :id "123456789123456789" :modified 0} :microformats {:type :like-of}}])} {}) - (mock/request :get (str "/api/" ~mftype "?url=http://localhost")))] - (is (= (:status ~'response) 200)) - (is (some? (:body ~'response))) - (when-not (-> ~'response :body parse-string empty?) - (is (-> ~'response :body parse-string first (get "webmention") some?)))))) - -(deftest api-routes-test - (test-mention-list-by-domain-route "mentions") - (test-mention-list-by-domain-route "likes") - (test-mention-list-by-domain-route "reposts") - (test-mention-list-by-domain-route "replies") - - (test-mention-list-by-url-route "mentions") - (test-mention-list-by-url-route "likes") - (test-mention-list-by-url-route "reposts") - (test-mention-list-by-url-route "replies") + (mock/request :get "/api/mentions?url=http://localhost"))] + (is (= (:status response) 200)) + (is (some? (:body response))) + (when-not (-> response :body parse-string empty?) + (is (-> response :body parse-string first (get "webmention") some?))))) (testing "/api/mention/:id" (let [response ((app nil {:get (fn [_] |
