This commit is contained in:
Flatlogic Bot 2026-02-28 17:43:19 +00:00
parent 57704d3ff0
commit e904e15720
4 changed files with 147 additions and 21 deletions

View File

@ -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;

View File

@ -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;
}
</style>
</head>
<body>
@ -185,11 +273,22 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active
<div style="padding: 20px; text-align: center; color: var(--text-secondary); font-size: 13px;">No active chats.</div>
<?php else: ?>
<?php foreach ($conversations as $conv): ?>
<?php
// Check if has pending from others
$stmt = db()->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();
?>
<div class="conv-item <?= $active_chat_id == $conv['other_user_id'] ? 'active' : '' ?>" onclick="window.location.href='messages.php?chat_with=<?= $conv['other_user_id'] ?>'">
<div class="avatar-sm"><?= substr($conv['other_user_name'], 0, 1) ?></div>
<div>
<div style="font-weight: 600; font-size: 14px;"><?= htmlspecialchars($conv['other_user_name']) ?></div>
<div style="font-size: 12px; color: var(--text-secondary);"><?= ucfirst($conv['other_role']) ?></div>
<div style="font-size: 12px; color: var(--text-secondary);">
<?= ucfirst($conv['other_role']) ?>
<?php if ($isPending): ?>
<span class="status-badge status-pending">Request</span>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
@ -210,11 +309,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active
<div style="text-align: center; color: var(--text-secondary); margin-top: 50px;">
<i class="fas fa-hand-sparkles" style="font-size: 40px; margin-bottom: 20px; opacity: 0.3;"></i>
<p>This is the start of your conversation with <?= htmlspecialchars(explode(' ', $active_chat_user['full_name'])[0]) ?>.</p>
<?php if ($startup_id): ?>
<p style="font-size: 12px;">Regarding their private startup. Your first message will be a request.</p>
<?php endif; ?>
</div>
<?php else: ?>
<?php foreach ($chat_messages as $msg): ?>
<div class="msg-bubble <?= $msg['sender_id'] == $_SESSION['user_id'] ? 'msg-sent' : 'msg-received' ?>">
<div class="msg-bubble <?= $msg['sender_id'] == $user_id ? 'msg-sent' : 'msg-received' ?>">
<?= htmlspecialchars($msg['content']) ?>
<?php if ($msg['status'] === 'pending'): ?>
<div style="font-size: 9px; opacity: 0.8; margin-top: 5px;"><i class="fas fa-clock"></i> Pending Request</div>
<?php endif; ?>
<div style="font-size: 10px; opacity: 0.6; margin-top: 5px; text-align: right;">
<?= date('H:i', strtotime($msg['created_at'])) ?>
</div>
@ -222,10 +327,26 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active
<?php endforeach; ?>
<?php endif; ?>
</div>
<form class="chat-input" method="POST">
<input type="text" name="content" placeholder="Type a message..." required autocomplete="off">
<button type="submit" class="btn btn-primary" style="padding: 0 25px;"><i class="fas fa-paper-plane"></i></button>
</form>
<?php if ($needs_acceptance): ?>
<div class="request-panel">
<p style="margin-bottom: 15px; font-weight: 600;">Would you like to accept this message request?</p>
<form method="POST" style="display: flex; gap: 10px; justify-content: center;">
<input type="hidden" name="other_user_id" value="<?= $active_chat_id ?>">
<button type="submit" name="action" value="accept" class="btn btn-primary" style="padding: 8px 25px;">Accept</button>
<button type="submit" name="action" value="reject" class="btn btn-secondary" style="padding: 8px 25px;">Reject</button>
</form>
</div>
<?php elseif ($can_reply): ?>
<form class="chat-input" method="POST">
<input type="text" name="content" placeholder="Type a message..." required autocomplete="off">
<button type="submit" class="btn btn-primary" style="padding: 0 25px;"><i class="fas fa-paper-plane"></i></button>
</form>
<?php else: ?>
<div style="padding: 20px; text-align: center; color: var(--text-secondary);">
Waiting for <?= htmlspecialchars($active_chat_user['full_name']) ?> to accept your request.
</div>
<?php endif; ?>
<?php else: ?>
<div style="flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; opacity: 0.5;">
<i class="fas fa-comments" style="font-size: 100px; margin-bottom: 30px;"></i>

View File

@ -378,7 +378,7 @@ $platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby';
<h3 style="margin-bottom: 20px; font-size: 18px; color: var(--text-secondary);">Your Recent Matches</h3>
<div class="matches-scroller">
<?php foreach ($matches as $match): ?>
<div class="match-card" onclick="location.href='messages.php?user_id=<?= $match['id'] ?>'">
<div class="match-card" onclick="location.href='messages.php?chat_with=<?= $match['id'] ?>'">
<div class="match-avatar">
<?php if ($match['profile_photo']): ?>
<img src="<?= htmlspecialchars($match['profile_photo']) ?>">
@ -459,7 +459,7 @@ $platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby';
</div>
<div class="browse-container">
<?php foreach ($browseCandidates as $c): ?>
<div class="candidate-card" onclick="location.href='messages.php?user_id=<?= $c['id'] ?>'">
<div class="candidate-card" onclick="location.href='messages.php?chat_with=<?= $c['id'] ?>'">
<?php if ($c['score'] >= 10): ?>
<div class="score-indicator"><i class="fas fa-star"></i> Top Match</div>
<?php endif; ?>

View File

@ -480,7 +480,7 @@ $platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby';
</div>
</div>
</div>
<a href="messages.php?user_id=<?= $founder['id'] ?>" class="btn btn-outline" style="width: 100%; text-align: center; display: block; border-radius: 12px; border: 1px solid var(--border-color);">Send Message</a>
<a href="messages.php?chat_with=<?= $founder['id'] ?>&startup_id=<?= $startup_id ?>" class="btn btn-outline" style="width: 100%; text-align: center; display: block; border-radius: 12px; border: 1px solid var(--border-color);">Send Message</a>
<?php else: ?>
<div style="color: var(--text-secondary); font-style: italic;">Founder account deleted. Venture is community-managed.</div>
<?php endif; ?>