|
Server : LiteSpeed System : Linux terra.hostitbro.com 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64 User : outerorb ( 1091) PHP Version : 8.1.34 Disable Function : mail Directory : /home2/outerorb/.trash/survey_system/ | |
|
Path: /home2/outerorb/.trash/survey_system/chat_api_reference.php
Size: 18.87 KB
Permissions: 0666
<?php
/**
* Chat System API Testing Page
* Use this page to test and understand all API endpoints
*/
require_once __DIR__ . '/config.php';
if (!is_logged_in()) {
header('Location: ' . BASE_URL . '/index.php');
exit;
}
require_once __DIR__ . '/inc/header.php';
$user = current_user();
?>
<div class="col-lg-10 p-4">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header bg-primary text-white">
<h3 class="mb-0">Chat System API Reference</h3>
</div>
<div class="card-body">
<h4>Overview</h4>
<p>The chat system provides REST API endpoints for managing conversations, messages, and notifications.</p>
<div class="alert alert-info">
<strong>Base URL:</strong> <code><?= BASE_URL ?>/api/chat.php</code>
</div>
<hr>
<h4>Quick Test</h4>
<p>Click any button below to test an endpoint:</p>
<div class="row mb-3">
<div class="col-md-6">
<button class="btn btn-outline-primary btn-block mb-2" onclick="testApi('list')">
📋 List My Chats
</button>
<button class="btn btn-outline-primary btn-block mb-2" onclick="testApi('unread_count')">
🔔 Get Unread Count
</button>
</div>
</div>
<div id="test-output" style="display:none;">
<div class="alert alert-light border">
<strong>Response:</strong>
<pre id="test-response"></pre>
</div>
</div>
<hr>
<h4>API Endpoints</h4>
<div class="accordion" id="apiAccordion">
<!-- List Chats -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#endpoint1">
📋 List User's Chats
</button>
</h2>
<div id="endpoint1" class="accordion-collapse collapse show" data-bs-parent="#apiAccordion">
<div class="accordion-body">
<p><strong>Endpoint:</strong> <code>?action=list</code></p>
<p><strong>Method:</strong> GET</p>
<p><strong>Description:</strong> Returns all chats for the current user, including unread message counts.</p>
<p><strong>Response:</strong></p>
<pre>{
"success": true,
"chats": [
{
"id": 1,
"type": "private",
"name": null,
"last_message": "Hello there!",
"last_message_at": "2025-01-15 10:30:00",
"unread_count": 2,
"is_deleted": false
}
]
}</pre>
</div>
</div>
</div>
<!-- Get Messages -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#endpoint2">
💬 Get Messages from Chat
</button>
</h2>
<div id="endpoint2" class="accordion-collapse collapse" data-bs-parent="#apiAccordion">
<div class="accordion-body">
<p><strong>Endpoint:</strong> <code>?action=messages&chat_id=ID&limit=50&offset=0</code></p>
<p><strong>Method:</strong> GET</p>
<p><strong>Parameters:</strong></p>
<ul>
<li><code>chat_id</code> (required): The chat ID</li>
<li><code>limit</code> (optional): Messages per page (default: 50)</li>
<li><code>offset</code> (optional): Pagination offset (default: 0)</li>
</ul>
<p><strong>Response:</strong></p>
<pre>{
"success": true,
"messages": [
{
"id": 1,
"chat_id": 1,
"user_id": 2,
"username": "john_doe",
"full_name": "John Doe",
"message": "Hello!",
"is_deleted": false,
"created_at": "2025-01-15 10:30:00"
}
]
}</pre>
</div>
</div>
</div>
<!-- Send Message -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#endpoint3">
✉️ Send Message
</button>
</h2>
<div id="endpoint3" class="accordion-collapse collapse" data-bs-parent="#apiAccordion">
<div class="accordion-body">
<p><strong>Endpoint:</strong> <code>?action=send</code></p>
<p><strong>Method:</strong> POST</p>
<p><strong>Body (JSON):</strong></p>
<pre>{
"chat_id": 1,
"message": "Hello there!"
}</pre>
<p><strong>Response:</strong></p>
<pre>{
"success": true,
"message_id": 123,
"message": "Message sent successfully"
}</pre>
</div>
</div>
</div>
<!-- Create Private Chat -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#endpoint4">
👤 Create/Get Private Chat
</button>
</h2>
<div id="endpoint4" class="accordion-collapse collapse" data-bs-parent="#apiAccordion">
<div class="accordion-body">
<p><strong>Endpoint:</strong> <code>?action=private</code></p>
<p><strong>Method:</strong> POST</p>
<p><strong>Description:</strong> Gets or creates a private chat with another user. If a chat already exists between these users, returns the existing chat.</p>
<p><strong>Body (JSON):</strong></p>
<pre>{
"other_user_id": 2
}</pre>
<p><strong>Response:</strong></p>
<pre>{
"success": true,
"chat_id": 1,
"message": "Chat created or retrieved successfully"
}</pre>
</div>
</div>
</div>
<!-- Create Group Chat -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#endpoint5">
👥 Create Group Chat
</button>
</h2>
<div id="endpoint5" class="accordion-collapse collapse" data-bs-parent="#apiAccordion">
<div class="accordion-body">
<p><strong>Endpoint:</strong> <code>?action=group</code></p>
<p><strong>Method:</strong> POST</p>
<p><strong>Description:</strong> Creates a new group chat with multiple participants.</p>
<p><strong>Body (JSON):</strong></p>
<pre>{
"name": "Sales Team",
"participant_ids": [2, 3, 4]
}</pre>
<p><strong>Response:</strong></p>
<pre>{
"success": true,
"chat_id": 5,
"message": "Group chat created successfully"
}</pre>
</div>
</div>
</div>
<!-- Mark as Read -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#endpoint6">
✅ Mark Chat as Read
</button>
</h2>
<div id="endpoint6" class="accordion-collapse collapse" data-bs-parent="#apiAccordion">
<div class="accordion-body">
<p><strong>Endpoint:</strong> <code>?action=mark_read&chat_id=ID</code></p>
<p><strong>Method:</strong> GET</p>
<p><strong>Description:</strong> Marks all messages in a chat as read (clears unread notifications).</p>
<p><strong>Response:</strong></p>
<pre>{
"success": true,
"message": "Chat marked as read"
}</pre>
</div>
</div>
</div>
<!-- Get Unread Count -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#endpoint7">
🔔 Get Unread Message Count
</button>
</h2>
<div id="endpoint7" class="accordion-collapse collapse" data-bs-parent="#apiAccordion">
<div class="accordion-body">
<p><strong>Endpoint:</strong> <code>?action=unread_count</code></p>
<p><strong>Method:</strong> GET</p>
<p><strong>Description:</strong> Returns the total number of unread messages across all chats.</p>
<p><strong>Response:</strong></p>
<pre>{
"success": true,
"unread_count": 5
}</pre>
</div>
</div>
</div>
<!-- Delete Message -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#endpoint8">
🗑️ Delete Message
</button>
</h2>
<div id="endpoint8" class="accordion-collapse collapse" data-bs-parent="#apiAccordion">
<div class="accordion-body">
<p><strong>Endpoint:</strong> <code>?action=delete_message</code></p>
<p><strong>Method:</strong> POST</p>
<p><strong>Description:</strong> Soft-deletes a message (only owner or admin can delete). Message is hidden from regular users but visible to admins.</p>
<p><strong>Body (JSON):</strong></p>
<pre>{
"message_id": 123
}</pre>
<p><strong>Response:</strong></p>
<pre>{
"success": true,
"message": "Message deleted successfully"
}</pre>
</div>
</div>
</div>
<!-- Admin: All Chats -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#endpoint9">
👮 [ADMIN] View All Chats (including deleted)
</button>
</h2>
<div id="endpoint9" class="accordion-collapse collapse" data-bs-parent="#apiAccordion">
<div class="accordion-body">
<p><strong>Endpoint:</strong> <code>?action=admin_all_chats</code></p>
<p><strong>Method:</strong> GET</p>
<p><strong>Description:</strong> [ADMIN ONLY] Returns all chats including deleted ones.</p>
<p><strong>Response:</strong></p>
<pre>{
"success": true,
"chats": [
{
"id": 1,
"type": "private",
"name": null,
"created_at": "2025-01-15 10:30:00",
"is_deleted": false
},
{
"id": 2,
"type": "group",
"name": "Old Team",
"created_at": "2025-01-10 15:20:00",
"is_deleted": true
}
]
}</pre>
</div>
</div>
</div>
<!-- Admin: All Messages -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#endpoint10">
👮 [ADMIN] View All Messages (including deleted)
</button>
</h2>
<div id="endpoint10" class="accordion-collapse collapse" data-bs-parent="#apiAccordion">
<div class="accordion-body">
<p><strong>Endpoint:</strong> <code>?action=admin_messages&chat_id=ID</code></p>
<p><strong>Method:</strong> GET</p>
<p><strong>Description:</strong> [ADMIN ONLY] Returns all messages in a chat including deleted ones with deletion metadata.</p>
<p><strong>Response:</strong></p>
<pre>{
"success": true,
"messages": [
{
"id": 1,
"chat_id": 1,
"user_id": 2,
"username": "john",
"message": "Active message",
"is_deleted": false,
"created_at": "2025-01-15 10:30:00"
},
{
"id": 2,
"chat_id": 1,
"user_id": 2,
"username": "john",
"message": "[DELETED]",
"is_deleted": true,
"deleted_by": 2,
"deleted_by_username": "john",
"deleted_by_name": "John Doe",
"deleted_at": "2025-01-15 11:00:00",
"created_at": "2025-01-15 10:50:00"
}
]
}</pre>
</div>
</div>
</div>
</div>
<hr>
<h4>Error Responses</h4>
<p>All endpoints return proper HTTP status codes:</p>
<ul>
<li><code>200</code> - Success</li>
<li><code>400</code> - Bad Request (missing parameters)</li>
<li><code>401</code> - Unauthorized (not logged in)</li>
<li><code>403</code> - Forbidden (insufficient permissions)</li>
<li><code>404</code> - Not Found (chat/message doesn't exist)</li>
<li><code>500</code> - Server Error</li>
</ul>
<hr>
<h4>Usage Example</h4>
<p><strong>JavaScript (Fetch API):</strong></p>
<pre>// Send a message
fetch('<?= BASE_URL ?>/api/chat.php?action=send', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
chat_id: 1,
message: 'Hello, this is a test message!'
})
})
.then(response => response.json())
.then(data => console.log(data));</pre>
<hr>
<div class="alert alert-success">
<strong>✓ Setup Complete!</strong> Your chat system is ready to use.
<ul class="mb-0 mt-2">
<li><a href="<?= BASE_URL ?>/admin_chats.php">👮 Admin Dashboard</a></li>
<li><a href="<?= BASE_URL ?>/dashboard.php">🏠 Back to Dashboard</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
function testApi(action) {
let url = '<?= BASE_URL ?>/api/chat.php?action=' + action;
fetch(url)
.then(response => response.json())
.then(data => {
document.getElementById('test-response').textContent = JSON.stringify(data, null, 2);
document.getElementById('test-output').style.display = 'block';
})
.catch(error => {
document.getElementById('test-response').textContent = 'Error: ' + error.message;
document.getElementById('test-output').style.display = 'block';
});
}
</script>
<?php require_once __DIR__ . '/inc/footer.php'; ?>