lib/cachai.rb in cachai-0.2.7 vs lib/cachai.rb in cachai-0.2.8
- old
+ new
@@ -104,14 +104,17 @@
:author_ip => request.ip,
:approved => is_blocked?(request.ip) ? 0 : 1
}
response = Response.create!(attrs.merge(:post_id => post.id))
-
Cachai.clear_cache(data['path'])
- notify_new_response(response, data['path']) if @recipient
+ if response.approved?
+ notify_new_response_to_admin(response, data['path']) if @recipient
+ # notify_new_response_to_parent(response, data['path']) if response.parent
+ end
+
headers['Access-Control-Allow-Origin'] = data['protocol'] + '//' + data['domain']
json({ :status => 'ok', :comment => response })
rescue JSON::ParserError
status 400 and json({ :error => 'Invalid JSON.' })
@@ -179,18 +182,28 @@
end
false
end
- def notify_new_response(response, path)
+ def notify_new_response_to_admin(response, path)
+ subject = "Nuevo comentario de #{response.author_name} at #{path}"
+ send_email(content: response.content, to: @recipient, path: path, subject: subject)
+ end
+
+ def notify_new_response_to_parent(response, path)
+ subject = "Respuesta de #{response.author_name} a tu comentario en #{path}"
+ send_email(content: response.content, to: response.parent.author_email, path: path, subject: subject)
+ end
+
+ def send_email(data)
RestClient.post "https://api:#{@mailgun_api_key}"\
"@api.mailgun.net/v3/#{@mailgun_domain}/messages",
- :from => 'comments@' + Cachai.domain,
- :to => @recipient,
- :subject => "New comment from #{response.author_name} at #{path}",
- :text => "#{response.content}\n\n--\nhttp://#{Cachai.domain}#{path}"
- rescue => e
- puts "MAIL ERROR: #{e.message}"
+ :from => data[:from] || 'comments@' + Cachai.domain,
+ :to => data[:to],
+ :subject => data[:subject],
+ :text => "#{data[:content]}\n\n--\nhttp://#{Cachai.domain}#{data[:path]}"
+ # rescue => e
+ # puts "MAIL ERROR: #{e.message}"
end
end
class Admin < Sinatra::Base