| 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 |
return $this->sendSuccess([ |
| 105 |
'message' => __('Comment has been added', 'fluent-boards'), |
| 106 |
'comment' => $comment |
| 107 |
], 201); |
| 108 |
} catch (\Exception $e) { |
| 109 |
return $this->sendError($e->getMessage(), 404); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public function update(Request $request, $board_id, $comment_id) |
| 114 |
{ |
| 115 |
// $commentData = $this->commentSanitizeAndValidate($request->all(), [ |
| 116 |
// 'description' => 'required|string', |
| 117 |
// ]); |
| 118 |
$requestData = [ |
| 119 |
'description' => $request->comment |
| 120 |
]; |
| 121 |
|
| 122 |
$validationRules = [ |
| 123 |
'description' => 'required|string' |
| 124 |
]; |
| 125 |
|
| 126 |
if ($request->images) { |
| 127 |
$validationRules['description'] = 'nullable|string'; |
| 128 |
} |
| 129 |
|
| 130 |
$commentData = $this->commentSanitizeAndValidate($requestData, $validationRules); |
| 131 |
|
| 132 |
try { |
| 133 |
$comment = $this->commentService->update($commentData, $comment_id, $request->mentionData); |
| 134 |
|
| 135 |
if($request->mentionData) |
| 136 |
{ |
| 137 |
$this->notificationService->mentionInComment($comment, $request->mentionData); |
| 138 |
} |
| 139 |
|
| 140 |
if($request->images) |
| 141 |
{ |
| 142 |
$this->commentService->attachCommentImages($comment, $request->images); |
| 143 |
$comment->load(['images']); |
| 144 |
} |
| 145 |
|
| 146 |
if ( !$comment ) { |
| 147 |
$errorMessage = __('Unauthorized Action', 'fluent-boards'); |
| 148 |
return $this->sendError($errorMessage, 401); |
| 149 |
} |
| 150 |
|
| 151 |
return $this->sendSuccess([ |
| 152 |
'description' => $comment->description, |
| 153 |
'message' => __('Comment has been updated', 'fluent-boards'), |
| 154 |
], 200); |
| 155 |
} catch (\Exception $e) { |
| 156 |
return $this->sendError($e->getMessage(), 404); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
public function delete($board_id, $comment_id) |
| 161 |
{ |
| 162 |
try { |
| 163 |
$this->commentService->delete($comment_id); |
| 164 |
|
| 165 |
return $this->sendSuccess([ |
| 166 |
'message' => __('Comment has been deleted', 'fluent-boards'), |
| 167 |
], 200); |
| 168 |
} catch (\Exception $e) { |
| 169 |
return $this->sendError($e->getMessage(), 404); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
public function updateReply(Request $request, $board_id, $reply_id) |
| 174 |
{ |
| 175 |
$requestData = [ |
| 176 |
'description' => $request->comment |
| 177 |
]; |
| 178 |
|
| 179 |
$validationRules = [ |
| 180 |
'description' => 'required|string' |
| 181 |
]; |
| 182 |
|
| 183 |
$replyData = $this->commentSanitizeAndValidate($requestData, $validationRules); |
| 184 |
|
| 185 |
try { |
| 186 |
$reply = $this->commentService->update($replyData, $reply_id, $request->mentionData); |
| 187 |
|
| 188 |
if (!$reply) { |
| 189 |
$errorMessage = __('Unauthorized Action', 'fluent-boards'); |
| 190 |
return $this->sendError($errorMessage, 401); |
| 191 |
} |
| 192 |
|
| 193 |
return $this->sendSuccess([ |
| 194 |
'description' => $reply->description, |
| 195 |
'message' => __('Reply has been updated', 'fluent-boards'), |
| 196 |
], 200); |
| 197 |
} catch (\Exception $e) { |
| 198 |
return $this->sendError($e->getMessage(), 404); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
public function deleteReply($board_id, $reply_id) |
| 203 |
{ |
| 204 |
try { |
| 205 |
$this->commentService->deleteReply($reply_id); |
| 206 |
|
| 207 |
return $this->sendSuccess([ |
| 208 |
'message' => __('Reply has been deleted', 'fluent-boards'), |
| 209 |
], 200); |
| 210 |
} catch (\Exception $e) { |
| 211 |
return $this->sendError($e->getMessage(), 404); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
public function sendMailAfterComment($commentId, $usersToSendEmail) |
| 216 |
{ |
| 217 |
$current_user_id = get_current_user_id(); |
| 218 |
|
| 219 |
/* this will run in background as soon as possible */ |
| 220 |
/* sending Model or Model Instance won't work here */ |
| 221 |
as_enqueue_async_action('fluent_boards/one_time_schedule_send_email_for_comment', [$commentId, $usersToSendEmail, $current_user_id], 'fluent-boards'); |
| 222 |
} |
| 223 |
|
| 224 |
private function commentSanitizeAndValidate($data, array $rules = []) |
| 225 |
{ |
| 226 |
$data = Helper::sanitizeComment($data); |
| 227 |
|
| 228 |
return $this->validate($data, $rules); |
| 229 |
} |
| 230 |
|
| 231 |
public function handleImageUpload(Request $request, $board_id, $task_id) |
| 232 |
{ |
| 233 |
$allowedTypes = implode(',', [ |
| 234 |
"image/jpeg", |
| 235 |
"image/gif", |
| 236 |
"image/png", |
| 237 |
"image/bmp", |
| 238 |
"image/tiff", |
| 239 |
"image/webp", |
| 240 |
"image/avif", |
| 241 |
"image/x-icon", |
| 242 |
"image/heic", |
| 243 |
]); |
| 244 |
|
| 245 |
$files = $this->validate($request->files(), [ |
| 246 |
'file' => 'mimetypes:' . $allowedTypes, |
| 247 |
], [ |
| 248 |
'file.mimetypes' => __('The file must be a image type.', 'fluent-boards') |
| 249 |
]); |
| 250 |
|
| 251 |
$uploadInfo = UploadService::handleFileUpload( $files, $board_id); |
| 252 |
|
| 253 |
$imageData = $uploadInfo[0]; |
| 254 |
// $attachmentService = new AttachmentService(); |
| 255 |
$attachment = $this->commentService->createCommentImage($imageData, $board_id); |
| 256 |
|
| 257 |
return $this->sendSuccess([ |
| 258 |
'message' => __('attachment has been added', 'fluent-boards'), |
| 259 |
'imageAttachment' => $attachment |
| 260 |
], 200); |
| 261 |
|
| 262 |
} |
| 263 |
} |
| 264 |
|