| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Task; |
| 6 |
use FluentBoards\App\Services\NotificationService; |
| 7 |
use FluentBoards\App\Services\Constant; |
| 8 |
use FluentBoards\App\Services\Helper; |
| 9 |
use FluentBoards\Framework\Http\Request\Request; |
| 10 |
use FluentBoards\App\Services\CommentService; |
| 11 |
|
| 12 |
class CommentController extends Controller |
| 13 |
{ |
| 14 |
private $commentService; |
| 15 |
private $notificationService; |
| 16 |
|
| 17 |
public function __construct(CommentService $commentService, NotificationService $notificationService) |
| 18 |
{ |
| 19 |
parent::__construct(); |
| 20 |
$this->commentService = $commentService; |
| 21 |
$this->notificationService = $notificationService; |
| 22 |
} |
| 23 |
|
| 24 |
public function getComments(Request $request, $board_id, $task_id) |
| 25 |
{ |
| 26 |
try { |
| 27 |
$filter = $request->getSafe('filter'); |
| 28 |
$per_page = 10; |
| 29 |
|
| 30 |
$comments = $this->commentService->getComments($task_id, $per_page, $filter); |
| 31 |
$totalComments = $this->commentService->getTotal($task_id); |
| 32 |
|
| 33 |
return $this->sendSuccess([ |
| 34 |
'comments' => $comments, |
| 35 |
'total' => $totalComments |
| 36 |
], 200); |
| 37 |
} catch (\Exception $e) { |
| 38 |
return $this->sendError($e->getMessage(), 404); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/* |
| 43 |
* handles comment or reply creation |
| 44 |
* @param $board_id int |
| 45 |
* @param $task_id int |
| 46 |
* @return json |
| 47 |
*/ |
| 48 |
public function create(Request $request, $board_id, $task_id) |
| 49 |
{ |
| 50 |
// TODO: Refactor the whole request and sanitize process here.. minimize the code in this functions. |
| 51 |
$requestData = [ |
| 52 |
'parent_id' => $request->parent_id, |
| 53 |
'description' => $request->comment, |
| 54 |
'created_by' => $request->comment_by, |
| 55 |
'task_id' => $task_id, |
| 56 |
'type' => $request->comment_type ? $request->comment_type : 'comment', |
| 57 |
'board_id' => (int) $board_id, |
| 58 |
]; |
| 59 |
|
| 60 |
$commentData = $this->commentSanitizeAndValidate($requestData, [ |
| 61 |
'description' => 'required|string', |
| 62 |
'created_by' => 'required|integer', |
| 63 |
'board_id' => 'required|integer', |
| 64 |
'task_id' => 'required|integer', |
| 65 |
'type' => 'required|string' |
| 66 |
]); |
| 67 |
|
| 68 |
try { |
| 69 |
$comment = $this->commentService->create($commentData, $task_id); |
| 70 |
$comment['user'] = $comment->user; |
| 71 |
//sending emails to assignees who enabled their email |
| 72 |
$usersToSendEmail = $this->notificationService->filterAssigneeToSendEmail($task_id, Constant::BOARD_EMAIL_COMMENT); |
| 73 |
|
| 74 |
$this->sendMailAfterComment($comment->id, $usersToSendEmail); |
| 75 |
|
| 76 |
// if($request->mentions) |
| 77 |
// { |
| 78 |
// do_action('fluent_boards/mention_comment_notification', $task_id, get_current_user_id(), $request->mentions); |
| 79 |
// } |
| 80 |
|
| 81 |
return $this->sendSuccess([ |
| 82 |
'message' => __('Comment has been added', 'fluent-boards'), |
| 83 |
'comment' => $comment |
| 84 |
], 201); |
| 85 |
} catch (\Exception $e) { |
| 86 |
return $this->sendError($e->getMessage(), 404); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
public function update(Request $request, $comment_id) |
| 91 |
{ |
| 92 |
$commentData = $this->commentSanitizeAndValidate($request->all(), [ |
| 93 |
'description' => 'required|string', |
| 94 |
]); |
| 95 |
try { |
| 96 |
$comment = $this->commentService->update($commentData, $comment_id); |
| 97 |
|
| 98 |
if ( !$comment ) { |
| 99 |
$errorMessage = __('Unauthorized Action', 'fluent-boards'); |
| 100 |
return $this->sendError($errorMessage, 401); |
| 101 |
} |
| 102 |
|
| 103 |
return $this->sendSuccess([ |
| 104 |
'description' => $comment->description, |
| 105 |
'message' => __('Comment has been updated', 'fluent-boards'), |
| 106 |
], 200); |
| 107 |
} catch (\Exception $e) { |
| 108 |
return $this->sendError($e->getMessage(), 404); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
public function delete($comment_id) |
| 113 |
{ |
| 114 |
try { |
| 115 |
$this->commentService->delete($comment_id); |
| 116 |
|
| 117 |
return $this->sendSuccess([ |
| 118 |
'message' => __('Comment has been deleted', 'fluent-boards'), |
| 119 |
], 200); |
| 120 |
} catch (\Exception $e) { |
| 121 |
return $this->sendError($e->getMessage(), 404); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
public function updateReply(Request $request, $task_id) |
| 126 |
{ |
| 127 |
$replyData = $this->commentSanitizeAndValidate($request->all(), [ |
| 128 |
'description' => 'required|string', |
| 129 |
]); |
| 130 |
try { |
| 131 |
$reply = $this->commentService->updateReply($replyData, $task_id); |
| 132 |
|
| 133 |
if (!$reply) { |
| 134 |
$errorMessage = __('Unauthorized Action', 'fluent-boards'); |
| 135 |
return $this->sendError($errorMessage, 401); |
| 136 |
} |
| 137 |
|
| 138 |
return $this->sendSuccess([ |
| 139 |
'description' => $reply->description, |
| 140 |
'message' => __('Reply has been updated', 'fluent-boards'), |
| 141 |
], 200); |
| 142 |
} catch (\Exception $e) { |
| 143 |
return $this->sendError($e->getMessage(), 404); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
public function deleteReply($comment_id) |
| 148 |
{ |
| 149 |
try { |
| 150 |
$this->commentService->deleteReply($comment_id); |
| 151 |
|
| 152 |
return $this->sendSuccess([ |
| 153 |
'message' => __('Reply has been deleted', 'fluent-boards'), |
| 154 |
], 200); |
| 155 |
} catch (\Exception $e) { |
| 156 |
return $this->sendError($e->getMessage(), 404); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
public function sendMailAfterComment($commentId, $usersToSendEmail) |
| 161 |
{ |
| 162 |
$current_user_id = get_current_user_id(); |
| 163 |
|
| 164 |
/* this will run in background as soon as possible */ |
| 165 |
/* sending Model or Model Instance won't work here */ |
| 166 |
as_enqueue_async_action('fluent_boards/one_time_schedule_send_email_for_comment', [$commentId, $usersToSendEmail, $current_user_id], 'fluent-boards'); |
| 167 |
} |
| 168 |
|
| 169 |
private function commentSanitizeAndValidate($data, array $rules = []) |
| 170 |
{ |
| 171 |
$data = Helper::sanitizeComment($data); |
| 172 |
|
| 173 |
return $this->validate($data, $rules); |
| 174 |
} |
| 175 |
} |
| 176 |
|