lila/modules/api/src/main/Context.scala

113 lines
3.0 KiB
Scala
Raw Normal View History

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
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
)
2014-01-02 10:46:51 -07:00
object PageData {
2014-06-06 06:33:04 -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,
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,
nonce = nonce
2017-05-05 04:39:04 -06:00
)
2018-01-27 08:55:55 -07: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
2014-04-18 03:51:19 -06:00
def teamNbRequests = pageData.teamNbRequests
2016-01-31 22:40:31 -07:00
def nbChallenges = pageData.nbChallenges
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)
2017-05-05 04:39:04 -06:00
def currentSoundSet = lila.pref.SoundSet(pref.soundSet)
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
lazy val mobileApiVersion = Mobile.Api requestVersion req
def isMobileApi = mobileApiVersion.isDefined
lazy val isMobileBrowser = HTTPRequest isMobile req
def requiresFingerprint = isAuth && !pageData.hasFingerprint
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
) 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
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
}