2013-12-27 15:12:20 -07:00
|
|
|
package lila.api
|
|
|
|
|
2017-01-15 05:26:08 -07:00
|
|
|
import play.api.mvc.RequestHeader
|
2013-12-27 15:12:20 -07:00
|
|
|
|
2018-12-05 03:42:39 -07:00
|
|
|
import lila.common.{ HTTPRequest, Nonce, Lang }
|
2013-12-27 15:12:20 -07:00
|
|
|
import lila.pref.Pref
|
2017-05-05 04:39:04 -06:00
|
|
|
import lila.relation.actorApi.OnlineFriends
|
2013-12-27 15:12:20 -07:00
|
|
|
import lila.user.{ UserContext, HeaderUserContext, BodyUserContext }
|
|
|
|
|
2014-01-02 10:46:51 -07:00
|
|
|
case class PageData(
|
2017-08-23 17:56:39 -06:00
|
|
|
onlineFriends: OnlineFriends,
|
|
|
|
teamNbRequests: Int,
|
|
|
|
nbChallenges: Int,
|
|
|
|
nbNotifications: Int,
|
|
|
|
pref: Pref,
|
|
|
|
blindMode: Boolean,
|
|
|
|
hasFingerprint: Boolean,
|
2018-01-27 08:55:55 -07:00
|
|
|
inquiry: Option[lila.mod.Inquiry],
|
2018-05-07 18:55:47 -06:00
|
|
|
nonce: Option[Nonce],
|
2018-01-27 08:55:55 -07:00
|
|
|
error: Boolean = false
|
2017-02-14 08:34:07 -07:00
|
|
|
)
|
2014-01-02 10:46:51 -07:00
|
|
|
|
|
|
|
object PageData {
|
2014-06-06 06:33:04 -06:00
|
|
|
|
2018-09-09 07:53:22 -06:00
|
|
|
def anon(req: RequestHeader, nonce: Option[Nonce], blindMode: Boolean = false) = PageData(
|
2017-05-05 04:39:04 -06:00
|
|
|
OnlineFriends.empty,
|
|
|
|
teamNbRequests = 0,
|
|
|
|
nbChallenges = 0,
|
|
|
|
nbNotifications = 0,
|
2017-05-30 06:41:39 -06:00
|
|
|
lila.pref.RequestPref fromRequest req,
|
2017-05-05 04:39:04 -06:00
|
|
|
blindMode = blindMode,
|
|
|
|
hasFingerprint = false,
|
2018-05-07 17:49:39 -06:00
|
|
|
inquiry = none,
|
2018-05-07 18:19:25 -06:00
|
|
|
nonce = nonce
|
2017-05-05 04:39:04 -06:00
|
|
|
)
|
2018-01-27 08:55:55 -07:00
|
|
|
|
2018-09-09 07:53:22 -06:00
|
|
|
def error(req: RequestHeader, nonce: Option[Nonce]) = anon(req, nonce).copy(error = true)
|
2014-01-02 10:46:51 -07:00
|
|
|
}
|
|
|
|
|
2013-12-27 15:12:20 -07:00
|
|
|
sealed trait Context extends lila.user.UserContextWrapper {
|
|
|
|
|
|
|
|
val userContext: UserContext
|
2014-01-02 10:46:51 -07:00
|
|
|
val pageData: PageData
|
2013-12-29 14:22:29 -07:00
|
|
|
|
2017-05-26 07:53:11 -06:00
|
|
|
def lang = userContext.lang
|
|
|
|
|
2016-07-24 10:25:11 -06:00
|
|
|
def onlineFriends = pageData.onlineFriends
|
2016-07-03 16:14:10 -06:00
|
|
|
|
2014-04-18 03:51:19 -06:00
|
|
|
def teamNbRequests = pageData.teamNbRequests
|
2016-01-31 22:40:31 -07:00
|
|
|
def nbChallenges = pageData.nbChallenges
|
2016-05-30 06:51:36 -06:00
|
|
|
def nbNotifications = pageData.nbNotifications
|
2014-01-02 10:46:51 -07:00
|
|
|
def pref = pageData.pref
|
2019-01-19 02:08:45 -07:00
|
|
|
def blind = pageData.blindMode
|
|
|
|
def noBlind = !blind
|
2018-05-07 17:49:39 -06:00
|
|
|
def nonce = pageData.nonce
|
2013-12-27 15:12:20 -07:00
|
|
|
|
2017-05-05 04:39:04 -06:00
|
|
|
def currentTheme = lila.pref.Theme(pref.theme)
|
2014-10-09 13:46:42 -06:00
|
|
|
|
2017-05-05 04:39:04 -06:00
|
|
|
def currentTheme3d = lila.pref.Theme3d(pref.theme3d)
|
2014-10-08 17:12:55 -06:00
|
|
|
|
2017-05-05 04:39:04 -06:00
|
|
|
def currentPieceSet = lila.pref.PieceSet(pref.pieceSet)
|
2014-10-09 13:46:42 -06:00
|
|
|
|
2017-05-05 04:39:04 -06:00
|
|
|
def currentPieceSet3d = lila.pref.PieceSet3d(pref.pieceSet3d)
|
2014-05-09 05:51:55 -06:00
|
|
|
|
2017-05-05 04:39:04 -06:00
|
|
|
def currentSoundSet = lila.pref.SoundSet(pref.soundSet)
|
2015-06-26 15:34:22 -06:00
|
|
|
|
2017-05-05 04:39:04 -06:00
|
|
|
lazy val currentBg = if (pref.transp) "transp" else if (pref.dark) "dark" else "light"
|
2013-12-27 15:12:20 -07:00
|
|
|
|
2017-05-06 01:25:17 -06:00
|
|
|
def transpBgImg = currentBg == "transp" option pref.bgImgOrDefault
|
2015-07-17 08:43:14 -06:00
|
|
|
|
2016-09-01 10:39:43 -06:00
|
|
|
lazy val mobileApiVersion = Mobile.Api requestVersion req
|
|
|
|
|
2016-09-05 05:13:41 -06:00
|
|
|
def isMobileApi = mobileApiVersion.isDefined
|
|
|
|
|
|
|
|
lazy val isMobileBrowser = HTTPRequest isMobile req
|
2015-02-05 15:15:07 -07:00
|
|
|
|
2015-08-12 04:27:45 -06:00
|
|
|
def requiresFingerprint = isAuth && !pageData.hasFingerprint
|
|
|
|
|
2019-02-25 17:49:07 -07:00
|
|
|
def zoom: Option[Int] = req.session get "zoom" flatMap parseIntOption filter (100<=)
|
2019-02-13 04:48:10 -07:00
|
|
|
|
2019-04-16 01:00:20 -06:00
|
|
|
def respZoom = zoom.fold(85)(_ - 100)
|
2013-12-27 15:12:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
sealed abstract class BaseContext(
|
2017-08-23 17:56:39 -06:00
|
|
|
val userContext: lila.user.UserContext,
|
2018-05-07 17:49:39 -06:00
|
|
|
val pageData: PageData
|
2017-02-14 08:34:07 -07:00
|
|
|
) extends Context
|
2013-12-27 15:12:20 -07:00
|
|
|
|
2015-09-16 09:32:02 -06:00
|
|
|
final class BodyContext[A](
|
|
|
|
val bodyContext: BodyUserContext[A],
|
2018-05-07 17:49:39 -06:00
|
|
|
data: PageData
|
|
|
|
) extends BaseContext(bodyContext, data) {
|
2013-12-29 02:51:40 -07:00
|
|
|
|
2013-12-27 15:12:20 -07:00
|
|
|
def body = bodyContext.body
|
|
|
|
}
|
|
|
|
|
2013-12-29 14:22:29 -07:00
|
|
|
final class HeaderContext(
|
2017-08-23 17:56:39 -06:00
|
|
|
headerContext: HeaderUserContext,
|
2018-05-07 17:49:39 -06:00
|
|
|
data: PageData
|
|
|
|
) extends BaseContext(headerContext, data)
|
2013-12-27 15:12:20 -07:00
|
|
|
|
2017-05-26 07:53:11 -06:00
|
|
|
object Context {
|
2013-12-27 15:12:20 -07:00
|
|
|
|
2018-09-09 07:53:22 -06:00
|
|
|
def error(req: RequestHeader, lang: Lang, nonce: Option[Nonce]): HeaderContext =
|
|
|
|
new HeaderContext(UserContext(req, none, none, lang), PageData.error(req, nonce))
|
2013-12-27 15:12:20 -07:00
|
|
|
|
2014-01-02 10:46:51 -07:00
|
|
|
def apply(userContext: HeaderUserContext, pageData: PageData): HeaderContext =
|
2018-05-07 17:49:39 -06:00
|
|
|
new HeaderContext(userContext, pageData)
|
2013-12-27 15:12:20 -07:00
|
|
|
|
2015-09-16 09:32:02 -06:00
|
|
|
def apply[A](userContext: BodyUserContext[A], pageData: PageData): BodyContext[A] =
|
2018-05-07 17:49:39 -06:00
|
|
|
new BodyContext(userContext, pageData)
|
2013-12-27 15:12:20 -07:00
|
|
|
}
|