From fc9456471886ec2f4129cbc33fafdd6a7c2e85b6 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Tue, 31 May 2016 22:54:05 +0200 Subject: [PATCH] inject light user API into notify JSON handlers --- app/controllers/Notify.scala | 4 +-- modules/notify/src/main/Env.scala | 16 +++++++-- modules/notify/src/main/JsonHandlers.scala | 27 ++++++++------- .../notify/src/main/NotificationRepo.scala | 16 ++++----- modules/notify/src/main/NotifyApi.scala | 33 ++++++++++--------- 5 files changed, 54 insertions(+), 42 deletions(-) diff --git a/app/controllers/Notify.scala b/app/controllers/Notify.scala index 95564f5eb9..8f6eb3e238 100644 --- a/app/controllers/Notify.scala +++ b/app/controllers/Notify.scala @@ -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 => diff --git a/modules/notify/src/main/Env.scala b/modules/notify/src/main/Env.scala index 6674dc2217..b8a8bbaafa 100644 --- a/modules/notify/src/main/Env.scala +++ b/modules/notify/src/main/Env.scala @@ -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) -} \ No newline at end of file +} diff --git a/modules/notify/src/main/JsonHandlers.scala b/modules/notify/src/main/JsonHandlers.scala index 77fd018feb..0d07ddf4f0 100644 --- a/modules/notify/src/main/JsonHandlers.scala +++ b/modules/notify/src/main/JsonHandlers.scala @@ -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 { } } - diff --git a/modules/notify/src/main/NotificationRepo.scala b/modules/notify/src/main/NotificationRepo.scala index 203c046c05..2e67d55581 100644 --- a/modules/notify/src/main/NotificationRepo.scala +++ b/modules/notify/src/main/NotificationRepo.scala @@ -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) } diff --git a/modules/notify/src/main/NotifyApi.scala b/modules/notify/src/main/NotifyApi.scala index c86a329b3e..38fa3512fa 100644 --- a/modules/notify/src/main/NotifyApi.scala +++ b/modules/notify/src/main/NotifyApi.scala @@ -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)