rosa-build/app/models/subscribe.rb

50 lines
2.6 KiB
Ruby
Raw Normal View History

class Subscribe < ActiveRecord::Base
belongs_to :subscribeable, :polymorphic => true
belongs_to :user
2012-01-23 19:42:54 +00:00
belongs_to :project
validates :status, :inclusion => {:in => 0..1}
scope :subscribed, where(:status => 1)
scope :unsubscribed, where(:status => 0)
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
2012-01-23 19:42:54 +00:00
subscribes = Subscribe.where(:subscribeable_id => comment.commentable.id, :subscribeable_type => comment.commentable.class.name.to_s, :project_id => comment.project).subscribed(true) # FIXME (true) for rspec
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
2012-01-23 19:42:54 +00:00
UserMailer.delay.new_comment_notification(comment, user) if commentable_class == Grit::Commit
2012-01-19 18:20:03 +00:00
end
end
2012-01-23 19:42:54 +00:00
def self.subscribe_user_to_commit(comment, user_id)
subscribe = Subscribe.where(:subscribeable_id => comment.commentable.id, :subscribeable_type => comment.commentable.class.name, :project_id => comment.project).unsubscribed.first
subscribe.update_attribute(:status, 1) if subscribe
Subscribe.create(:subscribeable_id => comment.commentable.id, :subscribeable_type => comment.commentable.class.name.to_s, :user_id => user_id, :project_id => comment.project, :status => 1) unless subscribe
2012-01-19 18:20:03 +00:00
end
2012-01-23 19:42:54 +00:00
def self.subscribed_for_commit?(project, user, commentable)
2012-01-21 19:13:33 +00:00
is_owner = (project.owner_id == user.id)
2012-01-23 19:42:54 +00:00
is_commentor = (Comment.where(:commentable_type => commentable.class.name, :commentable_id => commentable.id).exists?(:user_id => user.id))
is_committer = (user.email == commentable.committer.email)
(is_owner && user.notifier.new_comment_commit_repo_owner) or (is_commentor && user.notifier.new_comment_commit_commentor) or (is_committer && committer.notifier.new_comment_commit_owner)
end
def self.set_subscribe(project, commit, user, status)
# FIXME maybe?
subscribe = Subscribe.where(:subscribeable_id => commit.id, :subscribeable_type => commit.class.name.to_s,
:user_id => user, :project_id => project).first
if subscribe
subscribe.update_attribute(:status, status)
else
Subscribe.create(:subscribeable_id => commit.id, :subscribeable_type => commit.class.name.to_s,
:user_id => user, :project_id => project, :status => status)
end
2012-01-21 19:13:33 +00:00
end
end