diff options
author | cmrd Senya <senya@riseup.net> | 2016-08-22 15:12:35 +0300 |
---|---|---|
committer | cmrd Senya <senya@riseup.net> | 2016-11-28 16:56:56 +0300 |
commit | 33ad411bbd0d48652d161a60627657f75d467292 (patch) | |
tree | fee7d5c6e0ae71f251e83452bf89d12aa1bed725 /app/models/notifications | |
parent | ef5751b808f7aa47b1ad10790e1c30894ae67b43 (diff) |
Mentions in comments backend changes
Diffstat (limited to 'app/models/notifications')
-rw-r--r-- | app/models/notifications/also_commented.rb | 8 | ||||
-rw-r--r-- | app/models/notifications/comment_on_post.rb | 7 | ||||
-rw-r--r-- | app/models/notifications/commented.rb | 15 | ||||
-rw-r--r-- | app/models/notifications/mentioned.rb | 35 | ||||
-rw-r--r-- | app/models/notifications/mentioned_in_comment.rb | 38 | ||||
-rw-r--r-- | app/models/notifications/mentioned_in_post.rb | 22 |
6 files changed, 95 insertions, 30 deletions
diff --git a/app/models/notifications/also_commented.rb b/app/models/notifications/also_commented.rb index a345566f3..cc28c047c 100644 --- a/app/models/notifications/also_commented.rb +++ b/app/models/notifications/also_commented.rb @@ -1,5 +1,7 @@ module Notifications class AlsoCommented < Notification + include Notifications::Commented + def mail_job Workers::Mail::AlsoCommented end @@ -8,17 +10,13 @@ module Notifications "notifications.also_commented" end - def deleted_translation_key - "notifications.also_commented_deleted" - end - def self.notify(comment, _recipient_user_ids) actor = comment.author commentable = comment.commentable recipient_ids = commentable.participants.local.where.not(id: [commentable.author_id, actor.id]).pluck(:owner_id) User.where(id: recipient_ids).find_each do |recipient| - next if recipient.is_shareable_hidden?(commentable) + next if recipient.is_shareable_hidden?(commentable) || mention_notification_exists?(comment, recipient.person) concatenate_or_create(recipient, commentable, actor).try(:email_the_user, comment, actor) end diff --git a/app/models/notifications/comment_on_post.rb b/app/models/notifications/comment_on_post.rb index df23b558e..ee3320e39 100644 --- a/app/models/notifications/comment_on_post.rb +++ b/app/models/notifications/comment_on_post.rb @@ -1,5 +1,7 @@ module Notifications class CommentOnPost < Notification + include Notifications::Commented + def mail_job Workers::Mail::CommentOnPost end @@ -8,15 +10,12 @@ module Notifications "notifications.comment_on_post" end - def deleted_translation_key - "notifications.also_commented_deleted" - end - def self.notify(comment, _recipient_user_ids) actor = comment.author commentable_author = comment.commentable.author return unless commentable_author.local? && actor != commentable_author + return if mention_notification_exists?(comment, commentable_author) concatenate_or_create(commentable_author.owner, comment.commentable, actor).email_the_user(comment, actor) end diff --git a/app/models/notifications/commented.rb b/app/models/notifications/commented.rb new file mode 100644 index 000000000..e5b1b7d48 --- /dev/null +++ b/app/models/notifications/commented.rb @@ -0,0 +1,15 @@ +module Notifications + module Commented + extend ActiveSupport::Concern + + def deleted_translation_key + "notifications.also_commented_deleted" + end + + module ClassMethods + def mention_notification_exists?(comment, recipient_person) + Notifications::MentionedInComment.exists?(target: comment.mentions.where(person: recipient_person)) + end + end + end +end diff --git a/app/models/notifications/mentioned.rb b/app/models/notifications/mentioned.rb index bede65ee4..b128791bb 100644 --- a/app/models/notifications/mentioned.rb +++ b/app/models/notifications/mentioned.rb @@ -1,30 +1,23 @@ module Notifications - class Mentioned < Notification - def mail_job - Workers::Mail::Mentioned - end - - def popup_translation_key - "notifications.mentioned" - end - - def deleted_translation_key - "notifications.mentioned_deleted" - end + module Mentioned + extend ActiveSupport::Concern def linked_object - target.post + target.mentions_container end - def self.notify(mentionable, recipient_user_ids) - actor = mentionable.author - - mentionable.mentions.select {|mention| mention.person.local? }.each do |mention| - recipient = mention.person - - next if recipient == actor || !(mentionable.public || recipient_user_ids.include?(recipient.owner_id)) + module ClassMethods + def notify(mentionable, recipient_user_ids) + actor = mentionable.author + relevant_mentions = filter_mentions( + mentionable.mentions.local.where.not(person: actor), + mentionable, + recipient_user_ids + ) - create_notification(recipient.owner, mention, actor).try(:email_the_user, mention, actor) + relevant_mentions.each do |mention| + create_notification(mention.person.owner, mention, actor).try(:email_the_user, mention, actor) + end end end end diff --git a/app/models/notifications/mentioned_in_comment.rb b/app/models/notifications/mentioned_in_comment.rb new file mode 100644 index 000000000..77038864a --- /dev/null +++ b/app/models/notifications/mentioned_in_comment.rb @@ -0,0 +1,38 @@ +module Notifications + class MentionedInComment < Notification + include Notifications::Mentioned + + def popup_translation_key + "notifications.mentioned_in_comment" + end + + def deleted_translation_key + "notifications.mentioned_in_comment_deleted" + end + + def self.filter_mentions(mentions, mentionable, _recipient_user_ids) + people = mentionable.people_allowed_to_be_mentioned + if people == :all + mentions + else + mentions.where(person_id: people) + end + end + + def mail_job + if !recipient.user_preferences.exists?(email_type: "mentioned_in_comment") + Workers::Mail::MentionedInComment + elsif shareable.author.owner_id == recipient_id + Workers::Mail::CommentOnPost + elsif shareable.participants.local.where(owner_id: recipient_id) + Workers::Mail::AlsoCommented + end + end + + private + + def shareable + linked_object.parent + end + end +end diff --git a/app/models/notifications/mentioned_in_post.rb b/app/models/notifications/mentioned_in_post.rb new file mode 100644 index 000000000..35ec36c45 --- /dev/null +++ b/app/models/notifications/mentioned_in_post.rb @@ -0,0 +1,22 @@ +module Notifications + class MentionedInPost < Notification + include Notifications::Mentioned + + def mail_job + Workers::Mail::Mentioned + end + + def popup_translation_key + "notifications.mentioned" + end + + def deleted_translation_key + "notifications.mentioned_deleted" + end + + def self.filter_mentions(mentions, mentionable, recipient_user_ids) + return mentions if mentionable.public + mentions.where(person: Person.where(owner_id: recipient_user_ids).ids) + end + end +end |