Skip to content

Commit

Permalink
Comment and sent email when co-author added (publiclab#2259)
Browse files Browse the repository at this point in the history
* barnstar tag commenting

* comment addotion on co-author

* mailer for coauthor created

* mailer updated

* mailer finalized

* testing coauthor

* Co-author test done

* tag addition test
  • Loading branch information
grvsachdeva authored and Souravirus committed Mar 12, 2018
1 parent 133bca2 commit f45817e
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ def create
uid: current_user.uid,
body: "@#{current_user.username} awards a #{barnstar_info_link} to #{node.drupal_user.name} for their awesome contribution!")

elsif tagname.split(':')[0] == "with"
user = User.find_by_username_case_insensitive(tagname.split(':')[1])
CommentMailer.notify_coauthor(user, node)
node.add_comment(subject: 'co-author',
uid: current_user.uid,
body: " @#{current_user.username} has marked #{tagname.split(':')[1]} as a co-author. ")

end

if saved
Expand Down
8 changes: 8 additions & 0 deletions app/mailers/comment_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ def notify_answer_author(user, comment)
@footer = feature('email-footer')
mail(to: user.email, subject: "New comment on your answer on '" + comment.parent.title + "'")
end

def notify_coauthor(user, note)
@user = user
@note = note
@author = note.author
@footer = feature('email-footer')
mail(to: user.email, subject: 'You were added as a co-author!').deliver
end
end
2 changes: 1 addition & 1 deletion app/views/comment_mailer/notify_answer_author.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Hi! There's been a new comment to your answer on '<a href='https://<%= ActionMai

<p>https://<%= ActionMailer::Base.default_url_options[:host] %><%= @comment.parent.path(:question) %>#answer-<%= @comment.aid %>-comment-<%= @comment.cid %></p>

<p><b><a href="https://<%= ActionMailer::Base.default_url_options[:host] %>/profile/<%= @comment.author.name %>"><%= @comment.author.name %> wrote:</b></p>
<p><b><a href="https://<%= ActionMailer::Base.default_url_options[:host] %>/profile/<%= @comment.author.name %>"><%= @comment.author.name %></a> wrote:</b></p>

<hr />

Expand Down
15 changes: 15 additions & 0 deletions app/views/comment_mailer/notify_coauthor.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'<a href='https://<%= ActionMailer::Base.default_url_options[:host] %>/profile/<%= @author.name %>'><%= @author.name %></a>' has added you as a co-author of '<a href='https://<%= ActionMailer::Base.default_url_options[:host] %><%= @note.path %>'><%= @note.title %></a>'. Do NOT reply to this email; click this link to respond:

<p>https://<%= ActionMailer::Base.default_url_options[:host] %><%= @note.path %></p>

<hr />

<div style="color:#aaa;">

<p>Reply at: https://<%= ActionMailer::Base.default_url_options[:host] %><%= @note.path %>#comments</p>

<p>Report abuse to: moderators@<%= ActionMailer::Base.default_url_options[:host] %></p>

<%= @footer %>

</div>
2 changes: 1 addition & 1 deletion app/views/comment_mailer/notify_note_author.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Do NOT reply to this email; click this link to respond:
<p>https://<%= ActionMailer::Base.default_url_options[:host] %><%= @comment.node.path %>#c<%= @comment.cid %></p>
<% end %>

<p><b><a href="https://<%= ActionMailer::Base.default_url_options[:host] %>/profile/<%= @comment.author.name %>"><%= @comment.author.name %> wrote:</b></p>
<p><b><a href="https://<%= ActionMailer::Base.default_url_options[:host] %>/profile/<%= @comment.author.name %>"><%= @comment.author.name %></a> wrote:</b></p>

<hr />

Expand Down
15 changes: 15 additions & 0 deletions test/functional/tag_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,21 @@ def setup
end
end

test 'adds comment when creating coauthor' do
UserSession.create(users(:jeff))
user = users(:bob)
node = nodes(:one)

assert_difference 'Comment.count' do
tagname = "with:#{user.name}"
post :create,
name: tagname,
nid: node.id

assert_equal " [@#{node.author.name}](/profile/#{node.author.name}) has marked #{tagname.split(':')[1]} as a co-author. ", Comment.last.body
end
end

test 'should take node type as question if tag is a question tag' do
tag = tags(:question)

Expand Down
19 changes: 17 additions & 2 deletions test/unit/comment_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class CommentMailerTest < ActionMailer::TestCase
assert_equal "New comment on your answer on '" + comment.parent.title + "'", email.subject
assert email.body.include?("Hi! There's been a new comment to your answer on '<a href='https://#{request_host}#{comment.parent.path(:question)}#a#{comment.answer.id}'>#{comment.parent.title}</a>'")
end

test 'notify barnstar' do
user = users(:bob)
note = nodes(:one)
Expand All @@ -90,4 +90,19 @@ class CommentMailerTest < ActionMailer::TestCase
assert_equal "You were awarded a Barnstar!", email.subject
assert email.body.include?("'<a href='https://#{request_host}/profile/#{user.name}'>#{user.name}</a>' has awarded you a '<a href='https://#{request_host}/wiki/barnstars'>Barnstar</a>' for your work in the research note '<a href='https://#{request_host}#{note.path}'>#{note.title}</a>'")
end
end

test 'notify coauthor' do
user = users(:bob)
note = nodes(:one)
assert_difference 'ActionMailer::Base.deliveries.size', 1 do
CommentMailer.notify_coauthor(user, note)
end
assert !ActionMailer::Base.deliveries.empty?

email = ActionMailer::Base.deliveries.last
assert_equal ["do-not-reply@#{request_host}"], email.from
assert_equal [user.email], email.to
assert_equal "You were added as a co-author!", email.subject
assert email.body.include?("'<a href='https://#{request_host}/profile/#{note.author.name}'>#{note.author.name}</a>' has added you as a co-author of '<a href='https://#{request_host}#{note.path}'>#{note.title}</a>'")
end
end

0 comments on commit f45817e

Please sign in to comment.