Skip to content

Commit a07b6e1

Browse files
committed
add ?withBookmarked flag to /api/games/user for #15773
1 parent 77ba6b0 commit a07b6e1

File tree

7 files changed

+20
-16
lines changed

7 files changed

+20
-16
lines changed

app/Env.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ final class Env(
5151
val analyse: lila.analyse.Env = wire[lila.analyse.Env]
5252
val fishnet: lila.fishnet.Env = wire[lila.fishnet.Env]
5353
val history: lila.history.Env = wire[lila.history.Env]
54-
val round: lila.round.Env = wire[lila.round.Env]
5554
val bookmark: lila.bookmark.Env = wire[lila.bookmark.Env]
55+
val round: lila.round.Env = wire[lila.round.Env]
5656
val search: lila.search.Env = wire[lila.search.Env]
5757
val gameSearch: lila.gameSearch.Env = wire[lila.gameSearch.Env]
5858
val perfStat: lila.perfStat.Env = wire[lila.perfStat.Env]

app/controllers/Game.scala

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import lila.core.id.GameAnyId
1212
final class Game(env: Env, apiC: => Api) extends LilaController(env):
1313

1414
def bookmark(gameId: GameId) = AuthOrScopedBody(_.Web.Mobile) { _ ?=> me ?=>
15-
env.bookmark.api.toggle(gameId, me, getBoolOpt("v")).inject(NoContent)
15+
env.bookmark.api
16+
.toggle(env.round.gameProxy.updateIfPresent)(gameId, me, getBoolOpt("v"))
17+
.inject(NoContent)
1618
}
1719

1820
def delete(gameId: GameId) = Auth { _ ?=> me ?=>
@@ -146,7 +148,8 @@ final class Game(env: Env, apiC: => Api) extends LilaController(env):
146148
delayMoves = delayMovesFromReq,
147149
lastFen = getBool("lastFen"),
148150
accuracy = getBool("accuracy"),
149-
division = getBoolOpt("division") | extended
151+
division = getBoolOpt("division") | extended,
152+
bookmark = getBool("withBookmarked")
150153
)
151154

152155
private[controllers] def delayMovesFromReq(using RequestHeader) =

modules/api/src/main/Env.scala

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ final class Env(
6060
cacheApi: lila.memo.CacheApi,
6161
webConfig: lila.web.WebConfig,
6262
realPlayerApi: lila.web.RealPlayerApi,
63+
bookmarkExists: lila.core.bookmark.BookmarkExists,
6364
manifest: lila.web.AssetManifest
6465
)(using val mode: Mode, scheduler: Scheduler)(using
6566
Executor,

modules/api/src/main/GameApiV2.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ final class GameApiV2(
3232
getLightUser: LightUser.Getter,
3333
realPlayerApi: RealPlayerApi,
3434
gameProxy: GameProxyRepo,
35-
division: Divider
35+
division: Divider,
36+
bookmarkExists: lila.core.bookmark.BookmarkExists
3637
)(using Executor, akka.actor.ActorSystem):
3738

3839
import GameApiV2.*
@@ -278,6 +279,7 @@ final class GameApiV2(
278279
.apply(g, initialFen, analysisOption, config.flags, realPlayers = realPlayers)
279280
.dmap(annotator.toPgnString)
280281
)
282+
bookmarked <- config.flags.bookmark.so(bookmarkExists(g, config.by.map(_.userId)))
281283
accuracy = analysisOption
282284
.ifTrue(flags.accuracy)
283285
.flatMap:
@@ -325,6 +327,7 @@ final class GameApiV2(
325327
.add("lastFen" -> flags.lastFen.option(Fen.write(g.chess.situation)))
326328
.add("lastMove" -> flags.lastFen.option(g.lastMoveKeys))
327329
.add("division" -> flags.division.option(division(g, initialFen)))
330+
.add("bookmarked" -> bookmarked)
328331

329332
private def gameLightUsers(game: Game): Future[ByColor[(lila.core.game.Player, Option[LightUser])]] =
330333
game.players.traverse(_.userId.so(getLightUser)).dmap(game.players.zip(_))

modules/bookmark/src/main/BookmarkApi.scala

+5-8
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ import lila.db.dsl.{ *, given }
77

88
case class Bookmark(game: Game, user: User)
99

10-
final class BookmarkApi(
11-
coll: Coll,
12-
gameApi: GameApi,
13-
gameProxy: lila.core.game.GameProxy,
14-
paginator: PaginatorBuilder
15-
)(using Executor):
10+
final class BookmarkApi(coll: Coll, gameApi: GameApi, paginator: PaginatorBuilder)(using Executor):
1611

1712
private def exists(gameId: GameId, userId: UserId): Fu[Boolean] =
1813
coll.exists(selectId(gameId, userId))
@@ -38,7 +33,9 @@ final class BookmarkApi(
3833

3934
def remove(gameId: GameId, userId: UserId): Funit = coll.delete.one(selectId(gameId, userId)).void
4035

41-
def toggle(gameId: GameId, userId: UserId, v: Option[Boolean]): Funit =
36+
def toggle(
37+
updateProxy: GameId => Update[Game] => Funit
38+
)(gameId: GameId, userId: UserId, v: Option[Boolean]): Funit =
4239
exists(gameId, userId)
4340
.flatMap: e =>
4441
val newValue = v.getOrElse(!e)
@@ -48,7 +45,7 @@ final class BookmarkApi(
4845
_ <- if newValue then add(gameId, userId, nowInstant) else remove(gameId, userId)
4946
inc = if newValue then 1 else -1
5047
_ <- gameApi.incBookmarks(gameId, inc)
51-
_ <- gameProxy.updateIfPresent(gameId)(g => g.copy(bookmarks = g.bookmarks + inc))
48+
_ <- updateProxy(gameId)(g => g.copy(bookmarks = g.bookmarks + inc))
5249
yield ()
5350
.recover:
5451
lila.db.ignoreDuplicateKey

modules/bookmark/src/main/Env.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import lila.core.config.*
88
final class Env(
99
db: lila.db.Db,
1010
gameApi: lila.core.game.GameApi,
11-
gameRepo: lila.core.game.GameRepo,
12-
gameProxyRepo: lila.core.game.GameProxy
11+
gameRepo: lila.core.game.GameRepo
1312
)(using Executor):
1413

1514
private lazy val bookmarkColl = db(CollName("bookmark"))

modules/core/src/main/game/misc.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ abstract class GameRepo(val coll: BSONCollection):
123123
def sortedCursor(user: UserId, pk: PerfKey): AkkaStreamCursor[Game]
124124

125125
trait GameProxy:
126-
def updateIfPresent(gameId: GameId)(f: Game => Game): Funit
126+
def updateIfPresent(gameId: GameId)(f: Update[Game]): Funit
127127
def game(gameId: GameId): Fu[Option[Game]]
128128
def upgradeIfPresent(games: List[Game]): Fu[List[Game]]
129129
def flushIfPresent(gameId: GameId): Funit
@@ -177,7 +177,8 @@ object PgnDump:
177177
delayMoves: Boolean = false,
178178
lastFen: Boolean = false,
179179
accuracy: Boolean = false,
180-
division: Boolean = false
180+
division: Boolean = false,
181+
bookmark: Boolean = false
181182
):
182183
def requiresAnalysis = evals || accuracy
183184
def keepDelayIf(cond: Boolean) = copy(delayMoves = delayMoves && cond)

0 commit comments

Comments
 (0)