Add mailer for subscriptions

This commit is contained in:
Maximilian Grundke
2017-09-13 07:07:06 +02:00
parent 4baab7430c
commit 811fc72e1e
4 changed files with 119 additions and 13 deletions

View File

@ -51,12 +51,16 @@ class CommentsController < ApplicationController
def create
@comment = Comment.new(comment_params_without_request_id)
if comment_params[:request_id]
UserMailer.got_new_comment(@comment, RequestForComment.find(comment_params[:request_id]), current_user).deliver_now
end
respond_to do |format|
if @comment.save
if comment_params[:request_id]
request_for_comment = RequestForComment.find(comment_params[:request_id])
send_mail_to_author @comment, request_for_comment
send_mail_to_subscribers @comment, request_for_comment
end
format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: @comment }
else
@ -94,6 +98,7 @@ class CommentsController < ApplicationController
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
@ -109,4 +114,23 @@ class CommentsController < ApplicationController
# fuer production mode, damit böse menschen keine falsche user_id uebergeben:
params.require(:comment).permit(:file_id, :row, :column, :text, :request_id).merge(user_id: current_user.id, user_type: current_user.class.name)
end
def send_mail_to_author(comment, request_for_comment)
if current_user != request_for_comment.user
UserMailer.got_new_comment(comment, request_for_comment, current_user).deliver_now
end
end
def send_mail_to_subscribers(comment, request_for_comment)
request_for_comment.commenters.each do |commenter|
subscriptions = Subscription.where(
:request_for_comment_id => request_for_comment.id,
:user_id => commenter.id, :user_type => commenter.class.name)
subscriptions.each do |subscription|
if (subscription.type == 'author' and current_user == request_for_comment.user) or subscription.type == 'all'
UserMailer.got_new_comment_for_subscription(comment, request_for_comment, request_for_comment.user, current_user).deliver_now
end
end
end
end
end

View File

@ -21,6 +21,14 @@ class UserMailer < ActionMailer::Base
mail(subject: t('mailers.user_mailer.got_new_comment.subject', commenting_user_displayname: @commenting_user_displayname), to: request_for_comment.user.email)
end
def got_new_comment_for_subscription(comment, request_for_comment, from_user, to_user)
@receiver_displayname = user.displayname
@author_displayname = from_user.displayname
@comment_text = comment.text
@rfc_link = request_for_comment_url(request_for_comment)
mail(subject: t('mailers.user_mailer.got_new_comment_for_subscription.subject', author_displayname: @author_displayname), to: to_user.email)
end
def send_thank_you_note(request_for_comments, receiver)
@receiver_displayname = receiver.displayname
@author = request_for_comments.user.displayname

View File

@ -449,6 +449,43 @@ de:
<br>
This mail was automatically sent by CodeOcean. <br>
subject: "%{author} sagt Danke!"
got_new_comment_for_subscription:
body: |
English version below <br>
_________________________<br>
<br>
Hallo %{receiver_displayname}, <br>
<br>
es gibt einen neuen Kommentar von %{author_displayname} zu einer Kommentaranfrage auf CodeOcean, die Sie aboniert haben. <br>
<br>
%{author_displayname} schreibt: %{comment_text}<br>
<br>
Sie finden ihre Kommentaranfrage hier: %{link_to_comment} <br>
<br>
Falls Sie beim Klick auf diesen Link eine Fehlermeldung erhalten, dass Sie nicht berechtigt wären diese Aktion auszuführen, öffnen Sie bitte eine beliebige Programmieraufgabe aus einem Kurs heraus und klicken den Link danach noch einmal.<br>
<br>
Eine Übersicht Ihrer Kommentaranfragen gibt es hier: %{link_my_comments} <br>
Alle Kommentaranfragen aller Benutzer finden Sie hier: %{link_all_comments} <br>
<br>
Diese Mail wurde automatisch von CodeOcean verschickt.<br>
<br>
_________________________<br>
<br>
Dear %{receiver_displayname}, <br>
<br>
you received a new comment from %{author_displayname} to a request for comments on CodeOcean that you have subscribed to. <br>
<br>
%{author_displayname} wrote: %{comment_text} <br>
<br>
You can find your request for comments here: %{link_to_comment} <br>
<br>
If you receive an error that you are not authorized to perform this action when clicking the link, please log-in through any course exercise beforehand and click the link again. <br>
<br>
An overview of all your comments can be accessed here: %{link_my_comments} <br>
All comments of all participants are available here: %{link_all_comments} <br>
<br>
This mail was automatically sent by CodeOcean. <br>
subject: "%{author_displayname} hat einen neuen Kommentar in einer Diskussion veröffentlicht, die Sie aboniert haben."
request_for_comments:
click_here: Zum Kommentieren auf die Seitenleiste klicken!
comments: Kommentare

View File

@ -470,6 +470,43 @@ en:
<br>
This mail was automatically sent by CodeOcean. <br>
subject: "%{author} says thank you!"
got_new_comment_for_subscription:
body: |
English version below <br>
_________________________<br>
<br>
Hallo %{receiver_displayname}, <br>
<br>
es gibt einen neuen Kommentar von %{author_displayname} zu einer Kommentaranfrage auf CodeOcean, die Sie aboniert haben. <br>
<br>
%{author_displayname} schreibt: %{comment_text}<br>
<br>
Sie finden ihre Kommentaranfrage hier: %{link_to_comment} <br>
<br>
Falls Sie beim Klick auf diesen Link eine Fehlermeldung erhalten, dass Sie nicht berechtigt wären diese Aktion auszuführen, öffnen Sie bitte eine beliebige Programmieraufgabe aus einem Kurs heraus und klicken den Link danach noch einmal.<br>
<br>
Eine Übersicht Ihrer Kommentaranfragen gibt es hier: %{link_my_comments} <br>
Alle Kommentaranfragen aller Benutzer finden Sie hier: %{link_all_comments} <br>
<br>
Diese Mail wurde automatisch von CodeOcean verschickt.<br>
<br>
_________________________<br>
<br>
Dear %{receiver_displayname}, <br>
<br>
you received a new comment from %{author_displayname} to a request for comments on CodeOcean that you have subscribed to. <br>
<br>
%{author_displayname} wrote: %{comment_text} <br>
<br>
You can find your request for comments here: %{link_to_comment} <br>
<br>
If you receive an error that you are not authorized to perform this action when clicking the link, please log-in through any course exercise beforehand and click the link again. <br>
<br>
An overview of all your comments can be accessed here: %{link_my_comments} <br>
All comments of all participants are available here: %{link_all_comments} <br>
<br>
This mail was automatically sent by CodeOcean. <br>
subject: "%{author_displayname} has posted a new comment to a discussion you subscribed to on CodeOcean."
request_for_comments:
click_here: Click on this sidebar to comment!
comments: Comments