Skip to content

Commit

Permalink
remove given userIdOfWriter, it writes UserName as UserId, lowercased
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Aug 28, 2024
1 parent 46e800a commit cfabe95
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion modules/coach/src/main/CoachApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class CoachApi(

import BsonHandlers.given

def byId[U: UserIdOf](u: U): Fu[Option[Coach]] = coachColl.byId[Coach](u)
def byId[U: UserIdOf](u: U): Fu[Option[Coach]] = coachColl.byId[Coach](u.id)

def find(username: UserStr): Fu[Option[Coach.WithUser]] =
userApi.byId(username).flatMapz(find)
Expand Down
6 changes: 2 additions & 4 deletions modules/db/src/main/Handlers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ trait Handlers:
def toBdoc[A](a: A)(using writer: BSONDocumentWriter[A]): Option[BSONDocument] = writer.writeOpt(a)
def tryBdoc[A](a: A)(using writer: BSONDocumentWriter[A]): Try[BSONDocument] = writer.writeTry(a)

def toBson[A](a: A)(using writer: BSONWriter[A]): Option[BSONValue] = writer.writeOpt(a)

// free writer for all types with TotalWrapper
// unless they are given an instance of lila.db.NoBSONWriter[T]
given opaqueWriter[T, A](using
Expand All @@ -34,10 +36,6 @@ trait Handlers:

given NoDbHandler[Blurs] with {}

given userIdOfWriter[U: UserIdOf](using writer: BSONWriter[UserId]): BSONWriter[U] with
inline def writeTry(u: U) = writer.writeTry(u.id)
given noUserIdOf[A: lila.core.userId.UserIdOf]: NoBSONWriter[A] with {}

given NoBSONWriter[UserId] with {}
given userIdHandler: BSONHandler[UserId] = stringIsoHandler

Expand Down
2 changes: 1 addition & 1 deletion modules/game/src/main/Query.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object Query:
"p1.ai".$exists(false)
)

def nowPlaying[U: UserIdOf](u: U) = $doc(F.playingUids -> u)
def nowPlaying[U: UserIdOf](u: U) = $doc(F.playingUids -> u.id)

def recentlyPlaying(u: UserId) =
nowPlaying(u) ++ $doc(F.movedAt.$gt(nowInstant.minusMinutes(5)))
Expand Down
2 changes: 1 addition & 1 deletion modules/report/src/main/ReportApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ final class ReportApi(
.toMap
}

def ofModId[U: UserIdOf](modId: U): Fu[Option[Report]] = coll.one[Report]($doc("inquiry.mod" -> modId))
def ofModId[U: UserIdOf](mod: U): Fu[Option[Report]] = coll.one[Report]($doc("inquiry.mod" -> mod.id))

def ofSuspectId(suspectId: UserId): Fu[Option[Report.Inquiry]] =
coll.primitiveOne[Report.Inquiry]($doc("inquiry.mod".$exists(true), "user" -> suspectId), "inquiry")
Expand Down
4 changes: 2 additions & 2 deletions modules/simul/src/main/SimulRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ final private[simul] class SimulRepo(val coll: Coll, gameRepo: GameRepo)(using E
def findPending(hostId: UserId): Fu[List[Simul]] =
coll.list[Simul](createdSelect ++ $doc("hostId" -> hostId))

def byTeamLeaders[U: UserIdOf](teamId: TeamId, hostIds: Seq[U]): Fu[List[Simul]] =
def byTeamLeaders[U: UserIdOf](teamId: TeamId, hosts: Seq[U]): Fu[List[Simul]] =
coll
.find(createdSelect ++ $doc("hostId".$in(hostIds), "team" -> teamId))
.find(createdSelect ++ $doc("hostId".$in(hosts.map(_.id)), "team" -> teamId))
.hint(coll.hint($doc("hostId" -> 1)))
.cursor[Simul]()
.listAll()
Expand Down
2 changes: 1 addition & 1 deletion modules/team/src/main/TeamMemberRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ final class TeamMemberRepo(val coll: Coll)(using Executor):
private def selectId[U: UserIdOf](teamId: TeamId, user: U) = $id(TeamMember.makeId(teamId, user.id))
private def selectIds[U: UserIdOf](teamIds: Seq[TeamId], user: U) = $inIds:
teamIds.map(TeamMember.makeId(_, user.id))
private def selectUser[U: UserIdOf](user: U) = $doc("user" -> user)
private def selectUser[U: UserIdOf](user: U) = $doc("user" -> user.id)
private def selectAnyPerm = $doc("perms".$exists(true))
private def selectPerm(perm: Permission.Selector) = $doc("perms" -> perm(Permission))
4 changes: 2 additions & 2 deletions modules/user/src/main/UserApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ final class UserApi(userRepo: UserRepo, perfsRepo: UserPerfsRepo, cacheApi: Cach

def withPerfs(u: User): Fu[UserWithPerfs] = perfsRepo.withPerfs(u)

def withPerfs[U: UserIdOf](id: U): Fu[Option[UserWithPerfs]] =
def withPerfs[U: UserIdOf](u: U): Fu[Option[UserWithPerfs]] =
userRepo.coll
.aggregateOne(): framework =>
import framework.*
Match($id(id)) -> List:
Match($id(u.id)) -> List:
PipelineOperator(perfsRepo.aggregate.lookup)
.map: docO =>
for
Expand Down
8 changes: 4 additions & 4 deletions modules/user/src/main/UserRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class UserRepo(c: Coll)(using Executor) extends lila.core.user.UserRepo(c)
def byId[U: UserIdOf](u: U): Fu[Option[User]] =
u.id.noGhost.so:
coll
.byId[User](u)
.byId[User](u.id)
.recover:
case _: reactivemongo.api.bson.exceptions.BSONValueNotFoundException =>
none // probably GDPRed user
Expand All @@ -41,7 +41,7 @@ final class UserRepo(c: Coll)(using Executor) extends lila.core.user.UserRepo(c)
coll.byIds[User, UserId](ids, _.sec)

def enabledById[U: UserIdOf](u: U): Fu[Option[User]] =
u.id.noGhost.so(coll.one[User](enabledSelect ++ $id(u)))
u.id.noGhost.so(coll.one[User](enabledSelect ++ $id(u.id)))

def enabledByIds[U: UserIdOf](us: Iterable[U]): Fu[List[User]] =
val ids = us.map(_.id).filter(_.noGhost)
Expand Down Expand Up @@ -277,7 +277,7 @@ final class UserRepo(c: Coll)(using Executor) extends lila.core.user.UserRepo(c)

def setKid(user: User, v: Boolean) = coll.updateField($id(user.id), F.kid, v).void

def isKid[U: UserIdOf](id: U) = coll.exists($id(id) ++ $doc(F.kid -> true))
def isKid[U: UserIdOf](u: U) = coll.exists($id(u.id) ++ $doc(F.kid -> true))

def updateTroll(user: User) = setTroll(user.id, user.marks.troll)

Expand All @@ -301,7 +301,7 @@ final class UserRepo(c: Coll)(using Executor) extends lila.core.user.UserRepo(c)
coll.updateField($id(id), F.roles, roles).void

def getRoles[U: UserIdOf](u: U): Fu[List[RoleDbKey]] =
coll.primitiveOne[List[RoleDbKey]]($id(u), BSONFields.roles).dmap(_.orZero)
coll.primitiveOne[List[RoleDbKey]]($id(u.id), BSONFields.roles).dmap(_.orZero)

def addPermission(id: UserId, perm: lila.core.perm.Permission): Funit =
coll.update.one($id(id), $push(F.roles -> perm.dbKey)).void
Expand Down

0 comments on commit cfabe95

Please sign in to comment.