type safety: use Status rather than Int - for #9850

also filters out exotic status numbers
pull/9850/head
Thibault Duplessis 2021-09-20 18:58:00 +02:00
parent ab0c01e2a1
commit 31ba5d3a08
4 changed files with 11 additions and 6 deletions

View File

@ -272,12 +272,12 @@ final class Api(
def tournamentsByOwner(name: String, status: List[Int]) =
Action.async { implicit req =>
implicit val lang = reqLang
(name != "lichess") ?? env.user.repo.named(name) flatMap {
(name != lila.user.User.lichessId) ?? env.user.repo.named(name) flatMap {
_ ?? { user =>
val nb = getInt("nb", req) | Int.MaxValue
jsonStream {
env.tournament.api
.byOwnerStream(user, status, MaxPerSecond(20), nb)
.byOwnerStream(user, status flatMap lila.tournament.Status.apply, MaxPerSecond(20), nb)
.mapAsync(1)(env.tournament.apiJsonView.fullJson)
}.fuccess
}

View File

@ -10,7 +10,7 @@ sealed abstract private[tournament] class Status(val id: Int) extends Ordered[St
def is(f: Status.type => Status): Boolean = is(f(Status))
}
private[tournament] object Status {
object Status {
case object Created extends Status(10)
case object Started extends Status(20)

View File

@ -668,7 +668,12 @@ final class TournamentApi(
}
}
def byOwnerStream(owner: User, status: List[Int], perSecond: MaxPerSecond, nb: Int): Source[Tournament, _] =
def byOwnerStream(
owner: User,
status: List[Status],
perSecond: MaxPerSecond,
nb: Int
): Source[Tournament, _] =
tournamentRepo
.sortedCursor(owner, status, perSecond.value)
.documentSource(nb)

View File

@ -389,12 +389,12 @@ final class TournamentRepo(val coll: Coll, playerCollName: CollName)(implicit
private[tournament] def sortedCursor(
owner: lila.user.User,
status: List[Int],
status: List[Status],
batchSize: Int,
readPreference: ReadPreference = ReadPreference.secondaryPreferred
): AkkaStreamCursor[Tournament] =
coll
.find($doc("createdBy" -> owner.id ) ++ ( !status.isEmpty ?? $doc("status" $in status) ))
.find($doc("createdBy" -> owner.id) ++ (status.nonEmpty ?? $doc("status" $in status)))
.sort($sort desc "startsAt")
.batchSize(batchSize)
.cursor[Tournament](readPreference)