diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 90922fb1..bc784e8f 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -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,19 +98,39 @@ class CommentsController < ApplicationController end private - # Use callbacks to share common setup or constraints between actions. - def set_comment - @comment = Comment.find(params[:id]) - end + + # Use callbacks to share common setup or constraints between actions. + def set_comment + @comment = Comment.find(params[:id]) + end def comment_params_without_request_id comment_params.except :request_id end - # Never trust parameters from the scary internet, only allow the white list through. - def comment_params - #params.require(:comment).permit(:user_id, :file_id, :row, :column, :text) - # 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) + # Never trust parameters from the scary internet, only allow the white list through. + def comment_params + #params.require(:comment).permit(:user_id, :file_id, :row, :column, :text) + # 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 diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 180e9b33..1cf7e7a0 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -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 diff --git a/config/locales/de.yml b/config/locales/de.yml index 76a01d1f..ba98cba5 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -449,6 +449,43 @@ de:
This mail was automatically sent by CodeOcean.
subject: "%{author} sagt Danke!" + got_new_comment_for_subscription: + body: | + English version below
+ _________________________
+
+ Hallo %{receiver_displayname},
+
+ es gibt einen neuen Kommentar von %{author_displayname} zu einer Kommentaranfrage auf CodeOcean, die Sie aboniert haben.
+
+ %{author_displayname} schreibt: %{comment_text}
+
+ Sie finden ihre Kommentaranfrage hier: %{link_to_comment}
+
+ 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.
+
+ Eine Übersicht Ihrer Kommentaranfragen gibt es hier: %{link_my_comments}
+ Alle Kommentaranfragen aller Benutzer finden Sie hier: %{link_all_comments}
+
+ Diese Mail wurde automatisch von CodeOcean verschickt.
+
+ _________________________
+
+ Dear %{receiver_displayname},
+
+ you received a new comment from %{author_displayname} to a request for comments on CodeOcean that you have subscribed to.
+
+ %{author_displayname} wrote: %{comment_text}
+
+ You can find your request for comments here: %{link_to_comment}
+
+ 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.
+
+ An overview of all your comments can be accessed here: %{link_my_comments}
+ All comments of all participants are available here: %{link_all_comments}
+
+ This mail was automatically sent by CodeOcean.
+ 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 diff --git a/config/locales/en.yml b/config/locales/en.yml index 8fde6821..b5af3476 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -470,6 +470,43 @@ en:
This mail was automatically sent by CodeOcean.
subject: "%{author} says thank you!" + got_new_comment_for_subscription: + body: | + English version below
+ _________________________
+
+ Hallo %{receiver_displayname},
+
+ es gibt einen neuen Kommentar von %{author_displayname} zu einer Kommentaranfrage auf CodeOcean, die Sie aboniert haben.
+
+ %{author_displayname} schreibt: %{comment_text}
+
+ Sie finden ihre Kommentaranfrage hier: %{link_to_comment}
+
+ 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.
+
+ Eine Übersicht Ihrer Kommentaranfragen gibt es hier: %{link_my_comments}
+ Alle Kommentaranfragen aller Benutzer finden Sie hier: %{link_all_comments}
+
+ Diese Mail wurde automatisch von CodeOcean verschickt.
+
+ _________________________
+
+ Dear %{receiver_displayname},
+
+ you received a new comment from %{author_displayname} to a request for comments on CodeOcean that you have subscribed to.
+
+ %{author_displayname} wrote: %{comment_text}
+
+ You can find your request for comments here: %{link_to_comment}
+
+ 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.
+
+ An overview of all your comments can be accessed here: %{link_my_comments}
+ All comments of all participants are available here: %{link_all_comments}
+
+ This mail was automatically sent by CodeOcean.
+ 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