[refs #114] refactoring subscribe
This commit is contained in:
parent
d8df6c6279
commit
21d422c313
|
@ -7,38 +7,18 @@ class Comment < ActiveRecord::Base
|
|||
|
||||
after_create :invoke_helper, :if => "commentable_type == 'Grit::Commit'"
|
||||
after_create :subscribe_on_reply
|
||||
after_create :deliver_new_comment_notification
|
||||
after_create {|comment| Subscribe.new_comment_notification(comment)}
|
||||
|
||||
def helper
|
||||
class_eval "def commentable; project.git_repository.commit('#{commentable_id}'); end" if commentable_type == 'Grit::Commit'
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def deliver_new_comment_notification
|
||||
subscribes = self.commentable.subscribes if self.commentable_type == 'Issue'
|
||||
|
||||
if self.commentable_type == 'Grit::Commit'
|
||||
subscribe_committer
|
||||
subscribes = self.project.commit_comments_subscribes(true) # FIXME (true) for rspec
|
||||
end
|
||||
subscribes.each do |subscribe|
|
||||
if self.commentable_type == 'Issue' && self.user_id != subscribe.user_id && User.find(subscribe.user).notifier.new_comment_reply && User.find(subscribe.user).notifier.can_notify
|
||||
if self.commentable.comments.exists?(:user_id => subscribe.user.id)
|
||||
UserMailer.delay.new_comment_reply_notification(self, subscribe.user)
|
||||
else
|
||||
UserMailer.delay.new_comment_notification(self, subscribe.user)
|
||||
end
|
||||
elsif self.commentable_type == 'Grit::Commit' && self.user_id != subscribe.user_id && User.find(subscribe.user).notifier.new_comment_commit_repo_owner && User.find(subscribe.user).notifier.can_notify
|
||||
if Comment.where(:commentable_type => 'Grit::Commit', :commentable_id => self.commentable.id, :user_id => subscribe.user.id).exists?
|
||||
UserMailer.delay.new_comment_reply_notification(self, subscribe.user)
|
||||
else
|
||||
UserMailer.delay.new_comment_notification(self, subscribe.user)
|
||||
end
|
||||
end
|
||||
end
|
||||
def own_comment?(user)
|
||||
user_id == user.id
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def subscribe_on_reply
|
||||
self.commentable.subscribes.create(:user_id => self.user_id) if self.commentable_type == 'Issue' && !self.commentable.subscribes.exists?(:user_id => self.user_id)
|
||||
self.project.commit_comments_subscribes.create(:user_id => self.user_id) if self.commentable_type == 'Grit::Commit' && !self.project.commit_comments_subscribes.exists?(:user_id => self.user_id)
|
||||
|
|
|
@ -2,25 +2,33 @@ class Subscribe < ActiveRecord::Base
|
|||
belongs_to :subscribeable, :polymorphic => true
|
||||
belongs_to :user
|
||||
|
||||
def self.subscribe_users(project)
|
||||
recipients = Subscribe.collect_recipient_ids(project)
|
||||
recipients.each do |recipient_id|
|
||||
ss = project.commit_comments_subscribes.build(:user_id => recipient_id)
|
||||
ss.save!
|
||||
def self.new_comment_notification(comment)
|
||||
commentable_class = comment.commentable.class
|
||||
subscribes = comment.commentable.subscribes if commentable_class == Issue
|
||||
if commentable_class == Grit::Commit
|
||||
Subscribe.subscribe_committer(comment)
|
||||
subscribes = comment.project.commit_comments_subscribes(true) # FIXME (true) for rspec
|
||||
end
|
||||
subscribes.each do |subscribe|
|
||||
user = subscribe.user
|
||||
next if comment.own_comment?(user) || !user.notifier.can_notify
|
||||
Subscribe.send_notification(comment, user) if commentable_class == Issue && user.notifier.new_comment_reply
|
||||
Subscribe.send_notification(comment, user) if commentable_class == Grit::Commit && user.notifier.new_comment_commit_repo_owner
|
||||
end
|
||||
end
|
||||
|
||||
def self.collect_recipient_ids(project)
|
||||
recipients = project.relations.by_role('admin').where(:object_type => 'User').map { |rel| rel.read_attribute(:object_id) }
|
||||
# recipients = recipients | [commentable.user_id] if commentable.user_id
|
||||
# recipients = recipients | [commentable.project.owner_id] if commentable.project.owner_type == 'User'
|
||||
|
||||
# filter by notification settings
|
||||
recipients = recipients.select do |recipient|
|
||||
User.find(recipient).notifier.new_issue && User.find(recipient).notifier.can_notify
|
||||
def self.subscribe_committer(comment)
|
||||
committer = User.where(:email => comment.commentable.committer.email).first
|
||||
if committer && !comment.project.commit_comments_subscribes.exists?(:user_id => committer.id) && committer.notifier.new_comment_commit_owner
|
||||
comment.project.commit_comments_subscribes.create(:user_id => committer.id)
|
||||
end
|
||||
|
||||
recipients
|
||||
end
|
||||
|
||||
def self.send_notification(comment, user)
|
||||
if Comment.where(:commentable_type => comment.commentable_type, :commentable_id => comment.commentable.id.to_s, :user_id => user.id).exists?
|
||||
UserMailer.delay.new_comment_reply_notification(comment, user)
|
||||
else
|
||||
UserMailer.delay.new_comment_notification(comment, user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue