Line data Source code
1 4 : (ns matchscore.api.player-follows
2 : "Follow and unfollow players of the imported tours (PSA, ESF).
3 : Following is a private, in-app bookmark: it marks players in rankings and
4 : calendars and sends no notifications."
5 : (:require
6 : [matchscore.domain.player-follow.repository.postgres :as follow-repo]
7 : [ring.util.http-status :as status]
8 : [schemas.api.core :as api-schemas]))
9 :
10 :
11 4 : (def PlayerFollowPathParamsSchema
12 20 : [:map {:swagger/title "PlayerFollowPathParamsSchema"}
13 20 : [:player-id {:description
14 : "PSA: PSA player id. ESF: ESF id, with or without the ES prefix (ES662969162)."}
15 4 : string?]])
16 :
17 :
18 4 : (def PlayerFollowResponseSchema
19 28 : [:or {:swagger/title "PlayerFollowResponseSchema"
20 : :swagger/description "Follow state of a player for the authenticated user"}
21 8 : [:map
22 20 : [:status [:= status/ok]]
23 8 : [:body
24 8 : [:map
25 12 : [:message string?]
26 52 : [:data [:map [:source string?] [:player-id string?] [:followed boolean?]]]]]]
27 56 : [:map [:status [:= status/bad-request]] [:body [:map [:message string?]]]]
28 56 : [:map [:status [:= status/not-found]] [:body [:map [:message string?]]]]])
29 :
30 :
31 4 : (defn- follow-response
32 : [{:keys [repos auth-headers]
33 : :as request}
34 : source
35 : follow?]
36 4 : (let [repo (:player-follow-postgres-repo repos)
37 3 : user-id (:user/uuid auth-headers)
38 9 : player-key (follow-repo/parse-key source (get-in request [:route-params :player-id]))]
39 7 : (cond (nil? player-key)
40 4 : {:status status/bad-request
41 3 : :body {:message "Invalid player id."}}
42 :
43 7 : (not (follow-repo/player-exists? repo source player-key))
44 4 : {:status status/not-found
45 3 : :body {:message "Player not found."}}
46 :
47 : :else
48 3 : (let [followed (if follow?
49 6 : (follow-repo/follow! repo source user-id player-key)
50 6 : (follow-repo/unfollow! repo source user-id player-key))]
51 4 : {:status status/ok
52 7 : :body {:message (if followed "Following player." "Stopped following player.")
53 5 : :data {:source source
54 3 : :player-id (str player-key)
55 1 : :followed followed}}}))))
56 :
57 :
58 4 : (def ^:private authz
59 : "Following is a change to the user's own account: same rule as PUT /account."
60 28 : {:obj :account
61 : :act :update
62 : :create? true})
63 :
64 :
65 4 : (defn psa-player-follow-handler
66 : {:malli/schema [:=> [:cat api-schemas/RingRequest] PlayerFollowResponseSchema]
67 : :malli/parameters {:path PlayerFollowPathParamsSchema}
68 : :authz authz
69 : :summary "Follow a PSA player"
70 : :description
71 : "Adds the PSA player to the authenticated user's followed players. Idempotent. 404 for an unknown PSA id."}
72 : [request]
73 5 : (follow-response request "psa" true))
74 :
75 :
76 4 : (defn psa-player-unfollow-handler
77 : {:malli/schema [:=> [:cat api-schemas/RingRequest] PlayerFollowResponseSchema]
78 : :malli/parameters {:path PlayerFollowPathParamsSchema}
79 : :authz authz
80 : :summary "Stop following a PSA player"
81 : :description
82 : "Removes the PSA player from the authenticated user's followed players. Idempotent. 404 for an unknown PSA id."}
83 : [request]
84 5 : (follow-response request "psa" false))
85 :
86 :
87 4 : (defn esf-player-follow-handler
88 : {:malli/schema [:=> [:cat api-schemas/RingRequest] PlayerFollowResponseSchema]
89 : :malli/parameters {:path PlayerFollowPathParamsSchema}
90 : :authz authz
91 : :summary "Follow an ESF player"
92 : :description
93 : "Adds the ESF player (by ESF id) to the authenticated user's followed players. Idempotent. 404 when the ESF id is in no ranking and no imported draw."}
94 : [request]
95 5 : (follow-response request "esf" true))
96 :
97 :
98 4 : (defn esf-player-unfollow-handler
99 : {:malli/schema [:=> [:cat api-schemas/RingRequest] PlayerFollowResponseSchema]
100 : :malli/parameters {:path PlayerFollowPathParamsSchema}
101 : :authz authz
102 : :summary "Stop following an ESF player"
103 : :description
104 : "Removes the ESF player from the authenticated user's followed players. Idempotent. 404 when the ESF id is in no ranking and no imported draw."}
105 : [request]
106 5 : (follow-response request "esf" false))
|