| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Comment; |
| 6 |
use FluentBoards\App\Models\Task; |
| 7 |
use FluentBoards\App\Models\User; |
| 8 |
use FluentBoards\App\Services\NotificationService; |
| 9 |
use FluentBoards\App\Services\Constant; |
| 10 |
use FluentBoards\App\Services\Helper; |
| 11 |
use FluentBoards\App\Services\UploadService; |
| 12 |
use FluentBoards\Framework\Http\Request\Request; |
| 13 |
use FluentBoards\App\Services\CommentService; |
| 14 |
use FluentBoardsPro\App\Services\AttachmentService; |
| 15 |
|
| 16 |
class CommentController extends Controller |
| 17 |
{ |
| 18 |
private $commentService; |
| 19 |
private $notificationService; |
| 20 |
|
| 21 |
public function __construct(CommentService $commentService, NotificationService $notificationService) |
| 22 |
{ |
| 23 |
parent::__construct(); |
| 24 |
$this->commentService = $commentService; |
| 25 |
$this->notificationService = $notificationService; |
| 26 |
} |
| 27 |
|
| 28 |
public function getComments(Request $request, $board_id, $task_id) |
| 29 |
{ |
| 30 |
try { |
| 31 |
$filter = $request->getSafe('filter'); |
| 32 |
$per_page = 10; |
| 33 |
|
| 34 |
$comments = $this->commentService->getComments($task_id, $per_page, $filter); |
| 35 |
$totalComments = $this->commentService->getTotal($task_id); |
| 36 |
|
| 37 |
return $this->sendSuccess([ |
| 38 |
'comments' => $comments, |
| 39 |
'total' => $totalComments |
| 40 |
], 200); |
| 41 |
} catch (\Exception $e) { |
| 42 |
return $this->sendError($e->getMessage(), 404); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/* |
| 47 |
* handles comment or reply creation |
| 48 |
* @param $board_id int |
| 49 |
* @param $task_id int |
| 50 |
* @return json |
| 51 |
*/ |
| 52 |
public function create(Request $request, $board_id, $task_id) |
| 53 |
{ |
| 54 |
// TODO: Refactor the whole request and sanitize process here.. minimize the code in this functions. |
| 55 |
$requestData = [ |
| 56 |
'parent_id' => $request->parent_id, |
| 57 |
'description' => $request->comment, |
| 58 |
'created_by' => $request->comment_by, |
| 59 |
'task_id' => $task_id, |
| 60 |
'type' => $request->comment_type ? $request->comment_type : 'comment', |
| 61 |
'board_id' => (int) $board_id, |
| 62 |
]; |
| 63 |
$validationRules = [ |
| 64 |
'description' => 'required|string', |
| 65 |
'created_by' => 'required|integer', |
| 66 |
'board_id' => 'required|integer', |
| 67 |
'task_id' => 'required|integer', |
| 68 |
'type' => 'required|string' |
| 69 |
]; |
| 70 |
|
| 71 |
if ($request->images) { |
| 72 |
$validationRules['description'] = 'nullable|string'; |
| 73 |
} |
| 74 |
|
| 75 |
$commentData = $this->commentSanitizeAndValidate($requestData, $validationRules); |
| 76 |
|
| 77 |
|
| 78 |
try { |
| 79 |
$rawDescription = $commentData['description']; |
| 80 |
$commentData['settings'] = [ 'raw_description' => $rawDescription, 'mentioned_id' => $request->mentionData ]; |
| 81 |
if($request->mentionData) { |
| 82 |
$commentData['description'] = $this->commentService->processMentionAndLink($commentData['description'], $request->mentionData); |
| 83 |
} else { |
| 84 |
$commentData['description'] = $this->commentService->checkIfCommentHaveLinks($commentData['description']); |
| 85 |
} |
| 86 |
|
| 87 |
$comment = $this->commentService->create($commentData, $task_id); |
| 88 |
$comment['user'] = $comment->user; |
| 89 |
|
| 90 |
$usersToSendEmail = []; |
| 91 |
if ($comment->type == 'reply') { |
| 92 |
$parentComment = Comment::findOrFail($comment->parent_id); |
| 93 |
$commenterId = $parentComment->created_by; |
| 94 |
if ($commenterId != get_current_user_id()) |
| 95 |
{ |
| 96 |
$commenter = User::select('user_email')->findOrFail($commenterId); |
| 97 |
$commenterEmail = $commenter->user_email; |
| 98 |
$usersToSendEmail[] = $commenterEmail; |
| 99 |
} |
| 100 |
$this->sendMailAfterComment($comment->id, $usersToSendEmail); |
| 101 |
} else { |
| 102 |
//sending emails to assignees who enabled their email |
| 103 |
$usersToSendEmail = $this->notificationService->filterAssigneeToSendEmail($task_id, Constant::BOARD_EMAIL_COMMENT); |
| 104 |
$this->sendMailAfterComment($comment->id, $usersToSendEmail); |
| 105 |
} |
| 106 |
|
| 107 |
if($request->mentionData) |
| 108 |
{ |
| 109 |
$this->notificationService->mentionInComment($comment, $request->mentionData); |
| 110 |
} |
| 111 |
|
| 112 |
if($request->images) |
| 113 |
{ |
| 114 |
$this->commentService->attachCommentImages($comment, $request->images); |
| 115 |
$comment->load(['images']); |
| 116 |
} |
| 117 |
|
| 118 |
if ($comment->type == 'comment') |
| 119 |
{ |
| 120 |
$comment->load('replies'); |
| 121 |
} |
| 122 |
|
| 123 |
return $this->sendSuccess([ |
| 124 |
'message' => __('Comment has been added', 'fluent-boards'), |
| 125 |
'comment' => $comment |
| 126 |
], 201); |
| 127 |
} catch (\Exception $e) { |
| 128 |
return $this->sendError($e->getMessage(), 404); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
public function update(Request $request, $board_id, $comment_id) |
| 133 |
{ |
| 134 |
// $commentData = $this->commentSanitizeAndValidate($request->all(), [ |
| 135 |
// 'description' => 'required|string', |
| 136 |
// ]); |
| 137 |
$requestData = [ |
| 138 |
'description' => $request->comment |
| 139 |
]; |
| 140 |
|
| 141 |
$validationRules = [ |
| 142 |
'description' => 'required|string' |
| 143 |
]; |
| 144 |
|
| 145 |
if ($request->images) { |
| 146 |
$validationRules['description'] = 'nullable|string'; |
| 147 |
} |
| 148 |
|
| 149 |
$commentData = $this->commentSanitizeAndValidate($requestData, $validationRules); |
| 150 |
|
| 151 |
try { |
| 152 |
$comment = $this->commentService->update($commentData, $comment_id, $request->mentionData); |
| 153 |
|
| 154 |
if($request->mentionData) |
| 155 |
{ |
| 156 |
$this->notificationService->mentionInComment($comment, $request->mentionData); |
| 157 |
} |
| 158 |
|
| 159 |
if($request->images) |
| 160 |
{ |
| 161 |
$this->commentService->attachCommentImages($comment, $request->images); |
| 162 |
$comment->load(['images']); |
| 163 |
} |
| 164 |
|
| 165 |
if ( !$comment ) { |
| 166 |
$errorMessage = __('Unauthorized Action', 'fluent-boards'); |
| 167 |
return $this->sendError($errorMessage, 401); |
| 168 |
} |
| 169 |
|
| 170 |
$comment->load('user'); |
| 171 |
|
| 172 |
return $this->sendSuccess([ |
| 173 |
'comment' => $comment, |
| 174 |
'message' => __('Comment has been updated', 'fluent-boards'), |
| 175 |
], 200); |
| 176 |
} catch (\Exception $e) { |
| 177 |
return $this->sendError($e->getMessage(), 404); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
public function deleteComment($board_id, $comment_id) |
| 182 |
{ |
| 183 |
try { |
| 184 |
$this->commentService->delete($comment_id); |
| 185 |
|
| 186 |
return $this->sendSuccess([ |
| 187 |
'message' => __('Comment has been deleted', 'fluent-boards'), |
| 188 |
], 200); |
| 189 |
} catch (\Exception $e) { |
| 190 |
return $this->sendError($e->getMessage(), 404); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
public function updateReply(Request $request, $board_id, $reply_id) |
| 195 |
{ |
| 196 |
$requestData = [ |
| 197 |
'description' => $request->comment |
| 198 |
]; |
| 199 |
|
| 200 |
$validationRules = [ |
| 201 |
'description' => 'required|string' |
| 202 |
]; |
| 203 |
|
| 204 |
$replyData = $this->commentSanitizeAndValidate($requestData, $validationRules); |
| 205 |
|
| 206 |
try { |
| 207 |
$reply = $this->commentService->update($replyData, $reply_id, $request->mentionData); |
| 208 |
|
| 209 |
if (!$reply) { |
| 210 |
$errorMessage = __('Unauthorized Action', 'fluent-boards'); |
| 211 |
return $this->sendError($errorMessage, 401); |
| 212 |
} |
| 213 |
|
| 214 |
return $this->sendSuccess([ |
| 215 |
'description' => $reply->description, |
| 216 |
'message' => __('Reply has been updated', 'fluent-boards'), |
| 217 |
], 200); |
| 218 |
} catch (\Exception $e) { |
| 219 |
return $this->sendError($e->getMessage(), 404); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
public function deleteReply($board_id, $reply_id) |
| 224 |
{ |
| 225 |
try { |
| 226 |
$this->commentService->deleteReply($reply_id); |
| 227 |
|
| 228 |
return $this->sendSuccess([ |
| 229 |
'message' => __('Reply has been deleted', 'fluent-boards'), |
| 230 |
], 200); |
| 231 |
} catch (\Exception $e) { |
| 232 |
return $this->sendError($e->getMessage(), 404); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
public function sendMailAfterComment($commentId, $usersToSendEmail) |
| 237 |
{ |
| 238 |
$current_user_id = get_current_user_id(); |
| 239 |
|
| 240 |
/* this will run in background as soon as possible */ |
| 241 |
/* sending Model or Model Instance won't work here */ |
| 242 |
as_enqueue_async_action('fluent_boards/one_time_schedule_send_email_for_comment', [$commentId, $usersToSendEmail, $current_user_id], 'fluent-boards'); |
| 243 |
} |
| 244 |
|
| 245 |
private function commentSanitizeAndValidate($data, array $rules = []) |
| 246 |
{ |
| 247 |
$data = Helper::sanitizeComment($data); |
| 248 |
|
| 249 |
return $this->validate($data, $rules); |
| 250 |
} |
| 251 |
|
| 252 |
public function handleImageUpload(Request $request, $board_id, $task_id) |
| 253 |
{ |
| 254 |
$allowedTypes = implode(',', [ |
| 255 |
"image/jpeg", |
| 256 |
"image/gif", |
| 257 |
"image/png", |
| 258 |
"image/bmp", |
| 259 |
"image/tiff", |
| 260 |
"image/webp", |
| 261 |
"image/avif", |
| 262 |
"image/x-icon", |
| 263 |
"image/heic", |
| 264 |
]); |
| 265 |
|
| 266 |
$files = $this->validate($request->files(), [ |
| 267 |
'file' => 'mimetypes:' . $allowedTypes, |
| 268 |
], [ |
| 269 |
'file.mimetypes' => __('The file must be a image type.', 'fluent-boards') |
| 270 |
]); |
| 271 |
|
| 272 |
$uploadInfo = UploadService::handleFileUpload( $files, $board_id); |
| 273 |
|
| 274 |
$imageData = $uploadInfo[0]; |
| 275 |
$attachment = $this->commentService->createCommentImage($imageData, $board_id); |
| 276 |
if(!!defined('FLUENT_BOARDS_PRO_VERSION')) { |
| 277 |
$mediaData = (new AttachmentService())->processMediaData($imageData, $files['file']); |
| 278 |
$attachment['driver'] = $mediaData['driver']; |
| 279 |
$attachment['file_path'] = $mediaData['file_path']; |
| 280 |
$attachment['full_url'] = $mediaData['full_url']; |
| 281 |
$attachment->save(); |
| 282 |
} |
| 283 |
$attachment->public_url = $this->commentService->createPublicUrl($attachment, $board_id); |
| 284 |
|
| 285 |
return $this->sendSuccess([ |
| 286 |
'message' => __('attachment has been added', 'fluent-boards'), |
| 287 |
'imageAttachment' => $attachment |
| 288 |
], 200); |
| 289 |
|
| 290 |
} |
| 291 |
} |
| 292 |
|