code tweaks

This commit is contained in:
Thibault Duplessis 2017-01-25 13:29:30 +01:00
parent 31e254d69f
commit 4f69774336
4 changed files with 19 additions and 15 deletions

View file

@ -70,8 +70,7 @@ object User extends LilaController {
html = notFound,
api = _ => env.cached top50Online true map { list =>
Ok(Json.toJson(list.take(getInt("nb").fold(10)(_ min max)).map(env.jsonView(_))))
}
)
})
}
private def filter(

View file

@ -150,7 +150,7 @@ trait UserHelper { self: I18nHelper with StringHelper with NumberHelper =>
withBestRating: Boolean = false,
withPerfRating: Option[PerfType] = None,
text: Option[String] = None,
params: String = "") = Html {
params: String = ""): Html = Html {
val klass = userClass(user.id, cssClass, withOnline, withPowerTip)
val href = userHref(user.username, params)
val content = text | user.username

View file

@ -10,6 +10,8 @@ import User.{ BSONFields => F }
final class LightUserApi(coll: Coll)(implicit system: akka.actor.ActorSystem) {
import LightUserApi._
def sync(id: String): Option[LightUser] = cache sync id
def async(id: String): Fu[Option[LightUser]] = cache async id
@ -22,17 +24,6 @@ final class LightUserApi(coll: Coll)(implicit system: akka.actor.ActorSystem) {
def usernameList(ids: List[String]): List[String] = getList(ids).map(_.name)
private implicit val lightUserReader = new BSONDocumentReader[LightUser] {
def read(doc: BSONDocument) = LightUser(
id = doc.getAs[String](F.id) err "LightUser id missing",
name = doc.getAs[String](F.username) err "LightUser username missing",
title = doc.getAs[String](F.title),
isPatron = ~doc.getAs[Bdoc](F.plan).flatMap(_.getAs[Boolean]("active")))
}
private val projection = $doc(F.username -> true, F.title -> true, s"${F.plan}.active" -> true)
private val cache = new Syncache[String, Option[LightUser]](
name = "user.light",
compute = id => coll.find($id(id), projection).uno[LightUser],
@ -41,3 +32,17 @@ final class LightUserApi(coll: Coll)(implicit system: akka.actor.ActorSystem) {
timeToLive = 20 minutes,
logger = logger branch "LightUserApi")
}
private object LightUserApi {
implicit val lightUserBSONReader = new BSONDocumentReader[LightUser] {
def read(doc: BSONDocument) = LightUser(
id = doc.getAs[String](F.id) err "LightUser id missing",
name = doc.getAs[String](F.username) err "LightUser username missing",
title = doc.getAs[String](F.title),
isPatron = ~doc.getAs[Bdoc](F.plan).flatMap(_.getAs[Boolean]("active")))
}
val projection = $doc(F.username -> true, F.title -> true, s"${F.plan}.active" -> true)
}

View file

@ -58,7 +58,7 @@ object UserRepo {
def nameds(usernames: List[String]): Fu[List[User]] = coll.byIds[User](usernames.map(normalize))
// expensive, send to secondary
def byIdsSortRating(ids: Iterable[ID], nb: Int) =
def byIdsSortRating(ids: Iterable[ID], nb: Int): Fu[List[User]] =
coll.find($inIds(ids) ++ goodLadSelectBson)
.sort($sort desc "perfs.standard.gl.r")
.cursor[User](ReadPreference.secondaryPreferred)