Would you like to accept this message request?
Select a conversation to start chatting
Find co-founders in the matching section.
prepare("SELECT * FROM users WHERE id = ?"); $stmt->execute([$user_id]); $user = $stmt->fetch(); $platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby'; // Handle Accept/Reject Actions if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && isset($_POST['other_user_id'])) { $other_id = (int)$_POST['other_user_id']; $action = $_POST['action']; if ($action === 'accept') { $stmt = db()->prepare("UPDATE messages SET status = 'accepted' WHERE sender_id = ? AND receiver_id = ? AND status = 'pending'"); $stmt->execute([$other_id, $user_id]); // Notify the sender $stmt = db()->prepare("INSERT INTO notifications (user_id, content) VALUES (?, ?)"); $stmt->execute([$other_id, "Your message request to " . $user['full_name'] . " has been accepted."]); header("Location: messages.php?chat_with=$other_id"); exit; } elseif ($action === 'reject') { $stmt = db()->prepare("UPDATE messages SET status = 'rejected' WHERE sender_id = ? AND receiver_id = ? AND status = 'pending'"); $stmt->execute([$other_id, $user_id]); header("Location: messages.php"); exit; } } // Fetch Matches for Founders $matches = []; if ($user['role'] === 'founder') { $stmt = db()->prepare(" SELECT m.*, u.full_name as match_name, u.id as match_id, u.university, u.degree_program FROM matches m JOIN users u ON (m.user1_id = u.id OR m.user2_id = u.id) WHERE (m.user1_id = ? OR m.user2_id = ?) AND u.id != ? "); $stmt->execute([$user_id, $user_id, $user_id]); $matches = $stmt->fetchAll(); } // Fetch Conversations (accepted or pending from others) $stmt = db()->prepare(" 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 = ?)) ORDER BY (SELECT MAX(created_at) FROM messages WHERE (sender_id = m.sender_id AND receiver_id = m.receiver_id) OR (sender_id = m.receiver_id AND receiver_id = m.sender_id)) DESC "); $stmt->execute([$user_id, $user_id, $user_id, $user_id]); $conversations = $stmt->fetchAll(); $active_chat_id = isset($_GET['chat_with']) ? (int)$_GET['chat_with'] : null; $startup_id = isset($_GET['startup_id']) ? (int)$_GET['startup_id'] : null; $active_chat_user = null; $chat_messages = []; $can_reply = true; $needs_acceptance = false; if ($active_chat_id) { $stmt = db()->prepare("SELECT id, full_name, role FROM users WHERE id = ?"); $stmt->execute([$active_chat_id]); $active_chat_user = $stmt->fetch(); if ($active_chat_user) { // Fetch messages (only show accepted or pending ones) $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(); // Check if there are any accepted messages or a match $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 = ?)"); $stmt->execute([$user_id, $active_chat_id, $active_chat_id, $user_id]); $has_match = (bool)$stmt->fetchColumn(); // If receiver of pending messages, can't reply yet $stmt = db()->prepare("SELECT 1 FROM messages WHERE sender_id = ? AND receiver_id = ? AND status = 'pending'"); $stmt->execute([$active_chat_id, $user_id]); $has_pending_from_other = (bool)$stmt->fetchColumn(); if (!$has_accepted && !$has_match && $has_pending_from_other) { $can_reply = false; $needs_acceptance = true; } } } // Handle sending message if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active_chat_id && $can_reply) { $content = trim($_POST['content']); if (!empty($content)) { // Determine status $status = 'accepted'; // Check if a match exists $stmt = db()->prepare("SELECT 1 FROM matches WHERE (user1_id = ? AND user2_id = ?) OR (user1_id = ? AND user2_id = ?)"); $stmt->execute([$user_id, $active_chat_id, $active_chat_id, $user_id]); $is_match = (bool)$stmt->fetchColumn(); // Check if previous accepted exists $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]); $is_accepted = (bool)$stmt->fetchColumn(); if (!$is_match && !$is_accepted) { // Check startup status if ($startup_id) { $stmt = db()->prepare("SELECT status, name FROM startups WHERE id = ?"); $stmt->execute([$startup_id]); $s = $stmt->fetch(); if ($s && $s['status'] === 'private') { $status = 'pending'; // Notify receiver about the request $stmt = db()->prepare("INSERT INTO notifications (user_id, content) VALUES (?, ?)"); $stmt->execute([$active_chat_id, "New message request from " . $user['full_name'] . " regarding " . $s['name'] . "."]); } } } $stmt = db()->prepare("INSERT INTO messages (sender_id, receiver_id, content, status, startup_id) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$user_id, $active_chat_id, $content, $status, $startup_id]); header("Location: messages.php?chat_with=$active_chat_id"); exit; } } ?>
Would you like to accept this message request?
Find co-founders in the matching section.