-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_messages.php
More file actions
150 lines (137 loc) · 5.14 KB
/
get_messages.php
File metadata and controls
150 lines (137 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
require_once 'config.php';
session_start();
if (!isset($_SESSION['user_id'])) {
http_response_code(401);
exit(json_encode(['error' => 'Unauthorized access']));
}
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$limit = isset($_GET['limit']) ? min(max(10, intval($_GET['limit'])), 50) : 50;
$offset = ($page - 1) * $limit;
try {
$conn = getDbConnection();
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$countStmt = $conn->prepare("
SELECT COUNT(*) as total
FROM messages m
JOIN users u ON m.user_id = u.id
WHERE m.deleted_at IS NULL
AND (m.visibility = 'public' OR m.user_id = ?)
");
$countStmt->bind_param("i", $_SESSION['user_id']);
$countStmt->execute();
$totalResult = $countStmt->get_result();
$total = $totalResult->fetch_assoc()['total'];
$stmt = $conn->prepare("
SELECT
m.id,
m.message,
m.timestamp,
m.file_path,
m.file_type,
m.file_size,
m.file_name,
m.edited_at,
m.reply_to,
u.id as user_id,
u.username,
u.avatar_url,
u.role,
(SELECT COUNT(*) FROM message_reactions WHERE message_id = m.id) as reaction_count,
(SELECT GROUP_CONCAT(DISTINCT reaction_type) FROM message_reactions WHERE message_id = m.id) as reactions,
(SELECT COUNT(*) FROM message_attachments WHERE message_id = m.id) as attachment_count
FROM messages m
JOIN users u ON m.user_id = u.id
WHERE m.deleted_at IS NULL
AND (m.visibility = 'public' OR m.user_id = ?)
ORDER BY m.timestamp DESC
LIMIT ? OFFSET ?
");
$stmt->bind_param("iii", $_SESSION['user_id'], $limit, $offset);
$stmt->execute();
$result = $stmt->get_result();
$messages = [];
while ($row = $result->fetch_assoc()) {
$attachments = [];
if ($row['attachment_count'] > 0) {
$attachStmt = $conn->prepare("
SELECT * FROM message_attachments
WHERE message_id = ?
");
$attachStmt->bind_param("i", $row['id']);
$attachStmt->execute();
$attachResult = $attachStmt->get_result();
while ($attach = $attachResult->fetch_assoc()) {
$attachments[] = [
'id' => $attach['id'],
'file_path' => $attach['file_path'],
'file_type' => $attach['file_type'],
'file_name' => $attach['file_name'],
'file_size' => $attach['file_size']
];
}
}
$reactions = [];
if ($row['reactions']) {
$reactionTypes = explode(',', $row['reactions']);
$reactionStmt = $conn->prepare("
SELECT reaction_type, COUNT(*) as count
FROM message_reactions
WHERE message_id = ?
GROUP BY reaction_type
");
$reactionStmt->bind_param("i", $row['id']);
$reactionStmt->execute();
$reactionResult = $reactionStmt->get_result();
while ($reaction = $reactionResult->fetch_assoc()) {
$reactions[$reaction['reaction_type']] = $reaction['count'];
}
}
$messages[] = [
'id' => $row['id'],
'message' => $row['message'],
'username' => $row['username'],
'user_id' => $row['user_id'],
'avatar_url' => $row['avatar_url'],
'role' => $row['role'],
'timestamp' => $row['timestamp'],
'edited_at' => $row['edited_at'],
'file_path' => $row['file_path'],
'file_type' => $row['file_type'],
'file_name' => $row['file_name'],
'file_size' => $row['file_size'],
'reply_to' => $row['reply_to'],
'attachments' => $attachments,
'reactions' => $reactions,
'reaction_count' => $row['reaction_count'],
'can_edit' => ($_SESSION['user_id'] === $row['user_id'] || $_SESSION['user_role'] === 'admin'),
'can_delete' => ($_SESSION['user_id'] === $row['user_id'] || $_SESSION['user_role'] === 'admin')
];
}
echo json_encode([
'messages' => $messages,
'pagination' => [
'total' => $total,
'page' => $page,
'limit' => $limit,
'pages' => ceil($total / $limit),
'hasMore' => ($offset + $limit) < $total
],
'timestamp' => time(),
'server_time' => date('c')
], JSON_PRETTY_PRINT);
} catch (Exception $e) {
error_log("Error in get_messages.php: " . $e->getMessage());
http_response_code(500);
echo json_encode([
'error' => 'An error occurred while fetching messages',
'debug' => DEBUG_MODE ? $e->getMessage() : null
]);
} finally {
if (isset($stmt)) $stmt->close();
if (isset($countStmt)) $countStmt->close();
if (isset($attachStmt)) $attachStmt->close();
if (isset($reactionStmt)) $reactionStmt->close();
if (isset($conn)) $conn->close();
}
?>