#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:
2018-10-05 21:08:14 +01:00
parent a61029cf78
commit 62659c13f7
8 changed files with 216 additions and 14 deletions
@@ -10,6 +10,8 @@ use App\Helpers\PermissionsHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\StorePhotoCommentRequest;
use App\Mail\ModeratePhotoComment;
use App\Mail\PhotoCommentApproved;
use App\Mail\PhotoCommentApprovedUser;
use App\Permission;
use App\Photo;
use App\PhotoComment;
@@ -38,7 +40,7 @@ class PhotoCommentController extends Controller
return null;
}
if (Gate::denies('moderate-comments', $photo))
if (!User::currentOrAnonymous()->can('moderate-comments', $photo))
{
App::abort(403);
return null;
@@ -52,6 +54,7 @@ class PhotoCommentController extends Controller
$comment->approved_user_id = $this->getUser()->id;
$comment->save();
$this->notifyAlbumOwner($album, $photo, $comment);
$request->getSession()->flash('success', trans('gallery.photo_comment_approved_successfully'));
}
else if ($request->has('reject'))
@@ -124,7 +127,6 @@ class PhotoCommentController extends Controller
if (is_null($parentComment))
{
//TODO $request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully'));
return redirect($photo->url());
}
}
@@ -158,7 +160,7 @@ class PhotoCommentController extends Controller
// Auto-approve the comment if we're allowed to moderate comments
$isAutoApproved = false;
if (Gate::allows('moderate-comments', $photo))
if (User::currentOrAnonymous()->can('moderate-comments', $photo))
{
$comment->approved_at = new \DateTime();
$comment->approved_user_id = $user->id;
@@ -179,27 +181,25 @@ class PhotoCommentController extends Controller
$isAutoApproved = true;
}
$comment->save();
// Send notification e-mails to moderators or album owner
if (!$isAutoApproved)
{
$this->notifyAlbumModerators($album, $photo, $comment);
}
$comment->save();
if ($isAutoApproved)
{
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully'));
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully_pending_moderation'));
}
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())
{
return response()->json(['redirect_url' => $photo->url()]);
} else
}
else
{
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)
{
$allowedHtmlTags = explode(',', UserConfig::get('photo_comments_allowed_html'));
+60
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
]);
}
}
+60
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
]);
}
}
+5
View File
@@ -22,6 +22,11 @@ class PhotoComment extends Model
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()
{
return $this->hasMany(PhotoComment::class, 'parent_comment_id');