2012-03-22 17:11:12 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2012-01-25 08:31:49 +00:00
|
|
|
class ActivityFeedObserver < ActiveRecord::Observer
|
2012-03-16 15:37:58 +00:00
|
|
|
observe :issue, :comment, :user, :build_list
|
2012-01-25 08:31:49 +00:00
|
|
|
|
2013-04-04 12:12:07 +01:00
|
|
|
BUILD_LIST_STATUSES = [
|
|
|
|
BuildList::BUILD_PUBLISHED,
|
|
|
|
BuildList::SUCCESS,
|
|
|
|
BuildList::BUILD_ERROR,
|
|
|
|
BuildList::PROJECT_VERSION_NOT_FOUND,
|
|
|
|
BuildList::FAILED_PUBLISH,
|
|
|
|
BuildList::TESTS_FAILED
|
|
|
|
].freeze
|
|
|
|
|
2012-01-25 08:31:49 +00:00
|
|
|
def after_create(record)
|
|
|
|
case record.class.to_s
|
|
|
|
when 'User'
|
2012-01-27 14:59:28 +00:00
|
|
|
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}
|
2012-01-27 14:59:28 +00:00
|
|
|
)
|
2012-01-25 13:05:39 +00:00
|
|
|
|
2012-01-25 08:31:49 +00:00
|
|
|
when 'Issue'
|
2012-12-13 18:18:22 +00:00
|
|
|
record.collect_recipients.each do |recipient|
|
2013-01-11 14:00:22 +00:00
|
|
|
next if record.user_id == recipient.id
|
2012-12-13 18:18:22 +00:00
|
|
|
UserMailer.new_issue_notification(record, recipient).deliver if recipient.notifier.can_notify && recipient.notifier.new_issue
|
2012-01-26 17:04:58 +00:00
|
|
|
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}
|
2012-01-26 17:04:58 +00:00
|
|
|
)
|
2012-01-25 08:31:49 +00:00
|
|
|
end
|
|
|
|
|
2012-04-13 20:44:04 +01:00
|
|
|
if record.assignee_id_changed?
|
2012-06-16 19:27:46 +01:00
|
|
|
UserMailer.new_issue_notification(record, record.assignee).deliver if record.assignee.notifier.issue_assign && record.assignee.notifier.can_notify
|
2012-01-27 12:06:04 +00:00
|
|
|
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}
|
2012-01-27 12:06:04 +00:00
|
|
|
)
|
|
|
|
end
|
2013-04-17 11:58:09 +01:00
|
|
|
record.project.hooks.each{ |h| h.receive_issues(record, :create) }
|
2013-04-09 19:46:58 +01:00
|
|
|
Comment.create_link_on_issues_from_item(record)
|
2012-01-25 08:31:49 +00:00
|
|
|
when 'Comment'
|
2013-03-22 16:46:26 +00:00
|
|
|
return if record.automatic
|
2012-04-04 22:43:06 +01:00
|
|
|
if record.issue_comment?
|
2012-03-13 18:47:37 +00:00
|
|
|
subscribes = record.commentable.subscribes
|
2012-02-24 18:25:35 +00:00
|
|
|
subscribes.each do |subscribe|
|
|
|
|
if record.user_id != subscribe.user_id
|
2012-06-16 19:27:46 +01:00
|
|
|
UserMailer.new_comment_notification(record, subscribe.user).deliver if record.can_notify_on_new_comment?(subscribe)
|
2012-01-26 17:04:58 +00:00
|
|
|
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}
|
2012-01-26 17:04:58 +00:00
|
|
|
)
|
2012-01-25 08:31:49 +00:00
|
|
|
end
|
|
|
|
end
|
2012-03-03 00:19:31 +00:00
|
|
|
elsif record.commit_comment?
|
2012-02-24 18:25:35 +00:00
|
|
|
subscribes = Subscribe.comment_subscribes(record).where(:status => true)
|
|
|
|
subscribes.each do |subscribe|
|
|
|
|
next if record.own_comment?(subscribe.user)
|
2012-03-14 14:33:16 +00:00
|
|
|
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) )
|
2012-06-16 19:27:46 +01:00
|
|
|
UserMailer.new_comment_notification(record, subscribe.user).deliver
|
2012-03-14 14:33:16 +00:00
|
|
|
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}
|
|
|
|
)
|
2012-02-24 18:25:35 +00:00
|
|
|
end
|
2012-01-25 08:31:49 +00:00
|
|
|
end
|
2013-03-26 10:47:42 +00:00
|
|
|
Comment.create_link_on_issues_from_item(record)
|
2012-03-05 09:11:50 +00:00
|
|
|
|
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
|
2012-03-22 17:11:12 +00:00
|
|
|
branch_name = record.refname.split('/').last
|
2013-03-07 08:06:34 +00:00
|
|
|
|
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
|
2013-03-07 08:06:34 +00:00
|
|
|
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'
|
2013-03-07 08:06:34 +00:00
|
|
|
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
|
2013-03-07 08:06:34 +00:00
|
|
|
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
|
|
|
|
2012-12-13 18:18:22 +00:00
|
|
|
record.project.admins.each do |recipient|
|
2013-03-07 08:06:34 +00:00
|
|
|
next if record.user && record.user.id == recipient.id
|
2012-06-16 19:27:46 +01:00
|
|
|
ActivityFeed.create!(
|
2012-12-13 18:18:22 +00:00
|
|
|
:user => recipient,
|
2012-02-14 16:46:08 +00:00
|
|
|
:kind => kind,
|
|
|
|
:data => options
|
2012-02-10 17:33:05 +00:00
|
|
|
)
|
|
|
|
end
|
2012-03-05 09:11:50 +00:00
|
|
|
|
2012-06-16 19:27:46 +01:00
|
|
|
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
|
|
|
|
2012-12-13 18:18:22 +00:00
|
|
|
project.admins.each do |recipient|
|
2012-06-16 19:27:46 +01:00
|
|
|
ActivityFeed.create!(
|
2012-12-13 18:18:22 +00:00
|
|
|
: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,
|
2012-06-16 19:27:46 +01:00
|
|
|
:project_name => project.name, :commit_sha => record[:commit_sha], :project_owner => project.owner.uname}
|
2012-02-14 16:09:41 +00:00
|
|
|
)
|
|
|
|
end
|
2012-01-25 08:31:49 +00:00
|
|
|
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?
|
2012-06-16 19:27:46 +01:00
|
|
|
UserMailer.issue_assign_notification(record, record.assignee).deliver if record.assignee.notifier.issue_assign && record.assignee.notifier.can_notify
|
2012-01-27 12:06:04 +00:00
|
|
|
ActivityFeed.create(
|
2012-04-13 20:44:04 +01:00
|
|
|
:user => record.assignee,
|
2012-01-27 12:06:04 +00:00
|
|
|
: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}
|
2012-01-27 12:06:04 +00:00
|
|
|
)
|
|
|
|
end
|
2013-04-17 11:58:09 +01:00
|
|
|
record.project.hooks.each{ |h| h.receive_issues(record, :update) } if record.status_changed?
|
2013-04-09 19:46:58 +01:00
|
|
|
# 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
|
2013-04-04 12:12:07 +01:00
|
|
|
record.status_changed? && BUILD_LIST_STATUSES.include?(record.status) ||
|
|
|
|
record.status == BuildList::BUILD_PENDING && record.bs_id_changed?
|
|
|
|
)
|
|
|
|
|
2012-12-13 18:18:22 +00:00
|
|
|
record.project.admins.each do |recipient|
|
2013-04-02 15:20:22 +01:00
|
|
|
user = record.publisher || record.user
|
2012-03-16 15:37:58 +00:00
|
|
|
ActivityFeed.create(
|
2012-12-13 18:18:22 +00:00
|
|
|
: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,
|
2013-04-02 15:20:22 +01:00
|
|
|
: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
|
2013-03-26 17:27:46 +00:00
|
|
|
when 'Comment'
|
|
|
|
# dont remove outdated issues link
|
|
|
|
Comment.create_link_on_issues_from_item(record)
|
2012-01-25 08:31:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|