Skip to content

Commit

Permalink
remove more deps to user
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Apr 12, 2024
1 parent 1dbd581 commit 8fc08df
Show file tree
Hide file tree
Showing 24 changed files with 70 additions and 64 deletions.
2 changes: 1 addition & 1 deletion app/controllers/Game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ final class Game(env: Env, apiC: => Api) extends LilaController(env):

private[controllers] def preloadUsers(game: lila.game.Game): Funit =
env.user.lightUserApi.preloadMany(game.userIds)
private[controllers] def preloadUsers(users: lila.user.GameUsers): Unit =
private[controllers] def preloadUsers(users: lila.core.user.GameUsers): Unit =
env.user.lightUserApi.preloadUsers(users.all.collect:
case Some(lila.core.user.WithPerf(u, _)) => u
)
8 changes: 4 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ lazy val puzzle = module("puzzle",
)

lazy val storm = module("storm",
Seq(puzzle),
Seq(puzzle, user),
reactivemongo.bundle
)

Expand Down Expand Up @@ -255,17 +255,17 @@ lazy val pool = module("pool",
)

lazy val activity = module("activity",
Seq(puzzle),
Seq(puzzle, user),
reactivemongo.bundle
)

lazy val lobby = module("lobby",
Seq(game, user),
Seq(game),
Seq(lettuce) ++ reactivemongo.bundle
)

lazy val setup = module("setup",
Seq(lobby),
Seq(lobby, user),
reactivemongo.bundle
)

Expand Down
2 changes: 1 addition & 1 deletion modules/api/src/main/RoundApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import lila.simul.Simul
import lila.swiss.GameView as SwissView
import lila.tournament.GameView as TourView
import lila.tree.Node.partitionTreeJsonWriter
import lila.user.{ GameUsers, User, Me }
import lila.core.user.GameUsers
import lila.core.i18n.Translate
import lila.core.data.Preload

Expand Down
2 changes: 1 addition & 1 deletion modules/bot/src/main/BotJsonView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import lila.game.{ Game, GameRepo, Pov }
import lila.core.i18n.Translate

final class BotJsonView(
lightUserApi: lila.user.LightUserApi,
lightUserApi: lila.core.user.LightUserApi,
gameRepo: GameRepo,
rematches: lila.game.Rematches
)(using Executor):
Expand Down
2 changes: 1 addition & 1 deletion modules/bot/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import lila.core.socket.IsOnline
final class Env(
chatApi: lila.chat.ChatApi,
gameRepo: lila.game.GameRepo,
lightUserApi: lila.user.LightUserApi,
lightUserApi: lila.core.user.LightUserApi,
rematches: lila.game.Rematches,
isOfferingRematch: lila.core.round.IsOfferingRematch,
spam: lila.core.security.SpamApi,
Expand Down
2 changes: 1 addition & 1 deletion modules/challenge/src/main/Challenge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import lila.core.i18n.I18nKey
import lila.core.{ challenge as hub }
import lila.core.game.GameRule
import lila.rating.PerfType
import lila.user.{ GameUser, Me, User }
import lila.core.user.WithPerf
import lila.core.user.GameUser

case class Challenge(
@Key("_id") id: Challenge.Id,
Expand Down
2 changes: 1 addition & 1 deletion modules/challenge/src/main/ChallengeJoiner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import chess.{ ByColor, Mode, Situation }
import scala.util.chaining.*

import lila.game.{ Game, Player, Pov }
import lila.user.GameUser
import lila.core.user.GameUser

final private class ChallengeJoiner(
gameRepo: lila.game.GameRepo,
Expand Down
3 changes: 2 additions & 1 deletion modules/challenge/src/main/ChallengeMaker.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package lila.challenge

import lila.game.{ Game, GameRepo, Player, Pov, Rematches }
import lila.user.{ GameUser, User, UserApi, UserPerfsRepo }
import lila.user.{ UserApi, UserPerfsRepo }

import Challenge.TimeControl
import lila.core.user.WithPerf
import lila.core.user.GameUser

final class ChallengeMaker(
userApi: UserApi,
Expand Down
8 changes: 6 additions & 2 deletions modules/chat/src/main/JsonView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ object JsonView:
lineWriter.writes(line)

def userModInfo(using LightUser.GetterSync)(u: UserModInfo) =
lila.user.JsonView.modWrites.writes(u.user) ++ Json.obj:
"history" -> u.history
modView(u.user) ++ Json.obj("history" -> u.history)

private def modView(u: User) =
Json.toJsObject(u.light) ++ Json
.obj("games" -> u.count.game)
.add("tos" -> u.marks.dirty)

def mobile(chat: AnyChat, writeable: Boolean = true)(using FlairGetMap, Executor): Fu[JsObject] =
asyncLines(chat).map: lines =>
Expand Down
15 changes: 14 additions & 1 deletion modules/core/src/main/user.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import reactivemongo.api.bson.Macros.Annotations.Key
import reactivemongo.api.bson.{ BSONDocument, BSONDocumentHandler, BSONDocumentReader }
import reactivemongo.api.bson.collection.BSONCollection
import play.api.i18n.Lang
import _root_.chess.PlayerTitle
import _root_.chess.{ ByColor, PlayerTitle }

import lila.core.perf.Perf
import lila.core.rating.data.{ IntRating, IntRatingDiff }
import lila.core.perf.{ PerfKey, UserPerfs, UserWithPerfs }
import lila.core.userId.*
import lila.core.email.*
import lila.core.id.Flair
import lila.core.rating.Glicko

object user:

Expand Down Expand Up @@ -210,6 +211,15 @@ object user:
def dubiousPuzzle(id: UserId, puzzle: Perf): Fu[Boolean]
def setPerf(userId: UserId, pk: PerfKey, perf: Perf): Funit
def userIdsWithRoles(roles: List[String]): Fu[Set[UserId]]
def incColor(userId: UserId, value: Int): Unit
def firstGetsWhite(u1O: Option[UserId], u2O: Option[UserId]): Fu[Boolean]
def gamePlayersAny(userIds: ByColor[Option[UserId]], perf: PerfKey): Fu[GameUsers]
def gamePlayersLoggedIn(
ids: ByColor[UserId],
perf: PerfKey,
useCache: Boolean = true
): Fu[Option[ByColor[WithPerf]]]
def glicko(userId: UserId, perf: PerfKey): Fu[Glicko]

trait LightUserApiMinimal:
val async: LightUser.Getter
Expand Down Expand Up @@ -311,3 +321,6 @@ object user:
trait FlagApi:
val all: List[Flag]
val nonCountries: List[Flag.Code]

type GameUser = Option[WithPerf]
type GameUsers = ByColor[GameUser]
8 changes: 4 additions & 4 deletions modules/lobby/src/main/AbortListener.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import lila.game.Pov
import lila.core.game.Source

final private class AbortListener(
userRepo: lila.user.UserRepo,
userApi: lila.core.user.UserApi,
gameRepo: lila.game.GameRepo,
seekApi: SeekApi,
lobbyActor: LobbySyncActor
Expand All @@ -22,8 +22,8 @@ final private class AbortListener(
then
pov.game.userIds match
case List(u1, u2) =>
userRepo.incColor(u1, -1)
userRepo.incColor(u2, 1)
userApi.incColor(u1, -1)
userApi.incColor(u2, 1)
case _ =>

private def recreateSeek(pov: Pov): Funit =
Expand All @@ -35,7 +35,7 @@ final private class AbortListener(
}

private def worthRecreating(seek: Seek): Fu[Boolean] =
userRepo.byId(seek.user.id).map {
userApi.byId(seek.user.id).map {
_.exists: u =>
u.enabled.yes && !u.lame
}
16 changes: 6 additions & 10 deletions modules/lobby/src/main/Biter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import chess.{ ByColor, Game as ChessGame, Situation }

import lila.game.{ Game, Player }
import lila.core.socket.Sri
import lila.user.{ GameUsers, User }
import lila.core.user.GameUsers
import lila.core.user.WithPerf

final private class Biter(
userRepo: lila.user.UserRepo,
userApi: lila.user.UserApi,
userApi: lila.core.user.UserApi,
gameRepo: lila.game.GameRepo
)(using Executor, lila.game.IdGenerator):

Expand All @@ -25,7 +24,7 @@ final private class Biter(

private def join(hook: Hook, sri: Sri, lobbyUserOption: Option[LobbyUser]): Fu[JoinHook] =
for
users <- userApi.gamePlayers(ByColor(lobbyUserOption.map(_.id), hook.userId), hook.perfType)
users <- userApi.gamePlayersAny(ByColor(lobbyUserOption.map(_.id), hook.userId), hook.perfType)
(joiner, owner) = users.toPair
ownerColor <- assignCreatorColor(owner, joiner, hook.realColor)
game <- makeGame(
Expand All @@ -40,11 +39,8 @@ final private class Biter(

private def join(seek: Seek, lobbyUser: LobbyUser): Fu[JoinSeek] =
for
users <- userApi.gamePlayers
.loggedIn(
ByColor(lobbyUser.id, seek.user.id),
seek.perfType
)
users <- userApi
.gamePlayersLoggedIn(ByColor(lobbyUser.id, seek.user.id), seek.perfType)
.orFail(s"No such seek users: $seek")
(joiner, owner) = users.toPair
ownerColor <- assignCreatorColor(owner.some, joiner.some, seek.realColor)
Expand All @@ -68,7 +64,7 @@ final private class Biter(
): Fu[chess.Color] =
color match
case Color.Random =>
userRepo.firstGetsWhite(creatorUser.map(_.id), joinerUser.map(_.id)).map { chess.Color.fromWhite(_) }
userApi.firstGetsWhite(creatorUser.map(_.id), joinerUser.map(_.id)).map { chess.Color.fromWhite(_) }
case Color.White => fuccess(chess.White)
case Color.Black => fuccess(chess.Black)

Expand Down
4 changes: 1 addition & 3 deletions modules/lobby/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ final class Env(
relationApi: lila.core.relation.RelationApi,
hasCurrentPlayban: lila.core.playban.HasCurrentPlayban,
gameCache: lila.game.Cached,
userRepo: lila.user.UserRepo,
perfsRepo: lila.user.UserPerfsRepo,
userApi: lila.user.UserApi,
userApi: lila.core.user.UserApi,
gameRepo: lila.game.GameRepo,
poolApi: lila.core.pool.PoolApi,
cacheApi: lila.memo.CacheApi,
Expand Down
5 changes: 2 additions & 3 deletions modules/lobby/src/main/LobbySocket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ case class LobbyCounters(members: Int, rounds: Int)

final class LobbySocket(
biter: Biter,
perfsRepo: lila.user.UserPerfsRepo,
userApi: lila.user.UserApi,
userApi: lila.core.user.UserApi,
socketKit: SocketKit,
lobby: LobbySyncActor,
relationApi: lila.core.relation.RelationApi,
Expand Down Expand Up @@ -193,7 +192,7 @@ final class LobbySocket(
blocking = d.get[UserId]("blocking")
yield
lobby ! CancelHook(member.sri) // in case there's one...
perfsRepo.glicko(user.id, perfType).foreach { glicko =>
userApi.glicko(user.id, perfType).foreach { glicko =>
poolApi.join(
PoolConfigId(id),
lila.core.pool.Joiner(
Expand Down
6 changes: 3 additions & 3 deletions modules/lobby/src/main/SeekApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import lila.memo.CacheApi.*
import lila.core.perf.UserWithPerfs

final class SeekApi(
userApi: lila.core.user.UserApi,
config: SeekApi.Config,
biter: Biter,
relationApi: lila.core.relation.RelationApi,
perfsRepo: lila.user.UserPerfsRepo,
cacheApi: lila.memo.CacheApi
)(using Executor):
import config.*
Expand Down Expand Up @@ -41,7 +41,7 @@ final class SeekApi(
def forMe(using me: User | UserWithPerfs): Fu[List[Seek]] = for
user <- me match
case u: UserWithPerfs => fuccess(u)
case u: User => perfsRepo.withPerfs(u)
case u: User => userApi.withPerfs(u)
blocking <- relationApi.fetchBlocking(user.id)
seeks <- forUser(LobbyUser.make(user, lila.core.pool.Blocking(blocking)))
yield seeks
Expand Down Expand Up @@ -106,7 +106,7 @@ final class SeekApi(
.void
.andDo(cacheClear())

def removeByUser(user: lila.user.User) =
def removeByUser(user: User) =
coll.delete.one($doc("user.id" -> user.id)).void.andDo(cacheClear())

private object SeekApi:
Expand Down
1 change: 0 additions & 1 deletion modules/puzzle/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ final class Env(
cacheApi: lila.memo.CacheApi,
mongoCacheApi: lila.memo.MongoCache.Api,
gameRepo: lila.game.GameRepo,
perfsRepo: lila.user.UserPerfsRepo,
mongo: lila.db.Env
)(using Executor, akka.actor.ActorSystem, akka.stream.Materializer, lila.core.i18n.Translator)(using
scheduler: Scheduler,
Expand Down
2 changes: 1 addition & 1 deletion modules/round/src/main/JsonView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import lila.game.JsonView.given
import lila.game.{ Game, Player as GamePlayer, Pov }
import lila.pref.Pref

import lila.user.{ GameUser, GameUsers }
import lila.core.user.{ GameUser, GameUsers }
import lila.core.user.WithPerf
import lila.core.net.ApiVersion
import lila.core.perf.KeyedPerf
Expand Down
4 changes: 2 additions & 2 deletions modules/round/src/main/Rematcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import lila.common.Bus
import lila.game.{ AnonCookie, Event, Game, GameRepo, Pov, Rematches }
import lila.core.i18n.{ I18nKey as trans, defaultLang, Translator }
import scalalib.cache.ExpireSetMemo
import lila.user.{ GameUsers, UserApi }
import lila.core.user.{ GameUsers, UserApi }

import ChessColor.White

Expand Down Expand Up @@ -99,7 +99,7 @@ final private class Rematcher(
initialFen,
!chess960.get(pov.gameId)
)
users <- userApi.gamePlayers(pov.game.userIdPair, pov.game.perfType)
users <- userApi.gamePlayersAny(pov.game.userIdPair, pov.game.perfType)
sloppy = Game.make(
chess = newGame,
players = ByColor(returnPlayer(pov.game, _, users)),
Expand Down
2 changes: 1 addition & 1 deletion modules/setup/src/main/AiConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scalalib.model.Days
import lila.game.{ Game, IdGenerator, Player, Pov }
import lila.lobby.Color
import lila.rating.PerfType
import lila.user.GameUser
import lila.core.user.GameUser
import lila.core.game.Source

case class AiConfig(
Expand Down
2 changes: 1 addition & 1 deletion modules/setup/src/main/ApiAiConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scalalib.model.Days
import lila.game.{ Game, IdGenerator, Player, Pov }
import lila.lobby.Color
import lila.rating.PerfType
import lila.user.GameUser
import lila.core.user.GameUser
import lila.core.game.Source

final case class ApiAiConfig(
Expand Down
1 change: 0 additions & 1 deletion modules/timeline/src/main/TimelineApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import akka.actor.*

import lila.core.timeline.{ Atom, Propagate, Propagation, ReloadTimelines }
import lila.core.perm.Permission
import lila.user.UserRepo
import lila.core.team.Access
import lila.core.timeline.*

Expand Down
5 changes: 0 additions & 5 deletions modules/user/src/main/JsonView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ object JsonView:
.add("title" -> l.user.title)
.add("patron" -> l.user.isPatron)

val modWrites = OWrites[User]: u =>
Json.toJsObject(u.light) ++ Json
.obj("games" -> u.count.game)
.add("tos" -> u.marks.dirty)

given perfWrites: OWrites[Perf] = OWrites: o =>
Json
.obj(
Expand Down
Loading

0 comments on commit 8fc08df

Please sign in to comment.