From 5584a2de6aaf6a54dc7cc893c47348f439fd8d61 Mon Sep 17 00:00:00 2001 From: Sabrina Tseng Date: Sat, 20 Nov 2021 19:50:08 -0800 Subject: [PATCH] Hide ratings when importing game to existing study if option selected --- app/controllers/Study.scala | 3 ++- modules/study/src/main/ChapterMaker.scala | 9 ++++---- modules/study/src/main/StudyApi.scala | 25 ++++++++++++++++------- modules/study/src/main/StudySocket.scala | 2 +- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/app/controllers/Study.scala b/app/controllers/Study.scala index cf5e21e83d..4c496a9127 100644 --- a/app/controllers/Study.scala +++ b/app/controllers/Study.scala @@ -328,7 +328,8 @@ final class Study( env.study.api.importPgns( StudyModel.Id(id), data.toChapterDatas, - sticky = data.sticky + sticky = data.sticky, + ctx.pref.showRatings )(Who(me.id, lila.socket.Socket.Sri(sri))) ) } diff --git a/modules/study/src/main/ChapterMaker.scala b/modules/study/src/main/ChapterMaker.scala index d2ca3e969a..248f214af3 100644 --- a/modules/study/src/main/ChapterMaker.scala +++ b/modules/study/src/main/ChapterMaker.scala @@ -19,14 +19,14 @@ final private class ChapterMaker( import ChapterMaker._ - def apply(study: Study, data: Data, order: Int, userId: User.ID): Fu[Chapter] = + def apply(study: Study, data: Data, order: Int, userId: User.ID, withRatings: Boolean): Fu[Chapter] = data.game.??(parseGame) flatMap { case None => data.game ?? pgnFetch.fromUrl flatMap { case Some(pgn) => fromFenOrPgnOrBlank(study, data.copy(pgn = pgn.some), order, userId) case _ => fromFenOrPgnOrBlank(study, data, order, userId) } - case Some(game) => fromGame(study, game, data, order, userId) + case Some(game) => fromGame(study, game, data, order, userId, withRatings) } map { (c: Chapter) => if (c.name.value.isEmpty) c.copy(name = Chapter defaultName order) else c } @@ -125,14 +125,15 @@ final private class ChapterMaker( data: Data, order: Int, userId: User.ID, + withRatings: Boolean, initialFen: Option[FEN] = None ): Fu[Chapter] = for { root <- game2root(game, initialFen) - tags <- pgnDump.tags(game, initialFen, none, withOpening = true, withRating = true) + tags <- pgnDump.tags(game, initialFen, none, withOpening = true, withRatings) name <- { if (data.isDefaultName) - Namer.gameVsText(game, withRatings = false)(lightUser.async) dmap Chapter.Name.apply + Namer.gameVsText(game, withRatings)(lightUser.async) dmap Chapter.Name.apply else fuccess(data.name) } _ = notifyChat(study, game, userId) diff --git a/modules/study/src/main/StudyApi.scala b/modules/study/src/main/StudyApi.scala index df05b134c2..7c5a143722 100644 --- a/modules/study/src/main/StudyApi.scala +++ b/modules/study/src/main/StudyApi.scala @@ -132,7 +132,8 @@ final class StudyApi( addChapter( studyId = study.id, data = data.form.toChapterData, - sticky = study.settings.sticky + sticky = study.settings.sticky, + withRatings )(Who(user.id, Sri(""))) >> byIdWithLastChapter(studyId) case _ => fuccess(none) } orElse importGame(data.copy(form = data.form.copy(asStr = none)), user, withRatings) @@ -587,11 +588,13 @@ final class StudyApi( } } - def addChapter(studyId: Study.Id, data: ChapterMaker.Data, sticky: Boolean)(who: Who): Funit = + def addChapter(studyId: Study.Id, data: ChapterMaker.Data, sticky: Boolean, withRatings: Boolean)( + who: Who + ): Funit = data.manyGames match { case Some(datas) => lila.common.Future.applySequentially(datas) { data => - addChapter(studyId, data, sticky)(who) + addChapter(studyId, data, sticky, withRatings)(who) } case _ => sequenceStudy(studyId) { study => @@ -605,7 +608,7 @@ final class StudyApi( } } >> chapterRepo.nextOrderByStudy(study.id) flatMap { order => - chapterMaker(study, data, order, who.u) flatMap { chapter => + chapterMaker(study, data, order, who.u, withRatings) flatMap { chapter => doAddChapter(study, chapter, sticky, who) } addFailureEffect { case ChapterMaker.ValidationException(error) => @@ -624,9 +627,11 @@ final class StudyApi( studyRepo.updateSomeFields(study) >>- indexStudy(study) } - def importPgns(studyId: Study.Id, datas: List[ChapterMaker.Data], sticky: Boolean)(who: Who) = + def importPgns(studyId: Study.Id, datas: List[ChapterMaker.Data], sticky: Boolean, withRatings: Boolean)( + who: Who + ) = lila.common.Future.applySequentially(datas) { data => - addChapter(studyId, data, sticky)(who) + addChapter(studyId, data, sticky, withRatings)(who) } def doAddChapter(study: Study, chapter: Chapter, sticky: Boolean, who: Who) = @@ -731,7 +736,13 @@ final class StudyApi( chapterRepo.orderedMetadataByStudy(studyId).flatMap { chaps => // deleting the only chapter? Automatically create an empty one if (chaps.sizeIs < 2) { - chapterMaker(study, ChapterMaker.Data(Chapter.Name("Chapter 1")), 1, who.u) flatMap { c => + chapterMaker( + study, + ChapterMaker.Data(Chapter.Name("Chapter 1")), + 1, + who.u, + withRatings = true + ) flatMap { c => doAddChapter(study, c, sticky = true, who) >> doSetChapter(study, c.id, who) } } // deleting the current chapter? Automatically move to another one diff --git a/modules/study/src/main/StudySocket.scala b/modules/study/src/main/StudySocket.scala index 2fe9263b94..8056ecb14a 100644 --- a/modules/study/src/main/StudySocket.scala +++ b/modules/study/src/main/StudySocket.scala @@ -124,7 +124,7 @@ final private class StudySocket( case "addChapter" => reading[ChapterMaker.Data](o) { data => val sticky = o.obj("d").flatMap(_.boolean("sticky")) | true - who foreach api.addChapter(studyId, data, sticky = sticky) + who foreach api.addChapter(studyId, data, sticky = sticky, withRatings = true) } case "setChapter" => o.get[Chapter.Id]("d") foreach { chapterId =>