inject light user API into notify JSON handlers

pull/1955/head
Thibault Duplessis 2016-05-31 22:54:05 +02:00
parent 33ebe4a10e
commit fc94564718
5 changed files with 54 additions and 42 deletions

View File

@ -8,10 +8,10 @@ import views.html
object Notify extends LilaController {
import lila.notify.JSONHandlers._
val env = Env.notifyModule
import env.jsonHandlers._
val appMaxNotifications = 10
def recent = Auth { implicit ctx =>

View File

@ -3,7 +3,11 @@ package lila.notify
import akka.actor.ActorSystem
import com.typesafe.config.Config
final class Env(db: lila.db.Env, config: Config, system: ActorSystem) {
final class Env(
db: lila.db.Env,
config: Config,
getLightUser: lila.common.LightUser.Getter,
system: ActorSystem) {
val settings = new {
val collectionNotifications = config getString "collection.notify"
@ -12,15 +16,21 @@ final class Env(db: lila.db.Env, config: Config, system: ActorSystem) {
import settings._
val jsonHandlers = new JSONHandlers(getLightUser)
private lazy val repo = new NotificationRepo(coll = db(collectionNotifications))
lazy val notifyApi = new NotifyApi(bus = system.lilaBus, repo = repo)
lazy val notifyApi = new NotifyApi(
bus = system.lilaBus,
jsonHandlers = jsonHandlers,
repo = repo)
}
object Env {
lazy val current = "notify" boot new Env(db = lila.db.Env.current,
config = lila.common.PlayApp loadConfig "notify",
getLightUser = lila.user.Env.current.lightUser,
system = lila.common.PlayApp.system)
}
}

View File

@ -1,13 +1,13 @@
package lila.notify
import play.api.libs.json.{JsValue, Json, Writes}
import lila.common.LightUser
import lila.user.User
import play.api.libs.json.{ JsValue, Json, Writes }
final class JSONHandlers(
getLightUser: LightUser.Getter) {
object JSONHandlers {
implicit val notificationWrites : Writes[Notification] = new Writes[Notification] {
implicit val notificationWrites: Writes[Notification] = new Writes[Notification] {
def writeBody(notificationContent: NotificationContent) = {
notificationContent match {
case MentionedInThread(mentionedBy, topic, _, category, postId) =>
@ -22,17 +22,17 @@ object JSONHandlers {
}
def writes(notification: Notification) = {
val body = notification.content
val body = notification.content
val notificationType = body match {
case MentionedInThread(_,_, _, _, _) => "mentioned"
case InvitedToStudy(_,_,_) => "invitedStudy"
}
val notificationType = body match {
case MentionedInThread(_, _, _, _, _) => "mentioned"
case InvitedToStudy(_, _, _) => "invitedStudy"
}
Json.obj("content" -> writeBody(body),
"type" -> notificationType,
"read" -> notification.read.value,
"date" -> notification.createdAt)
Json.obj("content" -> writeBody(body),
"type" -> notificationType,
"read" -> notification.read.value,
"date" -> notification.createdAt)
}
}
@ -44,4 +44,3 @@ object JSONHandlers {
}
}

View File

@ -11,33 +11,33 @@ private final class NotificationRepo(val coll: Coll) {
coll.insert(notification).void
}
def markAllRead(notifies: Notification.Notifies) : Funit = {
coll.update(unreadOnlyQuery(notifies), $set("read" -> true), multi=true).void
def markAllRead(notifies: Notification.Notifies): Funit = {
coll.update(unreadOnlyQuery(notifies), $set("read" -> true), multi = true).void
}
def unreadNotificationsCount(userId: Notification.Notifies) : Fu[Int] = {
def unreadNotificationsCount(userId: Notification.Notifies): Fu[Int] = {
coll.count(unreadOnlyQuery(userId).some)
}
def hasRecentUnseenStudyInvitation(userId: Notification.Notifies, studyId: InvitedToStudy.StudyId) : Fu[Boolean] = {
def hasRecentUnseenStudyInvitation(userId: Notification.Notifies, studyId: InvitedToStudy.StudyId): Fu[Boolean] = {
val query = $doc(
"notifies" -> userId,
"read" -> false,
"content.type" -> "invitedStudy",
"content.studyId" -> studyId,
"created" -> $doc("$gt" ->DateTime.now.minusDays(7))
"created" -> $doc("$gt" -> DateTime.now.minusDays(3))
)
coll.exists(query)
}
def hasRecentUnseenNotificationsInThread(userId: Notification.Notifies, topicId: MentionedInThread.TopicId) : Fu[Boolean] = {
def hasRecentUnseenNotificationsInThread(userId: Notification.Notifies, topicId: MentionedInThread.TopicId): Fu[Boolean] = {
val query = $doc(
"notifies" -> userId,
"read" -> false,
"content.type" -> "mention",
"content.topicId" -> topicId,
"created" -> $doc("$gt" ->DateTime.now.minusDays(7))
"created" -> $doc("$gt" -> DateTime.now.minusDays(3))
)
coll.exists(query)
@ -47,6 +47,6 @@ private final class NotificationRepo(val coll: Coll) {
def userNotificationsQuery(userId: Notification.Notifies) = $doc("notifies" -> userId)
private def unreadOnlyQuery(userId:Notification.Notifies) = $doc("notifies" -> userId, "read" -> false)
private def unreadOnlyQuery(userId: Notification.Notifies) = $doc("notifies" -> userId, "read" -> false)
}

View File

@ -1,18 +1,21 @@
package lila.notify
import scala.concurrent.Future
import lila.common.paginator.Paginator
import lila.db.dsl._
import lila.db.paginator.Adapter
import lila.hub.actorApi.SendTo
import lila.memo.AsyncCache
import scala.concurrent.Future
final class NotifyApi(bus: lila.common.Bus, repo: NotificationRepo) {
final class NotifyApi(
bus: lila.common.Bus,
jsonHandlers: JSONHandlers,
repo: NotificationRepo) {
import BSONHandlers.NotificationBSONHandler
import JSONHandlers._
import jsonHandlers._
def getNotifications(userId: Notification.Notifies, page: Int, perPage: Int) : Fu[Paginator[Notification]] = Paginator(
def getNotifications(userId: Notification.Notifies, page: Int, perPage: Int): Fu[Paginator[Notification]] = Paginator(
adapter = new Adapter(
collection = repo.coll,
selector = repo.userNotificationsQuery(userId),
@ -32,24 +35,24 @@ final class NotifyApi(bus: lila.common.Bus, repo: NotificationRepo) {
insertOrDiscardNotification(notification) map {
_ ?? {
notif =>
getUnseenNotificationCount(notif.notifies).
map(NewNotification(notif, _)).
foreach(notifyConnectedClients)
getUnseenNotificationCount(notif.notifies).
map(NewNotification(notif, _)).
foreach(notifyConnectedClients)
}
}
}
def addNotifications(notifications: List[Notification]) : Funit = {
def addNotifications(notifications: List[Notification]): Funit = {
notifications.map(addNotification).sequenceFu.void
}
/**
* Inserts notification into the repository.
*
* If the user already has an unread notification on the topic, discard it.
*
* If the user does not already have an unread notification on the topic, returns it unmodified.
*/
* Inserts notification into the repository.
*
* If the user already has an unread notification on the topic, discard it.
*
* If the user does not already have an unread notification on the topic, returns it unmodified.
*/
private def insertOrDiscardNotification(notification: Notification): Fu[Option[Notification]] = {
notification.content match {
@ -65,7 +68,7 @@ final class NotifyApi(bus: lila.common.Bus, repo: NotificationRepo) {
}
}
private def notifyConnectedClients(newNotification: NewNotification) : Unit = {
private def notifyConnectedClients(newNotification: NewNotification): Unit = {
val notificationsEventKey = "new_notification"
val notificationEvent = SendTo(newNotification.notification.notifies.value, notificationsEventKey, newNotification)
bus.publish(notificationEvent, 'users)