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

120 lines
3.2 KiB
Scala
Raw Normal View History

2013-12-27 15:12:20 -07:00
package lila.api
2019-12-07 13:45:01 -07:00
import play.api.i18n.Lang
2020-10-13 10:15:38 -06:00
import play.api.mvc.RequestHeader
2013-12-27 15:12:20 -07:00
2019-12-07 13:45:01 -07:00
import lila.common.{ HTTPRequest, Nonce }
2013-12-27 15:12:20 -07:00
import lila.pref.Pref
2019-12-13 07:30:20 -07:00
import lila.user.{ BodyUserContext, HeaderUserContext, UserContext }
2013-12-27 15:12:20 -07:00
2014-01-02 10:46:51 -07:00
case class PageData(
2017-08-23 17:56:39 -06:00
teamNbRequests: Int,
nbChallenges: Int,
nbNotifications: Int,
pref: Pref,
blindMode: Boolean,
hasFingerprint: Boolean,
hasClas: 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
2020-05-05 22:11:15 -06:00
def anon(req: RequestHeader, nonce: Option[Nonce], blindMode: Boolean = false) =
PageData(
teamNbRequests = 0,
nbChallenges = 0,
nbNotifications = 0,
lila.pref.RequestPref fromRequest req,
blindMode = blindMode,
hasFingerprint = false,
hasClas = false,
inquiry = none,
nonce = nonce
)
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
2019-12-13 07:30:20 -07:00
def teamNbRequests = pageData.teamNbRequests
def nbChallenges = pageData.nbChallenges
def nbNotifications = pageData.nbNotifications
2019-12-13 07:30:20 -07:00
def pref = pageData.pref
def blind = pageData.blindMode
def noBlind = !blind
def nonce = pageData.nonce
def hasClas = pageData.hasClas
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)
2020-10-13 10:15:38 -06:00
lazy val currentBg =
if (pref.bg == Pref.Bg.TRANSPARENT) "transp"
else if (pref.bg == Pref.Bg.LIGHT) "light"
else "dark"
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
2019-05-04 18:56:12 -06:00
def zoom: Int = {
2019-11-28 18:34:46 -07:00
req.session get "zoom2" flatMap (_.toIntOption) map (_ - 100) filter (0 <=) filter (100 >=)
2019-05-04 18:56:12 -06:00
} | 85
2020-01-16 16:41:46 -07:00
def flash(name: String): Option[String] = req.flash get name
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)
2020-02-09 13:37:40 -07:00
trait ToLang {
implicit def ctxLang(implicit ctx: Context): Lang = ctx.lang
}
2013-12-27 15:12:20 -07:00
}