take into account blocks when issuing notifications.

This commit is contained in:
Gordon Martin 2016-05-30 22:31:43 +01:00
parent b2d1f2f196
commit 7422b90bd4
7 changed files with 48 additions and 20 deletions

@ -1 +1 @@
Subproject commit 14039ec16a3d5e0515e96f6a03786028276d53a3
Subproject commit 2f202a1e1ea6eaad55efd81da77fd1a29f084736

View file

@ -9,6 +9,18 @@ object Future {
}
}
def filter[A](list: List[A])(f: A => Fu[Boolean]): Fu[List[A]] = {
list.map {
element => f(element) map (_ option element)
}.sequenceFu.map(_.flatten)
}
def filterNot[A](list: List[A])(f: A => Fu[Boolean]): Fu[List[A]] = {
list.map {
element => !f(element) map (_ option element)
}.sequenceFu.map(_.flatten)
}
def traverseSequentially[A, B](list: List[A])(f: A => Fu[B]): Fu[List[B]] =
list match {
case h :: t => f(h).flatMap { r =>
@ -20,6 +32,6 @@ object Future {
def applySequentially[A](list: List[A])(f: A => Funit): Funit =
list match {
case h :: t => f(h) >> applySequentially(t)(f)
case Nil => funit
case Nil => funit
}
}

View file

@ -8,6 +8,7 @@ import lila.common.PimpedConfig._
import lila.hub.actorApi.forum._
import lila.mod.ModlogApi
import lila.notify.NotifyApi
import lila.relation.RelationApi
final class Env(
@ -18,6 +19,7 @@ final class Env(
hub: lila.hub.Env,
detectLanguage: DetectLanguage,
notifyApi : NotifyApi,
relationApi: RelationApi,
system: ActorSystem) {
private val settings = new {
@ -36,7 +38,7 @@ final class Env(
lazy val categApi = new CategApi(env = this)
lazy val mentionNotifier = new MentionNotifier(notifyApi = notifyApi)
lazy val mentionNotifier = new MentionNotifier(notifyApi = notifyApi, relationApi = relationApi)
lazy val topicApi = new TopicApi(
env = this,
@ -84,5 +86,6 @@ object Env {
hub = lila.hub.Env.current,
detectLanguage = DetectLanguage(lila.common.PlayApp loadConfig "detectlanguage"),
notifyApi = lila.notify.Env.current.notifyApi,
relationApi = lila.relation.Env.current.api,
system = lila.common.PlayApp.system)
}

View file

@ -2,15 +2,17 @@ package lila.forum
import lila.notify.{Notification, MentionedInThread}
import lila.notify.NotifyApi
import lila.relation.RelationApi
import lila.user.{UserRepo, User}
import org.joda.time.DateTime
import lila.common.Future
/**
* Notifier to inform users if they have been mentioned in a post
*
* @param notifyApi Api for sending inbox messages
*/
final class MentionNotifier(notifyApi: NotifyApi) {
final class MentionNotifier(notifyApi: NotifyApi, relationApi: RelationApi) {
def notifyMentionedUsers(post: Post, topic: Topic): Unit = {
post.userId foreach { author =>
@ -18,7 +20,7 @@ final class MentionNotifier(notifyApi: NotifyApi) {
val mentionedBy = MentionedInThread.MentionedBy(author)
for {
validUsers <- filterValidUsers(mentionedUsers)
validUsers <- filterValidUsers(mentionedUsers, author)
notifications = validUsers.map(createMentionNotification(post, topic, _, mentionedBy))
} yield notifyApi.addNotifications(notifications)
}
@ -26,15 +28,20 @@ final class MentionNotifier(notifyApi: NotifyApi) {
/**
* Checks the database to make sure that the users mentioned exist, and removes any users that do not exist
* from the returned list.
* or block the mentioner from the returned list.
*/
private def filterValidUsers(users: Set[String]) : Fu[List[Notification.Notifies]] = {
private def filterValidUsers(users: Set[String], mentionedBy: String) : Fu[List[Notification.Notifies]] = {
for {
validUsers <- UserRepo.existingUsernameIds(users)
validNotifies = validUsers.map(Notification.Notifies.apply)
validUsers <- UserRepo.existingUsernameIds(users take 20).map(_.take(5))
validUnblockedUsers <- filterNotBlockedByUsers(validUsers, mentionedBy)
validNotifies = validUnblockedUsers.map(Notification.Notifies.apply)
} yield validNotifies
}
private def filterNotBlockedByUsers(usersMentioned: List[String], mentionedBy: String) : Fu[List[String]]= {
Future.filterNot(usersMentioned)(mentioned => relationApi.fetchBlocks(mentioned, mentionedBy))
}
private def createMentionNotification(post: Post, topic: Topic, mentionedUser: Notification.Notifies, mentionedBy: MentionedInThread.MentionedBy): Notification = {
val notificationContent = MentionedInThread(
mentionedBy,

View file

@ -77,7 +77,8 @@ final class Env(
chapterMaker = chapterMaker,
studyMaker = studyMaker,
notifier = new StudyNotifier(
notifyApi = lila.notify.Env.current.notifyApi
notifyApi = lila.notify.Env.current.notifyApi,
relationApi = lila.relation.Env.current.api
),
lightUser = getLightUser,
chat = hub.actor.chat,

View file

@ -7,20 +7,25 @@ import lila.hub.actorApi.HasUserId
import lila.hub.actorApi.message.LichessThread
import lila.notify.InvitedToStudy.InvitedBy
import lila.notify.{InvitedToStudy, NotifyApi, Notification}
import lila.relation.RelationApi
import makeTimeout.short
import org.joda.time.DateTime
private final class StudyNotifier(
notifyApi: NotifyApi) {
notifyApi: NotifyApi,
relationApi: RelationApi) {
def apply(study: Study, invited: lila.user.User, socket: ActorRef) =
socket ? HasUserId(invited.id) mapTo manifest[Boolean] map { isPresent =>
study.owner.ifFalse(isPresent) foreach { owner =>
if (!isPresent) {
val notificationContent = InvitedToStudy(InvitedToStudy.InvitedBy(owner.id), InvitedToStudy.StudyName(study.name), InvitedToStudy.StudyId(study.id))
val notification = Notification(Notification.Notifies(invited.id), notificationContent, Notification.NotificationRead(false), DateTime.now())
notifyApi.addNotification(notification)
relationApi.fetchBlocks(invited.id, study.ownerId).flatMap {
blocked =>
socket ? HasUserId(invited.id) mapTo manifest[Boolean] map { isPresent =>
study.owner.ifFalse(isPresent) foreach { owner =>
if (!blocked && !isPresent) {
val notificationContent = InvitedToStudy(InvitedToStudy.InvitedBy(owner.id), InvitedToStudy.StudyName(study.name), InvitedToStudy.StudyId(study.id))
val notification = Notification(Notification.Notifies(invited.id), notificationContent, Notification.NotificationRead(false), DateTime.now())
notifyApi.addNotification(notification)
}
}
}
}
}
}

View file

@ -238,7 +238,7 @@ object ApplicationBuild extends Build {
libraryDependencies ++= provided(play.api, RM)
)
lazy val study = project("study", Seq(common, db, hub, socket, game, round, importer, notifications)).settings(
lazy val study = project("study", Seq(common, db, hub, socket, game, round, importer, notifications, relation)).settings(
libraryDependencies ++= provided(play.api, RM)
)
@ -320,7 +320,7 @@ object ApplicationBuild extends Build {
libraryDependencies ++= provided(play.api)
)
lazy val notifications = project("notify", Seq(common, db, user, hub)).settings(
lazy val notifications = project("notify", Seq(common, db, user, hub, relation)).settings(
libraryDependencies ++= provided(play.api, RM)
)