user bot title

This commit is contained in:
Thibault Duplessis 2018-04-18 00:18:04 +02:00
parent ba42fd499f
commit 32c42589ce
9 changed files with 29 additions and 22 deletions

View file

@ -39,14 +39,12 @@ object Bot extends LilaController {
}
private def WithMyBotGame(anyId: String, me: lila.user.User)(f: lila.game.Pov => Fu[Result]) =
lila.user.UserRepo.isBot(me) flatMap {
case false => BadRequest(jsonError("This endpoint only works for bot accounts. See https://lichess.org/api#operation/botAccountUpgrade")).fuccess
case _ => Env.round.roundProxyGame(lila.game.Game takeGameId anyId) flatMap {
case None => NotFound(jsonError("No such game")).fuccess
case Some(game) => lila.game.Pov(game, me) match {
case None => NotFound(jsonError("Not your game")).fuccess
case Some(pov) => f(pov)
}
if (!me.isBot) BadRequest(jsonError("This endpoint only works for bot accounts. See https://lichess.org/api#operation/botAccountUpgrade")).fuccess
else Env.round.roundProxyGame(lila.game.Game takeGameId anyId) flatMap {
case None => NotFound(jsonError("No such game")).fuccess
case Some(game) => lila.game.Pov(game, me) match {
case None => NotFound(jsonError("Not your game")).fuccess
case Some(pov) => f(pov)
}
}
}

View file

@ -139,9 +139,8 @@ trait UserHelper { self: I18nHelper with StringHelper with NumberHelper =>
): Html = userIdLink(userId.some, cssClass)
def titleTag(title: Option[String]) = Html {
title match {
case None => ""
case Some(t) => s"""<span class="title" title="${User titleName t}">$t</span>&nbsp;"""
title.fold("") { t =>
s"""<span class="title" data-title="$t" title="${User titleName t}">$t</span>&nbsp;"""
}
}

View file

@ -51,10 +51,9 @@ private[api] final class UserApi(
bookmarkApi.countByUser(u) zip
gameCache.nbPlaying(u.id) zip
gameCache.nbImportedBy(u.id) zip
lila.user.UserRepo.isBot(u) zip
playBanApi.completionRate(u.id).map(_.map { cr => math.round(cr * 100) }) map {
case gameOption ~ nbGamesWithMe ~ following ~ followers ~ followable ~ relation ~
isFollowed ~ nbBookmarks ~ nbPlaying ~ nbImported ~ isBot ~ completionRate =>
isFollowed ~ nbBookmarks ~ nbPlaying ~ nbImported ~ completionRate =>
jsonView(u) ++ {
Json.obj(
"url" -> makeUrl(s"@/${u.username}"), // for app BC
@ -77,8 +76,7 @@ private[api] final class UserApi(
"import" -> nbImported,
"me" -> nbGamesWithMe
)
).add("streaming", isStreaming(u.id))
.add("bot" -> isBot) ++
).add("streaming", isStreaming(u.id)) ++
as.isDefined.??(Json.obj(
"followable" -> followable,
"following" -> relation.has(true),

View file

@ -11,10 +11,14 @@ case class LightUser(
def titleName = title.fold(name)(_ + " " + name)
def titleNameHtml = title.fold(name)(_ + "&nbsp;" + name)
def isBot = title has LightUser.botTitle
}
object LightUser {
val botTitle = "BOT"
implicit val lightUserWrites = OWrites[LightUser] { u =>
Json.obj(
"id" -> u.id,

View file

@ -13,7 +13,7 @@ object Namer {
p.aiLevel.fold(
p.userId.flatMap(lightUser).fold(lila.user.User.anonymous) { user =>
val title = (user.title ifTrue withTitle) ?? { t =>
s"""<span class="title" title="${User titleName t}">$t</span>&nbsp;"""
s"""<span class="title" data-title="$t" title="${User titleName t}">$t</span>&nbsp;"""
}
if (withRating) s"$title${user.name}&nbsp;(${ratingString(p)})"
else s"$title${user.name}"

View file

@ -108,6 +108,8 @@ case class User(
def createdSinceDays(days: Int) = createdAt isBefore DateTime.now.minusDays(days)
def is(name: String) = id == User.normalize(name)
def isBot = title has User.botTitle
}
object User {
@ -173,9 +175,12 @@ object User {
"CM" -> "Candidate Master",
"WCM" -> "Woman Candidate Master",
"WNM" -> "Woman National Master",
"LM" -> "Lichess Master"
"LM" -> "Lichess Master",
"BOT" -> "Chess Robot"
)
val botTitle = LightUser.botTitle
val titlesMap = titles.toMap
def titleName(title: String) = titlesMap get title getOrElse title
@ -197,7 +202,6 @@ object User {
val createdAt = "createdAt"
val seenAt = "seenAt"
val kid = "kid"
val bot = "bot"
val createdWithApiVersion = "createdWithApiVersion"
val lang = "lang"
val title = "title"

View file

@ -326,10 +326,9 @@ object UserRepo {
def setBot(user: User): Funit =
if (user.count.game > 0) fufail("You already have games played. Make a new account.")
else coll.updateField($id(user.id), F.bot, true).void
else coll.updateField($id(user.id), F.title, User.botTitle).void
def isBot(user: User): Fu[Boolean] =
coll.primitiveOne[Boolean]($id(user.id), F.bot) map (~_)
private def botSelector(v: Boolean) = $doc(F.title -> v)
def getTitle(id: ID): Fu[Option[String]] = coll.primitiveOne[String]($id(id), F.title)

View file

@ -1036,6 +1036,9 @@ span.progress > .negative {
color: #d59120;
font-weight: bold;
}
.user_link span.title[data-title=BOT] {
color: #c320d5!important;
}
div.warning {
padding: 5px;
background: #f2dede;

View file

@ -40,7 +40,9 @@ export function userHtml(ctrl: RoundController, player: Player) {
href: '/@/' + user.username,
target: ctrl.isPlaying() ? '_blank' : '_self'
}
}, user.title ? [h('span.title', user.title), ' ', user.username] : [user.username]),
}, user.title ? [h('span.title', {
attrs: { 'data-title': user.title }
}, user.title), ' ', user.username] : [user.username]),
rating ? h('rating', rating + (player.provisional ? '?' : '')) : null,
ratingDiff(player),
player.engine ? h('span', {