rosa-build/app/models/subscribe.rb

32 lines
1.7 KiB
Ruby
Raw Normal View History

class Subscribe < ActiveRecord::Base
belongs_to :subscribeable, :polymorphic => true
belongs_to :user
2012-01-19 18:20:03 +00:00
2012-01-21 13:32:22 +00:00
def self.new_comment_notification(comment)
commentable_class = comment.commentable.class
subscribes = comment.commentable.subscribes if commentable_class == Issue
if commentable_class == Grit::Commit
subscribes = comment.project.commit_comments_subscribes(true) # FIXME (true) for rspec
2012-01-21 19:13:33 +00:00
committer = User.where(:email => comment.commentable.committer.email).first
2012-01-22 17:49:41 +00:00
UserMailer.delay.new_comment_notification(comment, committer) if committer && !comment.own_comment?(committer) && committer.notifier.new_comment_commit_owner && !committer.notifier.can_notify && subscribes.where(:user_id => committer).empty?
2012-01-21 13:32:22 +00:00
end
subscribes.each do |subscribe|
2012-01-21 19:13:33 +00:00
user = subscribe.user
next if comment.own_comment?(user) || !user.notifier.can_notify
UserMailer.delay.new_comment_notification(comment, user) if commentable_class == Issue && user.notifier.new_comment_reply
UserMailer.delay.new_comment_notification(comment, user) if commentable_class == Grit::Commit && Subscribe.send_notification_for_commit_comment?(subscribe.subscribeable, user, comment)
2012-01-19 18:20:03 +00:00
end
end
2012-01-21 19:13:33 +00:00
def self.subscribe_user(project_id, user_id)
list = Project.find(project_id).commit_comments_subscribes
list.create(:user_id => user_id) unless list.exists?(:user_id => user_id)
2012-01-19 18:20:03 +00:00
end
2012-01-21 19:13:33 +00:00
def self.send_notification_for_commit_comment?(project, user, comment)
is_owner = (project.owner_id == user.id)
is_commentor = (project.commit_comments_subscribes.exists?(:user_id => user.id))
(is_owner && user.notifier.new_comment_commit_repo_owner) or (is_commentor && user.notifier.new_comment_commit_commentor)
end
end