forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageBatch.scala
38 lines (32 loc) · 1.13 KB
/
MessageBatch.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package lila.message
import lila.db.dsl._
import lila.user.{ User, UserRepo }
final class MessageBatch(
coll: Coll,
notifyApi: lila.notify.NotifyApi) {
def apply(me: User, action: String, ids: List[String]): Funit = ids.nonEmpty ?? {
action match {
case "read" => markRead(me, ids)
case "unread" => markUnread(me, ids)
case "delete" => delete(me, ids)
case x => fufail(s"Invalid message batch action: $x")
}
}
def markRead(me: User, ids: List[String]): Funit =
ThreadRepo.visibleByUserByIds(me, ids).flatMap {
_.map(ThreadRepo.setReadFor(me)).sequenceFu
}.void
def markUnread(me: User, ids: List[String]): Funit =
ThreadRepo.visibleByUserByIds(me, ids).flatMap {
_.map(ThreadRepo.setUnreadFor(me)).sequenceFu
}.void
def delete(me: User, ids: List[String]): Funit =
ThreadRepo.visibleByUserByIds(me, ids).flatMap {
_.map { thread =>
ThreadRepo.deleteFor(me.id)(thread.id) zip
notifyApi.remove(
lila.notify.Notification.Notifies(me.id),
$doc("content.thread.id" -> thread.id)) void
}.sequenceFu.void
}
}