redirect /username to /@/username when possible

but never actually link to it, because these URLs may break
when we add root-level URLs or when a game ID is generated.
notfound-user-redirect
Thibault Duplessis 2020-08-19 18:18:54 +02:00
parent c0eca22058
commit 849fed2a5c
3 changed files with 18 additions and 5 deletions

View File

@ -2,16 +2,16 @@ package controllers
import play.api.libs.json._
import play.api.mvc._
import views._
import lila.api.Context
import lila.app._
import lila.chat.Chat
import lila.common.HTTPRequest
import lila.game.{ Pov, Game => GameModel, PgnDump }
import lila.tournament.{ Tournament => Tour }
import lila.swiss.Swiss.{ Id => SwissId }
import lila.tournament.{ Tournament => Tour }
import lila.user.{ User => UserModel }
import views._
final class Round(
env: Env,
@ -19,7 +19,8 @@ final class Round(
challengeC: => Challenge,
analyseC: => Analyse,
tournamentC: => Tournament,
swissC: => Swiss
swissC: => Swiss,
userC: => User
) extends LilaController(env)
with TheftPrevention {
@ -143,7 +144,7 @@ final class Round(
case None =>
watch(pov)
}
case None => challengeC showId gameId
case None => userC.tryRedirect(gameId) getOrElse challengeC.showId(gameId)
}
}

View File

@ -10,6 +10,7 @@ import scala.concurrent.duration._
import scala.language.existentials
import scala.util.chaining._
import scalatags.Text.Frag
import views._
import lila.api.{ BodyContext, Context }
import lila.app._
@ -20,7 +21,6 @@ import lila.game.{ Pov, Game => GameModel }
import lila.rating.PerfType
import lila.socket.UserLagCache
import lila.user.{ User => UserModel }
import views._
final class User(
env: Env,
@ -552,4 +552,14 @@ final class User(
Auth { _ => me =>
fuccess(Redirect(routes.User.show(me.username)))
}
def redirect(username: String) =
Open { implicit ctx =>
tryRedirect(username) getOrElse notFound
}
def tryRedirect(username: String)(implicit ctx: Context): Fu[Option[Result]] =
env.user.repo named username map2 { user =>
Redirect(routes.User.show(user.username))
}
}

View File

@ -719,5 +719,7 @@ GET /assets/*file controllers.ExternalAssets.at(path="publi
GET /manifest.json controllers.Main.manifest
GET /robots.txt controllers.Main.robots
GET /$username<\w{2,30}> controllers.User.redirect(username: String)
OPTIONS / controllers.Options.root
OPTIONS /*url controllers.Options.all(url: String)