forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Api.scala
34 lines (26 loc) · 1.02 KB
/
Api.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
package lila.notification
import scala.collection.mutable
import akka.actor.ActorSelection
import akka.pattern.ask
import play.twirl.api.Html
import lila.hub.actorApi.SendTo
import lila.user.User
private[notification] final class Api(bus: lila.common.Bus, renderer: ActorSelection) {
private val repo = mutable.Map[String, List[Notification]]()
import makeTimeout.large
def add(userId: String, html: String, from: Option[String] = None) {
val notif = Notification(userId, html, from)
repo.update(userId, notif :: get(userId))
val request = actorApi.RenderNotification(notif.id, notif.from, notif.html)
renderer ? request foreach {
case rendered: Html =>
val event = SendTo(userId, "notificationAdd", rendered.toString)
bus.publish(event, 'users)
}
}
def get(userId: String): List[Notification] = ~(repo get userId)
def remove(userId: String, id: String) {
repo.update(userId, get(userId) filter (_.id != id))
bus.publish(SendTo(userId, "notificationRemove", id), 'users)
}
}