forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserInfo.scala
121 lines (110 loc) · 3.93 KB
/
UserInfo.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package lila.app
package mashup
import chess.Color
import org.joda.time.Period
import lila.api.Context
import lila.bookmark.BookmarkApi
import lila.forum.PostApi
import lila.game.{ GameRepo, Game, Crosstable, PlayTime }
import lila.relation.RelationApi
import lila.security.Granter
import lila.user.{ User, Trophy, Trophies, TrophyApi }
case class UserInfo(
user: User,
ranks: lila.rating.UserRankMap,
nbPlaying: Int,
hasSimul: Boolean,
crosstable: Option[Crosstable],
nbBookmark: Int,
nbImported: Int,
ratingChart: Option[String],
nbFollowers: Int,
nbBlockers: Option[Int],
nbPosts: Int,
nbStudies: Int,
playTime: User.PlayTime,
trophies: Trophies,
isStreamer: Boolean,
isCoach: Boolean,
insightVisible: Boolean,
completionRate: Option[Double]) {
def nbRated = user.count.rated
def nbWithMe = crosstable ?? (_.nbGames)
def percentRated: Int = math.round(nbRated / user.count.game.toFloat * 100)
def completionRatePercent = completionRate.map { cr => math.round(cr * 100) }
def isPublicMod = lila.security.Granter(_.PublicMod)(user)
def isDeveloper = lila.security.Granter(_.Developer)(user)
def allTrophies = List(
isPublicMod option Trophy(
_id = "",
user = user.id,
kind = Trophy.Kind.Moderator,
date = org.joda.time.DateTime.now),
isDeveloper option Trophy(
_id = "",
user = user.id,
kind = Trophy.Kind.Developer,
date = org.joda.time.DateTime.now),
isStreamer option Trophy(
_id = "",
user = user.id,
kind = Trophy.Kind.Streamer,
date = org.joda.time.DateTime.now)
).flatten ::: trophies
}
object UserInfo {
def apply(
bookmarkApi: BookmarkApi,
relationApi: RelationApi,
trophyApi: TrophyApi,
gameCached: lila.game.Cached,
crosstableApi: lila.game.CrosstableApi,
postApi: PostApi,
studyRepo: lila.study.StudyRepo,
getRatingChart: User => Fu[Option[String]],
getRanks: String => Fu[Map[String, Int]],
isHostingSimul: String => Fu[Boolean],
isStreamer: String => Boolean,
fetchIsCoach: User => Fu[Boolean],
insightShare: lila.insight.Share,
getPlayTime: User => Fu[User.PlayTime],
completionRate: User.ID => Fu[Option[Double]])(user: User, ctx: Context): Fu[UserInfo] =
getRanks(user.id) zip
(gameCached nbPlaying user.id) zip
gameCached.nbImportedBy(user.id) zip
(ctx.me.filter(user!=) ?? { me => crosstableApi(me.id, user.id) }) zip
getRatingChart(user) zip
relationApi.countFollowers(user.id) zip
(ctx.me ?? Granter(_.UserSpy) ?? { relationApi.countBlockers(user.id) map (_.some) }) zip
postApi.nbByUser(user.id) zip
studyRepo.countByOwner(user.id) zip
trophyApi.findByUser(user) zip
fetchIsCoach(user) zip
(user.count.rated >= 10).??(insightShare.grant(user, ctx.me)) zip
getPlayTime(user) zip
completionRate(user.id) zip
bookmarkApi.countByUser(user) flatMap {
case ((((((((((((((ranks, nbPlaying), nbImported), crosstable), ratingChart), nbFollowers), nbBlockers), nbPosts), nbStudies), trophies), isCoach), insightVisible), playTime), completionRate), nbBookmarks) =>
(nbPlaying > 0) ?? isHostingSimul(user.id) map { hasSimul =>
new UserInfo(
user = user,
ranks = ranks,
nbPlaying = nbPlaying,
hasSimul = hasSimul,
crosstable = crosstable,
nbBookmark = nbBookmarks,
nbImported = nbImported,
ratingChart = ratingChart,
nbFollowers = nbFollowers,
nbBlockers = nbBlockers,
nbPosts = nbPosts,
nbStudies = nbStudies,
playTime = playTime,
trophies = trophies,
isStreamer = isStreamer(user.id),
isCoach = isCoach,
insightVisible = insightVisible,
completionRate = completionRate)
}
}
}