2013-12-30 19:00:56 -07:00
|
|
|
package controllers
|
|
|
|
|
2015-01-17 04:35:54 -07:00
|
|
|
import play.api.libs.json._
|
2013-12-30 19:00:56 -07:00
|
|
|
import play.api.mvc._, Results._
|
2016-07-10 02:08:02 -06:00
|
|
|
import scala.concurrent.duration._
|
2013-12-30 19:00:56 -07:00
|
|
|
|
|
|
|
import lila.app._
|
|
|
|
|
|
|
|
object Api extends LilaController {
|
|
|
|
|
2014-01-07 18:43:20 -07:00
|
|
|
private val userApi = Env.api.userApi
|
|
|
|
private val gameApi = Env.api.gameApi
|
|
|
|
|
2015-01-17 04:35:54 -07:00
|
|
|
def status = Action { req =>
|
2015-01-24 03:36:05 -07:00
|
|
|
val api = lila.api.Mobile.Api
|
2015-01-17 04:35:54 -07:00
|
|
|
Ok(Json.obj(
|
2015-01-24 03:36:05 -07:00
|
|
|
"api" -> Json.obj(
|
2016-07-15 11:41:48 -06:00
|
|
|
"current" -> api.currentVersion.value,
|
2015-01-24 03:36:05 -07:00
|
|
|
"olds" -> api.oldVersions.map { old =>
|
|
|
|
Json.obj(
|
2016-07-15 11:41:48 -06:00
|
|
|
"version" -> old.version.value,
|
2015-01-24 03:36:05 -07:00
|
|
|
"deprecatedAt" -> old.deprecatedAt,
|
|
|
|
"unsupportedAt" -> old.unsupportedAt)
|
2016-04-08 23:30:49 -06:00
|
|
|
})
|
2015-01-17 04:35:54 -07:00
|
|
|
)) as JSON
|
|
|
|
}
|
|
|
|
|
2016-01-21 23:45:07 -07:00
|
|
|
def user(name: String) = ApiResult { implicit ctx =>
|
2016-02-25 18:25:28 -07:00
|
|
|
userApi one name
|
2013-12-30 19:00:56 -07:00
|
|
|
}
|
2014-01-07 18:43:20 -07:00
|
|
|
|
2015-05-04 16:19:27 -06:00
|
|
|
def users = ApiResult { implicit ctx =>
|
2016-02-25 18:25:28 -07:00
|
|
|
get("team") ?? { teamId =>
|
|
|
|
userApi.list(
|
|
|
|
teamId = teamId,
|
|
|
|
engine = getBoolOpt("engine"),
|
|
|
|
nb = getInt("nb")
|
|
|
|
).map(_.some)
|
|
|
|
}
|
2014-01-08 17:06:20 -07:00
|
|
|
}
|
|
|
|
|
2016-07-08 17:32:54 -06:00
|
|
|
private val GamesRateLimit = new lila.memo.RateLimit(100, 10 minutes, "user games API")
|
|
|
|
|
2016-01-21 23:45:07 -07:00
|
|
|
def userGames(name: String) = ApiResult { implicit ctx =>
|
2016-07-10 02:08:02 -06:00
|
|
|
if (lila.common.HTTPRequest.isBot(ctx.req)) fuccess(none)
|
|
|
|
else GamesRateLimit(ctx.req.remoteAddress) {
|
2016-07-08 17:32:54 -06:00
|
|
|
val page = getInt("page")
|
2016-07-10 02:08:02 -06:00
|
|
|
if (~page > 200) fuccess(Json.obj("error" -> "Going too far back in time.").some)
|
2016-07-08 17:32:54 -06:00
|
|
|
else lila.user.UserRepo named name flatMap {
|
|
|
|
_ ?? { user =>
|
|
|
|
gameApi.byUser(
|
|
|
|
user = user,
|
|
|
|
rated = getBoolOpt("rated"),
|
|
|
|
analysed = getBoolOpt("analysed"),
|
|
|
|
withAnalysis = getBool("with_analysis"),
|
|
|
|
withMoves = getBool("with_moves"),
|
|
|
|
withOpening = getBool("with_opening"),
|
|
|
|
withMoveTimes = getBool("with_movetimes"),
|
|
|
|
token = get("token"),
|
|
|
|
nb = getInt("nb"),
|
|
|
|
page = page
|
2016-07-10 02:08:02 -06:00
|
|
|
) map some
|
2016-07-08 17:32:54 -06:00
|
|
|
}
|
2016-01-21 23:45:07 -07:00
|
|
|
}
|
|
|
|
}
|
2013-12-30 19:00:56 -07:00
|
|
|
}
|
|
|
|
|
2015-05-04 16:19:27 -06:00
|
|
|
def game(id: String) = ApiResult { implicit ctx =>
|
2014-06-06 03:43:00 -06:00
|
|
|
gameApi.one(
|
|
|
|
id = id take lila.game.Game.gameIdSize,
|
2015-05-04 16:19:27 -06:00
|
|
|
withAnalysis = getBool("with_analysis"),
|
|
|
|
withMoves = getBool("with_moves"),
|
|
|
|
withOpening = getBool("with_opening"),
|
|
|
|
withFens = getBool("with_fens"),
|
2016-01-20 08:11:32 -07:00
|
|
|
withMoveTimes = getBool("with_movetimes"),
|
2015-05-04 16:19:27 -06:00
|
|
|
token = get("token"))
|
2014-06-06 03:08:43 -06:00
|
|
|
}
|
|
|
|
|
2015-05-04 16:19:27 -06:00
|
|
|
private def ApiResult(js: lila.api.Context => Fu[Option[JsValue]]) = Open { implicit ctx =>
|
|
|
|
js(ctx) map {
|
2014-02-17 02:12:19 -07:00
|
|
|
case None => NotFound
|
2015-05-04 16:19:27 -06:00
|
|
|
case Some(json) => get("callback") match {
|
2014-02-17 02:12:19 -07:00
|
|
|
case None => Ok(json) as JSON
|
|
|
|
case Some(callback) => Ok(s"$callback($json)") as JAVASCRIPT
|
2013-12-31 03:45:37 -07:00
|
|
|
}
|
2013-12-30 19:00:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|