v30
This commit is contained in:
parent
9624830ffb
commit
d3e6f9d8d1
13
api/chat.php
13
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."]);
|
||||
}
|
||||
}
|
||||
9
db/migrations/14_ai_chats_table.sql
Normal file
9
db/migrations/14_ai_chats_table.sql
Normal file
@ -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
|
||||
);
|
||||
14
db/migrations/15_faqs_table.sql
Normal file
14
db/migrations/15_faqs_table.sql
Normal file
@ -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.');
|
||||
28
messages.php
28
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'])) ?>
|
||||
<div style="font-size: 10px; margin-top: 8px; opacity: 0.6; text-align: right;">
|
||||
<?= date('H:i', strtotime($msg['created_at'])) ?>
|
||||
<?php if ($msg['status'] === 'pending' && $is_me): ?>
|
||||
<i class="fas fa-clock" title="Pending acceptance" style="margin-left: 5px;"></i>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -229,7 +247,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active
|
||||
<?php if ($needs_acceptance): ?>
|
||||
<div style="padding: 20px 30px; background: rgba(0, 242, 255, 0.05); border-top: 1px solid var(--border-color); text-align: center;">
|
||||
<p style="font-size: 14px; margin-bottom: 15px;">Accept this message request to reply.</p>
|
||||
<form method="POST" action="api/chat.php">
|
||||
<form method="POST">
|
||||
<input type="hidden" name="action" value="accept">
|
||||
<input type="hidden" name="sender_id" value="<?= $active_chat_id ?>">
|
||||
<button type="submit" class="btn btn-primary">Accept & Reply</button>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user