lila/app/controllers/Game.scala

66 lines
2.1 KiB
Scala
Raw Normal View History

2013-05-06 19:57:42 -06:00
package controllers
2018-03-07 16:55:02 -07:00
import scala.concurrent.duration._
2013-05-06 19:57:42 -06:00
import lila.app._
2017-01-25 09:45:00 -07:00
import lila.game.{ GameRepo, Game => GameModel }
2013-05-06 19:57:42 -06:00
import views._
2015-04-23 01:01:32 -06:00
object Game extends LilaController {
2013-05-06 19:57:42 -06:00
2017-01-25 09:45:00 -07:00
def delete(gameId: String) = Auth { implicit ctx => me =>
OptionFuResult(GameRepo game gameId) { game =>
if (game.pgnImport.flatMap(_.user) ?? (me.id==)) {
Env.hub.actor.bookmark ! lila.hub.actorApi.bookmark.Remove(game.id)
(GameRepo remove game.id) >>
(lila.analyse.AnalysisRepo remove game.id) >>
Env.game.cached.clearNbImportedByCache(me.id) inject
Redirect(routes.User.show(me.username))
} else fuccess {
2017-01-25 09:45:00 -07:00
Redirect(routes.Round.watcher(game.id, game.firstColor.name))
}
}
}
2018-03-07 16:50:59 -07:00
def exportForm = Auth { implicit ctx => me =>
2017-01-25 09:45:00 -07:00
Env.security.forms.emptyWithCaptcha map {
2018-03-07 16:50:59 -07:00
case (form, captcha) => Ok(html.game.export(form, captcha))
2017-01-25 09:45:00 -07:00
}
}
2018-03-07 16:50:59 -07:00
def exportConfirm = AuthBody { implicit ctx => me =>
2017-01-25 09:45:00 -07:00
implicit val req = ctx.body
2018-03-07 16:50:59 -07:00
Env.security.forms.empty.bindFromRequest.fold(
err => Env.security.forms.anyCaptcha map { captcha =>
BadRequest(html.game.export(err, captcha))
},
_ => fuccess(streamGamesPgn(me))
)
}
def exportApi = Auth { implicit ctx => me =>
fuccess(streamGamesPgn(me))
}
2018-03-07 16:55:02 -07:00
private val ExportRateLimitPerUser = new lila.memo.RateLimit[lila.user.User.ID](
credits = 10,
duration = 1 day,
name = "game export per user",
key = "game_export.user"
)
private def streamGamesPgn(user: lila.user.User) =
ExportRateLimitPerUser(user.id, cost = 1) {
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
val date = (DateTimeFormat forPattern "yyyy-MM-dd") print new DateTime
Ok.chunked(Env.api.pgnDump exportUserGames user.id).withHeaders(
CONTENT_TYPE -> pgnContentType,
CONTENT_DISPOSITION -> ("attachment; filename=" + s"lichess_${user.username}_$date.pgn")
)
}
2017-01-25 09:45:00 -07:00
private[controllers] def preloadUsers(game: GameModel): Funit =
Env.user.lightUserApi preloadMany game.userIds
2013-05-06 19:57:42 -06:00
}