Skip to content

Commit

Permalink
study/broadcast chapter XHR: only include chapters when ?chapters=1
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Sep 24, 2024
1 parent 9979af1 commit f718e30
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 58 deletions.
2 changes: 1 addition & 1 deletion app/controllers/Practice.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class Practice(
private def analysisJson(us: UserStudy)(using Context): Fu[(JsObject, JsObject)] = us match
case UserStudy(_, _, chapters, WithChapter(study, chapter), _) =>
for
studyJson <- env.study.jsonView.full(study, chapters, chapter, none, withMembers = false)
studyJson <- env.study.jsonView.full(study, chapter, chapters.some, none, withMembers = false)
initialFen = chapter.root.fen.some
pov = userAnalysisC.makePov(initialFen, chapter.setup.variant)
baseData = env.round.jsonView
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/RelayRound.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ final class RelayRound(
.orNotFoundEmbed: oldSc =>
studyC.CanView(oldSc.study)(
for
(sc, studyData) <- studyC.getJsonData(oldSc)
(sc, studyData) <- studyC.getJsonData(oldSc, withChapters = true)
rounds <- env.relay.api.byTourOrdered(rt.tour)
group <- env.relay.api.withTours.get(rt.tour.id)
data = env.relay.jsonView.makeData(
Expand Down Expand Up @@ -266,7 +266,7 @@ final class RelayRound(
): Fu[Result] =
studyC.CanView(oldSc.study)(
for
(sc, studyData) <- studyC.getJsonData(oldSc)
(sc, studyData) <- studyC.getJsonData(oldSc, withChapters = true)
rounds <- env.relay.api.byTourOrdered(rt.tour)
group <- env.relay.api.withTours.get(rt.tour.id)
isSubscribed <- ctx.me.soFu: me =>
Expand Down
61 changes: 30 additions & 31 deletions app/controllers/Study.scala
Original file line number Diff line number Diff line change
Expand Up @@ -190,42 +190,41 @@ final class Study(
private def showQuery(query: Fu[Option[WithChapter]])(using ctx: Context): Fu[Result] =
Found(query): oldSc =>
CanView(oldSc.study) {
for
(sc, data) <- getJsonData(oldSc)
res <- negotiate(
html =
val noCrawler = HTTPRequest.isCrawler(ctx.req).no
for
chat <- noCrawler.so(chatOf(sc.study))
sVersion <- noCrawler.so(env.study.version(sc.study.id))
streamers <- noCrawler.so(streamerCache.get(sc.study.id))
page <- renderPage(views.study.show(sc.study, data, chat, sVersion, streamers))
yield Ok(page)
.withCanonical(routes.Study.chapter(sc.study.id, sc.chapter.id))
.enforceCrossSiteIsolation
,
json = for
chatOpt <- chatOf(sc.study)
jsChat <- chatOpt.soFu: c =>
lila.chat.JsonView.mobile(c.chat, writeable = ctx.userId.so(sc.study.canChat))
yield Ok:
Json.obj(
"study" -> data.study.add("chat" -> jsChat),
"analysis" -> data.analysis
)
)
yield res
negotiate(
html =
val noCrawler = HTTPRequest.isCrawler(ctx.req).no
for
(sc, data) <- getJsonData(oldSc, withChapters = true)
chat <- noCrawler.so(chatOf(sc.study))
sVersion <- noCrawler.so(env.study.version(sc.study.id))
streamers <- noCrawler.so(streamerCache.get(sc.study.id))
page <- renderPage(views.study.show(sc.study, data, chat, sVersion, streamers))
yield Ok(page)
.withCanonical(routes.Study.chapter(sc.study.id, sc.chapter.id))
.enforceCrossSiteIsolation
,
json = for
(sc, data) <- getJsonData(oldSc, withChapters = getBool("chapters"))
chatOpt <- chatOf(sc.study)
jsChat <- chatOpt.soFu: c =>
lila.chat.JsonView.mobile(c.chat, writeable = ctx.userId.so(sc.study.canChat))
yield Ok:
Json.obj(
"study" -> data.study.add("chat" -> jsChat),
"analysis" -> data.analysis
)
)
}(privateUnauthorizedFu(oldSc.study), privateForbiddenFu(oldSc.study))
.dmap(_.noCache)

private[controllers] def getJsonData(sc: WithChapter)(using
private[controllers] def getJsonData(sc: WithChapter, withChapters: Boolean)(using
ctx: Context
): Fu[(WithChapter, JsData)] =
for
(study, chapter, previews) <-
env.study.api.maybeResetAndGetChapterPreviews(sc.study, sc.chapter)
_ <- env.user.lightUserApi.preloadMany(study.members.ids.toList)
fedNames <- env.study.preview.federations.get(sc.study.id)
(study, chapter) <- env.study.api.maybeResetAndGetChapter(sc.study, sc.chapter)
previews <- withChapters.soFu(env.study.preview.jsonList(study.id))
_ <- env.user.lightUserApi.preloadMany(study.members.ids.toList)
fedNames <- env.study.preview.federations.get(sc.study.id)
pov = userAnalysisC.makePov(chapter.root.fen.some, chapter.setup.variant)
analysis <- chapter.serverEval
.exists(_.done)
Expand All @@ -242,7 +241,7 @@ final class Study(
)
)
withMembers = !study.isRelay || isGrantedOpt(_.StudyAdmin) || ctx.me.exists(study.isMember)
studyJson <- env.study.jsonView.full(study, previews, chapter, fedNames.some, withMembers = withMembers)
studyJson <- env.study.jsonView.full(study, chapter, previews, fedNames.some, withMembers = withMembers)
yield WithChapter(study, chapter) -> JsData(
study = studyJson,
analysis = baseData
Expand Down
6 changes: 3 additions & 3 deletions modules/study/src/main/JsonView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ final class JsonView(

def full(
study: Study,
previews: ChapterPreview.AsJsons,
chapter: Chapter,
previews: Option[ChapterPreview.AsJsons],
fedNames: Option[JsObject],
withMembers: Boolean
)(using me: Option[Me]) =
Expand Down Expand Up @@ -46,8 +46,7 @@ final class JsonView(
)
.add("sticky", study.settings.sticky)
.add("description", study.settings.description),
"topics" -> study.topicsOrEmpty,
"chapters" -> previews,
"topics" -> study.topicsOrEmpty,
"chapter" -> Json
.obj(
"id" -> chapter.id,
Expand All @@ -64,6 +63,7 @@ final class JsonView(
.add("relayPath", relayPath)
.pipe(addChapterMode(chapter))
)
.add("chapters", previews)
.add("description", study.description)
.add("federations", fedNames)

Expand Down
31 changes: 14 additions & 17 deletions modules/study/src/main/StudyApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,28 +172,25 @@ final class StudyApi(

export preview.dataList.{ apply as chapterPreviews }

def maybeResetAndGetChapterPreviews(
study: Study,
chapter: Chapter
): Fu[(Study, Chapter, ChapterPreview.AsJsons)] =
preview
.jsonList(study.id)
.flatMap: previews =>
val defaultResult = (study, chapter, previews)
if study.isRelay || !study.isOld || study.position == chapter.initialPosition
then fuccess(defaultResult)
else
ChapterPreview.json.readFirstId(previews) match
case Some(firstId) =>
val newStudy = study.rewindTo(firstId)
def maybeResetAndGetChapter(study: Study, chapter: Chapter): Fu[(Study, Chapter)] =
val defaultResult = (study, chapter)
if study.isRelay || !study.isOld || study.position == chapter.initialPosition
then fuccess(defaultResult)
else
preview
.dataList(study.id)
.flatMap:
_.headOption match
case Some(first) =>
val newStudy = study.rewindTo(first.id)
if newStudy == study then fuccess(defaultResult)
else
logger.info(s"Reset study ${study.id} to chapter $firstId")
logger.info(s"Reset study ${study.id} to chapter ${first.id}")
studyRepo
.updateSomeFields(newStudy)
.zip(chapterRepo.byId(firstId))
.zip(chapterRepo.byId(first.id))
.map: (_, newChapter) =>
(newStudy, newChapter | chapter, previews)
(newStudy, newChapter | chapter)
case None =>
logger.warn(s"Couldn't reset study ${study.id}, no first chapter id found?!")
fuccess(defaultResult)
Expand Down
2 changes: 1 addition & 1 deletion ui/analyse/src/study/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface StudyData {
}

export interface StudyDataFromServer extends StudyData {
chapters: ChapterPreviewFromServer[];
chapters?: ChapterPreviewFromServer[];
}

export type Topic = string;
Expand Down
6 changes: 3 additions & 3 deletions ui/analyse/src/study/studyCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class StudyCtrl {
const sticked = data.features.sticky && !ctrl.initialPath && !isManualChapter && !practiceData;
this.vm = {
loading: false,
tab: prop<Tab>(!relayData && data.chapters.length > 1 ? 'chapters' : 'members'),
tab: prop<Tab>(!relayData && data.chapters?.[1] ? 'chapters' : 'members'),
toolTab: prop<ToolTab>(relayData ? 'multiBoard' : 'tags'),
chapterId: sticked ? data.position.chapterId : data.chapter.id,
// path is at ctrl.path
Expand Down Expand Up @@ -150,7 +150,7 @@ export default class StudyCtrl {
trans: ctrl.trans,
});
this.chapters = new StudyChaptersCtrl(
data.chapters,
data.chapters!,
this.send,
() => this.setTab('chapters'),
chapterId => xhr.chapterConfig(data.id, chapterId),
Expand Down Expand Up @@ -345,7 +345,7 @@ export default class StudyCtrl {
this.studyDesc.set(this.data.description);
document.title = this.data.name;
this.members.dict(s.members);
this.chapters.loadFromServer(s.chapters);
if (s.chapters) this.chapters.loadFromServer(s.chapters);
this.ctrl.flipped = this.chapterFlipMapProp(this.data.chapter.id);

const merge = !this.vm.mode.write && sameChapter;
Expand Down

0 comments on commit f718e30

Please sign in to comment.