Skip to content

Commit

Permalink
LightUser.write
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Nov 18, 2023
1 parent 880b97c commit 3274bff
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/controllers/Account.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ final class Account(
negotiateJson(
env.pref.api.get(me).map { prefs =>
Ok:
lila.common.LightUser.lightUserWrites.writes(me.light) ++ Json.obj(
lila.common.LightUser.write(me.light) ++ Json.obj(
"coach" -> isGranted(_.Coach),
"prefs" -> lila.pref.JsonView.write(prefs, lichobileCompat = false)
)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/Api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ final class Api(
env.user.lightUserApi asyncMany ids dmap (_.flatten) flatMap { users =>
val streamingIds = env.streamer.liveStreamApi.userIds
def toJson(u: LightUser) =
LightUser.lightUserWrites
.writes(u)
LightUser
.write(u)
.add("online" -> env.socket.isOnline(u.id))
.add("playing" -> env.round.playing(u.id))
.add("streaming" -> streamingIds(u.id))
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/Msg.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controllers
import play.api.libs.json.*

import lila.app.{ given, * }
import lila.common.LightUser.lightUserWrites

final class Msg(env: Env) extends LilaController(env):

Expand Down Expand Up @@ -84,7 +83,7 @@ final class Msg(env: Env) extends LilaController(env):
private def inboxJson(using me: Me) =
env.msg.api.myThreads flatMap env.msg.json.threads map { threads =>
Json.obj(
"me" -> lightUserWrites.writes(me.light).add("bot" -> me.isBot),
"me" -> lila.common.LightUser.write(me.light).add("bot" -> me.isBot),
"contacts" -> threads
)
}
2 changes: 1 addition & 1 deletion app/controllers/Streamer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class Streamer(env: Env, apiC: => Api) extends LilaController(env):
users <- env.user.lightUserApi asyncManyFallback s.streams.map(_.streamer.userId)
yield apiC.toApiResult:
(s.streams zip users).map: (stream, user) =>
lila.common.LightUser.lightUserWrites.writes(user) ++
lila.common.LightUser.write(user) ++
lila.streamer.Stream.toJson(env.memo.picfitUrl, stream)

def show(username: UserStr) = Open:
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/User.scala
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,8 @@ final class User(
lightUserApi.asyncMany(userIds) map { users =>
Json.obj(
"result" -> JsArray(users collect { case Some(u) =>
lila.common.LightUser.lightUserWrites
.writes(u)
lila.common.LightUser
.write(u)
.add("online" -> env.socket.isOnline(u.id))
})
)
Expand Down
2 changes: 1 addition & 1 deletion app/views/insight.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object insight:
"initialQuestion" -> question,
"i18n" -> Json.obj(),
"myUserId" -> ctx.userId,
"user" -> (lila.common.LightUser.lightUserWrites.writes(u.light) ++ Json.obj(
"user" -> (lila.common.LightUser.write(u.light) ++ Json.obj(
"nbGames" -> insightUser.count,
"stale" -> stale,
"shareId" -> prefId
Expand Down
17 changes: 8 additions & 9 deletions modules/common/src/main/LightUser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ object LightUser:

given UserIdOf[LightUser] = _.id

given lightUserWrites: OWrites[LightUser] = OWrites: u =>
writeNoId(u) + ("id" -> JsString(u.id.value))

def writeNoId(u: LightUser): JsObject =
Json
.obj("name" -> u.name)
.add("title" -> u.title)
.add("flair" -> u.flair)
.add("patron" -> u.isPatron)
given lightUserWrites: OWrites[LightUser] = OWrites(write)

def write(u: LightUser): JsObject = writeNoId(u) + ("id" -> JsString(u.id.value))
def writeNoId(u: LightUser): JsObject = Json
.obj("name" -> u.name)
.add("title" -> u.title)
.add("flair" -> u.flair)
.add("patron" -> u.isPatron)

def fallback(name: UserName) = LightUser(
id = name.id,
Expand Down
2 changes: 1 addition & 1 deletion modules/msg/src/main/MsgCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ final class MsgCompat(
private case class ThreadData(user: UserStr, subject: String, text: String)

private def renderUser(user: LightUser) =
LightUser.lightUserWrites.writes(user) ++ Json.obj(
LightUser.write(user) ++ Json.obj(
"online" -> isOnline(user.id),
"username" -> user.name // for mobile app BC
)
1 change: 0 additions & 1 deletion modules/round/src/main/RoundMobileSocket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import lila.common.{ Bus, Preload, ApiVersion }
import lila.socket.Socket
import lila.common.LightUser
import lila.common.Json.given
import LightUser.lightUserWrites
import chess.{ Color, ByColor }
import lila.pref.Pref
import lila.chat.Chat
Expand Down
2 changes: 1 addition & 1 deletion modules/round/src/main/TvBroadcast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final private class TvBroadcast(
val user = p.userId.flatMap(lightUserSync)
Json
.obj("color" -> p.color.name)
.add("user" -> user.map(LightUser.lightUserWrites.writes))
.add("user" -> user.map(LightUser.write))
.add("ai" -> p.aiLevel)
.add("rating" -> p.rating)
.add("seconds" -> pov.game.clock.map(_.remainingTime(pov.color).roundSeconds))
Expand Down
5 changes: 2 additions & 3 deletions ui/tournament/src/view/arena.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@ function playerTr(ctrl: TournamentController, player: StandingPlayer) {
);
}

function podiumUsername(p: PodiumPlayer) {
return h(
const podiumUsername = (p: PodiumPlayer) =>
h(
'a.text.ulpt.user-link',
{
attrs: { href: '/@/' + p.name },
},
playerName(p),
);
}

function podiumStats(p: PodiumPlayer, berserkable: boolean, ctrl: TournamentController): VNode {
const noarg = ctrl.trans.noarg,
Expand Down

0 comments on commit 3274bff

Please sign in to comment.