#4: Added e-mail notifications to the album owner when a new comment has been approved, and to the comment poster when it is approved.

This commit is contained in:
Andy Heathershaw 2018-10-05 21:08:14 +01:00
parent a61029cf78
commit 62659c13f7
8 changed files with 216 additions and 14 deletions

View File

@ -10,6 +10,8 @@ use App\Helpers\PermissionsHelper;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\StorePhotoCommentRequest; use App\Http\Requests\StorePhotoCommentRequest;
use App\Mail\ModeratePhotoComment; use App\Mail\ModeratePhotoComment;
use App\Mail\PhotoCommentApproved;
use App\Mail\PhotoCommentApprovedUser;
use App\Permission; use App\Permission;
use App\Photo; use App\Photo;
use App\PhotoComment; use App\PhotoComment;
@ -38,7 +40,7 @@ class PhotoCommentController extends Controller
return null; return null;
} }
if (Gate::denies('moderate-comments', $photo)) if (!User::currentOrAnonymous()->can('moderate-comments', $photo))
{ {
App::abort(403); App::abort(403);
return null; return null;
@ -52,6 +54,7 @@ class PhotoCommentController extends Controller
$comment->approved_user_id = $this->getUser()->id; $comment->approved_user_id = $this->getUser()->id;
$comment->save(); $comment->save();
$this->notifyAlbumOwner($album, $photo, $comment);
$request->getSession()->flash('success', trans('gallery.photo_comment_approved_successfully')); $request->getSession()->flash('success', trans('gallery.photo_comment_approved_successfully'));
} }
else if ($request->has('reject')) else if ($request->has('reject'))
@ -124,7 +127,6 @@ class PhotoCommentController extends Controller
if (is_null($parentComment)) if (is_null($parentComment))
{ {
//TODO $request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully'));
return redirect($photo->url()); return redirect($photo->url());
} }
} }
@ -158,7 +160,7 @@ class PhotoCommentController extends Controller
// Auto-approve the comment if we're allowed to moderate comments // Auto-approve the comment if we're allowed to moderate comments
$isAutoApproved = false; $isAutoApproved = false;
if (Gate::allows('moderate-comments', $photo)) if (User::currentOrAnonymous()->can('moderate-comments', $photo))
{ {
$comment->approved_at = new \DateTime(); $comment->approved_at = new \DateTime();
$comment->approved_user_id = $user->id; $comment->approved_user_id = $user->id;
@ -179,27 +181,25 @@ class PhotoCommentController extends Controller
$isAutoApproved = true; $isAutoApproved = true;
} }
$comment->save();
// Send notification e-mails to moderators or album owner // Send notification e-mails to moderators or album owner
if (!$isAutoApproved) if (!$isAutoApproved)
{ {
$this->notifyAlbumModerators($album, $photo, $comment); $this->notifyAlbumModerators($album, $photo, $comment);
} $request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully_pending_moderation'));
$comment->save();
if ($isAutoApproved)
{
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully'));
} }
else else
{ {
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully_pending_moderation')); $this->notifyAlbumOwner($album, $photo, $comment);
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully'));
} }
if ($request->isXmlHttpRequest()) if ($request->isXmlHttpRequest())
{ {
return response()->json(['redirect_url' => $photo->url()]); return response()->json(['redirect_url' => $photo->url()]);
} else }
else
{ {
return redirect($photo->url()); return redirect($photo->url());
} }
@ -275,6 +275,24 @@ class PhotoCommentController extends Controller
} }
} }
/**
* Sends an e-mail notification to an album's owned that a comment has been posted/approved.
* @param Album $album
* @param Photo $photo
* @param PhotoComment $comment
*/
private function notifyAlbumOwner(Album $album, Photo $photo, PhotoComment $comment)
{
$owner = $album->user;
$poster = new User();
$poster->name = $comment->authorDisplayName();
$poster->email = $comment->authorEmail();
Mail::to($owner)->send(new PhotoCommentApproved($owner, $album, $photo, $comment));
Mail::to($poster)->send(new PhotoCommentApprovedUser($poster, $album, $photo, $comment));
}
private function stripDisallowedHtmlTags($commentText) private function stripDisallowedHtmlTags($commentText)
{ {
$allowedHtmlTags = explode(',', UserConfig::get('photo_comments_allowed_html')); $allowedHtmlTags = explode(',', UserConfig::get('photo_comments_allowed_html'));

View File

@ -0,0 +1,60 @@
<?php
namespace App\Mail;
use App\Album;
use App\Facade\Theme;
use App\Photo;
use App\PhotoComment;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
/**
* E-mail notification to the owner of an album that is sent when a new comment has been approved in their album.
* @package App\Mail
*/
class PhotoCommentApproved extends Mailable
{
use Queueable, SerializesModels;
private $album;
private $comment;
private $photo;
private $user;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(User $user, Album $album, Photo $photo, PhotoComment $comment)
{
$this->user = $user;
$this->album = $album;
$this->photo = $photo;
$this->comment = $comment;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$subject = trans('email.photo_comment_approved_subject', ['album_name' => $this->album->name]);
return $this
->subject($subject)
->markdown(Theme::viewName('email.photo_comment_approved'))
->with([
'album' => $this->album,
'comment' => $this->comment,
'photo' => $this->photo,
'subject' => $subject,
'user' => $this->user
]);
}
}

View File

@ -0,0 +1,60 @@
<?php
namespace App\Mail;
use App\Album;
use App\Facade\Theme;
use App\Photo;
use App\PhotoComment;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
/**
* E-mail notification to the poster of a comment that is sent when it has been approved.
* @package App\Mail
*/
class PhotoCommentApprovedUser extends Mailable
{
use Queueable, SerializesModels;
private $album;
private $comment;
private $photo;
private $user;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(User $user, Album $album, Photo $photo, PhotoComment $comment)
{
$this->user = $user;
$this->album = $album;
$this->photo = $photo;
$this->comment = $comment;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$subject = trans('email.photo_comment_approved_user_subject', ['album_name' => $this->album->name]);
return $this
->subject($subject)
->markdown(Theme::viewName('email.photo_comment_approved_user'))
->with([
'album' => $this->album,
'comment' => $this->comment,
'photo' => $this->photo,
'subject' => $subject,
'user' => $this->user
]);
}
}

View File

@ -22,6 +22,11 @@ class PhotoComment extends Model
return is_null($this->createdBy) ? $this->name : $this->createdBy->name; return is_null($this->createdBy) ? $this->name : $this->createdBy->name;
} }
public function authorEmail()
{
return is_null($this->createdBy) ? $this->email : $this->createdBy->email;
}
public function children() public function children()
{ {
return $this->hasMany(PhotoComment::class, 'parent_comment_id'); return $this->hasMany(PhotoComment::class, 'parent_comment_id');

View File

@ -15,5 +15,15 @@ return [
'moderate_photo_comment_p1' => 'A new comment has been posted in the :album_name album.', 'moderate_photo_comment_p1' => 'A new comment has been posted in the :album_name album.',
'moderate_photo_comment_p2' => 'Click the button below to open the photo\'s page and approve or reject the comment. You will need to login to see unapproved comments.', 'moderate_photo_comment_p2' => 'Click the button below to open the photo\'s page and approve or reject the comment. You will need to login to see unapproved comments.',
'moderate_photo_comment_subject' => 'New comment requires moderation in :album_name', 'moderate_photo_comment_subject' => 'New comment requires moderation in :album_name',
'test_email_subject' => 'Test e-mail from :app_name' 'test_email_subject' => 'Test e-mail from :app_name',
'photo_comment_approved_comment_label' => 'Comments:',
'photo_comment_approved_email_label' => 'Author e-mail:',
'photo_comment_approved_name_label' => 'Author name:',
'photo_comment_approved_p1' => 'A new comment has been posted in the :album_name album.',
'photo_comment_approved_p2' => 'Click the button below to view the photo\'s page to see the comment.',
'photo_comment_approved_subject' => 'New comment posted in :album_name',
'photo_comment_approved_user_comment_label' => 'Comments:',
'photo_comment_approved_user_p1' => 'Your comment has been successfully posted in the :album_name album.',
'photo_comment_approved_user_p2' => 'Click the button below to view the photo\'s page to see your comment.',
'photo_comment_approved_user_subject' => 'Your comment was posted in :album_name'
]; ];

View File

@ -99,5 +99,6 @@ return [
'storage_service_region_label' => 'Region:', 'storage_service_region_label' => 'Region:',
'storage_tenant_name_label' => 'Tenant:', 'storage_tenant_name_label' => 'Tenant:',
'username_label' => 'Username:', 'username_label' => 'Username:',
'upload_action' => 'Upload' 'upload_action' => 'Upload',
'view_photo_comment_action' => 'View comment'
]; ];

View File

@ -0,0 +1,26 @@
@component('mail::message')
@lang('email.generic_intro', ['user_name' => $user->name])
@lang('email.photo_comment_approved_p1', ['album_name' => $album->name])
@lang('email.photo_comment_approved_p2')
@lang('email.photo_comment_approved_name_label') {{ $comment->name }}
@lang('email.photo_comment_approved_email_label') {{ $comment->email }}
@lang('email.photo_comment_approved_comment_label')
{!! $comment->comment !!}
@component('mail::button', ['url' => $photo->url(), 'color' => 'blue'])
@lang('forms.view_photo_comment_action')
@endcomponent
@lang('email.generic_regards')<br/>
{{ UserConfig::get('app_name') }}<br/>
<a href="{{ route('home') }}">{{ route('home') }}</a>
@endcomponent

View File

@ -0,0 +1,22 @@
@component('mail::message')
@lang('email.generic_intro', ['user_name' => $user->name])
@lang('email.photo_comment_approved_user_p1', ['album_name' => $album->name])
@lang('email.photo_comment_approved_user_p2')
@lang('email.photo_comment_approved_user_comment_label')
{!! $comment->comment !!}
@component('mail::button', ['url' => $photo->url(), 'color' => 'blue'])
@lang('forms.view_photo_comment_action')
@endcomponent
@lang('email.generic_regards')<br/>
{{ UserConfig::get('app_name') }}<br/>
<a href="{{ route('home') }}">{{ route('home') }}</a>
@endcomponent