stream games until timestamp

This commit is contained in:
Thibault Duplessis 2018-04-03 00:01:00 +02:00
parent 9a89765e98
commit 15bdc70f48
3 changed files with 14 additions and 6 deletions

View file

@ -36,13 +36,14 @@ object Game extends LilaController {
err => Env.security.forms.anyCaptcha map { captcha =>
BadRequest(html.game.export(err, captcha))
},
_ => fuccess(streamGamesPgn(me, since = none))
_ => fuccess(streamGamesPgn(me, since = none, until = none))
)
}
def exportApi = Scoped(_.Game.Read) { req => me =>
val since = getLong("since", req) map { ts => new DateTime(ts) }
fuccess(streamGamesPgn(me, since))
val until = getLong("until", req) map { ts => new DateTime(ts) }
fuccess(streamGamesPgn(me, since, until))
}
private val ExportRateLimitPerUser = new lila.memo.RateLimit[lila.user.User.ID](
@ -52,10 +53,10 @@ object Game extends LilaController {
key = "game_export.user"
)
private def streamGamesPgn(user: lila.user.User, since: Option[DateTime]) =
private def streamGamesPgn(user: lila.user.User, since: Option[DateTime], until: Option[DateTime]) =
ExportRateLimitPerUser(user.id, cost = 1) {
val date = (DateTimeFormat forPattern "yyyy-MM-dd") print new DateTime
Ok.chunked(Env.api.pgnDump.exportUserGames(user.id, since)).withHeaders(
Ok.chunked(Env.api.pgnDump.exportUserGames(user.id, since, until)).withHeaders(
CONTENT_TYPE -> pgnContentType,
CONTENT_DISPOSITION -> ("attachment; filename=" + s"lichess_${user.username}_$date.pgn")
)

View file

@ -29,11 +29,11 @@ final class PgnDump(
}
}
def exportUserGames(userId: String, since: Option[DateTime]): Enumerator[String] = {
def exportUserGames(userId: String, since: Option[DateTime], until: Option[DateTime]): Enumerator[String] = {
import reactivemongo.play.iteratees.cursorProducer
import lila.db.dsl._
GameRepo.sortedCursor(
Query.user(userId) ++ since.??(Query.createdSince),
Query.user(userId) ++ Query.createdBetween(since, until),
Query.sortCreated
).enumerator() &> toPgn
}

View file

@ -119,6 +119,13 @@ object Query {
def createdSince(d: DateTime): Bdoc =
F.createdAt $gt d
def createdBetween(since: Option[DateTime], until: Option[DateTime]): Bdoc = (since, until) match {
case (Some(since), None) => createdSince(since)
case (None, Some(until)) => F.createdAt $lt until
case (Some(since), Some(until)) => F.createdAt $gt since $lt until
case _ => $empty
}
val sortCreated: Bdoc = $sort desc F.createdAt
val sortChronological: Bdoc = $sort asc F.createdAt
val sortAntiChronological: Bdoc = $sort desc F.createdAt