| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Modules\MCP\Tools; |
| 4 |
|
| 5 |
use FluentBoards\App\Modules\MCP\Helpers\MCPHelper; |
| 6 |
use FluentBoards\App\Services\CommentService; |
| 7 |
use FluentBoards\App\Services\NotificationService; |
| 8 |
|
| 9 |
/** |
| 10 |
* Comment write tools. |
| 11 |
*/ |
| 12 |
class CommentTools |
| 13 |
{ |
| 14 |
public static function addComment($params = []) |
| 15 |
{ |
| 16 |
$task = MCPHelper::resolveTask($params); |
| 17 |
if (is_wp_error($task)) { |
| 18 |
return $task; |
| 19 |
} |
| 20 |
|
| 21 |
if (!MCPHelper::canWriteBoard($task->board_id)) { |
| 22 |
return MCPHelper::error('forbidden', __('You do not have permission to comment on this task', 'fluent-boards')); |
| 23 |
} |
| 24 |
|
| 25 |
$description = isset($params['description']) ? wp_kses_post($params['description']) : ''; |
| 26 |
if ($description === '') { |
| 27 |
return MCPHelper::error('invalid_param', __('Comment description is required', 'fluent-boards')); |
| 28 |
} |
| 29 |
|
| 30 |
$privacy = !empty($params['privacy']) && $params['privacy'] === 'public' ? 'public' : 'private'; |
| 31 |
$mentionedIds = MCPHelper::sanitizeIdArray($params['mentioned_ids'] ?? []); |
| 32 |
$boardMemberIds = (new NotificationService())->resolveBoardMentionUserIds($task->board_id, $mentionedIds); |
| 33 |
if (array_diff($mentionedIds, $boardMemberIds)) { |
| 34 |
return MCPHelper::error('forbidden', __('One or more mentioned users are not members of this board', 'fluent-boards')); |
| 35 |
} |
| 36 |
|
| 37 |
$mentionedIds = $boardMemberIds; |
| 38 |
$commentService = new CommentService(); |
| 39 |
|
| 40 |
$comment = $commentService->create([ |
| 41 |
'board_id' => (int) $task->board_id, |
| 42 |
'task_id' => (int) $task->id, |
| 43 |
'type' => 'comment', |
| 44 |
'privacy' => $privacy, |
| 45 |
'status' => 'published', |
| 46 |
'description' => $commentService->processMentionAndLink($description, $mentionedIds), |
| 47 |
'settings' => [ |
| 48 |
'mentioned_users' => $mentionedIds, |
| 49 |
'source' => 'mcp', |
| 50 |
], |
| 51 |
'created_by' => get_current_user_id(), |
| 52 |
], $task->id); |
| 53 |
|
| 54 |
return [ |
| 55 |
'comment' => [ |
| 56 |
'id' => (int) $comment->id, |
| 57 |
'board_id' => (int) $comment->board_id, |
| 58 |
'task_id' => (int) $comment->task_id, |
| 59 |
'privacy' => $comment->privacy, |
| 60 |
'status' => $comment->status, |
| 61 |
'description' => $comment->description, |
| 62 |
'created_by' => $comment->created_by ? (int) $comment->created_by : null, |
| 63 |
'created_at' => MCPHelper::toIso8601($comment->created_at), |
| 64 |
], |
| 65 |
'message' => __('Comment has been added', 'fluent-boards'), |
| 66 |
]; |
| 67 |
} |
| 68 |
} |
| 69 |
|