forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeamInfo.scala
53 lines (47 loc) · 1.6 KB
/
TeamInfo.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
package lila.app
package mashup
import lila.forum.MiniForumPost
import lila.team.{ RequestRepo, RequestWithUser, Team, TeamApi }
import lila.tournament.{ Tournament, TournamentRepo }
import lila.user.User
case class TeamInfo(
mine: Boolean,
createdByMe: Boolean,
requestedByMe: Boolean,
requests: List[RequestWithUser],
forumNbPosts: Int,
forumPosts: List[MiniForumPost],
teamBattles: List[Tournament]
) {
def hasRequests = requests.nonEmpty
def userIds = forumPosts.flatMap(_.userId)
}
final class TeamInfoApi(
api: TeamApi,
categApi: lila.forum.CategApi,
forumRecent: lila.forum.Recent,
teamCached: lila.team.Cached,
tournamentRepo: TournamentRepo,
requestRepo: RequestRepo
)(implicit ec: scala.concurrent.ExecutionContext) {
def apply(team: Team, me: Option[User]): Fu[TeamInfo] =
for {
requests <- (team.enabled && me.??(m => team.isCreator(m.id))) ?? api.requestsWithUsers(team)
mine <- me.??(m => api.belongsTo(team.id, m.id))
requestedByMe <- !mine ?? me.??(m => requestRepo.exists(team.id, m.id))
forumNbPosts <- categApi.teamNbPosts(team.id)
forumPosts <- forumRecent.team(team.id)
tours <- tournamentRepo.byTeam(team.id, 10)
_ <- tours.nonEmpty ?? {
teamCached.preloadSet(tours.flatMap(_.teamBattle.??(_.teams)).toSet)
}
} yield TeamInfo(
mine = mine,
createdByMe = ~me.map(m => team.isCreator(m.id)),
requestedByMe = requestedByMe,
requests = requests,
forumNbPosts = forumNbPosts,
forumPosts = forumPosts,
teamBattles = tours
)
}