fix study game import tags - closes #5444

pull/5440/head
Thibault Duplessis 2019-08-23 10:57:28 +02:00
parent 690a172243
commit 356acbbed9
3 changed files with 53 additions and 49 deletions

View File

@ -4,7 +4,7 @@ import chess.format.pgn.Tags
import chess.format.{ Forsyth, FEN }
import chess.variant.{ Variant, Crazyhouse }
import lila.chat.Chat
import lila.game.{ Game, Pov, GameRepo, Namer }
import lila.game.{ Game, GameRepo, Namer }
import lila.importer.Importer
import lila.user.User
@ -13,20 +13,21 @@ private final class ChapterMaker(
lightUser: lila.user.LightUserApi,
chat: akka.actor.ActorSelection,
importer: Importer,
pgnFetch: PgnFetch
pgnFetch: PgnFetch,
pgnDump: lila.game.PgnDump
) {
import ChapterMaker._
def apply(study: Study, data: Data, order: Int, userId: User.ID): Fu[Option[Chapter]] =
data.game.??(parsePov) flatMap {
def apply(study: Study, data: Data, order: Int, userId: User.ID): 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) map some
case _ => fromFenOrPgnOrBlank(study, data, order, userId) map some
case Some(pgn) => fromFenOrPgnOrBlank(study, data.copy(pgn = pgn.some), order, userId)
case _ => fromFenOrPgnOrBlank(study, data, order, userId)
}
case Some(pov) => fromPov(study, pov, data, order, userId)
} map2 { (c: Chapter) =>
case Some(game) => fromGame(study, game, data, order, userId)
} map { (c: Chapter) =>
if (c.name.value.isEmpty) c.copy(name = Chapter defaultName order) else c
}
@ -104,37 +105,43 @@ private final class ChapterMaker(
}
}
private def fromPov(study: Study, pov: Pov, data: Data, order: Int, userId: User.ID, initialFen: Option[FEN] = None): Fu[Option[Chapter]] =
game2root(pov.game, initialFen) map { root =>
Chapter.make(
studyId = study.id,
name =
if (Chapter isDefaultName data.name)
Chapter.Name(Namer.gameVsText(pov.game, withRatings = false)(lightUser.sync))
else data.name,
setup = Chapter.Setup(
!pov.game.synthetic option pov.gameId,
pov.game.variant,
data.realOrientation
),
root = root,
tags = Tags.empty,
order = order,
ownerId = userId,
practice = data.isPractice,
gamebook = data.isGamebook,
conceal = data.isConceal option Chapter.Ply(root.ply)
).some
} addEffect { _ =>
notifyChat(study, pov.game, userId)
}
private def fromGame(
study: Study,
game: Game,
data: Data,
order: Int,
userId: User.ID,
initialFen: Option[FEN] = None
): Fu[Chapter] = for {
root <- game2root(game, initialFen)
tags <- pgnDump.tags(game, initialFen, none, withOpening = true)
_ = notifyChat(study, game, userId)
} yield Chapter.make(
studyId = study.id,
name =
if (Chapter isDefaultName data.name)
Chapter.Name(Namer.gameVsText(game, withRatings = false)(lightUser.sync))
else data.name,
setup = Chapter.Setup(
!game.synthetic option game.id,
game.variant,
data.realOrientation
),
root = root,
tags = PgnTags(tags),
order = order,
ownerId = userId,
practice = data.isPractice,
gamebook = data.isGamebook,
conceal = data.isConceal option Chapter.Ply(root.ply)
)
def notifyChat(study: Study, game: Game, userId: User.ID) =
if (study.isPublic) List(game.id, s"${game.id}/w") foreach { chatId =>
chat ! lila.chat.actorApi.UserTalk(
chatId = Chat.Id(chatId),
userId = userId,
text = s"I'm studying this game on lichess.org/study/${study.id}",
text = s"I'm studying this game on ${domain}/study/${study.id}",
publicSource = none
)
}
@ -149,10 +156,10 @@ private final class ChapterMaker(
s"""$escapedDomain/(\\w{8,12})"""
}.r.unanchored
private def parsePov(str: String): Fu[Option[Pov]] = str match {
case s if s.size == Game.gameIdSize => GameRepo.pov(s, chess.White)
case s if s.size == Game.fullIdSize => GameRepo.pov(s)
case UrlRegex(id) => parsePov(id)
private def parseGame(str: String): Fu[Option[Game]] = str match {
case s if s.size == Game.gameIdSize => GameRepo game s
case s if s.size == Game.fullIdSize => GameRepo game Game.takeGameId(s)
case UrlRegex(id) => parseGame(id)
case _ => fuccess(none)
}
}

View File

@ -85,6 +85,7 @@ final class Env(
private lazy val chapterMaker = new ChapterMaker(
importer = importer,
pgnFetch = new PgnFetch,
pgnDump = gamePgnDump,
lightUser = lightUserApi,
chat = hub.chat,
domain = NetDomain

View File

@ -494,14 +494,12 @@ final class StudyApi(
chapterRepo.countByStudyId(study.id) flatMap { count =>
if (count >= Study.maxChapters) funit
else chapterRepo.nextOrderByStudy(study.id) flatMap { order =>
chapterMaker(study, data, order, byUserId) flatMap {
_ ?? { chapter =>
data.initial ?? {
chapterRepo.firstByStudy(study.id) flatMap {
_.filter(_.isEmptyInitial) ?? chapterRepo.delete
}
} >> doAddChapter(study, chapter, sticky, sri)
}
chapterMaker(study, data, order, byUserId) flatMap { chapter =>
data.initial ?? {
chapterRepo.firstByStudy(study.id) flatMap {
_.filter(_.isEmptyInitial) ?? chapterRepo.delete
}
} >> doAddChapter(study, chapter, sticky, sri)
}
}
}
@ -604,10 +602,8 @@ final class StudyApi(
chapterRepo.orderedMetadataByStudy(studyId).flatMap { chaps =>
// deleting the only chapter? Automatically create an empty one
if (chaps.size < 2) {
chapterMaker(study, ChapterMaker.Data(Chapter.Name("Chapter 1")), 1, byUserId) flatMap {
_ ?? { c =>
doAddChapter(study, c, sticky = true, sri) >> doSetChapter(study, c.id, sri)
}
chapterMaker(study, ChapterMaker.Data(Chapter.Name("Chapter 1")), 1, byUserId) flatMap { c =>
doAddChapter(study, c, sticky = true, sri) >> doSetChapter(study, c.id, sri)
}
} // deleting the current chapter? Automatically move to another one
else (study.position.chapterId == chapterId).?? {