Skip to content

Commit 77ba6b0

Browse files
committed
add "bookmarked" field to round mobile API - for lichess-org/mobile#15773
1 parent 9e6136f commit 77ba6b0

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

modules/bookmark/src/main/BookmarkApi.scala

+4-6
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,18 @@ final class BookmarkApi(
1717
private def exists(gameId: GameId, userId: UserId): Fu[Boolean] =
1818
coll.exists(selectId(gameId, userId))
1919

20-
def exists(game: Game, user: User): Fu[Boolean] =
21-
if game.bookmarks > 0 then exists(game.id, user.id)
22-
else fuFalse
20+
def exists(game: Game, userId: UserId): Fu[Boolean] =
21+
(game.bookmarks > 0).so(exists(game.id, userId))
2322

24-
def exists(game: Game, user: Option[User]): Fu[Boolean] =
23+
def exists(game: Game, user: Option[UserId]): Fu[Boolean] =
2524
user.so { exists(game, _) }
2625

2726
def filterGameIdsBookmarkedBy(games: Seq[Game], user: Option[User]): Fu[Set[GameId]] =
2827
user.so: u =>
2928
val candidateIds = games.collect { case g if g.bookmarks > 0 => g.id }
30-
candidateIds.nonEmpty.so(
29+
candidateIds.nonEmpty.so:
3130
coll.secondaryPreferred
3231
.distinctEasy[GameId, Set]("g", userIdQuery(u.id) ++ $doc("g".$in(candidateIds)))
33-
)
3432

3533
def removeByGameId(gameId: GameId): Funit =
3634
coll.delete.one($doc("g" -> gameId)).void

modules/bookmark/src/main/Env.scala

+2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ final class Env(
1818

1919
lazy val api = wire[BookmarkApi]
2020

21+
def exists: lila.core.bookmark.BookmarkExists = api.exists
22+
2123
lila.common.Bus.subscribeFun("roundUnplayed"):
2224
case lila.core.round.DeleteUnplayed(gameId) => api.removeByGameId(gameId)

modules/core/src/main/bookmark.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package lila.core
2+
package bookmark
3+
4+
type BookmarkExists = (game.Game, Option[userId.UserId]) => Fu[Boolean]

modules/round/src/main/Env.scala

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ final class Env(
4747
socketKit: lila.core.socket.ParallelSocketKit,
4848
userLagPut: lila.core.socket.userLag.Put,
4949
lightUserApi: lila.user.LightUserApi,
50+
bookmarkExists: lila.core.bookmark.BookmarkExists,
5051
simulApiCircularDep: => lila.core.simul.SimulApi,
5152
settingStore: lila.memo.SettingStore.Builder,
5253
shutdown: akka.actor.CoordinatedShutdown

modules/round/src/main/RoundMobile.scala

+12-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ import lila.round.RoundGame.*
1212

1313
object RoundMobile:
1414

15-
enum UseCase(val socketStatus: Option[SocketStatus], val chat: Boolean, val prefs: Boolean):
15+
enum UseCase(
16+
val socketStatus: Option[SocketStatus],
17+
val chat: Boolean,
18+
val prefs: Boolean,
19+
val bookmark: Boolean
20+
):
1621
// full round for every-day use
17-
case Online(socket: SocketStatus) extends UseCase(socket.some, chat = true, prefs = true)
22+
case Online(socket: SocketStatus) extends UseCase(socket.some, chat = true, prefs = true, bookmark = true)
1823
// correspondence game sent through firebase data
1924
// https://github.com/lichess-org/mobile/blob/main/lib/src/model/correspondence/offline_correspondence_game.dart
20-
case Offline extends UseCase(none, chat = false, prefs = false)
25+
case Offline extends UseCase(none, chat = false, prefs = false, bookmark = false)
2126

2227
final class RoundMobile(
2328
lightUserGet: LightUser.Getter,
@@ -28,7 +33,8 @@ final class RoundMobile(
2833
takebacker: Takebacker,
2934
moretimer: Moretimer,
3035
isOfferingRematch: lila.core.round.IsOfferingRematch,
31-
chatApi: lila.chat.ChatApi
36+
chatApi: lila.chat.ChatApi,
37+
bookmarkExists: lila.core.bookmark.BookmarkExists
3238
)(using Executor, lila.core.user.FlairGetMap):
3339

3440
import RoundMobile.*
@@ -58,6 +64,7 @@ final class RoundMobile(
5864
moretimeable <- moretimer.isAllowedIn(game, Preload(prefs))
5965
chat <- use.chat.so(getPlayerChat(game, myPlayer.exists(_.hasUser)))
6066
chatLines <- chat.map(_.chat).soFu(lila.chat.JsonView.asyncLines)
67+
bookmarked <- use.bookmark.so(bookmarkExists(game, myPlayer.flatMap(_.userId)))
6168
yield
6269
def playerJson(color: Color) =
6370
val pov = Pov(game, color)
@@ -98,6 +105,7 @@ final class RoundMobile(
98105
.obj("lines" -> chatLines)
99106
.add("restricted", c.restricted)
100107
)
108+
.add("bookmarked", bookmarked)
101109

102110
private def prefsJson(game: Game, pref: Pref): JsObject = Json
103111
.obj(

0 commit comments

Comments
 (0)