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 Actions (Accept/Reject/Unmatch/Block) 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]); $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; } elseif ($action === 'unmatch') { $stmt = db()->prepare("UPDATE matches SET status = 'unmatched' WHERE (user1_id = ? AND user2_id = ?) OR (user1_id = ? AND user2_id = ?)"); $stmt->execute([$user_id, $other_id, $other_id, $user_id]); header("Location: messages.php"); exit; } elseif ($action === 'block') { $stmt = db()->prepare("INSERT IGNORE INTO blocked_users (blocker_id, blocked_id) VALUES (?, ?)"); $stmt->execute([$user_id, $other_id]); // Also unmatch if they were matched $stmt = db()->prepare("UPDATE matches SET status = 'unmatched' WHERE (user1_id = ? AND user2_id = ?) OR (user1_id = ? AND user2_id = ?)"); $stmt->execute([$user_id, $other_id, $other_id, $user_id]); header("Location: messages.php"); exit; } } // Fetch Blocked Users to filter them out $stmt = db()->prepare("SELECT blocked_id FROM blocked_users WHERE blocker_id = ?"); $stmt->execute([$user_id]); $blocked_ids = $stmt->fetchAll(PDO::FETCH_COLUMN); $stmt = db()->prepare("SELECT blocker_id FROM blocked_users WHERE blocked_id = ?"); $stmt->execute([$user_id]); $blocked_by_ids = $stmt->fetchAll(PDO::FETCH_COLUMN); $all_blocked = array_unique(array_merge($blocked_ids, $blocked_by_ids)); $placeholders = empty($all_blocked) ? "0" : implode(',', array_fill(0, count($all_blocked), '?')); // Fetch Matches for Founders (active only, not blocked) $matches = []; if ($user['role'] === 'founder') { $sql = " 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 != ? AND m.status = 'active' AND u.id NOT IN ($placeholders) "; $stmt = db()->prepare($sql); $params = array_merge([$user_id, $user_id, $user_id], $all_blocked ?: []); $stmt->execute($params); $matches = $stmt->fetchAll(); } // Fetch Conversations (accepted or pending from others, 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 u.id NOT IN ($placeholders) 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 = db()->prepare($sql); $params = array_merge([$user_id, $user_id, $user_id, $user_id], $all_blocked ?: []); $stmt->execute($params); $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; $is_currently_matched = false; if ($active_chat_id) { // Check if blocked if (in_array($active_chat_id, $all_blocked)) { header("Location: messages.php"); exit; } $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) { $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]); $has_pending_from_other = (bool)$stmt->fetchColumn(); if (!$has_accepted && !$is_currently_matched && $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)) { $status = 'accepted'; $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_match = (bool)$stmt->fetchColumn(); $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) { 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'; $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.