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()(
|
|
|
|
open = ctx => handleExport(username, ctx.me, ctx.req),
|
|
|
|
scoped = req => me => handleExport(username, me.some, req)
|
|
|
|
)
|
2018-03-07 16:50:59 -07:00
|
|
|
|
2018-04-03 16:50:22 -06:00
|
|
|
private def handleExport(username: String, me: Option[lila.user.User], req: RequestHeader) =
|
|
|
|
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) {
|
|
|
|
val since = getLong("since", req) map { ts => new DateTime(ts) }
|
|
|
|
val until = getLong("until", req) map { ts => new DateTime(ts) }
|
|
|
|
val moves = getBoolOpt("moves", req) | true
|
|
|
|
val tags = getBoolOpt("tags", req) | true
|
|
|
|
val clocks = getBoolOpt("clocks", req) | false
|
|
|
|
val max = getInt("max", req) map (_ atLeast 1)
|
|
|
|
val perSecond = MaxPerSecond(me match {
|
|
|
|
case None => 10
|
|
|
|
case Some(m) if m is user.id => 50
|
|
|
|
case Some(_) => 20
|
|
|
|
})
|
|
|
|
val formatFlags = lila.game.PgnDump.WithFlags(moves = moves, tags = tags, clocks = clocks)
|
|
|
|
val config = PgnDump.Config(user, since, until, max, formatFlags, perSecond)
|
|
|
|
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
|
|
|
}
|