删除私信分支链路

This commit is contained in:
2026-08-23 13:42:35 +08:00
parent 1c5c6d86bb
commit c6c2c0af6d
39 changed files with 32 additions and 1465 deletions
-132
View File
@@ -53,53 +53,6 @@ class MessageController extends ControllerBase {
'callback' => [$this, 'mark_all_read'],
'permission_callback' => [$this, 'check_logged_in_permission'],
]);
// ── Private Messages / Conversations ──
// List conversations
register_rest_route(self::API_NAMESPACE, '/conversations', [
'methods' => 'GET',
'callback' => [$this, 'list_conversations'],
'permission_callback' => [$this, 'check_logged_in_permission'],
'args' => [
'limit' => ['default' => 20, 'sanitize_callback' => 'absint'],
],
]);
// Get thread messages
register_rest_route(self::API_NAMESPACE, '/conversations/(?P<id>\d+)/messages', [
'methods' => 'GET',
'callback' => [$this, 'get_thread'],
'permission_callback' => [$this, 'check_logged_in_permission'],
'args' => [
'page' => ['default' => 1, 'sanitize_callback' => 'absint'],
'per_page' => ['default' => 30, 'sanitize_callback' => 'absint'],
],
]);
// Send message in conversation
register_rest_route(self::API_NAMESPACE, '/conversations/(?P<id>\d+)/messages', [
'methods' => 'POST',
'callback' => [$this, 'send_message'],
'permission_callback' => [$this, 'check_logged_in_permission'],
'args' => [
'content' => ['required' => true, 'sanitize_callback' => 'sanitize_textarea_field'],
],
]);
// Create or get conversation with a user
register_rest_route(self::API_NAMESPACE, '/conversations/with/(?P<user_id>\d+)', [
'methods' => 'POST',
'callback' => [$this, 'get_or_create_conversation'],
'permission_callback' => [$this, 'check_logged_in_permission'],
]);
// Mark conversation read
register_rest_route(self::API_NAMESPACE, '/conversations/(?P<id>\d+)/read', [
'methods' => 'POST',
'callback' => [$this, 'mark_conversation_read'],
'permission_callback' => [$this, 'check_logged_in_permission'],
]);
}
// ── Notification handlers ──
@@ -160,91 +113,6 @@ class MessageController extends ControllerBase {
return $this->success(['data' => ['success' => $result]]);
}
// ── Conversation handlers ──
public function list_conversations(\WP_REST_Request $request): \WP_REST_Response {
$user_id = get_current_user_id();
$limit = min(50, (int) $request->get_param('limit'));
if (!class_exists('\AKSRT\Messages\PrivateMessage')) {
return $this->success(['data' => []]);
}
$conversations = \AKSRT\Messages\PrivateMessage::get_instance()->get_conversations($user_id, $limit);
return $this->success(['data' => $conversations]);
}
public function get_thread(\WP_REST_Request $request): \WP_REST_Response {
$user_id = get_current_user_id();
$conv_id = (int) $request->get_param('id');
$page = max(1, (int) $request->get_param('page'));
$per_page = min(50, (int) $request->get_param('per_page'));
if (!class_exists('\AKSRT\Messages\PrivateMessage')) {
return $this->error('not_available', '私信系统未启用。', 501);
}
$result = \AKSRT\Messages\PrivateMessage::get_instance()->get_thread($conv_id, $user_id, $page, $per_page);
if (is_wp_error($result)) {
return $this->error('thread_error', $result->get_error_message(), 403);
}
return $this->success(['data' => $result]);
}
public function send_message(\WP_REST_Request $request): \WP_REST_Response {
$user_id = get_current_user_id();
$conv_id = (int) $request->get_param('id');
$content = trim((string) $request->get_param('content'));
if (!$content) {
return $this->error('content_required', '消息内容不能为空。', 400);
}
if (!class_exists('\AKSRT\Messages\PrivateMessage')) {
return $this->error('not_available', '私信系统未启用。', 501);
}
$result = \AKSRT\Messages\PrivateMessage::get_instance()->send($conv_id, $user_id, $content);
if (!$result['success']) {
return $this->error('send_failed', $result['message'] ?? '发送失败。', 500);
}
return $this->success(['data' => $result]);
}
public function get_or_create_conversation(\WP_REST_Request $request): \WP_REST_Response {
$user_id = get_current_user_id();
$other_id = (int) $request->get_param('user_id');
if ($user_id === $other_id) {
return $this->error('self_message', '不能与自己创建对话。', 400);
}
if (!class_exists('\AKSRT\Messages\PrivateMessage')) {
return $this->error('not_available', '私信系统未启用。', 501);
}
$conv_id = \AKSRT\Messages\PrivateMessage::get_instance()->get_or_create_conversation($user_id, $other_id);
if (is_wp_error($conv_id)) {
return $this->error('create_failed', $conv_id->get_error_message(), 500);
}
return $this->success(['data' => ['conversation_id' => $conv_id]]);
}
public function mark_conversation_read(\WP_REST_Request $request): \WP_REST_Response {
$user_id = get_current_user_id();
$conv_id = (int) $request->get_param('id');
if (!class_exists('\AKSRT\Messages\PrivateMessage')) {
return $this->error('not_available', '私信系统未启用。', 501);
}
\AKSRT\Messages\PrivateMessage::get_instance()->mark_conversation_read($conv_id, $user_id);
return $this->success(['data' => ['success' => true]]);
}
public function check_logged_in_permission(): bool {
return is_user_logged_in();
}