diff --git a/api/chat.php b/api/chat.php index dbe026c..4569493 100644 --- a/api/chat.php +++ b/api/chat.php @@ -3,6 +3,9 @@ header('Content-Type: application/json'); require_once __DIR__ . '/../db/config.php'; require_once __DIR__ . '/../ai/LocalAIApi.php'; +session_start(); +$user_id = $_SESSION['user_id'] ?? null; + $input = json_decode(file_get_contents('php://input'), true); $message = $input['message'] ?? ''; @@ -42,12 +45,12 @@ try { if (!empty($response['success'])) { $aiReply = LocalAIApi::extractText($response); - // 4. Save to Database + // 4. Save to Database (ai_chats table) try { - $stmt = db()->prepare("INSERT INTO messages (user_message, ai_response) VALUES (?, ?)"); - $stmt->execute([$message, $aiReply]); + $stmt = db()->prepare("INSERT INTO ai_chats (user_id, user_message, ai_response) VALUES (?, ?, ?)"); + $stmt->execute([$user_id, $message, $aiReply]); } catch (Exception $e) { - error_log("DB Save Error: " . $e->getMessage()); + error_log("AI DB Save Error: " . $e->getMessage()); // Continue even if save fails, so the user still gets a reply } @@ -61,4 +64,4 @@ try { } catch (Exception $e) { error_log("Chat Error: " . $e->getMessage()); echo json_encode(['reply' => "An internal error occurred."]); -} +} \ No newline at end of file diff --git a/db/migrations/14_ai_chats_table.sql b/db/migrations/14_ai_chats_table.sql new file mode 100644 index 0000000..b64d2fe --- /dev/null +++ b/db/migrations/14_ai_chats_table.sql @@ -0,0 +1,9 @@ +-- Migration: Create ai_chats table for AI assistant +CREATE TABLE IF NOT EXISTS ai_chats ( + id INT AUTO_INCREMENT PRIMARY KEY, + user_id INT NULL, + user_message TEXT NOT NULL, + ai_response TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL +); diff --git a/db/migrations/15_faqs_table.sql b/db/migrations/15_faqs_table.sql new file mode 100644 index 0000000..e4f42a0 --- /dev/null +++ b/db/migrations/15_faqs_table.sql @@ -0,0 +1,14 @@ +-- Migration: Create faqs table for AI assistant knowledge base +CREATE TABLE IF NOT EXISTS faqs ( + id INT AUTO_INCREMENT PRIMARY KEY, + keywords TEXT NOT NULL, + answer TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +-- Seed some initial FAQs +INSERT INTO faqs (keywords, answer) VALUES +('What is Gatsby?', 'Gatsby is an exclusive student-led startup network for university students and graduates to connect, hire talent, and raise capital.'), +('How can I raise money?', 'Startups can launch a funding round from their dashboard. Investors can browse startups and invest in verified ventures.'), +('Who can join Gatsby?', 'Any student or graduate with a valid university email address can join and become a founder or investor.'), +('What is the minimum investment?', 'The minimum investment is set by the founder of each startup round, but is typically around £100 for micro-investments.'); diff --git a/messages.php b/messages.php index da71b68..5e2eb98 100644 --- a/messages.php +++ b/messages.php @@ -12,6 +12,15 @@ if (!$user) { header('Location: login.php'); exit; } $platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby'; +// Action: Accept message request +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'accept' && isset($_POST['sender_id'])) { + $sender_id = (int)$_POST['sender_id']; + $stmt = db()->prepare("UPDATE messages SET status = 'accepted' WHERE sender_id = ? AND receiver_id = ? AND status = 'pending'"); + $stmt->execute([$sender_id, $user_id]); + header("Location: messages.php?chat_with=" . $sender_id); + exit; +} + // Fetch Blocked Users $stmt = db()->prepare("SELECT blocked_id FROM blocked_users WHERE blocker_id = ?"); $stmt->execute([$user_id]); @@ -45,17 +54,17 @@ if ($user['role'] === 'founder') { $matches = $stmt->fetchAll(); } -// Fetch Conversations (accepted or pending from others, excluding blocked) +// Fetch Conversations (accepted or pending, excluding blocked) $sql = " SELECT DISTINCT u.id as other_user_id, u.full_name as other_user_name, u.role as other_role FROM messages m JOIN users u ON (m.sender_id = u.id OR m.receiver_id = u.id) WHERE ((m.sender_id = ? OR m.receiver_id = ?) AND u.id != ?) - AND (m.status = 'accepted' OR (m.status = 'pending' AND m.receiver_id = ?)) + AND m.status != 'rejected' AND u.id NOT IN ($placeholders) "; $stmt = db()->prepare($sql); -$params = array_merge([$user_id, $user_id, $user_id, $user_id], $all_blocked ?: []); +$params = array_merge([$user_id, $user_id, $user_id], $all_blocked ?: []); $stmt->execute($params); $conversations = $stmt->fetchAll(); @@ -75,15 +84,21 @@ if ($active_chat_id) { $stmt = db()->prepare("SELECT * FROM messages WHERE ((sender_id = ? AND receiver_id = ?) OR (sender_id = ? AND receiver_id = ?)) AND status != 'rejected' ORDER BY created_at ASC"); $stmt->execute([$user_id, $active_chat_id, $active_chat_id, $user_id]); $chat_messages = $stmt->fetchAll(); + $stmt = db()->prepare("SELECT 1 FROM messages WHERE ((sender_id = ? AND receiver_id = ?) OR (sender_id = ? AND receiver_id = ?)) AND status = 'accepted'"); $stmt->execute([$user_id, $active_chat_id, $active_chat_id, $user_id]); $has_accepted = (bool)$stmt->fetchColumn(); + $stmt = db()->prepare("SELECT 1 FROM matches WHERE ((user1_id = ? AND user2_id = ?) OR (user1_id = ? AND user2_id = ?)) AND status = 'active'"); $stmt->execute([$user_id, $active_chat_id, $active_chat_id, $user_id]); $is_currently_matched = (bool)$stmt->fetchColumn(); + $stmt = db()->prepare("SELECT 1 FROM messages WHERE sender_id = ? AND receiver_id = ? AND status = 'pending'"); $stmt->execute([$active_chat_id, $user_id]); - if (!$has_accepted && !$is_currently_matched && (bool)$stmt->fetchColumn()) { $can_reply = false; $needs_acceptance = true; } + if (!$has_accepted && !$is_currently_matched && (bool)$stmt->fetchColumn()) { + $can_reply = false; + $needs_acceptance = true; + } } } @@ -219,6 +234,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active = nl2br(htmlspecialchars($msg['content'])) ?>
Accept this message request to reply.
-