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-04-02 22:27:40 -06:00
|
|
|
import play.api.mvc.RequestHeader
|
2018-04-03 16:50:22 -06:00
|
|
|
import scala.concurrent.duration._
|
2018-03-07 16:55:02 -07:00
|
|
|
|
2018-04-03 16:50:22 -06:00
|
|
|
import lila.api.PgnDump
|
2013-05-06 19:57:42 -06:00
|
|
|
import lila.app._
|
2018-04-09 18:06:35 -06:00
|
|
|
import lila.common.{ MaxPerSecond, HTTPRequest }
|
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-04-03 16:50:22 -06:00
|
|
|
def export(username: String) = OpenOrScoped()(
|
2018-04-26 17:58:29 -06:00
|
|
|
open = ctx => handleExport(username, ctx.me, ctx.req, oauth = false),
|
|
|
|
scoped = req => me => handleExport(username, me.some, req, oauth = true)
|
2018-04-03 16:50:22 -06:00
|
|
|
)
|
2018-03-07 16:50:59 -07:00
|
|
|
|
2018-04-26 17:58:29 -06:00
|
|
|
private def handleExport(username: String, me: Option[lila.user.User], req: RequestHeader, oauth: Boolean) =
|
2018-04-03 16:50:22 -06:00
|
|
|
lila.user.UserRepo named username flatMap {
|
|
|
|
_ ?? { user =>
|
|
|
|
RequireHttp11(req) {
|
2018-04-09 18:06:35 -06:00
|
|
|
Api.GlobalLinearLimitPerIP(HTTPRequest lastRemoteAddress req) {
|
|
|
|
Api.GlobalLinearLimitPerUserOption(me) {
|
2018-04-21 11:45:46 -06:00
|
|
|
val config = PgnDump.Config(
|
|
|
|
user = user,
|
|
|
|
since = getLong("since", req) map { ts => new DateTime(ts) },
|
|
|
|
until = getLong("until", req) map { ts => new DateTime(ts) },
|
|
|
|
max = getInt("max", req) map (_ atLeast 1),
|
|
|
|
rated = getBoolOpt("rated", req),
|
2018-04-23 06:35:52 -06:00
|
|
|
perfType = ~get("perfType", req) split "," flatMap { lila.rating.PerfType(_) } toSet,
|
2018-04-23 06:54:12 -06:00
|
|
|
color = get("color", req) flatMap chess.Color.apply,
|
2018-04-21 11:45:46 -06:00
|
|
|
flags = lila.game.PgnDump.WithFlags(
|
|
|
|
moves = getBoolOpt("moves", req) | true,
|
|
|
|
tags = getBoolOpt("tags", req) | true,
|
2018-04-26 20:00:31 -06:00
|
|
|
clocks = getBoolOpt("clocks", req) | false,
|
|
|
|
evals = getBoolOpt("evals", req) | false
|
2018-04-21 11:45:46 -06:00
|
|
|
),
|
|
|
|
perSecond = MaxPerSecond(me match {
|
|
|
|
case None => 10
|
|
|
|
case Some(m) if m is user.id => 50
|
2018-04-26 17:58:29 -06:00
|
|
|
case Some(_) if oauth => 20 // bonus for oauth logged in only (not to XSRF)
|
2018-04-21 11:45:46 -06:00
|
|
|
})
|
|
|
|
)
|
2018-04-09 18:06:35 -06:00
|
|
|
val date = (DateTimeFormat forPattern "yyyy-MM-dd") print new DateTime
|
|
|
|
Ok.chunked(Env.api.pgnDump.exportUserGames(config)).withHeaders(
|
|
|
|
CONTENT_TYPE -> pgnContentType,
|
|
|
|
CONTENT_DISPOSITION -> ("attachment; filename=" + s"lichess_${user.username}_$date.pgn")
|
|
|
|
).fuccess
|
|
|
|
}
|
2018-04-03 16:50:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 16:50:59 -07:00
|
|
|
|
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
|
|
|
}
|