From a769204ff489bc750c4931ecdb22ef055eac042a Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Fri, 10 Feb 2012 10:59:39 +0800 Subject: [PATCH] Improved commit author notification --- app/mailers/notify.rb | 3 +-- app/models/mailer_observer.rb | 31 ++++++++++++++++++------------- app/models/note.rb | 30 ++++++++++++++++++++++++++++++ app/views/notes/_form.html.haml | 2 +- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/app/mailers/notify.rb b/app/mailers/notify.rb index 6f40812..50ccb99 100644 --- a/app/mailers/notify.rb +++ b/app/mailers/notify.rb @@ -27,8 +27,7 @@ class Notify < ActionMailer::Base @user = user @note = note @project = note.project - @commit = @project.repo.commits(note.noteable_id).first - return unless ( note.notify or ( note.notify_author and @commit.author.email == @user.email ) ) + @commit = @note.target mail(:to => @user.email, :subject => "gitlab | note for commit | #{@note.project.name} ") end diff --git a/app/models/mailer_observer.rb b/app/models/mailer_observer.rb index 2bf383e..f84cbde 100644 --- a/app/models/mailer_observer.rb +++ b/app/models/mailer_observer.rb @@ -27,20 +27,25 @@ class MailerObserver < ActiveRecord::Observer end def new_note(note) - return unless note.notify or note.notify_author - note.project.users.reject { |u| u.id == current_user.id } .each do |u| - case note.noteable_type - when "Commit" then - Notify.note_commit_email(u, note).deliver - when "Issue" then - Notify.note_issue_email(u, note).deliver - when "MergeRequest" then - Notify.note_merge_request_email(u, note).deliver - when "Snippet" - true - else - Notify.note_wall_email(u, note).deliver + # Notify whole team except author of note + if note.notify + note.project.users.reject { |u| u.id == current_user.id } .each do |u| + case note.noteable_type + when "Commit" then + Notify.note_commit_email(u, note).deliver + when "Issue" then + Notify.note_issue_email(u, note).deliver + when "MergeRequest" then + Notify.note_merge_request_email(u, note).deliver + when "Snippet" + true + else + Notify.note_wall_email(u, note).deliver + end end + # Notify only author of resource + elsif note.notify_author + Notify.note_commit_email(note.commit_author, note).deliver end end diff --git a/app/models/note.rb b/app/models/note.rb index 01a5507..581f783 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -57,6 +57,36 @@ class Note < ActiveRecord::Base rescue nil end + + # Check if we can notify commit author + # with email about our comment + # + # If commit author email exist in project + # and commit author is not passed user we can + # send email to him + # + # params: + # user - current user + # + # return: + # Boolean + # + def notify_only_author?(user) + commit? && commit_author && + commit_author.email != user.email + end + + def commit? + noteable_type == "Commit" + end + + def commit_author + @commit_author ||= + project.users.find_by_email(target.author_email) || + project.users.find_by_name(target.author_name) + rescue + nil + end end # == Schema Information # diff --git a/app/views/notes/_form.html.haml b/app/views/notes/_form.html.haml index 7adfba8..4ba7555 100644 --- a/app/views/notes/_form.html.haml +++ b/app/views/notes/_form.html.haml @@ -17,7 +17,7 @@ = check_box_tag :notify, 1, @note.noteable_type != "Commit" %span Project team - -if @note.noteable_type == "Commit" + - if @note.notify_only_author?(current_user) = label_tag :notify_author do = check_box_tag :notify_author, 1 , @note.noteable_type == "Commit" %span Commit author -- libgit2 0.21.2