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