rewrite exports of ongoing games

pull/6413/head
Thibault Duplessis 2020-04-15 12:11:35 -06:00
parent eed164844c
commit a70eb1da11
4 changed files with 39 additions and 26 deletions

View File

@ -30,27 +30,30 @@ final class Game(
}
}
def exportOne(id: String) = Open { implicit ctx =>
OptionFuResult(env.round.proxyRepo.gameIfPresent(id) orElse env.game.gameRepo.game(id)) { game =>
val config = GameApiV2.OneConfig(
format = if (HTTPRequest acceptsJson ctx.req) GameApiV2.Format.JSON else GameApiV2.Format.PGN,
imported = getBool("imported"),
flags = requestPgnFlags(ctx.req, extended = true)
)
lila.mon.export.pgn.game.increment()
env.api.gameApiV2.exportOne(game, config) flatMap { content =>
env.api.gameApiV2.filename(game, config.format) map { filename =>
Ok(content)
.withHeaders(
CONTENT_DISPOSITION -> s"attachment; filename=$filename"
)
.withHeaders(
lila.app.http.ResponseHeaders.headersForApiOrApp(ctx.req): _*
) as gameContentType(config)
def exportOne(id: String) = Action.async { exportGame(id, _) }
private[controllers] def exportGame(gameId: GameModel.ID, req: RequestHeader): Fu[Result] =
env.round.proxyRepo.gameIfPresent(gameId) orElse env.game.gameRepo.game(gameId) flatMap {
case None => NotFound.fuccess
case Some(game) =>
lila.mon.export.pgn.game.increment()
val config = GameApiV2.OneConfig(
format = if (HTTPRequest acceptsJson req) GameApiV2.Format.JSON else GameApiV2.Format.PGN,
imported = getBool("imported", req),
flags = requestPgnFlags(req, extended = true)
)
env.api.gameApiV2.exportOne(game, config) flatMap { content =>
env.api.gameApiV2.filename(game, config.format) map { filename =>
Ok(content)
.withHeaders(
CONTENT_DISPOSITION -> s"attachment; filename=$filename"
)
.withHeaders(
lila.app.http.ResponseHeaders.headersForApiOrApp(req): _*
) as gameContentType(config)
}
}
}
}
}
def exportByUser(username: String) = OpenOrScoped()(
open = ctx => handleExport(username, ctx.me, ctx.req, oauth = false),

View File

@ -24,6 +24,7 @@ import views._
final class User(
env: Env,
roundC: => Round,
gameC: => Game,
modC: => Mod
) extends LilaController(env) {
@ -40,10 +41,12 @@ final class User(
}
}
def tvPgn(username: String) = Action.async {
env.game.cached.lastPlayedPlayingId(UserModel normalize username) flatMap {
def tvExport(username: String) = Action.async { req =>
val userId = UserModel normalize username
env.game.cached.lastPlayedPlayingId(userId) orElse
env.game.gameRepo.quickLastPlayedId(userId) flatMap {
case None => NotFound("No ongoing game").fuccess
case Some(gameId) => roundC.delayedPgnCache get gameId
case Some(gameId) => gameC.exportGame(gameId, req)
}
}

View File

@ -51,7 +51,6 @@ POST /@/:username/note controllers.User.writeNote(username: Stri
POST /note/delete/:id controllers.User.deleteNote(id: String)
GET /@/:username/mini controllers.User.showMini(username: String)
GET /@/:username/tv controllers.User.tv(username: String)
GET /@/:username/tv.pgn controllers.User.tvPgn(username: String)
GET /@/:username/perf/:perfKey controllers.User.perfStat(username: String, perfKey: String)
GET /@/:username/all controllers.User.gamesAll(username: String, page: Int ?= 1)
GET /@/:username/:filterName controllers.User.games(username: String, filterName: String, page: Int ?= 1)
@ -525,6 +524,7 @@ GET /api/user/:name/following controllers.Relation.apiFollowing(name: S
GET /api/user/:name/followers controllers.Relation.apiFollowers(name: String)
POST /api/user/:name/note controllers.User.apiWriteNote(name: String)
GET /api/user/:name/rating-history controllers.User.ratingHistory(name: String)
GET /api/user/:name/current-game controllers.User.tvExport(name: String)
GET /api/game/:id controllers.Api.game(id: String)
GET /api/tournament controllers.Api.currentTournaments
GET /api/tournament/:id controllers.Api.tournament(id: String)

View File

@ -197,23 +197,30 @@ final class GameRepo(val coll: Coll)(implicit ec: scala.concurrent.ExecutionCont
.find(Query recentlyPlaying userId, $id(true).some)
.sort(Query.sortMovedAtNoIndex)
.one[Bdoc](readPreference = ReadPreference.primary)
.map { _.flatMap(_.getAsOpt[Game.ID](F.id)) }
.dmap { _.flatMap(_.getAsOpt[Game.ID](F.id)) }
def allPlaying(userId: User.ID): Fu[List[Pov]] =
coll.ext
.find(Query nowPlaying userId)
.list[Game]()
.map { _ flatMap { Pov.ofUserId(_, userId) } }
.dmap { _ flatMap { Pov.ofUserId(_, userId) } }
def lastPlayed(user: User): Fu[Option[Pov]] =
coll.ext
.find(Query user user.id)
.sort($sort desc F.createdAt)
.list[Game](3)
.list[Game](2)
.dmap {
_.sortBy(_.movedAt).lastOption flatMap { Pov(_, user) }
}
def quickLastPlayedId(userId: User.ID): Fu[Option[Game.ID]] =
coll.ext
.find(Query user userId, $id(true))
.sort($sort desc F.createdAt)
.one[Bdoc]
.dmap { _.flatMap(_.getAsOpt[Game.ID](F.id)) }
def lastFinishedRatedNotFromPosition(user: User): Fu[Option[Game]] =
coll.ext
.find(