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 | |
parent | ef5751b808f7aa47b1ad10790e1c30894ae67b43 (diff) |
Mentions in comments backend changes
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/comment.rb | 19 | ||||
-rw-r--r-- | app/models/mention.rb | 8 | ||||
-rw-r--r-- | app/models/notification.rb | 11 | ||||
-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 | ||||
-rw-r--r-- | app/models/post.rb | 6 | ||||
-rw-r--r-- | app/models/status_message.rb | 49 | ||||
-rw-r--r-- | app/models/user.rb | 1 | ||||
-rw-r--r-- | app/models/user_preference.rb | 15 |
13 files changed, 143 insertions, 91 deletions
diff --git a/app/models/comment.rb b/app/models/comment.rb index 8d98e52ec..483215f03 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -11,6 +11,7 @@ class Comment < ActiveRecord::Base include Diaspora::Taggable include Diaspora::Likeable + include Diaspora::MentionsContainer acts_as_taggable_on :tags extract_tags_from :text @@ -49,14 +50,22 @@ class Comment < ActiveRecord::Base participation.unparticipate! if participation.present? end - def message - @message ||= Diaspora::MessageRenderer.new text - end - def text= text self[:text] = text.to_s.strip #to_s if for nil, for whatever reason end + def people_allowed_to_be_mentioned + if parent.public? + :all + else + [*parent.comments.pluck(:author_id), *parent.likes.pluck(:author_id), parent.author_id].uniq + end + end + + def add_mention_subscribers? + super && parent.author.local? + end + class Generator < Diaspora::Federated::Generator def self.federated_class Comment @@ -68,7 +77,7 @@ class Comment < ActiveRecord::Base end def relayable_options - {:post => @target, :text => @text} + {post: @target, text: @text} end end end diff --git a/app/models/mention.rb b/app/models/mention.rb index 9ddfd8dad..597cebe94 100644 --- a/app/models/mention.rb +++ b/app/models/mention.rb @@ -3,11 +3,15 @@ # the COPYRIGHT file. class Mention < ActiveRecord::Base - belongs_to :post + belongs_to :mentions_container, polymorphic: true belongs_to :person - validates :post, presence: true + validates :mentions_container, presence: true validates :person, presence: true + scope :local, -> { + joins(:person).where.not(people: {owner_id: nil}) + } + after_destroy :delete_notification def delete_notification diff --git a/app/models/notification.rb b/app/models/notification.rb index aaa56d53d..c4b557045 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -51,15 +51,4 @@ class Notification < ActiveRecord::Base private_class_method def self.suppress_notification?(recipient, actor) recipient.blocks.where(person: actor).exists? end - - def self.types - { - "also_commented" => "Notifications::AlsoCommented", - "comment_on_post" => "Notifications::CommentOnPost", - "liked" => "Notifications::Liked", - "mentioned" => "Notifications::Mentioned", - "reshared" => "Notifications::Reshared", - "started_sharing" => "Notifications::StartedSharing" - } - end end 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 diff --git a/app/models/post.rb b/app/models/post.rb index 3ad235f0f..576717f79 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -12,16 +12,15 @@ class Post < ActiveRecord::Base include Diaspora::Likeable include Diaspora::Commentable include Diaspora::Shareable + include Diaspora::MentionsContainer has_many :participations, dependent: :delete_all, as: :target, inverse_of: :target - has_many :participants, class_name: "Person", through: :participations, source: :author + has_many :participants, through: :participations, source: :author attr_accessor :user_like has_many :reports, as: :item - has_many :mentions, dependent: :destroy - has_many :reshares, class_name: "Reshare", foreign_key: :root_guid, primary_key: :guid has_many :resharers, class_name: "Person", through: :reshares, source: :author @@ -60,7 +59,6 @@ class Post < ActiveRecord::Base end def root; end - def mentioned_people; []; end def photos; []; end #prevents error when trying to access @post.address in a post different than Reshare and StatusMessage types; diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 6ee7d388e..28d838536 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -23,13 +23,12 @@ class StatusMessage < Post attr_accessor :oembed_url attr_accessor :open_graph_url - after_create :create_mentions after_commit :queue_gather_oembed_data, :on => :create, :if => :contains_oembed_url_in_text? after_commit :queue_gather_open_graph_data, :on => :create, :if => :contains_open_graph_url_in_text? #scopes scope :where_person_is_mentioned, ->(person) { - joins(:mentions).where(:mentions => {:person_id => person.id}) + owned_or_visible_by_user(person.owner).joins(:mentions).where(mentions: {person_id: person.id}) } def self.guids_for_author(person) @@ -52,36 +51,6 @@ class StatusMessage < Post text.try(:match, /#nsfw/i) || super end - def message - @message ||= Diaspora::MessageRenderer.new(text, mentioned_people: mentioned_people) - end - - def mentioned_people - if self.persisted? - self.mentions.includes(:person => :profile).map{ |mention| mention.person } - else - Diaspora::Mentionable.people_from_string(text) - end - end - - ## TODO ---- - # don't put presentation logic in the model! - def mentioned_people_names - self.mentioned_people.map(&:name).join(', ') - end - ## ---- ---- - - def create_mentions - ppl = Diaspora::Mentionable.people_from_string(text) - ppl.each do |person| - self.mentions.find_or_create_by(person_id: person.id) - end - end - - def mentions?(person) - mentioned_people.include? person - end - def comment_email_subject message.title end @@ -126,6 +95,22 @@ class StatusMessage < Post photos.each {|photo| photo.receive(recipient_user_ids) } end + # Only includes those people, to whom we're going to send a federation entity + # (and doesn't define exhaustive list of people who can receive it) + def people_allowed_to_be_mentioned + @aspects_ppl ||= + if public? + :all + else + Contact.joins(:aspect_memberships).where(aspect_memberships: {aspect: aspects}).distinct.pluck(:person_id) + end + end + + def filter_mentions + return if people_allowed_to_be_mentioned == :all + update(text: Diaspora::Mentionable.filter_people(text, people_allowed_to_be_mentioned)) + end + private def presence_of_content diff --git a/app/models/user.rb b/app/models/user.rb index e8a09a322..85ea59cb6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -333,6 +333,7 @@ class User < ActiveRecord::Base ######### Mailer ####################### def mail(job, *args) + return unless job.present? pref = job.to_s.gsub('Workers::Mail::', '').underscore if(self.disable_mail == false && !self.user_preferences.exists?(:email_type => pref)) job.perform_async(*args) diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index 354a48400..ea4f92425 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -5,13 +5,14 @@ class UserPreference < ActiveRecord::Base VALID_EMAIL_TYPES = ["someone_reported", - "mentioned", - "comment_on_post", - "private_message", - "started_sharing", - "also_commented", - "liked", - "reshared"] + "mentioned", + "mentioned_in_comment", + "comment_on_post", + "private_message", + "started_sharing", + "also_commented", + "liked", + "reshared"] def must_be_valid_email_type unless VALID_EMAIL_TYPES.include?(self.email_type) |