Skip to content

Commit

Permalink
redirect to the default study chapter when the chapter id is wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jun 12, 2023
1 parent c134cd0 commit 71188f0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
7 changes: 3 additions & 4 deletions app/controllers/RelayRound.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ final class RelayRound(
if rt.round.sync.ongoing then
env.study.chapterRepo relaysAndTagsByStudyId rt.round.studyId flatMap { chapters =>
chapters.find(_.looksAlive) orElse chapters.headOption match {
case Some(chapter) => env.study.api.byIdWithChapter(rt.round.studyId, chapter.id)
case Some(chapter) => env.study.api.byIdWithChapterOrFallback(rt.round.studyId, chapter.id)
case None => env.study.api byIdWithChapter rt.round.studyId
}
}
Expand Down Expand Up @@ -162,9 +162,8 @@ final class RelayRound(
}

def chapter(ts: String, rs: String, id: RelayRoundId, chapterId: StudyChapterId) = Open:
WithRoundAndTour(ts, rs, id) { rt =>
env.study.api.byIdWithChapter(rt.round.studyId, chapterId) flatMapz { doShow(rt, _) }
}
WithRoundAndTour(ts, rs, id): rt =>
env.study.api.byIdWithChapterOrFallback(rt.round.studyId, chapterId) flatMapz { doShow(rt, _) }

def push(id: RelayRoundId) = ScopedBody(parse.tolerantText)(Seq(_.Study.Write)) { ctx ?=> me =>
env.relay.api
Expand Down
19 changes: 13 additions & 6 deletions app/controllers/Study.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ final class Study(
.withCanonical(routes.Study.chapter(sc.study.id, sc.chapter.id))
.enableSharedArrayBuffer,
api = _ =>
chatOf(sc.study).map { chatOpt =>
Ok(
chatOf(sc.study).map: chatOpt =>
Ok:
Json.obj(
"study" -> data.study.add("chat" -> chatOpt.map { c =>
lila.chat.JsonView.mobile(
Expand All @@ -185,8 +185,6 @@ final class Study(
}),
"analysis" -> data.analysis
)
)
}
)
yield res
}(privateUnauthorizedFu(oldSc.study), privateForbiddenFu(oldSc.study))
Expand Down Expand Up @@ -231,7 +229,16 @@ final class Study(
def chapter(id: StudyId, chapterId: StudyChapterId) =
Open:
orRelay(id, chapterId.some):
showQuery(env.study.api.byIdWithChapter(id, chapterId))
env.study.api
.byIdWithChapter(id, chapterId)
.flatMap:
case None =>
env.study.studyRepo
.exists(id)
.flatMap:
if _ then Redirect(routes.Study.show(id)).toFuccess
else showQuery(fuccess(none))
case sc => showQuery(fuccess(sc))

def chapterMeta(id: StudyId, chapterId: StudyChapterId) = Open:
env.study.chapterRepo.byId(chapterId).map {
Expand Down Expand Up @@ -326,7 +333,7 @@ final class Study(
def embed(id: StudyId, chapterId: StudyChapterId) = Anon:
val studyWithChapter =
if (chapterId.value == "autochap") env.study.api.byIdWithChapter(id)
else env.study.api.byIdWithChapter(id, chapterId)
else env.study.api.byIdWithChapterOrFallback(id, chapterId)
studyWithChapter.map(_.filterNot(_.study.isPrivate)) flatMap {
_.fold(embedNotFound) { case WithChapter(study, chapter) =>
for
Expand Down
4 changes: 2 additions & 2 deletions modules/practice/src/main/PracticeApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class PracticeApi(
chapters <- studyApi.chapterMetadatas(studyId)
chapter = up.progress firstOngoingIn chapters
studyOption <- chapter.fold(studyApi byIdWithFirstChapter studyId) { chapter =>
studyApi.byIdWithChapter(studyId, chapter.id)
studyApi.byIdWithChapterOrFallback(studyId, chapter.id)
}
yield makeUserStudy(studyOption, up, chapters)

Expand All @@ -38,7 +38,7 @@ final class PracticeApi(
): Fu[Option[UserStudy]] = for
up <- get(user)
chapters <- studyApi.chapterMetadatas(studyId)
studyOption <- studyApi.byIdWithChapter(studyId, chapterId)
studyOption <- studyApi.byIdWithChapterOrFallback(studyId, chapterId)
yield makeUserStudy(studyOption, up, chapters)

private def makeUserStudy(
Expand Down
5 changes: 4 additions & 1 deletion modules/study/src/main/StudyApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ final class StudyApi(
}

def byIdWithChapter(id: StudyId, chapterId: StudyChapterId): Fu[Option[Study.WithChapter]] =
studyRepo.byIdWithChapter(chapterRepo.coll)(id, chapterId) orElse byIdWithChapter(id)
studyRepo.byIdWithChapter(chapterRepo.coll)(id, chapterId)

def byIdWithChapterOrFallback(id: StudyId, chapterId: StudyChapterId): Fu[Option[Study.WithChapter]] =
byIdWithChapter(id, chapterId) orElse byIdWithChapter(id)

def byIdWithFirstChapter(id: StudyId): Fu[Option[Study.WithChapter]] =
byIdWithChapterFinder(id, chapterRepo firstByStudy id)
Expand Down

0 comments on commit 71188f0

Please sign in to comment.