diff --git a/api.php b/api.php new file mode 100644 index 0000000..a64641a --- /dev/null +++ b/api.php @@ -0,0 +1,159 @@ + 'User not authenticated']); + exit; +} + +$action = $_GET['action'] ?? ''; + +switch ($action) { + case 'search_users': + search_users(); + break; + case 'start_conversation': + start_conversation(); + break; + case 'get_conversations': + get_conversations(); + break; + case 'get_messages': + get_messages(); + break; + case 'send_message': + send_message(); + break; + default: + http_response_code(400); + echo json_encode(['error' => 'Invalid action']); + exit; +} + +function search_users() { + $term = $_GET['term'] ?? ''; + if (empty($term)) { + echo json_encode([]); + exit; + } + + $pdo = db(); + $stmt = $pdo->prepare("SELECT id, username FROM users WHERE username LIKE ? AND id != ?"); + $stmt->execute(['%' . $term . '%', $_SESSION['user_id']]); + $users = $stmt->fetchAll(PDO::FETCH_ASSOC); + + header('Content-Type: application/json'); + echo json_encode($users); +} + +function start_conversation() { + $recipient_id = $_POST['recipient_id'] ?? ''; + if (empty($recipient_id)) { + http_response_code(400); + echo json_encode(['error' => 'Recipient ID is required']); + exit; + } + + $user_id = $_SESSION['user_id']; + + $pdo = db(); + + // Check if a conversation already exists between the two users + $stmt = $pdo->prepare(" + SELECT c.id + FROM conversations c + JOIN conversation_participants cp1 ON c.id = cp1.conversation_id + JOIN conversation_participants cp2 ON c.id = cp2.conversation_id + WHERE cp1.user_id = ? AND cp2.user_id = ? + "); + $stmt->execute([$user_id, $recipient_id]); + $conversation = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($conversation) { + // Conversation already exists + header('Content-Type: application/json'); + echo json_encode(['conversation_id' => $conversation['id']]); + exit; + } + + // Create a new conversation + $pdo->beginTransaction(); + try { + $stmt = $pdo->prepare("INSERT INTO conversations () VALUES ()"); + $stmt->execute(); + $conversation_id = $pdo->lastInsertId(); + + $stmt = $pdo->prepare("INSERT INTO conversation_participants (conversation_id, user_id) VALUES (?, ?), (?, ?)"); + $stmt->execute([$conversation_id, $user_id, $conversation_id, $recipient_id]); + + $pdo->commit(); + + header('Content-Type: application/json'); + echo json_encode(['conversation_id' => $conversation_id]); + } catch (Exception $e) { + $pdo->rollBack(); + http_response_code(500); + echo json_encode(['error' => 'Failed to create conversation']); + } +} + +function get_conversations() { + $user_id = $_SESSION['user_id']; + $pdo = db(); + + $stmt = $pdo->prepare(" + SELECT c.id, u.username, u.id as user_id + FROM conversations c + JOIN conversation_participants cp ON c.id = cp.conversation_id + JOIN users u ON u.id = cp.user_id + WHERE c.id IN ( + SELECT conversation_id + FROM conversation_participants + WHERE user_id = ? + ) AND cp.user_id != ? + "); + $stmt->execute([$user_id, $user_id]); + $conversations = $stmt->fetchAll(PDO::FETCH_ASSOC); + + header('Content-Type: application/json'); + echo json_encode($conversations); +} + +function get_messages() { + $conversation_id = $_GET['conversation_id'] ?? ''; + if (empty($conversation_id)) { + http_response_code(400); + echo json_encode(['error' => 'Conversation ID is required']); + exit; + } + + $pdo = db(); + $stmt = $pdo->prepare("SELECT * FROM messages WHERE conversation_id = ? ORDER BY created_at ASC"); + $stmt->execute([$conversation_id]); + $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); + + header('Content-Type: application/json'); + echo json_encode($messages); +} + +function send_message() { + $conversation_id = $_POST['conversation_id'] ?? ''; + $message_text = $_POST['message_text'] ?? ''; + + if (empty($conversation_id) || empty($message_text)) { + http_response_code(400); + echo json_encode(['error' => 'Conversation ID and message text are required']); + exit; + } + + $sender_id = $_SESSION['user_id']; + + $pdo = db(); + $stmt = $pdo->prepare("INSERT INTO messages (conversation_id, sender_id, message_text) VALUES (?, ?, ?)"); + $stmt->execute([$conversation_id, $sender_id, $message_text]); + + header('Content-Type: application/json'); + echo json_encode(['success' => true]); +} diff --git a/assets/css/custom.css b/assets/css/custom.css index fac9f56..aede516 100644 --- a/assets/css/custom.css +++ b/assets/css/custom.css @@ -158,3 +158,21 @@ body { background-color: #ffffff; border-top: 1px solid #e5e7eb; } + +.avatar-placeholder { + width: 48px; + height: 48px; + border-radius: 50%; + background-color: #e9ecef; + display: flex; + align-items: center; + justify-content: center; + font-size: 24px; + color: #6c757d; +} + +.avatar-placeholder.avatar-sm { + width: 40px; + height: 40px; + font-size: 20px; +} diff --git a/assets/js/main.js b/assets/js/main.js index 11b0b00..ab80e10 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -1,29 +1,191 @@ - document.addEventListener('DOMContentLoaded', function() { const chatForm = document.getElementById('chat-form'); const messageInput = document.getElementById('message-input'); + const userSearchForm = document.getElementById('user-search-form'); + const userSearchInput = document.getElementById('user-search-input'); + const searchResultsContainer = document.getElementById('search-results'); + const conversationListContainer = document.getElementById('conversation-list-container'); + const chatBody = document.querySelector('.chat-body'); + const chatHeaderName = document.querySelector('.chat-header .name'); + const chatHeaderStatus = document.querySelector('.chat-header .status'); + + let currentConversationId = null; if (chatForm) { chatForm.addEventListener('submit', function(e) { e.preventDefault(); const message = messageInput.value.trim(); - if (message) { - console.log('Sending message:', message); - // In a real app, you'd send this to the server. - // For this demo, we'll just clear the input. - messageInput.value = ''; + if (message && currentConversationId) { + sendMessage(currentConversationId, message); + } + }); + } - // Optional: Add the message to the UI for instant feedback - const chatBody = document.querySelector('.chat-body'); + if (userSearchForm) { + userSearchForm.addEventListener('submit', function(e) { + e.preventDefault(); + const searchTerm = userSearchInput.value.trim(); + if (searchTerm) { + searchUsers(searchTerm); + } + }); + } + + function searchUsers(term) { + fetch(`api.php?action=search_users&term=${term}`) + .then(response => response.json()) + .then(users => { + displaySearchResults(users); + }) + .catch(error => console.error('Error searching users:', error)); + } + + function displaySearchResults(users) { + searchResultsContainer.innerHTML = ''; + if (users.length === 0) { + searchResultsContainer.innerHTML = '
No users found.
'; + return; + } + + users.forEach(user => { + const userElement = document.createElement('div'); + userElement.classList.add('conversation-item'); + userElement.dataset.userId = user.id; + userElement.innerHTML = ` +No conversations yet.
'; + return; + } + + conversations.forEach(conversation => { + const conversationElement = document.createElement('div'); + conversationElement.classList.add('conversation-item'); + conversationElement.dataset.conversationId = conversation.id; + conversationElement.dataset.recipientId = conversation.user_id; + conversationElement.innerHTML = ` +No messages yet. Say hi!