Skip to content
This repository has been archived by the owner on Feb 20, 2020. It is now read-only.

send emails to users that are mentioned in bucket comments #195

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
30 changes: 29 additions & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,35 @@ def index

api :POST, '/comments'
def create
comment = Comment.create(comment_params)
body = comment_params[:body]
comment = Comment.create(body: body, user_id: comment_params[:user_id], bucket_id: comment_params[:bucket_id])

# convert markdown body to html
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, extensions = {})
red_body = markdown.render(body)

# break the html into fragments
noko_body = Nokogiri::HTML.fragment(red_body)

# get an array of all the user ids from the link values
user_links = noko_body.css("a").map {|a|
a.attributes.values[0].value.sub! 'uid:', ''
}

# format the links with the correct base url
noko_body.css("a").map do |a|
a['href'].sub! 'uid:', ''
a.attributes["href"].value = "#{Rails.application.config.action_mailer.default_url_options[:host]}/##{a['href'].sub! 'uid:', '/users/'}"
end

if user_links
email_body = noko_body.to_html
bucket = Bucket.find(comment_params[:bucket_id])
user_links.each do |userId|
user = User.find(userId)
UserMailer.notify_member_that_they_were_mentioned(author: current_user, member: user, bucket: bucket, body: email_body).deliver_later
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't put the code that converts from markdown to HTML in the controller, but rather in the user_mailer code that generates the mail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chime-mu It's not always the case that the user_mailer would get called though. If no one is mentioned in the comment then no mails would get sent. Because of this, I'm not sure that it makes sense to me to put this code in the user_mailer. Does that make sense? Or is there something I'm not seeing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you convert to HTML if no mail should be sent?

if comment.valid?
render json: [comment]
else
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ def me
render status: 200, json: [current_user]
end

api :GET, '/users/:id', 'User details'
def show
user = User.find(params[:id])
render json: user
end

private
def user_params
params.require(:user).permit(
Expand Down
10 changes: 10 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,14 @@ def check_transactions_email
from: "Cobudget Updates <[email protected]>",
subject: "DB transactions consistency check")
end

def notify_member_that_they_were_mentioned(author:, member:, bucket:, body:)
@bucket = bucket
@group = bucket.group
@body = body
@author = author
mail(to: member.email,
from: "Cobudget Updates <[email protected]>",
subject: "#{@author.name} mentioned you in the bucket #{@bucket.name}")
end
end
7 changes: 0 additions & 7 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ class Comment < ActiveRecord::Base
validates :bucket_id, presence: true
validates :body, presence: true

# add helper method to the UserMailer, takes an argument (text) returns rendered text
def body_as_markdown
renderer = Redcarpet::Render::HTML.new
markdown = Redcarpet::Markdown.new(renderer, extensions = {})
markdown.render(body).html_safe
end

def author_name
membership = user.membership_for(bucket.group)
membership.archived? ? "[removed user]" : user.name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p>
<%= @author.name %> mentioned you in the bucket <%= link_to "#{@bucket.name}", "#{root_url}#/buckets/#{@bucket.id}" %> in <%= @group.name %>.
</p>

<p>
<%= render html: @body.html_safe %>
</p>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
sessions: 'overrides/sessions'
}

resources :users, only: :create, defaults: { format: :json } do
resources :users, only: [:show, :create], defaults: { format: :json } do
collection do
post :confirm_account
post :request_password_reset
Expand Down