Skip to content

Commit

Permalink
remove dependencies to user
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Apr 12, 2024
1 parent 260cf8c commit 6bb24b6
Show file tree
Hide file tree
Showing 43 changed files with 82 additions and 85 deletions.
2 changes: 1 addition & 1 deletion app/templating/SecurityHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package templating

import lila.app.ui.ScalatagsTemplate.*
import lila.core.perm.{ Granter, Permission }
import lila.user.{ Me, User }
import lila.core.user.User

trait SecurityHelper:

Expand Down
1 change: 0 additions & 1 deletion app/views/appeal/discussion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import lila.mod.IpRender.RenderIp
import lila.mod.{ ModPreset, ModPresets, UserWithModlog }
import lila.report.Report.Inquiry
import lila.report.Suspect
import lila.user.{ Me, User }

import trans.appeal

Expand Down
3 changes: 2 additions & 1 deletion app/views/coach/index.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import lila.app.ui.ScalatagsTemplate.{ *, given }
import lila.core.app.LangPath
import scalalib.paginator.Paginator
import lila.i18n.LangList
import lila.user.{ Flag, Flags }
import lila.core.user.Flag
import lila.user.Flags

object index:

Expand Down
1 change: 0 additions & 1 deletion app/views/mod/communication.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import lila.mod.IpRender.RenderIp
import lila.mod.UserWithModlog
import lila.relation.Follow
import lila.shutup.Analyser
import lila.user.{ Me, User }

object communication:

Expand Down
2 changes: 1 addition & 1 deletion app/views/mod/permissions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import controllers.routes
import lila.app.templating.Environment.{ *, given }
import lila.app.ui.ScalatagsTemplate.{ *, given }
import lila.core.perm.Permission
import lila.user.{ Me, User }

import lila.security.Granter.canGrant
import lila.security.Permission.findGranterPackage

Expand Down
2 changes: 1 addition & 1 deletion app/views/mod/search.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import lila.common.String.html.richText
import scalalib.paginator.Paginator
import lila.mod.IpRender.RenderIp
import lila.security.{ FingerHash, IpTrust }
import lila.user.{ Me, User }

import lila.user.WithPerfsAndEmails

object search:
Expand Down
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ lazy val video = module("video",
)

lazy val coach = module("coach",
Seq(game, user),
Seq(game),
reactivemongo.bundle
)

Expand All @@ -165,12 +165,12 @@ lazy val feed = module("feed",
)

lazy val ublog = module("ublog",
Seq(user),
Seq(coreI18n, memo),
Seq(bloomFilter) ++ reactivemongo.bundle
)

lazy val evaluation = module("evaluation",
Seq(analyse, user),
Seq(analyse),
tests.bundle ++ reactivemongo.bundle
)

Expand Down
1 change: 0 additions & 1 deletion modules/api/src/main/AccountClosure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lila.api

import lila.common.Bus
import lila.core.perm.Granter
import lila.user.{ Me, User }

final class AccountClosure(
userRepo: lila.user.UserRepo,
Expand Down
2 changes: 1 addition & 1 deletion modules/api/src/main/Context.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import lila.core.i18n.{ Language, Translate, defaultLanguage }
import lila.notify.Notification.UnreadCount
import lila.oauth.{ OAuthScope, TokenScopes }
import lila.pref.Pref
import lila.user.{ Me, User }

import lila.core.user.KidMode
import lila.user.UserExt.userLanguage

Expand Down
2 changes: 1 addition & 1 deletion modules/api/src/main/GameApiV2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import lila.game.{ Divider, Game, Pov, Query }
import lila.round.GameProxyRepo
import lila.team.GameTeams
import lila.tournament.Tournament
import lila.user.{ Me, User }

import lila.core.i18n.Translate

final class GameApiV2(
Expand Down
2 changes: 1 addition & 1 deletion modules/challenge/src/main/ChallengeGranter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package lila.challenge
import lila.core.i18n.I18nKey.challenge as trans
import lila.rating.PerfType
import lila.core.relation.{ Block, Follow }
import lila.user.{ Me, User }

import lila.core.i18n.Translate

case class ChallengeDenied(dest: User, reason: ChallengeDenied.Reason)
Expand Down
2 changes: 0 additions & 2 deletions modules/chat/src/main/Chat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package lila.chat

import reactivemongo.api.bson.BSONDocumentHandler

import lila.user.{ Me, User }

sealed trait AnyChat:
def id: ChatId
def lines: List[Line]
Expand Down
2 changes: 0 additions & 2 deletions modules/clas/src/main/ClasInvite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package lila.clas

import scalalib.ThreadLocalRandom

import lila.user.{ Me, User }

case class ClasInvite(
_id: ClasInvite.Id, // random
userId: UserId,
Expand Down
20 changes: 10 additions & 10 deletions modules/coach/src/main/CoachApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package lila.coach
import lila.db.dsl.{ *, given }
import lila.memo.PicfitApi
import lila.core.perm.Granter
import lila.user.{ Me, User, UserPerfsRepo, UserRepo }
import lila.core.perf.UserPerfs
import lila.rating.UserPerfsExt.bestStandardRating

final class CoachApi(
coachColl: Coll,
userRepo: UserRepo,
perfsRepo: UserPerfsRepo,
userRepo: lila.core.user.UserRepo,
userApi: lila.core.user.UserApi,
flagApi: lila.core.user.FlagApi,
picfitApi: PicfitApi,
cacheApi: lila.memo.CacheApi
)(using Executor):
Expand All @@ -20,19 +20,19 @@ final class CoachApi(
def byId[U: UserIdOf](u: U): Fu[Option[Coach]] = coachColl.byId[Coach](u)

def find(username: UserStr): Fu[Option[Coach.WithUser]] =
userRepo.byId(username).flatMapz(find)
userApi.byId(username).flatMapz(find)

def canCoach = Granter.ofUser(_.Coach)

def find(user: User): Fu[Option[Coach.WithUser]] =
canCoach(user).so:
byId(user.id).flatMapz: coach =>
perfsRepo.withPerfs(user).dmap(coach.withUser).dmap(some)
userApi.withPerfs(user).dmap(coach.withUser).dmap(some)

def findOrInit(using me: Me): Fu[Option[Coach.WithUser]] =
val user = me.value
canCoach(user).so:
find(user).orElse(perfsRepo.withPerfs(user).flatMap { user =>
find(user).orElse(userApi.withPerfs(user).flatMap { user =>
val c = Coach.make(user).withUser(user)
coachColl.insert.one(c.coach).inject(c.some)
})
Expand All @@ -53,7 +53,7 @@ final class CoachApi(

def updateRatingFromDb(user: User): Funit =
canCoach(user).so:
perfsRepo.perfsOf(user).flatMap { perfs =>
userApi.perfsOf(user).flatMap { perfs =>
coachColl.update.one($id(perfs.id), $set("user.rating" -> perfs.bestStandardRating)).void
}

Expand Down Expand Up @@ -81,16 +81,16 @@ final class CoachApi(

private val countriesCache = cacheApi.unit[CountrySelection]:
_.refreshAfterWrite(1 hour).buildAsyncFuture: _ =>
import lila.user.{ Flag, Flags }
import lila.core.user.Flag
userRepo.coll.secondaryPreferred
.distinctEasy[Flag.Code, Set](
"profile.country",
$doc("roles" -> lila.core.perm.Permission.Coach.dbKey, "enabled" -> true)
)
.map: codes =>
("all", "All countries") :: Flags.all
("all", "All countries") :: flagApi.all
.collect:
case f if codes.contains(f.code) && !Flags.nonCountries.contains(f.code) => f.code -> f.name
case f if codes.contains(f.code) && !flagApi.nonCountries.contains(f.code) => f.code -> f.name
.sortBy(_._2)
.map(CountrySelection(_))

Expand Down
18 changes: 9 additions & 9 deletions modules/coach/src/main/CoachPager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import lila.coach.CoachPager.Order.{ Alphabetical, LichessRating, Login }
import scalalib.paginator.{ AdapterLike, Paginator }
import lila.db.dsl.{ *, given }
import lila.core.perm.Permission
import lila.user.{ Flag, User, UserPerfsRepo, UserRepo }
import lila.core.perf.UserPerfs
import lila.core.user.UserMark
import lila.core.perf.UserWithPerfs
import lila.core.user.Flag

final class CoachPager(
userRepo: UserRepo,
perfsRepo: UserPerfsRepo,
userRepo: lila.core.user.UserRepo,
perfsRepo: lila.core.user.PerfsRepo,
coll: Coll
)(using Executor):

Expand Down Expand Up @@ -57,25 +57,25 @@ final class CoachPager(
UnwindField("_user"),
Match(
$doc(
s"_user.${lila.user.BSONFields.roles}" -> Permission.Coach.dbKey,
s"_user.${lila.user.BSONFields.enabled}" -> true,
s"_user.${lila.user.BSONFields.marks}"
s"_user.${lila.core.user.BSONFields.roles}" -> Permission.Coach.dbKey,
s"_user.${lila.core.user.BSONFields.enabled}" -> true,
s"_user.${lila.core.user.BSONFields.marks}"
.$nin(List(UserMark.engine, UserMark.boost, UserMark.troll))
) ++ country.so { c =>
$doc("_user.profile.country" -> c.code)
}
),
Skip(offset),
Limit(length),
PipelineOperator(perfsRepo.aggregate.lookup)
PipelineOperator(perfsRepo.aggregateLookup)
)
.map: docs =>
import lila.user.BSONHandlers.userHandler
import userRepo.userHandler
for
doc <- docs
coach <- doc.asOpt[Coach]
user <- doc.getAsOpt[User]("_user")
perfs = perfsRepo.aggregate.readFirst(doc, user)
perfs = perfsRepo.aggregateReadFirst(doc, user)
yield coach.withUser(UserWithPerfs(user, perfs))

Paginator(
Expand Down
6 changes: 4 additions & 2 deletions modules/coach/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import lila.core.config.*
@Module
final class Env(
appConfig: Configuration,
userRepo: lila.user.UserRepo,
perfsRepo: lila.user.UserPerfsRepo,
perfsRepo: lila.core.user.PerfsRepo,
userRepo: lila.core.user.UserRepo,
userApi: lila.core.user.UserApi,
flagApi: lila.core.user.FlagApi,
cacheApi: lila.memo.CacheApi,
db: lila.db.Db,
picfitApi: lila.memo.PicfitApi
Expand Down
2 changes: 1 addition & 1 deletion modules/coach/src/main/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export lila.common.extensions.*

private val logger = lila.log("coach")

import lila.user.Flag
import lila.core.user.Flag
opaque type CountrySelection = List[(Flag.Code, Flag.Name)]
object CountrySelection extends TotalWrapper[CountrySelection, List[(Flag.Code, Flag.Name)]]
28 changes: 24 additions & 4 deletions modules/core/src/main/user.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package lila.core

import reactivemongo.api.bson.Macros.Annotations.Key
import reactivemongo.api.bson.{ BSONDocument, BSONDocumentHandler }
import reactivemongo.api.bson.collection.BSONCollection
import play.api.i18n.Lang
import _root_.chess.PlayerTitle

import lila.core.perf.Perf
import lila.core.rating.data.{ IntRating, IntRatingDiff }
import lila.core.perf.{ PerfKey, UserWithPerfs }
import lila.core.perf.{ PerfKey, UserPerfs, UserWithPerfs }
import lila.core.userId.*
import lila.core.email.*
import lila.core.id.Flair
Expand Down Expand Up @@ -205,6 +207,9 @@ object user:
def countEngines(userIds: List[UserId]): Fu[Int]
def getTitle(id: UserId): Fu[Option[PlayerTitle]]
def listWithPerfs[U: UserIdOf](us: List[U]): Fu[List[UserWithPerfs]]
def withPerfs(u: User): Fu[UserWithPerfs]
def withPerfs[U: UserIdOf](id: U): Fu[Option[UserWithPerfs]]
def perfsOf[U: UserIdOf](u: U): Fu[UserPerfs]

trait LightUserApiMinimal:
val async: LightUser.Getter
Expand Down Expand Up @@ -248,12 +253,17 @@ object user:
def arenaBan: Boolean = hasMark(UserMark.arenaban)
def alt: Boolean = hasMark(UserMark.alt)

abstract class UserRepo(val coll: reactivemongo.api.bson.collection.BSONCollection):
given userHandler: reactivemongo.api.bson.BSONDocumentHandler[User]
abstract class UserRepo(val coll: BSONCollection):
given userHandler: BSONDocumentHandler[User]
abstract class PerfsRepo(val coll: BSONCollection):
def aggregateLookup: BSONDocument
def aggregateReadFirst[U: UserIdOf](root: BSONDocument, u: U): UserPerfs

object BSONFields:
val enabled = "enabled"
val title = "title"
val roles = "roles"
val marks = "marks"

trait Note:
val text: String
Expand Down Expand Up @@ -289,5 +299,15 @@ object user:
def lightMe: LightUser.Me = LightUser.Me(me.value.light)
inline def modId: ModId = userId.into(ModId)
inline def myId: MyId = userId.into(MyId)
// given (using me: Me): LightUser.Me = LightUser.Me(me.light)

given (using me: Me): LightUser.Me = LightUser.Me(me.light)
final class Flag(val code: Flag.Code, val name: Flag.Name, val abrev: Option[String]):
def shortName = abrev | name

object Flag:
type Code = String
type Name = String

trait FlagApi:
val all: List[Flag]
val nonCountries: List[Flag.Code]
2 changes: 1 addition & 1 deletion modules/forum/src/main/ForumPost.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scalalib.ThreadLocalRandom
import reactivemongo.api.bson.Macros.Annotations.Key

import lila.core.perm.Granter
import lila.user.{ Me, User }

import lila.core.forum.ForumPostMini

case class OldVersion(text: String, createdAt: Instant)
Expand Down
2 changes: 1 addition & 1 deletion modules/mod/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import chess.ByColor

import lila.core.config.*
import lila.core.report.SuspectId
import lila.user.{ Me, User }

import lila.core.user.WithPerf
import lila.common.Bus
import lila.rating.UserWithPerfs.only
Expand Down
1 change: 0 additions & 1 deletion modules/msg/src/main/BsonHandlers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import reactivemongo.api.bson.*

import lila.db.BSON
import lila.db.dsl.{ *, given }
import lila.user.{ Me, User }

private object BsonHandlers:

Expand Down
1 change: 0 additions & 1 deletion modules/msg/src/main/MsgThread.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lila.msg

import lila.core.LightUser
import lila.user.{ Me, User }

case class MsgThread(
id: MsgThread.Id,
Expand Down
1 change: 0 additions & 1 deletion modules/puzzle/src/main/PuzzleSession.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import scala.util.chaining.*

import lila.memo.CacheApi

import lila.user.{ Me, User }
import lila.rating.GlickoExt.clueless

private case class PuzzleSession(
Expand Down
2 changes: 1 addition & 1 deletion modules/relay/src/main/RelayRoundForm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import scala.util.chaining.*
import lila.common.Form.{ cleanText, into }
import scalalib.model.Seconds
import lila.core.perm.Granter
import lila.user.{ Me, User }

import lila.relay.RelayRound.Sync

final class RelayRoundForm(using mode: play.api.Mode):
Expand Down
1 change: 0 additions & 1 deletion modules/security/src/main/Granter.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package lila.security

import lila.user.{ Me, User }
import lila.core.perm.Grantable
import lila.core.perm.Permission

Expand Down
Loading

0 comments on commit 6bb24b6

Please sign in to comment.