From e904e1572019182ca1d07acaac1a8b14f6076dd7 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Sat, 28 Feb 2026 17:43:19 +0000 Subject: [PATCH] v14 --- db/migrations/08_message_requests.sql | 5 + messages.php | 157 +++++++++++++++++++++++--- partners.php | 4 +- startup_details.php | 2 +- 4 files changed, 147 insertions(+), 21 deletions(-) create mode 100644 db/migrations/08_message_requests.sql diff --git a/db/migrations/08_message_requests.sql b/db/migrations/08_message_requests.sql new file mode 100644 index 0000000..d4cc702 --- /dev/null +++ b/db/migrations/08_message_requests.sql @@ -0,0 +1,5 @@ +-- Migration: Add status and startup_id to messages +ALTER TABLE messages +ADD COLUMN status ENUM('pending', 'accepted', 'rejected') DEFAULT 'accepted' AFTER content, +ADD COLUMN startup_id INT NULL AFTER status, +ADD CONSTRAINT fk_messages_startup FOREIGN KEY (startup_id) REFERENCES startups(id) ON DELETE SET NULL; diff --git a/messages.php b/messages.php index b76bece..6f96d09 100644 --- a/messages.php +++ b/messages.php @@ -7,12 +7,36 @@ if (!isset($_SESSION['user_id'])) { require_once __DIR__ . '/db/config.php'; +$user_id = $_SESSION['user_id']; $stmt = db()->prepare("SELECT * FROM users WHERE id = ?"); -$stmt->execute([$_SESSION['user_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') { @@ -27,24 +51,28 @@ if ($user['role'] === 'founder') { WHERE (m.user1_id = ? OR m.user2_id = ?) AND u.id != ? "); - $stmt->execute([$_SESSION['user_id'], $_SESSION['user_id'], $_SESSION['user_id']]); + $stmt->execute([$user_id, $user_id, $user_id]); $matches = $stmt->fetchAll(); } -// Fetch Conversations +// 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 != ? + 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([$_SESSION['user_id'], $_SESSION['user_id'], $_SESSION['user_id']]); +$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 = ?"); @@ -52,23 +80,73 @@ if ($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 = ?) + WHERE ((sender_id = ? AND receiver_id = ?) + OR (sender_id = ? AND receiver_id = ?)) + AND status != 'rejected' ORDER BY created_at ASC "); - $stmt->execute([$_SESSION['user_id'], $active_chat_id, $active_chat_id, $_SESSION['user_id']]); + $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) { +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active_chat_id && $can_reply) { $content = trim($_POST['content']); if (!empty($content)) { - $stmt = db()->prepare("INSERT INTO messages (sender_id, receiver_id, content) VALUES (?, ?, ?)"); - $stmt->execute([$_SESSION['user_id'], $active_chat_id, $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; } @@ -113,6 +191,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active display: flex; align-items: center; gap: 12px; + position: relative; } .conv-item:hover { background: rgba(255,255,255,0.05); } .conv-item.active { background: rgba(0, 122, 255, 0.1); border-left: 3px solid var(--accent-blue); } @@ -124,6 +203,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active .chat-messages { flex: 1; overflow-y: auto; padding: 25px; display: flex; flex-direction: column; gap: 15px; } .msg-bubble { max-width: 70%; padding: 12px 18px; border-radius: 18px; font-size: 14px; line-height: 1.5; + position: relative; } .msg-sent { align-self: flex-end; background: var(--accent-blue); color: #fff; border-bottom-right-radius: 4px; } .msg-received { align-self: flex-start; background: var(--surface-color); border: 1px solid var(--border-color); border-bottom-left-radius: 4px; } @@ -131,6 +211,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active .chat-input input { flex: 1; padding: 12px 20px; border-radius: 12px; background: var(--surface-color); border: 1px solid var(--border-color); color: #fff; } + .status-badge { + font-size: 10px; padding: 2px 6px; border-radius: 4px; font-weight: 700; text-transform: uppercase; + } + .status-pending { background: #ffcc00; color: #000; } + .request-panel { + padding: 20px; background: rgba(255, 204, 0, 0.1); border-top: 1px solid rgba(255, 204, 0, 0.2); + text-align: center; + } @@ -185,11 +273,22 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active
No active chats.
+ prepare("SELECT 1 FROM messages WHERE sender_id = ? AND receiver_id = ? AND status = 'pending'"); + $stmt->execute([$conv['other_user_id'], $user_id]); + $isPending = (bool)$stmt->fetchColumn(); + ?>
-
+
+ + + Request + +
@@ -210,11 +309,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active

This is the start of your conversation with .

+ +

Regarding their private startup. Your first message will be a request.

+
-
+
+ +
Pending Request
+
@@ -222,10 +327,26 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active
-
- - -
+ + +
+

Would you like to accept this message request?

+
+ + + +
+
+ +
+ + +
+ +
+ Waiting for to accept your request. +
+
diff --git a/partners.php b/partners.php index bbc546b..07f6d4d 100644 --- a/partners.php +++ b/partners.php @@ -378,7 +378,7 @@ $platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby';

Your Recent Matches

-
+
@@ -459,7 +459,7 @@ $platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby';
-
+
= 10): ?>
Top Match
diff --git a/startup_details.php b/startup_details.php index 634d953..37023e3 100644 --- a/startup_details.php +++ b/startup_details.php @@ -480,7 +480,7 @@ $platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby';
- Send Message + Send Message
Founder account deleted. Venture is community-managed.