more API features

This commit is contained in:
Thibault Duplessis 2014-01-11 01:31:57 +01:00
parent 6a8820dcae
commit 2705259b2c
3 changed files with 15 additions and 7 deletions

View file

@ -19,6 +19,7 @@ object Api extends LilaController {
def users = ApiResult { req
userApi.list(
team = get("team", req),
engine = get("engine", req) map ("1"==),
token = get("token", req),
nb = getInt("nb", req)
) map (_.some)

View file

@ -32,12 +32,19 @@ private[api] final class UserApi(
Json.obj(
"username" -> u.username,
"rating" -> u.rating,
"rd" -> u.perfs.global.glicko.deviation,
"progress" -> u.progress)
}
def list(team: Option[String], token: Option[String], nb: Option[Int]): Fu[JsObject] = (team match {
def list(
team: Option[String],
token: Option[String],
nb: Option[Int],
engine: Option[Boolean]): Fu[JsObject] = (team match {
case Some(teamId) lila.team.MemberRepo.userIdsByTeam(teamId) flatMap UserRepo.byIds
case None $find($query.all sort UserRepo.sortRatingDesc, makeNb(nb))
case None $find($query(
$select.all ++ (engine ?? UserRepo.engineSelect)
) sort UserRepo.sortRatingDesc, makeNb(nb, token))
}) flatMap { users
users.map(u makeUrl(R User u.username)).sequenceFu map { urls
Json.obj(
@ -76,7 +83,7 @@ private[api] final class UserApi(
def knownEnginesSharingIp(userId: String): Fu[List[String]] =
userIdsSharingIp(userId) flatMap UserRepo.filterByEngine
private def makeNb(nb: Option[Int]) = math.min(100, nb | 10)
private def makeNb(nb: Option[Int], token: Option[String]) = math.min(check(token) ? 1000 | 100, nb | 10)
private def check(token: Option[String]) = token ?? (apiToken==)
}

View file

@ -85,9 +85,9 @@ trait UserRepo {
$update($select(id), $setBson("profile" -> Profile.tube.handler.write(profile)))
val enabledSelect = Json.obj("enabled" -> true)
val noEngineSelect = Json.obj("engine" -> $ne(true))
def engineSelect(v: Boolean) = Json.obj(User.BSONFields.engine -> v.fold(JsBoolean(true), $ne(true)))
val stableSelect = Json.obj("perfs.global.gl.d" -> $lt(82))
val goodLadSelect = enabledSelect ++ noEngineSelect
val goodLadSelect = enabledSelect ++ engineSelect(false)
val stableGoodLadSelect = stableSelect ++ goodLadSelect
def perfSince(perf: String, since: DateTime) = Json.obj(
s"perfs.$perf.la" -> Json.obj("$gt" -> $date(since))
@ -184,7 +184,7 @@ trait UserRepo {
def updateTroll(user: User) = $update.field(user.id, "troll", user.troll)
def isEngine(id: ID): Fu[Boolean] = $count.exists($select(id) ++ Json.obj("engine" -> true))
def isEngine(id: ID): Fu[Boolean] = $count.exists($select(id) ++ engineSelect(true))
def isArtificial(id: ID): Fu[Boolean] = $count.exists($select(id) ++ Json.obj("artificial" -> true))
def setRoles(id: ID, roles: List[String]) = $update.field(id, "roles", roles)
@ -237,7 +237,7 @@ trait UserRepo {
}
def filterByEngine(userIds: List[String]): Fu[List[String]] =
$primitive(Json.obj("_id" -> $in(userIds), "engine" -> true), "username")(_.asOpt[String])
$primitive(Json.obj("_id" -> $in(userIds)) ++ engineSelect(true), "username")(_.asOpt[String])
private def newUser(username: String, password: String) = {