2013-05-06 19:57:42 -06:00
|
|
|
package controllers
|
|
|
|
|
2018-03-07 17:20:00 -07:00
|
|
|
import org.joda.time.DateTime
|
|
|
|
import org.joda.time.format.DateTimeFormat
|
2018-03-31 19:48:52 -06:00
|
|
|
import scala.concurrent.duration._
|
2018-03-07 16:55:02 -07:00
|
|
|
|
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))
|
2017-02-14 08:34:07 -07:00
|
|
|
} else fuccess {
|
2017-01-25 09:45:00 -07:00
|
|
|
Redirect(routes.Round.watcher(game.id, game.firstColor.name))
|
|
|
|
}
|
|
|
|
}
|
2015-04-22 08:27:10 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2015-01-15 10:38:57 -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))
|
|
|
|
},
|
2018-03-07 17:20:00 -07:00
|
|
|
_ => fuccess(streamGamesPgn(me, since = none))
|
2018-03-07 16:50:59 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-03-31 19:48:52 -06:00
|
|
|
def exportApi = Scoped(_.Game.Read) { req => me =>
|
|
|
|
val since = getLong("since", req) map { ts => new DateTime(ts) }
|
2018-03-07 17:20:00 -07:00
|
|
|
fuccess(streamGamesPgn(me, since))
|
2018-03-07 16:50:59 -07:00
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2018-03-07 17:20:00 -07:00
|
|
|
private def streamGamesPgn(user: lila.user.User, since: Option[DateTime]) =
|
2018-03-07 16:55:02 -07:00
|
|
|
ExportRateLimitPerUser(user.id, cost = 1) {
|
|
|
|
val date = (DateTimeFormat forPattern "yyyy-MM-dd") print new DateTime
|
2018-03-07 17:20:00 -07:00
|
|
|
Ok.chunked(Env.api.pgnDump.exportUserGames(user.id, since)).withHeaders(
|
2018-03-07 16:55:02 -07:00
|
|
|
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
|
|
|
}
|