Line data Source code
1 4 : (ns matchscore.domain.tournament-subscription.command
2 : "Subscribe to / unsubscribe from tournament updates. Subscribing seeds the
3 : no-backfill rows in the same transaction, so old results never flood the feed."
4 : (:require
5 : [cats.monad.either :as either]
6 : [matchscore.db.postgres.user-updates :as user-updates]
7 : [matchscore.domain.tournament-subscription.repository :as repository]
8 : [matchscore.domain.tournament-subscription.repository.postgres :as postgres]
9 : [next.jdbc :as jdbc]))
10 :
11 :
12 16 : (def ^:private sources #{"psa" "esf"})
13 :
14 :
15 20 : (def ^:private targets #{"tournament" "tier" "source"})
16 :
17 :
18 16 : (def ^:private scopes #{"all" "followed"})
19 :
20 :
21 9 : (defn- invalid [message] (either/left {:message message}))
22 :
23 :
24 4 : (defn validate
25 : "Returns Either<{:message}, normalized subscription>. Structure only; whether
26 : the target exists is checked against the database by `subscribe!`."
27 : [{:keys [source target target-key match-scope]}]
28 39 : (cond (not (contains? sources source)) (invalid "source must be psa or esf")
29 21 : (not (contains? targets target)) (invalid "target must be tournament, tier or source")
30 21 : (not (contains? scopes match-scope)) (invalid "matchScope must be all or followed")
31 29 : (and (= "source" target) (some? target-key))
32 3 : (invalid "targetKey must be empty for target source")
33 :
34 32 : (and (not= "source" target) (nil? target-key))
35 3 : (invalid "targetKey is required for this target")
36 :
37 : :else
38 6 : (either/right
39 18 : {:source source
40 3 : :target target
41 25 : :target-key (some->
42 3 : target-key
43 2 : str)
44 3 : :match-scope match-scope})))
45 :
46 :
47 4 : (defn subscribe!
48 : "Upserts the user's subscription (an existing one only changes its scope) and
49 : seeds user_updates in the same transaction. Returns Either<{:message}, row>."
50 : [ds user-id subscription]
51 12 : (let [validated (validate subscription)]
52 12 : (if (either/left? validated)
53 1 : validated
54 3 : (let [{:keys [source target target-key]
55 : :as subscription}
56 9 : (deref validated)]
57 33 : (if-not (repository/valid-target? (postgres/make-repository ds) source target target-key)
58 3 : (invalid "Unknown tournament, tier or source")
59 6 : (either/right
60 12 : (jdbc/with-transaction
61 3 : [tx ds]
62 12 : (let [repo (postgres/make-repository tx)
63 15 : _ (user-updates/flush-user! tx user-id source)
64 15 : row (repository/upsert-subscription! repo user-id subscription)]
65 6 : (user-updates/seed!
66 3 : tx
67 3 : user-id
68 18 : {:type :subscription
69 3 : :source source
70 3 : :target target
71 3 : :target-key target-key})
72 12 : (->
73 9 : (some
74 30 : #(when (= (:uuid row) (:uuid %)) %)
75 12 : (repository/list-subscriptions repo user-id))
76 : (or row))))))))))
77 :
78 :
79 4 : (defn unsubscribe!
80 : "Deletes only the caller's own subscription. Returns true when a row was removed."
81 : [ds user-id id]
82 8 : (boolean (repository/delete-subscription! (postgres/make-repository ds) user-id id)))
|