Skip to content

Commit

Permalink
Merge pull request lichess-org#14677 from schlawg/cf-socket-pref
Browse files Browse the repository at this point in the history
add cf-socket toggle to prefs
  • Loading branch information
ornicar authored Feb 15, 2024
2 parents aea0cd7 + e2e7dad commit 39c3f33
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 49 deletions.
6 changes: 6 additions & 0 deletions app/controllers/Account.scala
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ final class Account(
Redirect(routes.Account.twoFactor).flashSuccess
}

def network(usingAltSocket: Option[Boolean]) = Auth { _ ?=> _ ?=>
val page = (use: Option[Boolean]) => Ok.page(html.account.network(use))
if usingAltSocket.isEmpty || usingAltSocket.has(ctx.pref.isUsingAltSocket) then page(none)
else env.pref.api.setPref(ctx.pref.copy(usingAltSocket = usingAltSocket)) >> page(usingAltSocket)
}

def close = Auth { _ ?=> me ?=>
env.clas.api.student.isManaged(me) flatMap { managed =>
env.security.forms.closeAccount.flatMap: form =>
Expand Down
13 changes: 8 additions & 5 deletions app/views/account/layout.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,19 @@ object layout:
a(activeCls("twofactor"), href := routes.Account.twoFactor)(
trans.tfa.twoFactorAuth()
),
a(activeCls("oauth.token"), href := routes.OAuthToken.index)(
trans.oauthScope.apiAccessTokens()
),
a(activeCls("security"), href := routes.Account.security)(
trans.security()
),
div(cls := "sep"),
a(href := routes.Plan.index)(trans.patron.lichessPatron()),
div(cls := "sep"),
a(activeCls("oauth.token"), href := routes.OAuthToken.index)(
trans.oauthScope.apiAccessTokens()
a(activeCls("network"), href := routes.Account.network(none))(
"Network"
),
ctx.noBot option a(href := routes.DgtCtrl.index)(
trans.dgt.dgtBoard()
),
ctx.noBot option a(href := routes.DgtCtrl.index)(trans.dgt.dgtBoard()),
div(cls := "sep"),
a(activeCls("close"), href := routes.Account.close)(
trans.settings.closeAccount()
Expand Down
43 changes: 43 additions & 0 deletions app/views/account/network.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package views.html
package account

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

import controllers.routes

object network:

def apply(cfRouting: Option[Boolean])(using ctx: PageContext) =
account.layout(
title = "Network",
active = "network"
):
val usingCloudflare = cfRouting.getOrElse(ctx.pref.isUsingAltSocket)
div(cls := "box box-pad")(
h1(cls := "box__top")("Network"),
br,
if usingCloudflare then
frag(
flashMessage("warning")("You are currently using Content Delivery Network (CDN) routing."),
p("This feature is experimental but may improve reliability in some regions.")
)
else
p("If you have frequent disconnects, Content Delivery Network (CDN) routing may improve things.")
,
br,
st.section(a(href := "#routing")(h2(id := "routing")("Network Routing")))(
st.group(cls := "radio"):
List(("Use direct routing", false), ("Use CDN routing", true)) map: (key, value) =>
div(
a(value != usingCloudflare option (href := routes.Account.network(value.some)))(
label(value == usingCloudflare option (cls := "active-soft"))(key)
)
)
),
br,
br,
cfRouting.nonEmpty option p(cls := "saved text", dataIcon := licon.Checkmark)(
trans.preferences.yourPreferencesHaveBeenSaved()
)
)
2 changes: 2 additions & 0 deletions app/views/base/layout.scala
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ object layout:
)

private val dataVapid = attr("data-vapid")
private val dataAltSocket = attr("data-alt-socket")
private val dataSocketDomains = attr("data-socket-domains") := netConfig.socketDomains.mkString(",")
private val dataNonce = attr("data-nonce")
private val dataAnnounce = attr("data-announce")
Expand Down Expand Up @@ -308,6 +309,7 @@ object layout:
dataVapid := (ctx.isAuth && env.lilaCookie.isRememberMe(ctx.req)) option vapidPublicKey,
dataUser := ctx.userId,
dataSoundSet := pref.currentSoundSet.toString,
pref.isUsingAltSocket option (dataAltSocket := netConfig.altSocket.value),
dataSocketDomains,
dataAssetUrl,
dataAssetVersion := assetVersion,
Expand Down
1 change: 1 addition & 0 deletions conf/base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mongodb {
net {
domain = "localhost:9663"
socket.domains = [ "localhost:9664" ]
socket.alternate = ""
asset.domain = ${net.domain}
asset.base_url = "http://"${net.asset.domain}
asset.base_url_internal = ${net.asset.base_url}
Expand Down
2 changes: 2 additions & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,8 @@ POST /account/reopen/send controllers.Account.reopenApply
GET /account/reopen/sent controllers.Account.reopenSent
GET /account/reopen/login/:token controllers.Account.reopenLogin(token)
GET /account/personal-data controllers.Account.data
GET /account/network controllers.Account.network(usingAltSocket: Option[Boolean] ?= None)

# App BC
GET /account/security controllers.Account.security
POST /account/signout/:sessionId controllers.Account.signout(sessionId)
Expand Down
1 change: 1 addition & 0 deletions modules/common/src/main/config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ object config:
@ConfigName("stage.banner") stageBanner: Boolean,
@ConfigName("site.name") siteName: String,
@ConfigName("socket.domains") socketDomains: List[String],
@ConfigName("socket.alternate") altSocket: NetDomain,
crawlable: Boolean,
@ConfigName("ratelimit") rateLimit: RateLimit,
email: EmailAddress
Expand Down
6 changes: 5 additions & 1 deletion modules/pref/src/main/Pref.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ case class Pref(
pieceNotation: Int,
resizeHandle: Int,
agreement: Int,
usingAltSocket: Option[Boolean],
tags: Map[String, String] = Map.empty
):

Expand Down Expand Up @@ -94,7 +95,9 @@ case class Pref(

def hasKeyboardMove = keyboardMove == KeyboardMove.YES

def hasVoice = voice.contains(Voice.YES)
def hasVoice = voice.has(Voice.YES)

def isUsingAltSocket = usingAltSocket.has(true)

// atob("aHR0cDovL2NoZXNzLWNoZWF0LmNvbS9ob3dfdG9fY2hlYXRfYXRfbGljaGVzcy5odG1s")
def botCompatible =
Expand Down Expand Up @@ -464,6 +467,7 @@ object Pref:
pieceNotation = PieceNotation.SYMBOL,
resizeHandle = ResizeHandle.INITIAL,
agreement = Agreement.current,
usingAltSocket = none,
tags = Map.empty
)

Expand Down
84 changes: 43 additions & 41 deletions modules/pref/src/main/PrefHandlers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,50 +51,52 @@ private object PrefHandlers:
resizeHandle = r.getD("resizeHandle", Pref.default.resizeHandle),
moveEvent = r.getD("moveEvent", Pref.default.moveEvent),
agreement = r.getD("agreement", 0),
usingAltSocket = r.getO("usingAltSocket"),
tags = r.getD("tags", Pref.default.tags)
)

def writes(w: BSON.Writer, o: Pref) =
$doc(
"_id" -> o._id,
"bg" -> o.bg,
"bgImg" -> o.bgImg,
"is3d" -> o.is3d,
"theme" -> o.theme,
"pieceSet" -> o.pieceSet,
"theme3d" -> o.theme3d,
"pieceSet3d" -> o.pieceSet3d,
"soundSet" -> SoundSet.name2key(o.soundSet),
"autoQueen" -> o.autoQueen,
"autoThreefold" -> o.autoThreefold,
"takeback" -> o.takeback,
"moretime" -> o.moretime,
"clockTenths" -> o.clockTenths,
"clockBar" -> o.clockBar,
"clockSound" -> o.clockSound,
"premove" -> o.premove,
"animation" -> o.animation,
"captured" -> o.captured,
"follow" -> o.follow,
"highlight" -> o.highlight,
"destination" -> o.destination,
"coords" -> o.coords,
"replay" -> o.replay,
"challenge" -> o.challenge,
"message" -> o.message,
"studyInvite" -> o.studyInvite,
"submitMove" -> o.submitMove,
"confirmResign" -> o.confirmResign,
"insightShare" -> o.insightShare,
"keyboardMove" -> o.keyboardMove,
"voice" -> o.voice,
"zen" -> o.zen,
"ratings" -> o.ratings,
"flairs" -> o.flairs,
"rookCastle" -> o.rookCastle,
"moveEvent" -> o.moveEvent,
"pieceNotation" -> o.pieceNotation,
"resizeHandle" -> o.resizeHandle,
"agreement" -> o.agreement,
"tags" -> o.tags
"_id" -> o._id,
"bg" -> o.bg,
"bgImg" -> o.bgImg,
"is3d" -> o.is3d,
"theme" -> o.theme,
"pieceSet" -> o.pieceSet,
"theme3d" -> o.theme3d,
"pieceSet3d" -> o.pieceSet3d,
"soundSet" -> SoundSet.name2key(o.soundSet),
"autoQueen" -> o.autoQueen,
"autoThreefold" -> o.autoThreefold,
"takeback" -> o.takeback,
"moretime" -> o.moretime,
"clockTenths" -> o.clockTenths,
"clockBar" -> o.clockBar,
"clockSound" -> o.clockSound,
"premove" -> o.premove,
"animation" -> o.animation,
"captured" -> o.captured,
"follow" -> o.follow,
"highlight" -> o.highlight,
"destination" -> o.destination,
"coords" -> o.coords,
"replay" -> o.replay,
"challenge" -> o.challenge,
"message" -> o.message,
"studyInvite" -> o.studyInvite,
"submitMove" -> o.submitMove,
"confirmResign" -> o.confirmResign,
"insightShare" -> o.insightShare,
"keyboardMove" -> o.keyboardMove,
"voice" -> o.voice,
"zen" -> o.zen,
"ratings" -> o.ratings,
"flairs" -> o.flairs,
"rookCastle" -> o.rookCastle,
"moveEvent" -> o.moveEvent,
"pieceNotation" -> o.pieceNotation,
"resizeHandle" -> o.resizeHandle,
"agreement" -> o.agreement,
"usingAltSocket" -> o.usingAltSocket,
"tags" -> o.tags
)
7 changes: 6 additions & 1 deletion ui/site/css/_account.scss
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
margin-bottom: 4rem;
}

form section a {
section a {
text-decoration: none;
color: inherit;
}
Expand Down Expand Up @@ -238,6 +238,11 @@
}
}

.active-soft,
.active-soft:hover {
@extend %active-soft;
}

@include breakpoint($mq-not-xx-small) {
td.icon {
display: none;
Expand Down
2 changes: 1 addition & 1 deletion ui/site/src/component/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ export default class StrongSocket {
};

baseUrl = () => {
if (lichess.storage.get('socket.host')) return lichess.storage.get('socket.host'); // TODO - remove
if (document.body.dataset.altSocket) return document.body.dataset.altSocket;
let url = this.storage.get();
if (!url) {
url = this.baseUrls[Math.floor(Math.random() * this.baseUrls.length)];
Expand Down

0 comments on commit 39c3f33

Please sign in to comment.