lila/app/controllers/Insight.scala
Thibault Duplessis cdf2f56981 Revert "Merge branch 'scala-2.12-play-2.6' of github.com:ornicar/lila"
This reverts commit 2d3b4872c7, reversing
changes made to 04f1d4ae6d.
2017-09-09 10:01:35 -05:00

70 lines
2 KiB
Scala

package controllers
import lila.api.Context
import lila.app._
import lila.insight.{ Metric, Dimension }
import play.api.mvc._
import views._
object Insight extends LilaController {
private def env = Env.insight
def refresh(username: String) = Open { implicit ctx =>
Accessible(username) { user =>
env.api indexAll user inject Ok
}
}
def index(username: String) = path(
username,
metric = Metric.MeanCpl.key,
dimension = Dimension.Perf.key,
filters = ""
)
def path(username: String, metric: String, dimension: String, filters: String) = Open { implicit ctx =>
Accessible(username) { user =>
import lila.insight.InsightApi.UserStatus._
env.api userStatus user flatMap {
case NoGame => Ok(html.insight.noGame(user)).fuccess
case Empty => Ok(html.insight.empty(user)).fuccess
case s => for {
cache <- env.api userCache user
prefId <- env.share getPrefId user
} yield Ok(html.insight.index(
u = user,
cache = cache,
prefId = prefId,
ui = env.jsonView.ui(cache.ecos),
question = env.jsonView.question(metric, dimension, filters),
stale = s == Stale
))
}
}
}
def json(username: String) = OpenBody(BodyParsers.parse.json) { implicit ctx =>
import lila.insight.JsonQuestion, JsonQuestion._
Accessible(username) { user =>
ctx.body.body.validate[JsonQuestion].fold(
err => BadRequest(jsonError(err.toString)).fuccess,
qJson => qJson.question.fold(BadRequest.fuccess) { q =>
env.api.ask(q, user) map
lila.insight.Chart.fromAnswer(Env.user.lightUserSync) map
env.jsonView.chart.apply map { Ok(_) }
}
)
}
}
private def Accessible(username: String)(f: lila.user.User => Fu[Result])(implicit ctx: Context) =
lila.user.UserRepo named username flatMap {
_.fold(notFound) { u =>
env.share.grant(u, ctx.me) flatMap {
_.fold(f(u), fuccess(Forbidden(html.insight.forbidden(u))))
}
}
}
}