LCOV - code coverage report
Current view: top level - matchscore/domain/player_follow - query.clj Coverage Total Hit
Test: lcov.info Lines: 97.9 % 48 47
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.domain.player-follow.query
       2              :   "Read helpers that attach a user's follow state to imported-tour rows. Guests
       3              :   (no user id) and a missing repository mean nothing is followed."
       4              :   (:require
       5              :    [matchscore.domain.player-follow.rankings :as rankings]
       6              :    [matchscore.domain.player-follow.repository.postgres :as follow-repo]))
       7              : 
       8              : 
       9            4 : (defn followed?-fn
      10              :  "Returns (fn [player-key]) → true when the user follows that player of `source`."
      11              :  [repo source user-id]
      12           21 :  (let [followed (if (and repo user-id) (follow-repo/find-followed-keys repo source user-id) #{})]
      13            2 :    (fn [player-key]
      14            2 :     (boolean
      15            9 :      (when (seq followed)
      16           18 :        (when-some [k (follow-repo/parse-key source player-key)] (contains? followed (str k))))))))
      17              : 
      18              : 
      19            4 : (defn with-followed-players
      20              :  "Adds :followed-players to each row: followed players in the row's imported event,
      21              :   matched on :event-public-id."
      22              :  [repo source user-id rows]
      23           21 :  (let [by-event (if (and repo user-id)
      24            2 :                   (follow-repo/find-followed-players-by-event
      25            1 :                    repo
      26            1 :                    source
      27            1 :                    user-id
      28            4 :                    (keep :event-public-id rows))
      29            3 :                   {})]
      30           42 :    (mapv #(assoc % :followed-players (get by-event (:event-public-id %) [])) rows)))
      31              : 
      32              : 
      33            4 : (defn with-rankings
      34              :  "Adds :rankings to each player: current rankings of the player on its :source, best
      35              :   first ([] when unranked or the source has no rankings)."
      36              :  [repo players]
      37            3 :  (let [by-source (into
      38            1 :                   {}
      39           34 :                   (for [[source group] (group-by :source players)
      40            4 :                         :when (contains? rankings/sources source)]
      41            2 :                     [source
      42            8 :                      (follow-repo/find-current-rankings repo source (map :player-id group))]))]
      43            2 :    (mapv
      44            1 :     (fn
      45              :      [{:keys [source player-id]
      46              :        :as   player}]
      47           13 :      (assoc player :rankings (get-in by-source [source (str player-id)] [])))
      48            1 :     players)))
      49              : 
      50              : 
      51            4 : (defn get-followed-players
      52              :  "Every player the user follows (PSA, ESF) with their current rankings,
      53              :   newest follow first; empty for guests."
      54              :  [repo user-id]
      55           14 :  (if (and repo user-id) (with-rankings repo (follow-repo/find-followed-players repo user-id)) []))
      56              : 
      57              : 
      58            4 : (defn get-player-profile
      59              :  "Profile of an imported player with :followed for the user (false for guests and
      60              :   for players that can't be followed) and current :rankings; nil when unknown."
      61              :  [repo user-id source player-id]
      62            3 :  (when repo
      63           11 :    (when-let [profile (follow-repo/find-player-profile repo source player-id)]
      64            1 :      (let [followed?
      65           16 :            (and (:followable profile) ((followed?-fn repo source user-id) (:player-id profile)))]
      66           12 :        (first (with-rankings repo [(assoc profile :followed (boolean followed?))]))))))
      67              : 
      68              : 
      69            4 : (defn search-players
      70              :  "Players of `source` (psa, esf, rankedin, fsu, or all) matching `query`
      71              :   by name, each with :followed for the user (false for guests and for players that
      72              :   can't be followed). `exclude` lists sources an \"all\" search leaves out."
      73            8 :  ([repo user-id source query limit] (search-players repo user-id source query limit nil))
      74              :  ([repo user-id source query limit exclude]
      75            2 :   (if repo
      76            8 :     (let [players   (follow-repo/search-players repo source query limit exclude)
      77            2 :           followed? (into
      78            1 :                      {}
      79           42 :                      (for [s (distinct (keep #(when (:followable %) (:source %)) players))]
      80            7 :                        [s (followed?-fn repo s user-id)]))]
      81            2 :       (mapv
      82            1 :        (fn
      83              :         [{:keys [source player-id]
      84              :           :as   player}]
      85           17 :         (assoc player :followed (boolean (when-let [f (followed? source)] (f player-id)))))
      86            1 :        players))
      87            0 :     [])))
        

Generated by: LCOV version 2.0-1