36 lines
1.1 KiB
PHP
36 lines
1.1 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once 'db/config.php';
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$content = $data['content'] ?? '';
|
|
$channel_id = (int)($data['channel_id'] ?? 1);
|
|
$user_id = 1; // Mock logged in user
|
|
|
|
if (empty($content)) {
|
|
echo json_encode(['success' => false, 'error' => 'Empty content']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$stmt = db()->prepare("INSERT INTO messages (channel_id, user_id, content) VALUES (?, ?, ?)");
|
|
$stmt->execute([$channel_id, $user_id, $content]);
|
|
$last_id = db()->lastInsertId();
|
|
|
|
// Fetch message with username for the response
|
|
$stmt = db()->prepare("SELECT m.*, u.username FROM messages m JOIN users u ON m.user_id = u.id WHERE m.id = ?");
|
|
$stmt->execute([$last_id]);
|
|
$msg = $stmt->fetch();
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => [
|
|
'username' => $msg['username'],
|
|
'content' => htmlspecialchars($msg['content']),
|
|
'time' => date('H:i', strtotime($msg['created_at']))
|
|
]
|
|
]);
|
|
} catch (Exception $e) {
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|