lila/app/controllers/Lobby.scala

116 lines
3.3 KiB
Scala
Raw Normal View History

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._
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
2016-11-29 16:05:42 -07:00
private val lobbyJson = Json.obj(
2016-12-10 04:47:03 -07:00
"lobby" -> Json.obj(
"version" -> 0,
2016-12-10 04:58:29 -07:00
"pools" -> Env.api.lobbyApi.poolsJson
2016-12-10 04:47:03 -07:00
)
2016-11-29 16:05:42 -07:00
)
2014-02-17 02:12:19 -07:00
def home = Open { implicit ctx =>
negotiate(
2015-06-19 09:36:31 -06:00
html = renderHome(Results.Ok).map(NoCache),
2016-11-29 16:05:42 -07:00
api = _ => fuccess(Ok(lobbyJson))
)
2013-05-08 15:34:37 -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
}
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
}
private val MessageLimitPerIP = new lila.memo.RateLimit(
2016-12-20 12:37:14 -07:00
credits = 40,
duration = 10 seconds,
name = "lobby socket message per IP",
key = "lobby_socket.message.ip")
def socket(apiVersion: Int) = SocketOptionLimited[JsValue](MessageLimitPerIP, "lobby") { implicit ctx =>
2017-02-01 07:31:22 -07:00
getSocketUid("sri") ?? { uid =>
Env.lobby.socketHandler(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 = Env.memo.asyncCache.multi[RequestKey, Html](
name = "lobby.homeCache",
2016-09-22 17:29:39 -06:00
f = renderRequestKey,
expireAfter = _.ExpireAfterWrite(1 second))
2016-09-22 17:29:39 -06:00
private def renderCtx(implicit ctx: Context): Fu[Html] = Env.current.preloader(
2017-02-05 04:19:53 -07:00
posts = Env.forum.recent(ctx.me, Env.team.cached.teamIdsList).nevermind,
tours = Env.tournament.cached.promotable.get.nevermind,
events = Env.event.api.promotable.get.nevermind,
simuls = Env.simul.allCreatedFeaturable.get.nevermind
2016-09-22 17:29:39 -06:00
) 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 Env.api.assetVersion.get)
2016-09-22 17:29:39 -06:00
}
def apply(ctx: Context) =
if (ctx.isAuth) {
lila.mon.lobby.cache.user()
renderCtx(ctx)
}
else {
lila.mon.lobby.cache.anon()
cache get RequestKey(
2016-09-22 17:29:39 -06:00
uri = ctx.req.uri,
headers = new Headers(
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
}