2013-03-18 17:36:22 -06:00
|
|
|
package controllers
|
|
|
|
|
2014-12-18 16:25:37 -07:00
|
|
|
import play.api.libs.json._
|
2013-05-24 08:37:24 -06:00
|
|
|
import play.api.mvc._
|
2016-09-22 17:29:39 -06:00
|
|
|
import play.twirl.api.Html
|
|
|
|
import scala.concurrent.duration._
|
2013-05-24 08:37:24 -06:00
|
|
|
|
2013-12-29 02:51:40 -07:00
|
|
|
import lila.api.Context
|
2013-05-08 15:34:37 -06:00
|
|
|
import lila.app._
|
2014-12-18 18:35:55 -07:00
|
|
|
import lila.common.{ LilaCookie, HTTPRequest }
|
2013-05-08 15:34:37 -06:00
|
|
|
import views._
|
2013-03-18 17:36:22 -06:00
|
|
|
|
2013-10-28 12:23:37 -06:00
|
|
|
object Lobby extends LilaController {
|
2013-03-18 17:36:22 -06:00
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def home = Open { implicit ctx =>
|
2014-12-26 03:09:52 -07:00
|
|
|
negotiate(
|
2015-06-19 09:36:31 -06:00
|
|
|
html = renderHome(Results.Ok).map(NoCache),
|
2014-12-26 03:09:52 -07:00
|
|
|
api = _ => fuccess {
|
|
|
|
Ok(Json.obj(
|
|
|
|
"lobby" -> Json.obj(
|
|
|
|
"version" -> Env.lobby.history.version)
|
|
|
|
))
|
|
|
|
}
|
|
|
|
)
|
2013-05-08 15:34:37 -06:00
|
|
|
}
|
2013-03-20 10:34:38 -06:00
|
|
|
|
2016-03-08 22:15:53 -07:00
|
|
|
def handleStatus(req: RequestHeader, status: Results.Status): Fu[Result] = {
|
2014-02-17 02:12:19 -07:00
|
|
|
reqToCtx(req) flatMap { ctx => renderHome(status)(ctx) }
|
2016-03-08 22:15:53 -07:00
|
|
|
}
|
2013-03-20 10:34:38 -06:00
|
|
|
|
2016-03-09 05:59:54 -07:00
|
|
|
def renderHome(status: Results.Status)(implicit ctx: Context): Fu[Result] = {
|
2016-09-22 17:29:39 -06:00
|
|
|
HomeCache(ctx) map { status(_) } map ensureSessionId(ctx.req)
|
2016-03-10 20:08:34 -07:00
|
|
|
}.mon(_.http.response.home)
|
2013-05-08 15:34:37 -06:00
|
|
|
|
2014-12-17 17:02:59 -07:00
|
|
|
def seeks = Open { implicit ctx =>
|
2014-12-28 16:15:42 -07:00
|
|
|
negotiate(
|
|
|
|
html = fuccess(NotFound),
|
|
|
|
api = _ => ctx.me.fold(Env.lobby.seekApi.forAnon)(Env.lobby.seekApi.forUser) map { seeks =>
|
|
|
|
Ok(JsArray(seeks.map(_.render)))
|
|
|
|
}
|
|
|
|
)
|
2014-12-17 17:02:59 -07:00
|
|
|
}
|
|
|
|
|
2016-03-06 22:03:40 -07:00
|
|
|
private val socketConsumer = lila.api.TokenBucket.create(
|
2016-03-06 21:48:59 -07:00
|
|
|
system = lila.common.PlayApp.system,
|
|
|
|
size = 10,
|
2016-03-07 09:13:07 -07:00
|
|
|
rate = 6)
|
2016-03-06 21:48:59 -07:00
|
|
|
|
2016-03-06 23:00:45 -07:00
|
|
|
def socket(apiVersion: Int) = SocketOptionLimited[JsValue](socketConsumer, "lobby") { implicit ctx =>
|
2014-02-17 02:12:19 -07:00
|
|
|
get("sri") ?? { uid =>
|
2016-03-05 19:42:22 -07:00
|
|
|
Env.lobby.socketHandler(
|
|
|
|
uid = uid,
|
|
|
|
user = ctx.me,
|
|
|
|
mobile = getBool("mobile")) map some
|
2013-05-08 15:34:37 -06:00
|
|
|
}
|
|
|
|
}
|
2013-05-24 08:37:24 -06:00
|
|
|
|
2016-09-22 17:29:39 -06:00
|
|
|
def timeline = Auth { implicit ctx => me =>
|
|
|
|
Env.timeline.entryRepo.userEntries(me.id) map { html.timeline.entries(_) }
|
|
|
|
}
|
|
|
|
|
|
|
|
private object HomeCache {
|
|
|
|
|
|
|
|
private case class RequestKey(
|
|
|
|
uri: String,
|
|
|
|
headers: Headers)
|
|
|
|
|
|
|
|
private val cache = lila.memo.AsyncCache[RequestKey, Html](
|
2016-11-27 22:42:37 -07:00
|
|
|
name = "lobby.homeCache",
|
2016-09-22 17:29:39 -06:00
|
|
|
f = renderRequestKey,
|
2016-09-27 08:22:17 -06:00
|
|
|
timeToLive = 1 second)
|
2016-09-22 17:29:39 -06:00
|
|
|
|
|
|
|
private def renderCtx(implicit ctx: Context): Fu[Html] = Env.current.preloader(
|
|
|
|
posts = Env.forum.recent(ctx.me, Env.team.cached.teamIds),
|
|
|
|
tours = Env.tournament.cached promotable true,
|
|
|
|
events = Env.event.api promotable true,
|
|
|
|
simuls = Env.simul allCreatedFeaturable true
|
|
|
|
) map (html.lobby.home.apply _).tupled
|
|
|
|
|
|
|
|
private def renderRequestKey(r: RequestKey): Fu[Html] = renderCtx {
|
|
|
|
lila.mon.lobby.cache.miss()
|
|
|
|
val req = new RequestHeader {
|
|
|
|
def id = 1000l
|
|
|
|
def tags = Map.empty
|
|
|
|
def uri = r.uri
|
|
|
|
def path = "/"
|
|
|
|
def method = "GET"
|
|
|
|
def version = "1.1"
|
|
|
|
def queryString = Map.empty
|
|
|
|
def headers = r.headers
|
|
|
|
def remoteAddress = "0.0.0.0"
|
|
|
|
def secure = true
|
|
|
|
}
|
|
|
|
new lila.api.HeaderContext(
|
|
|
|
headerContext = new lila.user.HeaderUserContext(req, none),
|
|
|
|
data = lila.api.PageData.default)
|
|
|
|
}
|
|
|
|
|
|
|
|
def apply(ctx: Context) =
|
|
|
|
if (ctx.isAuth) {
|
|
|
|
lila.mon.lobby.cache.user()
|
|
|
|
renderCtx(ctx)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lila.mon.lobby.cache.anon()
|
|
|
|
cache(RequestKey(
|
|
|
|
uri = ctx.req.uri,
|
|
|
|
headers = new Headers(
|
2016-10-31 05:47:08 -06:00
|
|
|
List(HOST -> ctx.req.host) :::
|
|
|
|
ctx.req.headers.get(COOKIE).?? { cookie =>
|
|
|
|
List(COOKIE -> cookie)
|
|
|
|
}
|
2016-09-22 17:29:39 -06:00
|
|
|
)))
|
|
|
|
}
|
2013-05-24 08:37:24 -06:00
|
|
|
}
|
2013-03-18 17:36:22 -06:00
|
|
|
}
|