diff --git a/app/models/activity_feed_observer.rb b/app/models/activity_feed_observer.rb index ae3c1ae05..532b945dc 100644 --- a/app/models/activity_feed_observer.rb +++ b/app/models/activity_feed_observer.rb @@ -1,88 +1,8 @@ # -*- encoding : utf-8 -*- class ActivityFeedObserver < ActiveRecord::Observer - observe :comment, :user, :build_list - BUILD_LIST_STATUSES = [ - BuildList::BUILD_PUBLISHED, - BuildList::SUCCESS, - BuildList::BUILD_ERROR, - BuildList::FAILED_PUBLISH, - BuildList::TESTS_FAILED - ].freeze - - def after_commit(record) - # See: - # - http://rails-bestpractices.com/posts/695-use-after_commit - # - https://coderwall.com/p/f5-vlq - return unless record.send(:transaction_include_action?, :create) + def after_create(record) case record.class.to_s - when 'User' - ActivityFeed.create( - :user => record, - :kind => 'new_user_notification', - :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', - # :data => {:user_name => record.user.name, :user_email => record.user.email, :user_id => record.user_id,:issue_serial_id => record.serial_id, - # :issue_title => record.title, :project_id => record.project.id, :project_name => record.project.name, :project_owner => record.project.owner.uname} - # ) - # end - - # 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', - # :data => {:user_name => record.user.name, :user_email => record.user.email, :user_id => record.user_id, :issue_serial_id => record.serial_id, - # :project_id => record.project.id, :issue_title => record.title, :project_name => record.project.name, :project_owner => record.project.owner.uname} - # ) - # end - # record.project.hooks.each{ |h| h.receive_issues(record, :create) } - # Comment.create_link_on_issues_from_item(record) - when 'Comment' - 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', - :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, - :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 - 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 - Comment.create_link_on_issues_from_item(record) - when 'GitHook' return unless record.project PullRequest.where("from_project_id = ? OR to_project_id = ?", record.project, record.project).needed_checking.each {|pull| pull.check} @@ -140,50 +60,4 @@ class ActivityFeedObserver < ActiveRecord::Observer end end - def after_update(record) - case record.class.to_s - # when 'Issue' - # 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( - # :user => record.assignee, - # :kind => 'issue_assign_notification', - # :data => {:user_name => record.assignee.name, :user_email => record.assignee.email, :issue_serial_id => record.serial_id, :issue_title => record.title, - # :project_id => record.project.id, :project_name => record.project.name, :project_owner => record.project.owner.uname} - # ) - # end - # 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) - when 'BuildList' - 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 - ActivityFeed.create( - :user => recipient, - :kind => 'build_list_notification', - :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 - } - ) - end - end - when 'Comment' - # dont remove outdated issues link - Comment.create_link_on_issues_from_item(record) - end - end end diff --git a/app/models/build_list.rb b/app/models/build_list.rb index 7ab46c41c..adf967c07 100644 --- a/app/models/build_list.rb +++ b/app/models/build_list.rb @@ -3,6 +3,7 @@ class BuildList < ActiveRecord::Base include Modules::Models::CommitAndVersion include Modules::Models::FileStoreClean include AbfWorker::ModelHelper + include Modules::Observers::ActivityFeed::BuildList belongs_to :project belongs_to :arch diff --git a/app/models/comment.rb b/app/models/comment.rb index f78148b86..7272d6a9b 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,5 +1,7 @@ # -*- encoding : utf-8 -*- class Comment < ActiveRecord::Base + include Modules::Observers::ActivityFeed::Comment + # regexp take from http://code.google.com/p/concerto-platform/source/browse/v3/cms/lib/CodeMirror/mode/gfm/gfm.js?spec=svn861&r=861#71 # User/Project#Num # User#Num @@ -54,10 +56,6 @@ class Comment < ActiveRecord::Base user_id == user.id end - def can_notify_on_new_comment?(subscribe) - User.find(subscribe.user).notifier.new_comment && User.find(subscribe.user).notifier.can_notify - end - def actual_inline_comment?(diff = nil, force = false) unless force raise "This is not inline comment!" if data.blank? # for debug diff --git a/app/models/user.rb b/app/models/user.rb index a53f530e1..1c4322cc2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -60,6 +60,7 @@ class User < Avatar include Modules::Models::PersonalRepository include Modules::Models::ActsLikeMember + include Modules::Observers::ActivityFeed::User def admin? role == 'admin' diff --git a/lib/modules/observers/activity_feed/build_list.rb b/lib/modules/observers/activity_feed/build_list.rb new file mode 100644 index 000000000..ca904a788 --- /dev/null +++ b/lib/modules/observers/activity_feed/build_list.rb @@ -0,0 +1,46 @@ +# -*- encoding : utf-8 -*- +module Modules::Observers::ActivityFeed::BuildList + extend ActiveSupport::Concern + + included do + after_update :build_list_notifications + end + + private + + def build_list_notifications + if mass_build.blank? && ( # Do not show mass build activity in activity feeds + status_changed? && [ + BUILD_PUBLISHED, + SUCCESS, + BUILD_ERROR, + FAILED_PUBLISH, + TESTS_FAILED + ].include?(status) || + status == BUILD_PENDING && bs_id_changed? + ) + + updater = publisher || user + project.admins.each do |recipient| + ActivityFeed.create( + :user => recipient, + :kind => 'build_list_notification', + :data => { + :task_num => bs_id, + :build_list_id => id, + :status => status, + :updated_at => updated_at, + :project_id => project_id, + :project_name => project.name, + :project_owner => project.owner.uname, + :user_name => updater.name, + :user_email => updater.email, + :user_id => updater.id + } + ) + end + + end + end + +end \ No newline at end of file diff --git a/lib/modules/observers/activity_feed/comment.rb b/lib/modules/observers/activity_feed/comment.rb new file mode 100644 index 000000000..920169b4d --- /dev/null +++ b/lib/modules/observers/activity_feed/comment.rb @@ -0,0 +1,59 @@ +# -*- encoding : utf-8 -*- +module Modules::Observers::ActivityFeed::Comment + extend ActiveSupport::Concern + + included do + after_commit :new_comment_notifications, :on => :create, :unless => :automatic? + # dont remove outdated issues link + after_update -> { Comment.create_link_on_issues_from_item(self) } + end + + private + + def new_comment_notifications + if issue_comment? + commentable.subscribes.each do |subscribe| + if user_id != subscribe.user_id + UserMailer.new_comment_notification(self, subscribe.user).deliver if can_notify_on_new_comment?(subscribe) + send_new_comment_commit_notification(subscribe) + end + end + elsif commit_comment? + Subscribe.comment_subscribes(self).where(:status => true).each do |subscribe| + next if 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?(self.commentable) && subscribe.user.notifier.new_comment_commit_commentor) or + (subscribe.user.committer?(self.commentable) && subscribe.user.notifier.new_comment_commit_owner) ) + UserMailer.new_comment_notification(self, subscribe.user).deliver + end + send_new_comment_commit_notification(subscribe) + end + end + Comment.create_link_on_issues_from_item(self) + end + + def send_new_comment_commit_notification(subscribe) + ActivityFeed.create( + :user => subscribe.user, + :kind => 'new_comment_commit_notification', + :data => { + :user_name => user.name, + :user_email => user.email, + :user_id => user_id, + :comment_body => body, + :commit_message => commentable.message, + :commit_id => commentable.id, + :project_id => project.id, + :comment_id => id, + :project_name => project.name, + :project_owner => project.owner.uname + } + ) + end + + def can_notify_on_new_comment?(subscribe) + User.find(subscribe.user).notifier.new_comment && User.find(subscribe.user).notifier.can_notify + end + +end \ No newline at end of file diff --git a/lib/modules/observers/activity_feed/user.rb b/lib/modules/observers/activity_feed/user.rb new file mode 100644 index 000000000..971378b30 --- /dev/null +++ b/lib/modules/observers/activity_feed/user.rb @@ -0,0 +1,19 @@ +# -*- encoding : utf-8 -*- +module Modules::Observers::ActivityFeed::User + extend ActiveSupport::Concern + + included do + after_commit :new_user_notification, :on => :create + end + + private + + def new_user_notification + ActivityFeed.create( + :user => self, + :kind => 'new_user_notification', + :data => {:user_name => self.user_appeal, :user_email => self.email} + ) + end + +end \ No newline at end of file diff --git a/spec/factories/comments.rb b/spec/factories/comments.rb index 145b50ca6..76119494c 100644 --- a/spec/factories/comments.rb +++ b/spec/factories/comments.rb @@ -4,5 +4,6 @@ FactoryGirl.define do body { FactoryGirl.generate(:string) } association :user, :factory => :user association :commentable, :factory => :issue + project { |c| c.commentable.project } end end diff --git a/spec/lib/observers/activity_feed/comment_spec.rb b/spec/lib/observers/activity_feed/comment_spec.rb new file mode 100644 index 000000000..139e9e114 --- /dev/null +++ b/spec/lib/observers/activity_feed/comment_spec.rb @@ -0,0 +1,14 @@ +# -*- encoding : utf-8 -*- +require 'spec_helper' + +describe Modules::Observers::ActivityFeed::Comment do + before { stub_symlink_methods } + + it 'sends a notification email after create a issue comment' do + comment = FactoryGirl.build(:comment) + mailer = mock!.deliver + mock(UserMailer).new_comment_notification(comment, comment.commentable.assignee) { mailer } + comment.save + end + +end