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._
|
2016-08-01 06:38:07 -06:00
|
|
|
import lila.common.HTTPRequest
|
2013-12-30 19:00:56 -07:00
|
|
|
|
|
|
|
object Api extends LilaController {
|
|
|
|
|
2014-01-07 18:43:20 -07:00
|
|
|
private val userApi = Env.api.userApi
|
|
|
|
private val gameApi = Env.api.gameApi
|
|
|
|
|
2016-08-02 04:43:13 -06:00
|
|
|
private lazy val apiStatusResponse = {
|
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-08-02 04:43:13 -06:00
|
|
|
val status = Action { req =>
|
|
|
|
apiStatusResponse
|
|
|
|
}
|
|
|
|
|
2016-08-02 04:40:10 -06:00
|
|
|
def user(name: String) = ApiRequest { implicit ctx =>
|
|
|
|
userApi one name map toApiResult
|
2013-12-30 19:00:56 -07:00
|
|
|
}
|
2014-01-07 18:43:20 -07:00
|
|
|
|
2016-08-11 16:41:50 -06:00
|
|
|
private val UsersRateLimitGlobal = new lila.memo.RateLimit(
|
|
|
|
credits = 1000,
|
|
|
|
duration = 1 minute,
|
|
|
|
name = "team users API global")
|
|
|
|
|
|
|
|
private val UsersRateLimitPerIP = new lila.memo.RateLimit(
|
|
|
|
credits = 1000,
|
|
|
|
duration = 10 minutes,
|
|
|
|
name = "team users API per IP")
|
|
|
|
|
2016-08-02 04:40:10 -06:00
|
|
|
def users = ApiRequest { implicit ctx =>
|
2016-08-31 05:00:39 -06:00
|
|
|
val page = (getInt("page") | 1) atLeast 1 atMost 50
|
|
|
|
val nb = (getInt("nb") | 10) atLeast 1 atMost 50
|
2016-08-11 16:41:50 -06:00
|
|
|
val cost = page * nb + 10
|
|
|
|
val ip = HTTPRequest lastRemoteAddress ctx.req
|
|
|
|
implicit val default = ornicar.scalalib.Zero.instance[ApiResult](Limited)
|
|
|
|
UsersRateLimitPerIP(ip, cost = cost, msg = ip) {
|
|
|
|
UsersRateLimitGlobal("-", cost = cost, msg = ip) {
|
2016-08-11 16:46:05 -06:00
|
|
|
lila.mon.api.teamUsers.cost(cost)
|
2016-08-11 16:41:50 -06:00
|
|
|
(get("team") ?? Env.team.api.team).flatMap {
|
|
|
|
_ ?? { team =>
|
|
|
|
Env.team.pager(team, page, nb) map userApi.pager map some
|
|
|
|
}
|
|
|
|
} map toApiResult
|
2016-08-11 16:32:39 -06:00
|
|
|
}
|
2016-08-11 16:41:50 -06:00
|
|
|
}
|
2014-01-08 17:06:20 -07:00
|
|
|
}
|
|
|
|
|
2016-07-30 15:46:37 -06:00
|
|
|
private val GamesRateLimitPerIP = new lila.memo.RateLimit(
|
2016-07-29 09:57:48 -06:00
|
|
|
credits = 10 * 1000,
|
|
|
|
duration = 10 minutes,
|
2016-07-30 15:46:37 -06:00
|
|
|
name = "user games API per IP")
|
|
|
|
|
2016-08-01 06:38:07 -06:00
|
|
|
private val GamesRateLimitPerUA = new lila.memo.RateLimit(
|
|
|
|
credits = 10 * 1000,
|
2016-08-01 07:11:18 -06:00
|
|
|
duration = 5 minutes,
|
|
|
|
name = "user games API per UA")
|
2016-08-01 06:38:07 -06:00
|
|
|
|
2016-07-30 15:46:37 -06:00
|
|
|
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-08-02 04:40:10 -06:00
|
|
|
def userGames(name: String) = ApiRequest { implicit ctx =>
|
2016-08-31 05:00:39 -06:00
|
|
|
val page = (getInt("page") | 1) atLeast 1 atMost 200
|
|
|
|
val nb = (getInt("nb") | 10) atLeast 1 atMost 100
|
2016-07-29 09:57:48 -06:00
|
|
|
val cost = page * nb + 10
|
2016-08-02 03:00:41 -06:00
|
|
|
val ip = HTTPRequest lastRemoteAddress ctx.req
|
2016-08-02 04:40:10 -06:00
|
|
|
implicit val default = ornicar.scalalib.Zero.instance[ApiResult](Limited)
|
2016-08-02 03:00:41 -06:00
|
|
|
GamesRateLimitPerIP(ip, cost = cost, msg = ip) {
|
|
|
|
GamesRateLimitPerUA(~HTTPRequest.userAgent(ctx.req), cost = cost, msg = ip) {
|
|
|
|
GamesRateLimitGlobal("-", cost = cost, msg = ip) {
|
2016-08-11 16:46:05 -06:00
|
|
|
lila.mon.api.userGames.cost(cost)
|
2016-08-01 06:38:07 -06:00
|
|
|
lila.user.UserRepo named name flatMap {
|
|
|
|
_ ?? { user =>
|
|
|
|
gameApi.byUser(
|
|
|
|
user = user,
|
|
|
|
rated = getBoolOpt("rated"),
|
2016-08-11 13:56:05 -06:00
|
|
|
playing = getBoolOpt("playing"),
|
2016-08-01 06:38:07 -06:00
|
|
|
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-08-02 04:40:10 -06:00
|
|
|
} map toApiResult
|
2016-07-08 17:32:54 -06:00
|
|
|
}
|
2016-01-21 23:45:07 -07:00
|
|
|
}
|
|
|
|
}
|
2013-12-30 19:00:56 -07:00
|
|
|
}
|
|
|
|
|
2016-08-02 04:40:10 -06:00
|
|
|
def game(id: String) = ApiRequest { implicit ctx =>
|
2016-08-11 16:46:05 -06:00
|
|
|
lila.mon.api.game.cost(1)
|
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"),
|
2016-08-02 04:40:10 -06:00
|
|
|
token = get("token")) map toApiResult
|
2014-06-06 03:08:43 -06:00
|
|
|
}
|
|
|
|
|
2016-08-15 05:10:53 -06:00
|
|
|
def currentTournaments = ApiRequest { implicit ctx =>
|
|
|
|
Env.tournament.api.fetchVisibleTournaments map
|
|
|
|
Env.tournament.scheduleJsonView.apply map Data.apply
|
|
|
|
}
|
|
|
|
|
|
|
|
def tournament(id: String) = ApiRequest { implicit ctx =>
|
2016-08-31 05:00:39 -06:00
|
|
|
val page = (getInt("page") | 1) atLeast 1 atMost 200
|
2016-08-15 05:10:53 -06:00
|
|
|
lila.tournament.TournamentRepo byId id flatMap {
|
|
|
|
_ ?? { tour =>
|
|
|
|
Env.tournament.jsonView(tour, page.some, none, none, none) map some
|
|
|
|
}
|
|
|
|
} map toApiResult
|
|
|
|
}
|
|
|
|
|
2016-08-02 04:40:10 -06:00
|
|
|
sealed trait ApiResult
|
|
|
|
case class Data(json: JsValue) extends ApiResult
|
|
|
|
case object NoData extends ApiResult
|
|
|
|
case object Limited extends ApiResult
|
|
|
|
def toApiResult(json: Option[JsValue]) = json.fold[ApiResult](NoData)(Data.apply)
|
|
|
|
|
|
|
|
private def ApiRequest(js: lila.api.Context => Fu[ApiResult]) = Open { implicit ctx =>
|
2015-05-04 16:19:27 -06:00
|
|
|
js(ctx) map {
|
2016-08-02 04:40:10 -06:00
|
|
|
case Limited => TooManyRequest(jsonError("Try again later"))
|
|
|
|
case NoData => NotFound
|
|
|
|
case Data(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|