Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add embed route for puzzles with zen mode enforced #11968

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion app/controllers/Puzzle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ final class Puzzle(env: Env, apiC: => Api) extends LilaController(env):
).enableSharedArrayBuffer
}

private def renderEmbed(
puzzle: Puz,
angle: PuzzleAngle,
color: Option[Color] = None,
replay: Option[lila.puzzle.PuzzleReplay] = None,
langPath: Option[LangPath] = None
)(implicit ctx: Context) =
renderJson(puzzle, angle, replay) zip
ctx.me.??(u => env.puzzle.session.getSettings(u) dmap some) map { case (json, settings) =>
Ok(
views.html.puzzle
.embed(
puzzle,
json,
env.puzzle.jsonView.pref(ctx.pref),
settings | PuzzleSettings.default(color),
langPath
)
).enableSharedArrayBuffer
}

def daily =
Open { implicit ctx =>
NoBot {
Expand Down Expand Up @@ -346,6 +367,29 @@ final class Puzzle(env: Env, apiC: => Api) extends LilaController(env):
}
}

private def serveEmbed(angleOrId: String)(implicit ctx: Context) =
NoBot {
val langPath = LangPath(routes.Puzzle.show(angleOrId)).some
PuzzleAngle find angleOrId match {
case Some(angle) =>
nextPuzzleForMe(angle, none) flatMap {
renderEmbed(_, angle, langPath = langPath)
}
case _ if angleOrId.size == Puz.idSize =>
OptionFuResult(env.puzzle.api.puzzle find Puz.Id(angleOrId)) { puzzle =>
ctx.me.?? { env.puzzle.api.casual.setCasualIfNotYetPlayed(_, puzzle) } >>
renderEmbed(puzzle, PuzzleAngle.mix, langPath = langPath)
}
case _ =>
angleOrId.toLongOption
.flatMap(Puz.numericalId.apply)
.??(env.puzzle.api.puzzle.find) map {
case None => Redirect(routes.Puzzle.home)
case Some(puz) => Redirect(routes.Puzzle.show(puz.id.value))
}
}
}

def showWithAngle(angleKey: String, id: PuzzleId) = Open { implicit ctx =>
NoBot {
val angle = PuzzleAngle.findOrMix(angleKey)
Expand Down Expand Up @@ -374,10 +418,12 @@ final class Puzzle(env: Env, apiC: => Api) extends LilaController(env):
Action.async { implicit req =>
env.puzzle.daily.get map {
case None => NotFound
case Some(daily) => html.puzzle.embed(daily)
case Some(daily) => html.puzzle.dailyEmbed(daily)
}
}

def embed(angleOrId: String) = Open(serveEmbed(angleOrId)(_))

def activity =
Scoped(_.Puzzle.Read) { req => me =>
val config = lila.puzzle.PuzzleActivity.Config(
Expand Down
13 changes: 11 additions & 2 deletions app/views/base/layout.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ object layout:
<a data-icon="" id="zentog" class="text fbt active">${trans.preferences.zenMode.txt()}</a>
</div>""")

private def getZenZone(zen: Boolean)(implicit ctx: Context): Option[scalatags.Text.RawFrag] =
{
if (zen)
return None
else
return Some(zenZone)
}

private def dasher(me: lila.user.User) =
div(cls := "dasher")(
a(id := "user_tag", cls := "toggle link", href := routes.Auth.logoutGet)(me.username),
Expand Down Expand Up @@ -232,6 +240,7 @@ object layout:
chessground: Boolean = true,
zoomable: Boolean = false,
zenable: Boolean = false,
zen: Boolean = false,
csp: Option[ContentSecurityPolicy] = None,
wrapClass: String = "",
atomLinkTag: Option[Tag] = None,
Expand Down Expand Up @@ -291,7 +300,7 @@ object layout:
baseClass -> true,
"dark-board" -> (ctx.pref.bg == lila.pref.Pref.Bg.DARKBOARD),
"piece-letter" -> ctx.pref.pieceNotationIsLetter,
"zen" -> ctx.pref.isZen,
"zen" -> (if (zen) zen else ctx.pref.isZen),
"blind-mode" -> ctx.blind,
"kid" -> ctx.kid,
"mobile" -> ctx.isMobileBrowser,
Expand Down Expand Up @@ -322,7 +331,7 @@ object layout:
.get(ctx.req)
.ifTrue(ctx.isAnon)
.map(views.html.auth.bits.checkYourEmailBanner(_)),
zenable option zenZone,
zenable option getZenZone(zen),
siteHeader.apply,
div(
id := "main-wrap",
Expand Down
2 changes: 1 addition & 1 deletion app/views/lobby/home.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ object home:
)
},
puzzle map { p =>
views.html.puzzle.embed.dailyLink(p)(ctx.lang)(cls := "lobby__puzzle")
views.html.puzzle.dailyEmbed.dailyLink(p)(ctx.lang)(cls := "lobby__puzzle")
},
ctx.noBot option bits.underboards(tours, simuls, leaderboard, tournamentWinners),
bits.lastPosts(lastPost, ublogPosts),
Expand Down
35 changes: 35 additions & 0 deletions app/views/puzzle/dailyEmbed.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package views.html.puzzle

import controllers.routes
import play.api.i18n.Lang

import lila.app.templating.Environment.{ given, * }
import lila.app.ui.EmbedConfig
import lila.app.ui.ScalatagsTemplate.{ *, given }
import lila.puzzle.DailyPuzzle

object dailyEmbed:

import EmbedConfig.implicits.*

def apply(daily: DailyPuzzle.WithHtml)(implicit config: EmbedConfig) =
views.html.base.embed(
title = "lichess.org chess puzzle",
cssModule = "tv.embed"
)(
dailyLink(daily)(config.lang)(
targetBlank,
id := "daily-puzzle",
cls := "embedded"
),
jsModule("puzzle.embed")
)

def dailyLink(daily: DailyPuzzle.WithHtml)(implicit lang: Lang) = a(
href := routes.Puzzle.daily,
title := trans.puzzle.clickToSolve.txt()
)(
span(cls := "text")(trans.puzzle.puzzleOfTheDay()),
raw(daily.html),
span(cls := "text")(daily.puzzle.color.fold(trans.whitePlays, trans.blackPlays)())
)
100 changes: 75 additions & 25 deletions app/views/puzzle/embed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,84 @@ package views.html.puzzle

import controllers.routes
import play.api.i18n.Lang
import play.api.libs.json.{ JsObject, Json }

import lila.app.templating.Environment.{ given, * }
import lila.api.Context
import lila.app.templating.Environment._
import lila.app.ui.EmbedConfig
import lila.app.ui.ScalatagsTemplate.{ *, given }
import lila.puzzle.DailyPuzzle
import lila.app.ui.ScalatagsTemplate._
import lila.common.Json.colorWrites
import lila.common.String.html.safeJsonValue

object embed:
object embed {

import EmbedConfig.implicits.*
import EmbedConfig.implicits._

def apply(daily: DailyPuzzle.WithHtml)(implicit config: EmbedConfig) =
views.html.base.embed(
title = "lichess.org chess puzzle",
cssModule = "tv.embed"
)(
dailyLink(daily)(config.lang)(
targetBlank,
id := "daily-puzzle",
cls := "embedded"
def apply(
puzzle: lila.puzzle.Puzzle,
data: JsObject,
pref: JsObject,
settings: lila.puzzle.PuzzleSettings,
langPath: Option[lila.common.LangPath] = None
)(implicit ctx: Context) = {
val isStreak = data.value.contains("streak")
views.html.base.layout(
title = if (isStreak) "Puzzle Streak" else trans.puzzles.txt(),
zen = true,
moreCss = frag(
cssTag("puzzle"),
ctx.pref.hasKeyboardMove option cssTag("keyboardMove"),
ctx.blind option cssTag("round.nvui")
),
jsModule("puzzle.embed")
)

def dailyLink(daily: DailyPuzzle.WithHtml)(implicit lang: Lang) = a(
href := routes.Puzzle.daily,
title := trans.puzzle.clickToSolve.txt()
)(
span(cls := "text")(trans.puzzle.puzzleOfTheDay()),
raw(daily.html),
span(cls := "text")(daily.puzzle.color.fold(trans.whitePlays, trans.blackPlays)())
)
moreJs = frag(
puzzleTag,
puzzleNvuiTag,
embedJsUnsafeLoadThen(s"""LichessPuzzle(${safeJsonValue(
Json
.obj(
"data" -> data,
"pref" -> pref,
"i18n" -> bits.jsI18n(streak = isStreak),
"showRatings" -> ctx.pref.showRatings,
"settings" -> Json.obj("difficulty" -> settings.difficulty.key).add("color" -> settings.color)
)
.add("themes" -> ctx.isAuth.option(bits.jsonThemes))
)})""")
),
csp = analysisCsp.some,
chessground = false,
openGraph = lila.app.ui
.OpenGraph(
image = cdnUrl(
routes.Export.puzzleThumbnail(puzzle.id.value, ctx.pref.theme.some, ctx.pref.pieceSet.some).url
).some,
title =
if (isStreak) "Puzzle Streak"
else s"Chess tactic #${puzzle.id} - ${puzzle.color.name.capitalize} to play",
url = s"$netBaseUrl${routes.Puzzle.show(puzzle.id.value).url}",
description =
if (isStreak) trans.puzzle.streakDescription.txt()
else
s"Lichess tactic trainer: ${puzzle.color
.fold(
trans.puzzle.findTheBestMoveForWhite,
trans.puzzle.findTheBestMoveForBlack
)
.txt()}. Played by ${puzzle.plays} players."
)
.some,
zoomable = true,
zenable = true,
withHrefLangs = langPath
) {
main(cls := "puzzle")(
st.aside(cls := "puzzle__side")(
div(cls := "puzzle__side__metas")
),
div(cls := "puzzle__board main-board")(chessgroundBoard),
div(cls := "puzzle__tools"),
div(cls := "puzzle__controls")
)
}
}
}
1 change: 1 addition & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ GET /training/new controllers.Puzzle.mobileBcNew
GET /training/$numericalId<\d{6,}>/load controllers.Puzzle.mobileBcLoad(numericalId: Long)
POST /training/$numericalId<\d{6,}>/vote controllers.Puzzle.mobileBcVote(numericalId: Long)
GET /training/:angleOrId controllers.Puzzle.show(angleOrId)
GET /training/embed/:angleOrId controllers.Puzzle.embed(angleOrId)
GET /training/:angle/$color<white|black|random> controllers.Puzzle.angleAndColor(angle, color)
GET /training/:angle/$id<\w{5}> controllers.Puzzle.showWithAngle(angle, id)
POST /training/$numericalId<\d{6,}>/round2 controllers.Puzzle.mobileBcRound(numericalId: Long)
Expand Down