inject light user API into notify JSON handlers
This commit is contained in:
parent
33ebe4a10e
commit
fc94564718
|
@ -8,10 +8,10 @@ import views.html
|
||||||
|
|
||||||
object Notify extends LilaController {
|
object Notify extends LilaController {
|
||||||
|
|
||||||
import lila.notify.JSONHandlers._
|
|
||||||
|
|
||||||
val env = Env.notifyModule
|
val env = Env.notifyModule
|
||||||
|
|
||||||
|
import env.jsonHandlers._
|
||||||
|
|
||||||
val appMaxNotifications = 10
|
val appMaxNotifications = 10
|
||||||
|
|
||||||
def recent = Auth { implicit ctx =>
|
def recent = Auth { implicit ctx =>
|
||||||
|
|
|
@ -3,7 +3,11 @@ package lila.notify
|
||||||
import akka.actor.ActorSystem
|
import akka.actor.ActorSystem
|
||||||
import com.typesafe.config.Config
|
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 settings = new {
|
||||||
val collectionNotifications = config getString "collection.notify"
|
val collectionNotifications = config getString "collection.notify"
|
||||||
|
@ -12,15 +16,21 @@ final class Env(db: lila.db.Env, config: Config, system: ActorSystem) {
|
||||||
|
|
||||||
import settings._
|
import settings._
|
||||||
|
|
||||||
|
val jsonHandlers = new JSONHandlers(getLightUser)
|
||||||
|
|
||||||
private lazy val repo = new NotificationRepo(coll = db(collectionNotifications))
|
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 {
|
object Env {
|
||||||
|
|
||||||
lazy val current = "notify" boot new Env(db = lila.db.Env.current,
|
lazy val current = "notify" boot new Env(db = lila.db.Env.current,
|
||||||
config = lila.common.PlayApp loadConfig "notify",
|
config = lila.common.PlayApp loadConfig "notify",
|
||||||
|
getLightUser = lila.user.Env.current.lightUser,
|
||||||
system = lila.common.PlayApp.system)
|
system = lila.common.PlayApp.system)
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,13 +1,13 @@
|
||||||
package lila.notify
|
package lila.notify
|
||||||
|
|
||||||
import play.api.libs.json.{JsValue, Json, Writes}
|
|
||||||
import lila.common.LightUser
|
import lila.common.LightUser
|
||||||
import lila.user.User
|
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) = {
|
def writeBody(notificationContent: NotificationContent) = {
|
||||||
notificationContent match {
|
notificationContent match {
|
||||||
case MentionedInThread(mentionedBy, topic, _, category, postId) =>
|
case MentionedInThread(mentionedBy, topic, _, category, postId) =>
|
||||||
|
@ -22,17 +22,17 @@ object JSONHandlers {
|
||||||
}
|
}
|
||||||
|
|
||||||
def writes(notification: Notification) = {
|
def writes(notification: Notification) = {
|
||||||
val body = notification.content
|
val body = notification.content
|
||||||
|
|
||||||
val notificationType = body match {
|
val notificationType = body match {
|
||||||
case MentionedInThread(_,_, _, _, _) => "mentioned"
|
case MentionedInThread(_, _, _, _, _) => "mentioned"
|
||||||
case InvitedToStudy(_,_,_) => "invitedStudy"
|
case InvitedToStudy(_, _, _) => "invitedStudy"
|
||||||
}
|
}
|
||||||
|
|
||||||
Json.obj("content" -> writeBody(body),
|
Json.obj("content" -> writeBody(body),
|
||||||
"type" -> notificationType,
|
"type" -> notificationType,
|
||||||
"read" -> notification.read.value,
|
"read" -> notification.read.value,
|
||||||
"date" -> notification.createdAt)
|
"date" -> notification.createdAt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,4 +44,3 @@ object JSONHandlers {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,33 +11,33 @@ private final class NotificationRepo(val coll: Coll) {
|
||||||
coll.insert(notification).void
|
coll.insert(notification).void
|
||||||
}
|
}
|
||||||
|
|
||||||
def markAllRead(notifies: Notification.Notifies) : Funit = {
|
def markAllRead(notifies: Notification.Notifies): Funit = {
|
||||||
coll.update(unreadOnlyQuery(notifies), $set("read" -> true), multi=true).void
|
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)
|
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(
|
val query = $doc(
|
||||||
"notifies" -> userId,
|
"notifies" -> userId,
|
||||||
"read" -> false,
|
"read" -> false,
|
||||||
"content.type" -> "invitedStudy",
|
"content.type" -> "invitedStudy",
|
||||||
"content.studyId" -> studyId,
|
"content.studyId" -> studyId,
|
||||||
"created" -> $doc("$gt" ->DateTime.now.minusDays(7))
|
"created" -> $doc("$gt" -> DateTime.now.minusDays(3))
|
||||||
)
|
)
|
||||||
|
|
||||||
coll.exists(query)
|
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(
|
val query = $doc(
|
||||||
"notifies" -> userId,
|
"notifies" -> userId,
|
||||||
"read" -> false,
|
"read" -> false,
|
||||||
"content.type" -> "mention",
|
"content.type" -> "mention",
|
||||||
"content.topicId" -> topicId,
|
"content.topicId" -> topicId,
|
||||||
"created" -> $doc("$gt" ->DateTime.now.minusDays(7))
|
"created" -> $doc("$gt" -> DateTime.now.minusDays(3))
|
||||||
)
|
)
|
||||||
|
|
||||||
coll.exists(query)
|
coll.exists(query)
|
||||||
|
@ -47,6 +47,6 @@ private final class NotificationRepo(val coll: Coll) {
|
||||||
|
|
||||||
def userNotificationsQuery(userId: Notification.Notifies) = $doc("notifies" -> userId)
|
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)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,21 @@
|
||||||
package lila.notify
|
package lila.notify
|
||||||
|
|
||||||
import scala.concurrent.Future
|
|
||||||
import lila.common.paginator.Paginator
|
import lila.common.paginator.Paginator
|
||||||
import lila.db.dsl._
|
import lila.db.dsl._
|
||||||
import lila.db.paginator.Adapter
|
import lila.db.paginator.Adapter
|
||||||
import lila.hub.actorApi.SendTo
|
import lila.hub.actorApi.SendTo
|
||||||
import lila.memo.AsyncCache
|
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 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(
|
adapter = new Adapter(
|
||||||
collection = repo.coll,
|
collection = repo.coll,
|
||||||
selector = repo.userNotificationsQuery(userId),
|
selector = repo.userNotificationsQuery(userId),
|
||||||
|
@ -32,24 +35,24 @@ final class NotifyApi(bus: lila.common.Bus, repo: NotificationRepo) {
|
||||||
insertOrDiscardNotification(notification) map {
|
insertOrDiscardNotification(notification) map {
|
||||||
_ ?? {
|
_ ?? {
|
||||||
notif =>
|
notif =>
|
||||||
getUnseenNotificationCount(notif.notifies).
|
getUnseenNotificationCount(notif.notifies).
|
||||||
map(NewNotification(notif, _)).
|
map(NewNotification(notif, _)).
|
||||||
foreach(notifyConnectedClients)
|
foreach(notifyConnectedClients)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def addNotifications(notifications: List[Notification]) : Funit = {
|
def addNotifications(notifications: List[Notification]): Funit = {
|
||||||
notifications.map(addNotification).sequenceFu.void
|
notifications.map(addNotification).sequenceFu.void
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts notification into the repository.
|
* Inserts notification into the repository.
|
||||||
*
|
*
|
||||||
* If the user already has an unread notification on the topic, discard it.
|
* 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.
|
* If the user does not already have an unread notification on the topic, returns it unmodified.
|
||||||
*/
|
*/
|
||||||
private def insertOrDiscardNotification(notification: Notification): Fu[Option[Notification]] = {
|
private def insertOrDiscardNotification(notification: Notification): Fu[Option[Notification]] = {
|
||||||
|
|
||||||
notification.content match {
|
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 notificationsEventKey = "new_notification"
|
||||||
val notificationEvent = SendTo(newNotification.notification.notifies.value, notificationsEventKey, newNotification)
|
val notificationEvent = SendTo(newNotification.notification.notifies.value, notificationsEventKey, newNotification)
|
||||||
bus.publish(notificationEvent, 'users)
|
bus.publish(notificationEvent, 'users)
|
||||||
|
|
Loading…
Reference in a new issue