lila/app/controllers/Api.scala

100 lines
2.8 KiB
Scala
Raw Normal View History

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._
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 =>
val api = lila.api.Mobile.Api
2015-01-17 04:35:54 -07:00
Ok(Json.obj(
"api" -> Json.obj(
2016-07-15 11:41:48 -06:00
"current" -> api.currentVersion.value,
"olds" -> api.oldVersions.map { old =>
Json.obj(
2016-07-15 11:41:48 -06:00
"version" -> old.version.value,
"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
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
}
private val GamesRateLimitPerIP = new lila.memo.RateLimit(
credits = 10 * 1000,
duration = 10 minutes,
name = "user games API per IP")
private val GamesRateLimitGlobal = new lila.memo.RateLimit(
credits = 10 * 1000,
duration = 1 minute,
name = "user games API global")
2016-07-08 17:32:54 -06:00
2016-01-21 23:45:07 -07:00
def userGames(name: String) = ApiResult { implicit ctx =>
val page = (getInt("page") | 1) max 1 min 200
val nb = (getInt("nb") | 10) max 1 min 100
val cost = page * nb + 10
if (lila.common.HTTPRequest.isBot(ctx.req)) fuccess(none)
else GamesRateLimitGlobal("", cost = cost) {
GamesRateLimitPerIP(ctx.req.remoteAddress, cost = cost) {
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 = nb,
page = page
) 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
}
def game(id: String) = ApiResult { implicit ctx =>
gameApi.one(
id = id take lila.game.Game.gameIdSize,
withAnalysis = getBool("with_analysis"),
withMoves = getBool("with_moves"),
withOpening = getBool("with_opening"),
withFens = getBool("with_fens"),
withMoveTimes = getBool("with_movetimes"),
token = get("token"))
2014-06-06 03:08:43 -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
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
}
}
}