lila/modules/bookmark/src/main/BookmarkApi.scala

73 lines
2.4 KiB
Scala
Raw Normal View History

2013-03-27 09:41:40 -06:00
package lila.bookmark
2016-08-01 04:25:33 -06:00
import org.joda.time.DateTime
2019-11-29 19:16:11 -07:00
import reactivemongo.api.bson._
2016-08-01 04:25:33 -06:00
2016-04-01 11:50:57 -06:00
import lila.db.dsl._
2013-03-27 09:41:40 -06:00
import lila.game.{ Game, GameRepo }
import lila.user.User
2013-03-27 09:41:40 -06:00
2016-08-01 04:25:33 -06:00
case class Bookmark(game: lila.game.Game, user: lila.user.User)
2013-03-27 09:41:40 -06:00
final class BookmarkApi(
2016-04-02 02:19:34 -06:00
coll: Coll,
2019-11-30 15:45:44 -07:00
gameRepo: GameRepo,
paginator: PaginatorBuilder
)(implicit ec: scala.concurrent.ExecutionContext) {
2013-03-27 09:41:40 -06:00
2019-11-30 15:45:44 -07:00
private def exists(gameId: Game.ID, userId: User.ID): Fu[Boolean] =
2016-08-01 04:25:33 -06:00
coll exists selectId(gameId, userId)
def exists(game: Game, user: User): Fu[Boolean] =
if (game.bookmarks > 0) exists(game.id, user.id)
2018-03-16 17:20:53 -06:00
else fuFalse
2016-08-01 04:25:33 -06:00
def exists(game: Game, user: Option[User]): Fu[Boolean] =
user.?? { exists(game, _) }
2013-03-27 09:41:40 -06:00
2019-11-30 15:45:44 -07:00
def filterGameIdsBookmarkedBy(games: Seq[Game], user: Option[User]): Fu[Set[Game.ID]] =
user ?? { u =>
2019-11-30 15:45:44 -07:00
val candidateIds = games collect { case g if g.bookmarks > 0 => g.id }
candidateIds.nonEmpty ??
coll.secondaryPreferred
.distinctEasy[Game.ID, Set]("g", userIdQuery(u.id) ++ $doc("g" $in candidateIds))
}
2013-03-27 09:41:40 -06:00
2019-11-30 15:45:44 -07:00
def removeByGameId(gameId: Game.ID): Funit =
2019-12-01 09:46:36 -07:00
coll.delete.one($doc("g" -> gameId)).void
2019-11-30 15:45:44 -07:00
def removeByGameIds(gameIds: List[Game.ID]): Funit =
2019-12-01 09:46:36 -07:00
coll.delete.one($doc("g" $in gameIds)).void
2013-03-27 09:41:40 -06:00
2019-12-01 09:46:36 -07:00
def remove(gameId: Game.ID, userId: User.ID): Funit = coll.delete.one(selectId(gameId, userId)).void
2016-08-01 04:25:33 -06:00
// def remove(selector: Bdoc): Funit = coll.remove(selector).void
2019-11-30 15:45:44 -07:00
def toggle(gameId: Game.ID, userId: User.ID): Funit =
2016-08-01 04:25:33 -06:00
exists(gameId, userId) flatMap { e =>
(if (e) remove(gameId, userId) else add(gameId, userId, DateTime.now)) inject !e
} flatMap { bookmarked =>
2019-11-30 15:45:44 -07:00
gameRepo.incBookmarks(gameId, if (bookmarked) 1 else -1)
2016-08-01 04:25:33 -06:00
}
def countByUser(user: User): Fu[Int] = coll.countSel(userIdQuery(user.id))
2013-03-27 09:41:40 -06:00
def gamePaginatorByUser(user: User, page: Int) =
2020-08-12 00:23:33 -06:00
paginator.byUser(user, page) dmap { _.mapResults(_.game) }
2016-08-01 04:25:33 -06:00
2019-11-30 15:45:44 -07:00
private def add(gameId: Game.ID, userId: User.ID, date: DateTime): Funit =
2019-12-13 07:30:20 -07:00
coll.insert
.one(
$doc(
"_id" -> makeId(gameId, userId),
"g" -> gameId,
"u" -> userId,
"d" -> date
)
)
.void
private def userIdQuery(userId: User.ID) = $doc("u" -> userId)
private def makeId(gameId: Game.ID, userId: User.ID) = s"$gameId$userId"
2019-11-30 15:45:44 -07:00
private def selectId(gameId: Game.ID, userId: User.ID) = $id(makeId(gameId, userId))
2013-03-27 09:41:40 -06:00
}