Skip to content

Commit

Permalink
Merge branch 'master' into scalachess-15
Browse files Browse the repository at this point in the history
* master:
  fix manually setting date of swiss first round
  remove unused showPeriod (#12670)
  fix relay sync.period
  scalafmt
  finish switching to `Relay.Sync.period`
  Mod usertable: show absolute time of the mark
  rename and type Relay.Sync.period - DB migration required
  increase puzzle selector logging
  Revert "track supported encodings for web push subs"
  delete unused function
  escape unsafe db regex
  • Loading branch information
ornicar committed Apr 24, 2023
2 parents 24a8ee1 + 03bac54 commit 498860c
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 67 deletions.
2 changes: 1 addition & 1 deletion app/templating/DateHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import chess.format.pgn.Tag.Date

trait DateHelper { self: I18nHelper with StringHelper with NumberHelper =>

export PeriodLocales.{ showPeriod, showDuration }
export PeriodLocales.showDuration

private val dateTimeStyle = "MS"
private val dateStyle = "M-"
Expand Down
8 changes: 5 additions & 3 deletions app/views/relay/roundForm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ object roundForm:
)(form3.flatpickr(_, minDate = None)),
isGranted(_.Relay) option
form3.group(
form("throttle"),
raw("Throttle in seconds"),
help = raw("Optional, to manually throttle requests. Min 2s, max 60s.").some,
form("period"),
raw("Period in seconds"),
help = raw(
"Optional, how long to wait between requests. Min 2s, max 60s. Defaults to automatic based on the number of viewers."
).some,
half = true
)(form3.input(_, typ = "number"))
),
Expand Down
4 changes: 2 additions & 2 deletions app/views/user/mod.scala
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,8 @@ object mod:
private val clean: Frag = iconTag("")
private val reportban = iconTag("")
private val notesText = iconTag("")
private def markTd(nb: Int, content: => Frag, date: Option[Instant] = None) =
if (nb > 0) td(cls := "i", dataSort := nb, title := date.map(momentFromNowServerText(_, false)))(content)
private def markTd(nb: Int, content: => Frag, date: Option[Instant] = None)(using ctx: Context) =
if (nb > 0) td(cls := "i", dataSort := nb, title := date.map(d => showInstantUTC(d)))(content)
else td

def otherUsers(mod: Holder, u: User, data: UserLogins.TableData[UserWithModlog], appeals: List[Appeal])(
Expand Down
1 change: 1 addition & 0 deletions bin/mongodb/relay-round-period-migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
db.relay.updateMany({ 'sync.delay': { $exists: 1 } }, { $rename: { 'sync.delay': 'sync.period' } });
3 changes: 3 additions & 0 deletions modules/common/src/main/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ case class Template(value: String) extends AnyVal
opaque type Days = Int
object Days extends OpaqueInt[Days]

opaque type Seconds = Int
object Seconds extends OpaqueInt[Seconds]

case class Preload[A](value: Option[A]) extends AnyVal:
def orLoad(f: => Fu[A]): Fu[A] = value.fold(f)(fuccess)
object Preload:
Expand Down
3 changes: 0 additions & 3 deletions modules/db/src/main/dsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,6 @@ trait dsl:

def $startsWith(value: String, options: String = ""): SimpleExpression[BSONRegex] =
$regex(s"^$value", options)

def $endsWith(value: String, options: String = ""): SimpleExpression[BSONRegex] =
$regex(s"$value$$", options)
}

trait ArrayOperators { self: ElementBuilder =>
Expand Down
5 changes: 1 addition & 4 deletions modules/i18n/src/main/PeriodLocales.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package lila.i18n
import org.joda.time.format.{ PeriodFormat, PeriodFormatter }
import org.joda.time.{ DurationFieldType, PeriodType, Period as JodaPeriod }
import play.api.i18n.Lang
import java.time.{ Duration, Period }
import java.time.Duration

// TODO replace with threeten-extra?
// https://www.threeten.org/threeten-extra/apidocs/org.threeten.extra/org/threeten/extra/AmountFormats.html
Expand All @@ -18,8 +18,5 @@ object PeriodLocales:
private def periodFormatter(using lang: Lang): PeriodFormatter =
PeriodFormat wordBased lang.locale

def showPeriod(period: Period)(using Lang): String =
periodFormatter print JodaPeriod(period).normalizedStandard(periodType)

def showDuration(duration: Duration)(using Lang): String =
periodFormatter print JodaPeriod(duration.toMillis).normalizedStandard(periodType)
2 changes: 1 addition & 1 deletion modules/msg/src/main/MsgSearch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class MsgSearch(
$doc(
"users" -> $doc(
$eq(me.id),
"$regex" -> BSONRegex(s"^$q", "")
"$regex" -> BSONRegex(s"^${java.util.regex.Pattern.quote(q)}", "")
),
"del" $ne me.id
)
Expand Down
4 changes: 0 additions & 4 deletions modules/push/src/main/WebPush.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ final private class WebPush(
"keys" -> Json.obj(
"p256dh" -> sub.p256dh,
"auth" -> sub.auth
),
"encodings" -> Json.obj(
"aes128gcm" -> sub.aes128gcm,
"aesgcm" -> sub.aesgcm
)
)
}.toList),
Expand Down
8 changes: 2 additions & 6 deletions modules/push/src/main/WebSubscription.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package lila.push
case class WebSubscription(
endpoint: String,
auth: String,
p256dh: String,
aes128gcm: Option[Boolean],
aesgcm: Option[Boolean]
p256dh: String
)

object WebSubscription:
Expand All @@ -17,9 +15,7 @@ object WebSubscription:
given webSubscriptionReads: Reads[WebSubscription] = (
(__ \ "endpoint").read[String] and
(__ \ "keys" \ "auth").read[String] and
(__ \ "keys" \ "p256dh").read[String] and
(__ \ "encodings" \ "aes128gcm").readNullable[Boolean] and
(__ \ "encodings" \ "aesgcm").readNullable[Boolean]
(__ \ "keys" \ "p256dh").read[String]
)(WebSubscription.apply)

given webSubscriptionReader: BSONDocumentReader[WebSubscription] = Macros.reader[WebSubscription]
14 changes: 6 additions & 8 deletions modules/push/src/main/WebSubscriptionApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ final class WebSubscriptionApi(coll: Coll)(using Executor):
.one(
$id(sessionId),
$doc(
"userId" -> user.id,
"endpoint" -> subscription.endpoint,
"auth" -> subscription.auth,
"p256dh" -> subscription.p256dh,
"aes128gcm" -> subscription.aes128gcm,
"aesgcm" -> subscription.aesgcm,
"seenAt" -> nowInstant
"userId" -> user.id,
"endpoint" -> subscription.endpoint,
"auth" -> subscription.auth,
"p256dh" -> subscription.p256dh,
"seenAt" -> nowInstant
),
upsert = true
)
Expand All @@ -35,7 +33,7 @@ final class WebSubscriptionApi(coll: Coll)(using Executor):

private[push] def getSubscriptions(max: Int)(userId: UserId): Fu[List[WebSubscription]] =
coll
.find($doc("userId" -> userId))
.find($doc("userId" -> userId), $doc("endpoint" -> true, "auth" -> true, "p256dh" -> true).some)
.sort($doc("seenAt" -> -1))
.cursor[WebSubscription](ReadPreference.secondaryPreferred)
.list(max)
Expand Down
3 changes: 2 additions & 1 deletion modules/puzzle/src/main/PuzzleSelector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ final class PuzzleSelector(
nextPuzzleResult(user, session)
.flatMap {
case PathMissing | PathEnded if retries < 10 => switchPath(retries)(session.path.tier)
case PathMissing | PathEnded => fufail(s"Puzzle path missing or ended for ${user.id}")
case PathMissing => fufail(s"Puzzle path missing for ${user.id} $session")
case PathEnded => fufail(s"Puzzle path ended for ${user.id} $session")
case PuzzleMissing(id) =>
logger.warn(s"Puzzle missing: $id")
sessionApi.set(user, session.next)
Expand Down
2 changes: 1 addition & 1 deletion modules/puzzle/src/main/PuzzleSession.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private case class PuzzleSession(
settings.difficulty == other.settings.difficulty &&
settings.color == other.settings.color

override def toString = s"$path:$positionInPath"
override def toString = s"$path:$positionInPath($settings)"

case class PuzzleSettings(
difficulty: PuzzleDifficulty,
Expand Down
19 changes: 8 additions & 11 deletions modules/relay/src/main/RelayFetch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import play.api.libs.ws.StandaloneWSClient
import RelayRound.Sync.{ UpstreamIds, UpstreamUrl }

import lila.base.LilaInvalid
import lila.common.LilaScheduler
import lila.common.{ Seconds, LilaScheduler }
import lila.game.{ GameRepo, PgnDump }
import lila.memo.CacheApi
import lila.round.GameProxyRepo
Expand Down Expand Up @@ -101,7 +101,7 @@ final private class RelayFetch(

private def continueRelay(rt: RelayRound.WithTour): RelayRound =
rt.round.sync.upstream.fold(rt.round) { upstream =>
val seconds =
val seconds: Seconds =
if (rt.round.sync.log.alwaysFails)
rt.round.sync.log.events.lastOption
.filterNot(_.isTimeout)
Expand All @@ -110,17 +110,14 @@ final private class RelayFetch(
.filterNot(_ contains "Cannot parse moves")
.filterNot(_ contains "Found an empty PGN")
.foreach { irc.broadcastError(rt.round.id.value, rt.fullName, _) }
60
else
rt.round.sync.delay getOrElse {
if (upstream.local) 3 else 6
}
Seconds(60)
else rt.round.sync.period | Seconds(if upstream.local then 3 else 6)
rt.round.withSync {
_.copy(
nextAt = nowInstant plusSeconds {
seconds atLeast {
if (rt.round.sync.log.justTimedOut) 10 else 2
}
seconds.atLeast {
if rt.round.sync.log.justTimedOut then 10 else 2
}.value
} some
)
}
Expand All @@ -147,7 +144,7 @@ final private class RelayFetch(
gameProxy.upgradeIfPresent flatMap
gameRepo.withInitialFens flatMap { games =>
if (games.size == ids.size)
games.map { case (game, fen) =>
games.map { (game, fen) =>
pgnDump(game, fen, gameIdsUpstreamPgnFlags).dmap(_.render)
}.parallel dmap MultiPgn.apply
else
Expand Down
3 changes: 2 additions & 1 deletion modules/relay/src/main/RelayRound.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lila.relay
import ornicar.scalalib.ThreadLocalRandom

import lila.study.Study
import lila.common.Seconds

case class RelayRound(
_id: RelayRoundId,
Expand Down Expand Up @@ -72,7 +73,7 @@ object RelayRound:
upstream: Option[Sync.Upstream], // if empty, needs a client to push PGN
until: Option[Instant], // sync until then; resets on move
nextAt: Option[Instant], // when to run next sync
delay: Option[Int], // override time between two sync (rare)
period: Option[Seconds], // override time between two sync (rare)
log: SyncLog
):

Expand Down
9 changes: 5 additions & 4 deletions modules/relay/src/main/RelayRoundForm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import lila.game.Game
import lila.security.Granter
import lila.study.Study
import lila.user.User
import lila.common.Seconds

final class RelayRoundForm:

Expand All @@ -26,7 +27,7 @@ final class RelayRoundForm:
},
"syncUrlRound" -> optional(number(min = 1, max = 999)),
"startsAt" -> optional(ISOInstantOrTimestamp.mapping),
"throttle" -> optional(number(min = 2, max = 60))
"period" -> optional(number(min = 2, max = 60).into[Seconds])
)(Data.apply)(unapply)
.verifying("This source requires a round number. See the new form field below.", !_.roundMissing)

Expand Down Expand Up @@ -95,7 +96,7 @@ object RelayRoundForm:
syncUrl: Option[String] = None,
syncUrlRound: Option[Int] = None,
startsAt: Option[Instant] = None,
throttle: Option[Int] = None
period: Option[Seconds] = None
):

def requiresRound = syncUrl exists RelayRound.Sync.UpstreamUrl.LccRegex.matches
Expand Down Expand Up @@ -124,7 +125,7 @@ object RelayRoundForm:
},
until = none,
nextAt = none,
delay = throttle ifTrue Granter(_.Relay)(user),
period = period ifTrue Granter(_.Relay)(user),
log = SyncLog.empty
)

Expand Down Expand Up @@ -153,5 +154,5 @@ object RelayRoundForm:
},
syncUrlRound = relay.sync.upstream.flatMap(_.asUrl).flatMap(_.withRound.round),
startsAt = relay.startsAt,
throttle = relay.sync.delay
period = relay.sync.period
)
2 changes: 1 addition & 1 deletion modules/storm/src/main/StormSelector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class StormSelector(colls: PuzzleColls, cacheApi: CacheApi)(using Executor
)
)
Facet(
ratingBuckets.map { case (rating, nbPuzzles) =>
ratingBuckets.map { (rating, nbPuzzles) =>
rating.toString -> List(
Match(
$doc(
Expand Down
15 changes: 6 additions & 9 deletions modules/swiss/src/main/SwissApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import akka.stream.scaladsl.*
import alleycats.Zero
import java.nio.charset.StandardCharsets.UTF_8
import java.security.MessageDigest
import java.time.format.{ DateTimeFormatter, FormatStyle }
import reactivemongo.akkastream.cursorProducer
import reactivemongo.api.*
import reactivemongo.api.bson.*
Expand Down Expand Up @@ -145,24 +146,20 @@ final class SwissApi(

def scheduleNextRound(swiss: Swiss, date: Instant): Funit =
Sequencing(swiss.id)(cache.swissCache.notFinishedById) { old =>
(!old.settings.manualRounds).?? {
mongo.swiss
for
_ <- !old.settings.manualRounds ?? mongo.swiss
.updateField($id(old.id), "settings.i", Swiss.RoundInterval.manual)
.void
} >> {
if (old.isCreated) mongo.swiss.updateField($id(old.id), "startsAt", date).void
else if (old.isStarted && old.nbOngoing == 0)
_ <- old.isCreated ?? mongo.swiss.updateField($id(old.id), "startsAt", date).void
_ <- (!old.isFinished && old.nbOngoing == 0) ??
mongo.swiss.updateField($id(old.id), "nextRoundAt", date).void >>- {
import java.time.format.{ DateTimeFormatter, FormatStyle }
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT)
val showDate = formatter print date
systemChat(swiss.id, s"Round ${swiss.round.value + 1} scheduled at $showDate UTC")
}
else funit
} >>- {
yield
cache.swissCache clear swiss.id
socket.reload(swiss.id)
}
}

def verdicts(swiss: Swiss, me: Option[User]): Fu[SwissCondition.All.WithVerdicts] =
Expand Down
8 changes: 1 addition & 7 deletions ui/site/src/component/serviceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ export default async function () {
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
...newSub.toJSON(),
encodings: {
aes128gcm: PushManager.supportedContentEncodings.includes('aes128gcm'),
aesgcm: PushManager.supportedContentEncodings.includes('aesgcm'),
},
}),
body: JSON.stringify(newSub),
});
if (res.ok && !res.redirected) store.set('' + Date.now());
else newSub.unsubscribe();
Expand Down

0 comments on commit 498860c

Please sign in to comment.