From e0b154059341d58c12448634940e2b1fc8449493 Mon Sep 17 00:00:00 2001 From: jomo Date: Sat, 3 May 2014 03:33:09 +0200 Subject: [PATCH] add info mail for new blog comments --- app/controllers/comments_controller.rb | 3 ++ app/mailers/redstoner_mailer.rb | 8 ++++- app/models/comment.rb | 31 +++++++++++++++++++ app/models/threadreply.rb | 2 +- .../thread_reply_mail.html.erb | 29 ----------------- test/mailers/previews/registration_preview.rb | 11 +++++-- 6 files changed, 51 insertions(+), 33 deletions(-) delete mode 100644 app/views/redstoner_mailer/thread_reply_mail.html.erb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index a8b9d33..48fabf6 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,5 +1,7 @@ class CommentsController < ApplicationController + include MailerHelper + def edit @comment = Comment.find(params[:id]) if mod? || @comment.author.is?(current_user) @@ -15,6 +17,7 @@ class CommentsController < ApplicationController @comment.user_author = current_user @comment.blogpost = Blogpost.find(params[:blogpost_id]) if @comment.save + @comment.send_new_comment_mail redirect_to blogpost_path(@comment.blogpost) + "#comment-#{@comment.id}", notice: 'Comment created!' else flash[:alert] = "Could not create comment." diff --git a/app/mailers/redstoner_mailer.rb b/app/mailers/redstoner_mailer.rb index 1e2d50f..aa0ff26 100644 --- a/app/mailers/redstoner_mailer.rb +++ b/app/mailers/redstoner_mailer.rb @@ -16,12 +16,18 @@ class RedstonerMailer < ActionMailer::Base mail(to: "redstonerserver@gmail.com", subject: "#{@user.name} registered on Redstoner") end - def thread_reply_mail(user, reply) + def new_thread_reply_mail(user, reply) @user = user @reply = reply mail(to: @user.email, subject: "#{reply.author.name} replied to '#{reply.thread.title}' on Redstoner") end + def new_post_comment_mail(user, comment) + @user = user + @comment = comment + mail(to: @user.email, subject: "#{comment.author.name} replied to '#{comment.blogpost.title}' on Redstoner") + end + def email_change_confirm_mail(user) @user = user mail(to: @user.email, subject: "Email change on Redstoner.com") diff --git a/app/models/comment.rb b/app/models/comment.rb index 80bd530..616ed06 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,5 +1,7 @@ class Comment < ActiveRecord::Base + include MailerHelper + belongs_to :user_author, class_name: "User", foreign_key: "user_author_id" belongs_to :user_editor, class_name: "User", foreign_key: "user_editor_id" @@ -25,4 +27,33 @@ class Comment < ActiveRecord::Base def edited? !!user_editor_id end + + def send_new_comment_mail + userids = [] + + # thread + replies + posts = blogpost.comments.to_a + posts << blogpost # if thread.author.send_own_post_comment_mail (TODO) + posts.each do |post| + # don't send mail to the author, don't send to banned/disabled users + if post.author != author && post.author.normal? && post.author.confirmed? # && + userids << post.author.id # && post.author.send_commented_comment_mail (TODO) + end + end + # making sure we don't send multiple mails to the same user + userids.uniq! + + mails = [] + userids.each do |uid| + begin + mails << RedstonerMailer.new_post_comment_mail(User.find(uid), self) + rescue => e + Rails.logger.error "---" + Rails.logger.error "WARNING: Failed to create thread_reply mail (view) for reply#: #{@self.id}, user: #{@user.name}, #{@user.email}" + Rails.logger.error e.message + Rails.logger.error "---" + end + end + background_mailer(mails) + end end \ No newline at end of file diff --git a/app/models/threadreply.rb b/app/models/threadreply.rb index c013a10..901c957 100644 --- a/app/models/threadreply.rb +++ b/app/models/threadreply.rb @@ -50,7 +50,7 @@ class Threadreply < ActiveRecord::Base mails = [] userids.each do |uid| begin - mails << RedstonerMailer.thread_reply_mail(User.find(uid), self) + mails << RedstonerMailer.new_thread_reply_mail(User.find(uid), self) rescue => e Rails.logger.error "---" Rails.logger.error "WARNING: Failed to create thread_reply mail (view) for reply#: #{@self.id}, user: #{@user.name}, #{@user.email}" diff --git a/app/views/redstoner_mailer/thread_reply_mail.html.erb b/app/views/redstoner_mailer/thread_reply_mail.html.erb deleted file mode 100644 index d6bfa85..0000000 --- a/app/views/redstoner_mailer/thread_reply_mail.html.erb +++ /dev/null @@ -1,29 +0,0 @@ -
-
- Hi <%= @user.name %>! - -

<%= link_to @reply.author.name, user_url(@reply.author), style: "text-decoration: none; color: #4096EE;" %> has replied to '<%= @reply.thread.title %>' on the Redstoner forums!

- -
- <%# TODO: fix relative links + iframes %> - <%= render_md(@reply.content).html_safe %> -
- -

<%= link_to "Click here", forumthread_url(@reply.thread) + "#reply-#{@reply.id}", style: "text-decoration: none; color: #4096EE;" %> to view the thread.

- -

If you have any questions or problems, just ask one of our <%= link_to "Staff", users_url(role: "staff"), style: "text-decoration: none; color: #4096EE;" %> in-game or on the forums!

-

Your Redstoner team

- -
-
-
-

You cannot (yet) unsubscribe from these mails, sorry about that. Will add this ASAP! -

-

You can contact us via: - <%= link_to "Website", root_url, style: "text-decoration: none; color: #4096EE;" %> | - <%= link_to "Twitter", "https://twitter.com/RedstonerServer", style: "text-decoration: none; color: #4096EE;" %> | - <%= link_to "Google+", "https://google.com/+Redstoner", style: "text-decoration: none; color: #4096EE;" %> | - <%= link_to "Email", "mailto:redstonerserver+website@gmail.com", style: "text-decoration: none; color: #4096EE;" %> -

-
- \ No newline at end of file diff --git a/test/mailers/previews/registration_preview.rb b/test/mailers/previews/registration_preview.rb index 597d6d6..a7e9443 100644 --- a/test/mailers/previews/registration_preview.rb +++ b/test/mailers/previews/registration_preview.rb @@ -17,11 +17,18 @@ class RegistrationPreview < ActionMailer::Preview RedstonerMailer.register_info_mail(@@user, true) end - def thread_reply_mail + def new_thread_reply_mail op = User.new(id: 32, name: "TheOP", email: "theopuser@example.com") thread = Forumthread.new(id: 123, user_author: op, title: "Wow, test thread!", content: "You should not see this...") reply = Threadreply.new(id: 312, user_author: @@user, content: "# Markdown!\n\n`incline code`\n\nhtml?\n\n[yt:abcd1234]\n\n[link](/forums)", forumthread: thread) - RedstonerMailer.thread_reply_mail(@@user, reply) + RedstonerMailer.new_thread_reply_mail(@@user, reply) + end + + def new_post_comment_mail + op = User.new(id: 32, name: "TheOP", email: "theopuser@example.com") + post = Blogpost.new(id: 123, user_author: op, title: "Wow, test post!", content: "You should not see this...") + comment = Comment.new(id: 312, user_author: @@user, content: "Wow, that is **cool**!", blogpost: post) + RedstonerMailer.new_post_comment_mail(@@user, comment) end def email_change_confirm_mail