Skip to content

Commit

Permalink
remove more deps to pref
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Apr 11, 2024
1 parent 8b3f012 commit c31759b
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 45 deletions.
16 changes: 8 additions & 8 deletions app/templating/SetupHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ trait SetupHelper:

def translatedChallengeChoices(using Translate) =
List(
(Pref.Challenge.NEVER, trans.site.never.txt()),
(lila.core.pref.Challenge.NEVER, trans.site.never.txt()),
(
Pref.Challenge.RATING,
lila.core.pref.Challenge.RATING,
trans.site.ifRatingIsPlusMinusX.txt(lila.pref.Pref.Challenge.ratingThreshold)
),
(Pref.Challenge.FRIEND, trans.site.onlyFriends.txt()),
(Pref.Challenge.REGISTERED, trans.site.ifRegistered.txt()),
(Pref.Challenge.ALWAYS, trans.site.always.txt())
(lila.core.pref.Challenge.FRIEND, trans.site.onlyFriends.txt()),
(lila.core.pref.Challenge.REGISTERED, trans.site.ifRegistered.txt()),
(lila.core.pref.Challenge.ALWAYS, trans.site.always.txt())
)

def translatedMessageChoices(using Translate) =
Expand All @@ -298,9 +298,9 @@ trait SetupHelper:
def translatedPalantirChoices(using Translate) = privacyBaseChoices
private def privacyBaseChoices(using Translate) =
List(
(Pref.StudyInvite.NEVER, trans.site.never.txt()),
(Pref.StudyInvite.FRIEND, trans.site.onlyFriends.txt()),
(Pref.StudyInvite.ALWAYS, trans.site.always.txt())
(lila.core.pref.StudyInvite.NEVER, trans.site.never.txt()),
(lila.core.pref.StudyInvite.FRIEND, trans.site.onlyFriends.txt()),
(lila.core.pref.StudyInvite.ALWAYS, trans.site.always.txt())
)

def translatedInsightShareChoices(using Translate) =
Expand Down
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ lazy val shutup = module("shutup",
)

lazy val challenge = module("challenge",
Seq(user, pref, game, room, oauth),
Seq(user, game, room, oauth),
Seq(scalatags, lettuce) ++ tests.bundle ++ reactivemongo.bundle
)

Expand All @@ -345,7 +345,7 @@ lazy val fide = module("fide",
)

lazy val study = module("study",
Seq(importer, analyse, room, pref, user),
Seq(importer, analyse, room, user),
Seq(scalatags, lettuce) ++ tests.bundle ++ reactivemongo.bundle ++ Seq(scalacheck, munitCheck, chess.testKit)
).dependsOn(common % "test->test")

Expand Down Expand Up @@ -410,7 +410,7 @@ lazy val pref = module("pref",
)

lazy val msg = module("msg",
Seq(user, pref),
Seq(user),
reactivemongo.bundle
)

Expand Down
23 changes: 11 additions & 12 deletions modules/challenge/src/main/ChallengeGranter.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lila.challenge

import lila.core.i18n.I18nKey.challenge as trans
import lila.pref.Pref
import lila.rating.PerfType
import lila.core.relation.{ Block, Follow }
import lila.user.{ Me, User }
Expand Down Expand Up @@ -34,7 +33,7 @@ object ChallengeDenied:
case Reason.SelfChallenge => "You cannot challenge yourself."

final class ChallengeGranter(
prefApi: lila.pref.PrefApi,
prefApi: lila.core.pref.PrefApi,
perfsRepo: lila.user.UserPerfsRepo,
relationApi: lila.core.relation.RelationApi
):
Expand All @@ -47,20 +46,20 @@ final class ChallengeGranter(
Executor
)(using me: Option[Me]): Fu[Option[ChallengeDenied]] = me
.fold[Fu[Option[ChallengeDenied.Reason]]] {
prefApi.get(dest).map(_.challenge).map {
case Pref.Challenge.ALWAYS => none
case _ => YouAreAnon.some
prefApi.getChallenge(dest.id).map {
case lila.core.pref.Challenge.ALWAYS => none
case _ => YouAreAnon.some
}
} { from =>
type Res = Option[ChallengeDenied.Reason]
given Conversion[Res, Fu[Res]] = fuccess
relationApi.fetchRelation(dest.id, from.userId).zip(prefApi.get(dest).map(_.challenge)).flatMap {
relationApi.fetchRelation(dest.id, from.userId).zip(prefApi.getChallenge(dest.id)).flatMap {
case (Some(Block), _) => YouAreBlocked.some
case (_, Pref.Challenge.NEVER) => TheyDontAcceptChallenges.some
case (_, lila.core.pref.Challenge.NEVER) => TheyDontAcceptChallenges.some
case (Some(Follow), _) => none // always accept from followed
case (_, _) if from.marks.engine && !dest.marks.engine => YouAreBlocked.some
case (_, Pref.Challenge.FRIEND) => FriendsOnly.some
case (_, Pref.Challenge.RATING) =>
case (_, lila.core.pref.Challenge.FRIEND) => FriendsOnly.some
case (_, lila.core.pref.Challenge.RATING) =>
perfsRepo
.perfsOf(from.value -> dest, _.sec)
.map: (fromPerfs, destPerfs) =>
Expand All @@ -70,9 +69,9 @@ final class ChallengeGranter(
val diff =
math.abs(fromPerfs(perfType).intRating.value - destPerfs(perfType).intRating.value)
(diff > ratingThreshold).option(RatingOutsideRange(perfType))
case (_, Pref.Challenge.REGISTERED) => none
case _ if from == dest => SelfChallenge.some
case _ => none
case (_, lila.core.pref.Challenge.REGISTERED) => none
case _ if from == dest => SelfChallenge.some
case _ => none
}
}
.map:
Expand Down
2 changes: 1 addition & 1 deletion modules/challenge/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class Env(
isOnline: lila.core.socket.IsOnline,
db: lila.db.Db,
cacheApi: lila.memo.CacheApi,
prefApi: lila.pref.PrefApi,
prefApi: lila.core.pref.PrefApi,
relationApi: lila.core.relation.RelationApi,
socketKit: lila.core.socket.SocketKit,
getLagRating: lila.core.socket.userLag.GetLagRating,
Expand Down
16 changes: 15 additions & 1 deletion modules/core/src/main/pref.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ trait Pref:

trait PrefApi:
def followable(userId: UserId): Fu[Boolean]
def mentionableIds(userIds: Set[UserId]): Fu[Set[UserId]]
def getMessage(userId: UserId): Fu[Int]
def getInsightShare(userId: UserId): Future[Int]
def mentionableIds(userIds: Set[UserId]): Fu[Set[UserId]]
def getChallenge(userId: UserId): Future[Int]
def getStudyInvite(userId: UserId): Future[Int]

object Message:
val NEVER = 1
Expand All @@ -34,3 +36,15 @@ object InsightShare:
val NOBODY = 0
val FRIENDS = 1
val EVERYBODY = 2

object Challenge:
val NEVER = 1
val RATING = 2
val FRIEND = 3
val REGISTERED = 4
val ALWAYS = 5

object StudyInvite:
val NEVER = 1
val FRIEND = 2
val ALWAYS = 3
14 changes: 4 additions & 10 deletions modules/pref/src/main/Pref.scala
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,7 @@ object Pref:
)

object Challenge:
val NEVER = 1
val RATING = 2
val FRIEND = 3
val REGISTERED = 4
val ALWAYS = 5
import lila.core.pref.Challenge.*

val ratingThreshold = 300

Expand All @@ -369,9 +365,7 @@ object Pref:
)

object StudyInvite:
val NEVER = 1
val FRIEND = 2
val ALWAYS = 3
import lila.core.pref.StudyInvite.*

val choices = Seq(
NEVER -> "Never",
Expand Down Expand Up @@ -447,9 +441,9 @@ object Pref:
coords = Coords.INSIDE,
replay = Replay.ALWAYS,
clockTenths = ClockTenths.LOWTIME,
challenge = Challenge.REGISTERED,
challenge = lila.core.pref.Challenge.REGISTERED,
message = lila.core.pref.Message.ALWAYS,
studyInvite = StudyInvite.ALWAYS,
studyInvite = lila.core.pref.StudyInvite.ALWAYS,
submitMove = SubmitMove.CORRESPONDENCE,
confirmResign = ConfirmResign.YES,
insightShare = lila.core.pref.InsightShare.FRIENDS,
Expand Down
2 changes: 2 additions & 0 deletions modules/pref/src/main/PrefApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ final class PrefApi(

def getMessage(userId: UserId): Future[Int] = get(userId, _.message)
def getInsightShare(userId: UserId): Future[Int] = get(userId, _.insightShare)
def getChallenge(userId: UserId): Future[Int] = get(userId, _.challenge)
def getStudyInvite(userId: UserId): Future[Int] = get(userId, _.studyInvite)

def followable(userId: UserId): Fu[Boolean] =
coll.primitiveOne[Boolean]($id(userId), "follow").map(_ | Pref.default.follow)
Expand Down
2 changes: 1 addition & 1 deletion modules/study/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class Env(
notifyApi: lila.core.notify.NotifyApi,
federations: lila.core.fide.Federation.FedsOf,
federationNames: lila.core.fide.Federation.NamesOf,
prefApi: lila.pref.PrefApi,
prefApi: lila.core.pref.PrefApi,
relationApi: lila.core.relation.RelationApi,
socketKit: lila.core.socket.SocketKit,
socketReq: lila.core.socket.SocketRequester,
Expand Down
18 changes: 9 additions & 9 deletions modules/study/src/main/StudyInvite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lila.study

import lila.db.dsl.{ *, given }
import lila.core.notify.{ InvitedToStudy, NotifyApi }
import lila.pref.Pref
import lila.core.relation.Relation.{ Block, Follow }
import lila.core.perm.Granter
import lila.user.{ Me, User }
Expand All @@ -11,7 +10,7 @@ final private class StudyInvite(
studyRepo: StudyRepo,
userRepo: lila.user.UserRepo,
notifyApi: NotifyApi,
prefApi: lila.pref.PrefApi,
prefApi: lila.core.pref.PrefApi,
relationApi: lila.core.relation.RelationApi
)(using Executor):

Expand Down Expand Up @@ -48,13 +47,14 @@ final private class StudyInvite(
_ <-
if isPresent || Granter[Me](_.StudyAdmin) then funit
else
prefApi.get(invited).map(_.studyInvite).flatMap {
case Pref.StudyInvite.ALWAYS => funit
case Pref.StudyInvite.NEVER => fufail("This user doesn't accept study invitations")
case Pref.StudyInvite.FRIEND =>
if relation.has(Follow) then funit
else fufail("This user only accept study invitations from friends")
}
prefApi
.getStudyInvite(invited.id)
.flatMap:
case lila.core.pref.StudyInvite.ALWAYS => funit
case lila.core.pref.StudyInvite.NEVER => fufail("This user doesn't accept study invitations")
case lila.core.pref.StudyInvite.FRIEND =>
if relation.has(Follow) then funit
else fufail("This user only accept study invitations from friends")
_ <- studyRepo.addMember(study, StudyMember.make(invited))
shouldNotify = !isPresent && (!inviter.marks.troll || relation.has(Follow))
rateLimitCost =
Expand Down

0 comments on commit c31759b

Please sign in to comment.