Skip to content

Commit

Permalink
better 404 embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Sep 3, 2024
1 parent ead4f37 commit 017d1b9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/controllers/RelayRound.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ final class RelayRound(
def embedShow(ts: String, rs: String, id: RelayRoundId): EssentialAction =
Anon:
InEmbedContext:
Found(env.relay.api.byIdWithTour(id))(embedShow)
FoundEmbed(env.relay.api.byIdWithTour(id))(embedShow)

def embedShow(rt: RoundModel.WithTour)(using EmbedContext): Fu[Result] =
env.study.preview
.firstId(rt.round.studyId)
.flatMapz(env.study.api.byIdWithChapterOrFallback(rt.round.studyId, _))
.orNotFound: oldSc =>
.orNotFoundEmbed: oldSc =>
studyC.CanView(oldSc.study)(
for
(sc, studyData) <- studyC.getJsonData(oldSc)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/RelayTour.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ final class RelayTour(env: Env, apiC: => Api, roundC: => RelayRound) extends Lil

def embedShow(slug: String, id: RelayTourId) = Anon:
InEmbedContext:
Found(env.relay.api.tourById(id)): tour =>
FoundEmbed(env.relay.api.tourById(id)): tour =>
env.relay.listing.defaultRoundToShow
.get(tour.id)
.flatMap:
Expand Down
3 changes: 3 additions & 0 deletions app/http/KeyPages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ final class KeyPages(val env: Env)(using Executor)
def notFound(msg: Option[String])(using Context): Fu[Result] =
NotFound.page(views.base.notFound(msg))

def notFoundEmbed(msg: Option[String])(using EmbedContext): Result =
NotFound.snip(views.base.notFoundEmbed(msg))

def blacklisted(using ctx: Context): Result =
if lila.security.Mobile.Api.requested(ctx.req) then
Results.Unauthorized:
Expand Down
9 changes: 9 additions & 0 deletions app/http/ResponseBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ trait ResponseBuilder(using Executor)
def Found[A](a: Fu[Option[A]])(f: A => Fu[Result])(using Context): Fu[Result] =
a.flatMap(_.fold(notFound)(f))

def FoundEmbed[A](a: Fu[Option[A]])(f: A => Fu[Result])(using EmbedContext): Fu[Result] =
a.flatMap(_.fold(notFoundEmbed)(f))

def Found[A](a: Option[A])(f: A => Fu[Result])(using Context): Fu[Result] =
a.fold(notFound)(f)

Expand All @@ -41,6 +44,8 @@ trait ResponseBuilder(using Executor)
extension [A](fua: Fu[Option[A]])
def orNotFound(f: A => Fu[Result])(using Context): Fu[Result] =
fua.flatMap { _.fold(notFound)(f) }
def orNotFoundEmbed(f: A => Fu[Result])(using EmbedContext): Fu[Result] =
fua.flatMap { _.fold(notFoundEmbed)(f) }
extension [A](fua: Fu[Boolean])
def elseNotFound(f: => Fu[Result])(using Context): Fu[Result] =
fua.flatMap { if _ then f else notFound }
Expand Down Expand Up @@ -82,6 +87,10 @@ trait ResponseBuilder(using Executor)
json = msg.fold(notFoundJson())(notFoundJson)
)

def notFoundEmbed(using ctx: EmbedContext): Fu[Result] = notFoundEmbed(none)
def notFoundEmbed(msg: Option[String])(using ctx: EmbedContext): Fu[Result] =
keyPages.notFoundEmbed(msg)

def authenticationFailed(using ctx: Context): Fu[Result] =
negotiate(
html = Redirect(
Expand Down
4 changes: 1 addition & 3 deletions app/views/base/embed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ object embed:
modules: EsmList = Nil,
pageModule: Option[PageModule] = None,
csp: Update[ContentSecurityPolicy] = identity
)(body: Modifier*)(using
ctx: EmbedContext
) = lila.ui.Snippet:
)(body: Modifier*)(using ctx: EmbedContext) = lila.ui.Snippet:
val allModules = modules ++ pageModule.so(module => jsPageModule(module.name))
frag(
page.ui.doctype,
Expand Down
13 changes: 13 additions & 0 deletions app/views/base/notFound.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,16 @@ def notFound(msg: Option[String])(using Context) =
)
)
)

def notFoundEmbed(msg: Option[String])(using EmbedContext) =
views.base.embed.site(title = msg | "Page not found", cssKeys = List("bits.embed-not-found"))(
main(cls := "not-found page-small box box-pad")(
header(
h1("404"),
div(
strong("Page not found!"),
msg.map(em(_))
)
)
)
)
5 changes: 5 additions & 0 deletions ui/bits/css/build/bits.not-found-embed.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import '../../../common/css/embed';
@import '../../../common/css/base/util';
@import '../../../common/css/component/box';

@import '../not-found';

0 comments on commit 017d1b9

Please sign in to comment.