rosa-build/app/models/activity_feed_observer.rb

185 lines
9.0 KiB
Ruby
Raw Normal View History

# -*- encoding : utf-8 -*-
class ActivityFeedObserver < ActiveRecord::Observer
2012-03-16 15:37:58 +00:00
observe :issue, :comment, :user, :build_list
BUILD_LIST_STATUSES = [
BuildList::BUILD_PUBLISHED,
BuildList::SUCCESS,
BuildList::BUILD_ERROR,
BuildList::PROJECT_VERSION_NOT_FOUND,
BuildList::FAILED_PUBLISH,
BuildList::TESTS_FAILED
].freeze
def after_create(record)
case record.class.to_s
when 'User'
ActivityFeed.create(
:user => record,
:kind => 'new_user_notification',
2012-03-19 14:45:47 +00:00
:data => {:user_name => record.user_appeal, :user_email => record.email}
)
when 'Issue'
record.collect_recipients.each do |recipient|
next if record.user_id == recipient.id
UserMailer.new_issue_notification(record, recipient).deliver if recipient.notifier.can_notify && recipient.notifier.new_issue
ActivityFeed.create(
:user => recipient,
:kind => 'new_issue_notification',
2012-04-13 20:44:04 +01:00
:data => {:user_name => record.user.name, :user_email => record.user.email, :user_id => record.user_id,:issue_serial_id => record.serial_id,
2012-03-14 17:00:26 +00:00
:issue_title => record.title, :project_id => record.project.id, :project_name => record.project.name, :project_owner => record.project.owner.uname}
)
end
2012-04-13 20:44:04 +01:00
if record.assignee_id_changed?
UserMailer.new_issue_notification(record, record.assignee).deliver if record.assignee.notifier.issue_assign && record.assignee.notifier.can_notify
ActivityFeed.create(
:user => record.user,
:kind => 'issue_assign_notification',
2012-04-13 20:44:04 +01:00
:data => {:user_name => record.user.name, :user_email => record.user.email, :user_id => record.user_id, :issue_serial_id => record.serial_id,
2012-03-14 17:00:26 +00:00
:project_id => record.project.id, :issue_title => record.title, :project_name => record.project.name, :project_owner => record.project.owner.uname}
)
end
2013-04-17 11:58:09 +01:00
record.project.hooks.each{ |h| h.receive_issues(record, :create) }
Comment.create_link_on_issues_from_item(record)
when 'Comment'
2013-03-22 16:46:26 +00:00
return if record.automatic
if record.issue_comment?
subscribes = record.commentable.subscribes
subscribes.each do |subscribe|
if record.user_id != subscribe.user_id
UserMailer.new_comment_notification(record, subscribe.user).deliver if record.can_notify_on_new_comment?(subscribe)
ActivityFeed.create(
:user => subscribe.user,
:kind => 'new_comment_notification',
2012-03-14 14:40:41 +00:00
:data => {:user_name => record.user.name, :user_email => record.user.email, :user_id => record.user_id, :comment_body => record.body,
:issue_title => record.commentable.title, :issue_serial_id => record.commentable.serial_id, :project_id => record.commentable.project.id,
2012-03-14 17:00:26 +00:00
:comment_id => record.id, :project_name => record.project.name, :project_owner => record.project.owner.uname}
)
end
end
elsif record.commit_comment?
subscribes = Subscribe.comment_subscribes(record).where(:status => true)
subscribes.each do |subscribe|
next if record.own_comment?(subscribe.user)
if subscribe.user.notifier.can_notify and
( (subscribe.project.owner?(subscribe.user) && subscribe.user.notifier.new_comment_commit_repo_owner) or
(subscribe.user.commentor?(record.commentable) && subscribe.user.notifier.new_comment_commit_commentor) or
(subscribe.user.committer?(record.commentable) && subscribe.user.notifier.new_comment_commit_owner) )
UserMailer.new_comment_notification(record, subscribe.user).deliver
end
2013-03-26 10:47:42 +00:00
ActivityFeed.create(
:user => subscribe.user,
:kind => 'new_comment_commit_notification',
:data => {:user_name => record.user.name, :user_email => record.user.email, :user_id => record.user_id, :comment_body => record.body,
:commit_message => record.commentable.message, :commit_id => record.commentable.id,
:project_id => record.project.id, :comment_id => record.id, :project_name => record.project.name, :project_owner => record.project.owner.uname}
)
end
end
2013-03-26 10:47:42 +00:00
Comment.create_link_on_issues_from_item(record)
2012-02-10 17:33:05 +00:00
when 'GitHook'
2012-04-02 20:27:36 +01:00
return unless record.project
2012-10-05 18:32:29 +01:00
PullRequest.where("from_project_id = ? OR to_project_id = ?", record.project, record.project).needed_checking.each {|pull| pull.check}
2013-04-25 16:27:48 +01:00
record.project.hooks.each{ |h| h.receive_push(record) }
2012-08-06 19:13:07 +01:00
2012-02-10 17:33:05 +00:00
change_type = record.change_type
branch_name = record.refname.split('/').last
2012-02-10 17:33:05 +00:00
if change_type == 'delete'
2012-02-14 16:46:08 +00:00
kind = 'git_delete_branch_notification'
2012-03-14 17:00:26 +00:00
options = {:project_id => record.project.id, :project_name => record.project.name, :branch_name => branch_name,
:change_type => change_type, :project_owner => record.project.owner.uname}
2012-02-10 17:33:05 +00:00
else
if record.message # online update
last_commits, commits = [[record.newrev, record.message]], []
else
commits = record.project.repo.commits_between(record.oldrev, record.newrev)
last_commits = commits.last(3).collect { |commit| [commit.sha, commit.message] }
end
2012-02-14 16:46:08 +00:00
kind = 'git_new_push_notification'
options = {:project_id => record.project.id, :project_name => record.project.name, :last_commits => last_commits.reverse,
:branch_name => branch_name, :change_type => change_type, :project_owner => record.project.owner.uname}
if commits.count > 3
commits = commits[0...-3]
options.merge!({:other_commits_count => commits.count, :other_commits => "#{commits[0].sha[0..9]}...#{commits[-1].sha[0..9]}"})
end
2013-03-26 10:47:42 +00:00
Comment.create_link_on_issues_from_item(record, last_commits) if last_commits.count > 0
2012-02-14 16:46:08 +00:00
end
options.merge!({:user_id => record.user.id, :user_name => record.user.name, :user_email => record.user.email}) if record.user
2012-02-14 16:46:08 +00:00
record.project.admins.each do |recipient|
next if record.user && record.user.id == recipient.id
ActivityFeed.create!(
:user => recipient,
2012-02-14 16:46:08 +00:00
:kind => kind,
:data => options
2012-02-10 17:33:05 +00:00
)
end
when 'Hash' # 'Gollum::Committer'
actor = User.find_by_uname! record[:actor_name]
project = Project.find record[:project_id]
2012-02-14 16:09:41 +00:00
project.admins.each do |recipient|
ActivityFeed.create!(
:user => recipient,
2012-02-14 16:09:41 +00:00
:kind => 'wiki_new_commit_notification',
2012-03-14 17:00:26 +00:00
:data => {:user_id => actor.id, :user_name => actor.name, :user_email => actor.email, :project_id => project.id,
:project_name => project.name, :commit_sha => record[:commit_sha], :project_owner => project.owner.uname}
2012-02-14 16:09:41 +00:00
)
end
end
end
def after_update(record)
case record.class.to_s
when 'Issue'
2012-04-13 20:44:04 +01:00
if record.assignee_id && record.assignee_id_changed?
UserMailer.issue_assign_notification(record, record.assignee).deliver if record.assignee.notifier.issue_assign && record.assignee.notifier.can_notify
ActivityFeed.create(
2012-04-13 20:44:04 +01:00
:user => record.assignee,
:kind => 'issue_assign_notification',
2012-04-13 20:44:04 +01:00
:data => {:user_name => record.assignee.name, :user_email => record.assignee.email, :issue_serial_id => record.serial_id, :issue_title => record.title,
2012-03-14 17:00:26 +00:00
:project_id => record.project.id, :project_name => record.project.name, :project_owner => record.project.owner.uname}
)
end
2013-04-17 11:58:09 +01:00
record.project.hooks.each{ |h| h.receive_issues(record, :update) } if record.status_changed?
# dont remove outdated issues link
Comment.create_link_on_issues_from_item(record)
2012-03-16 15:37:58 +00:00
when 'BuildList'
2013-04-04 12:17:34 +01:00
if record.mass_build.blank? && ( # Do not show mass build activity in activity feeds
record.status_changed? && BUILD_LIST_STATUSES.include?(record.status) ||
record.status == BuildList::BUILD_PENDING && record.bs_id_changed?
)
record.project.admins.each do |recipient|
user = record.publisher || record.user
2012-03-16 15:37:58 +00:00
ActivityFeed.create(
:user => recipient,
2012-03-16 15:37:58 +00:00
:kind => 'build_list_notification',
2013-03-28 13:33:08 +00:00
:data => {
:task_num => record.bs_id,
:build_list_id => record.id,
:status => record.status,
:updated_at => record.updated_at,
:project_id => record.project_id,
:project_name => record.project.name,
:project_owner => record.project.owner.uname,
:user_name => user.name,
:user_email => user.email,
:user_id => user.id
2013-03-28 13:33:08 +00:00
}
2012-03-16 15:37:58 +00:00
)
end
end
when 'Comment'
# dont remove outdated issues link
Comment.create_link_on_issues_from_item(record)
end
end
end