when many challenges, show correspondence challenges last

pull/10033/head
Thibault Duplessis 2021-10-26 11:08:04 +02:00
parent 4dec6a6712
commit f17d2c6235
2 changed files with 22 additions and 14 deletions

View File

@ -11,6 +11,7 @@ import lila.game.{ Game, Pov }
import lila.hub.actorApi.socket.SendTo
import lila.i18n.I18nLangPicker
import lila.memo.CacheApi._
import lila.memo.ExpireSetMemo
import lila.user.{ User, UserRepo }
final class ChallengeApi(
@ -56,7 +57,10 @@ final class ChallengeApi(
def createdByChallengerId = repo.createdByChallengerId() _
def createdByDestId = repo.createdByDestId() _
def createdByDestId(userId: User.ID) = countInFor get userId flatMap { nb =>
if (nb > 5) repo.createdByPopularDestId()(userId)
else repo.createdByDestId()(userId)
}
def cancel(c: Challenge) =
repo.cancel(c) >>- {
@ -158,7 +162,7 @@ final class ChallengeApi(
private object notifyUser {
private val throttler = new lila.hub.EarlyMultiThrottler(logger)
def apply(userId: User.ID): Unit = throttler(userId, 2.seconds) {
def apply(userId: User.ID): Unit = throttler(userId, 3.seconds) {
for {
all <- allFor(userId)
lang <- userRepo langOf userId map I18nLangPicker.byStrOrDefault

View File

@ -5,6 +5,7 @@ import scala.annotation.nowarn
import lila.common.config.Max
import lila.db.dsl._
import lila.user.User
final private class ChallengeRepo(colls: ChallengeColls, maxPerUser: Max)(implicit
ec: scala.concurrent.ExecutionContext
@ -34,19 +35,22 @@ final private class ChallengeRepo(colls: ChallengeColls, maxPerUser: Max)(implic
def update(c: Challenge): Funit = coll.update.one($id(c.id), c).void
def createdByChallengerId(max: Int = 50)(userId: String): Fu[List[Challenge]] =
coll
.find(selectCreated ++ $doc("challenger.id" -> userId))
.sort($sort asc "createdAt")
.cursor[Challenge]()
.list(max)
private def createdList(selector: Bdoc, max: Int): Fu[List[Challenge]] =
coll.find(selectCreated ++ selector).sort($sort asc "createdAt").cursor[Challenge]().list(max)
def createdByDestId(max: Int = 50)(userId: String): Fu[List[Challenge]] =
coll
.find(selectCreated ++ $doc("destUser.id" -> userId))
.sort($doc($sort asc "createdAt"))
.cursor[Challenge]()
.list(max)
def createdByChallengerId(max: Int = 50)(userId: User.ID): Fu[List[Challenge]] =
createdList($doc("challenger.id" -> userId), max)
def createdByDestId(max: Int = 50)(userId: User.ID): Fu[List[Challenge]] =
createdList($doc("destUser.id" -> userId), max)
def createdByPopularDestId(max: Int = 50)(userId: User.ID): Fu[List[Challenge]] = for {
realTime <- createdList($doc("destUser.id" -> userId, "timeControl.l" $exists true), max)
corres <- (realTime.sizeIs < max) ?? createdList(
$doc($doc("destUser.id" -> userId), "timeControl.l" $exists false),
max - realTime.size
)
} yield realTime ::: corres
def setChallenger(c: Challenge, color: Option[chess.Color]) =
coll.update