namespace, '/history', [ 'methods' => 'GET', 'callback' => [ $this, 'list_conversations' ], 'permission_callback' => [ $this, 'check_permissions' ], ] ); register_rest_route( $this->namespace, '/history/(?P[a-f0-9-]+)', [ 'methods' => 'GET', 'callback' => [ $this, 'get_conversation' ], 'permission_callback' => [ $this, 'check_permissions' ], ] ); register_rest_route( $this->namespace, '/history/(?P[a-f0-9-]+)', [ 'methods' => 'DELETE', 'callback' => [ $this, 'delete_conversation' ], 'permission_callback' => [ $this, 'check_permissions' ], ] ); } /** * Check permissions for history endpoints * * @return bool|WP_Error */ public function check_permissions() { return current_user_can( 'manage_options' ); } /** * List conversations endpoint * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function list_conversations( WP_REST_Request $request ) { try { // Owner-scoped: a user can only ever see their own conversations. $user_id = get_current_user_id(); if ( ! $user_id ) { return $this->response( [ 'items' => [], 'total' => 0, 'page' => 1, 'per_page' => 0, ], __( 'Conversations retrieved successfully', 'aisentic' ), 200 ); } $page = intval( $request->get_param( 'page' ) ); $page = $page > 0 ? $page : 1; $per_page = intval( $request->get_param( 'per_page' ) ); $per_page = $per_page > 0 ? $per_page : 20; $per_page = min( $per_page, 50 ); $channel = Sanitization::sanitizeChannel( $request->get_param( 'channel' ) ) ?? ''; $conversation_repo = new Conversation_Repository(); $result = $conversation_repo->list( $page, $per_page, $user_id, $channel ); // Lean payload for the history list — no per-row message count (avoids an N+1 query). $items = array_map( function ( $conversation ) { return [ 'id' => $conversation->uuid, 'title' => $conversation->title, // Emit UTC ISO-8601 so the client renders relative times // correctly regardless of the viewer's browser timezone. 'updated_at' => ! empty( $conversation->updated_at ) ? get_gmt_from_date( $conversation->updated_at, 'c' ) : '', ]; }, $result['items'] ); return $this->response( [ 'items' => $items, 'total' => $result['total'], 'page' => $result['page'], 'per_page' => $result['per_page'], ], __( 'Conversations retrieved successfully', 'aisentic' ), 200 ); } catch ( \Exception $e ) { return Error_Handler::exceptionToRestError( $e ); } } /** * Get conversation endpoint * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function get_conversation( WP_REST_Request $request ) { try { $conversation_id = Sanitization::sanitizeConversationId( $request->get_param( 'conversationId' ) ); if ( empty( $conversation_id ) ) { return $this->error( __( 'Invalid conversation ID.', 'aisentic' ), 400 ); } $conversation_repo = new Conversation_Repository(); $conversation = $conversation_repo->findByUuid( $conversation_id ); if ( ! $conversation ) { return $this->error( __( 'Conversation not found.', 'aisentic' ), 404 ); } // Owner-scoped: users may only read their own conversations. if ( get_current_user_id() !== (int) $conversation->user_id ) { return $this->error( __( 'You do not have permission to access this conversation.', 'aisentic' ), 403 ); } $message_repo = new Message_Repository(); $messages = $message_repo->getByConversation( $conversation_id, 1000 ); // Get username or "Unknown User" for logged out users $username = 'Unknown User'; if ( $conversation->user_id > 0 ) { $user = get_user_by( 'id', $conversation->user_id ); if ( $user ) { $username = $user->display_name ? $user->display_name : $user->user_login; } } $conversation_data = $conversation->toArray(); $conversation_data['username'] = $username; return $this->response( [ 'conversationId' => $conversation_id, 'conversation' => $conversation_data, 'messages' => array_map( function ( $msg, $index ) { return $msg->toApiFormat( $index ); }, $messages, array_keys( $messages ) ), ], __( 'Conversation retrieved successfully', 'aisentic' ), 200 ); } catch ( \Exception $e ) { return Error_Handler::exceptionToRestError( $e ); } } /** * Delete conversation endpoint * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function delete_conversation( WP_REST_Request $request ) { try { $conversation_id = Sanitization::sanitizeConversationId( $request->get_param( 'conversationId' ) ); if ( empty( $conversation_id ) ) { return $this->error( __( 'Invalid conversation ID.', 'aisentic' ), 400 ); } $conversation_repo = new Conversation_Repository(); $message_repo = new Message_Repository(); $conversation = $conversation_repo->findByUuid( $conversation_id ); if ( ! $conversation ) { return $this->error( __( 'Conversation not found.', 'aisentic' ), 404 ); } // Owner-scoped: users may only delete their own conversations. if ( get_current_user_id() !== (int) $conversation->user_id ) { return $this->error( __( 'You do not have permission to delete this conversation.', 'aisentic' ), 403 ); } global $wpdb; // Delete the messages and the conversation atomically so a mid-way // failure can't leave an orphaned, message-less conversation behind. $wpdb->query( 'START TRANSACTION' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $messages_deleted = $message_repo->deleteByConversation( $conversation_id ); $conversation_deleted = $conversation_repo->delete( $conversation_id ); if ( ! $messages_deleted || ! $conversation_deleted ) { $wpdb->query( 'ROLLBACK' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return $this->error( __( 'Failed to delete conversation.', 'aisentic' ), 500 ); } $wpdb->query( 'COMMIT' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return $this->response( [ 'ok' => true ], __( 'Conversation deleted successfully', 'aisentic' ), 200 ); } catch ( \Exception $e ) { return Error_Handler::exceptionToRestError( $e ); } } }