diff --git a/api_v1_channels.php b/api_v1_channels.php index dfb854c..87e0e60 100644 --- a/api_v1_channels.php +++ b/api_v1_channels.php @@ -70,6 +70,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Explicitly exclude position from update to prevent jumping to bottom $stmt = db()->prepare("UPDATE channels SET name = ?, type = ?, status = ?, allow_file_sharing = ?, message_limit = ?, icon = ?, category_id = ?, rules_role_id = ? WHERE id = ?"); $stmt->execute([$name, $type, $status, $allow_file_sharing, $message_limit, $icon, $category_id, $rules_role_id, $channel_id]); + + if ($message_limit !== null) { + require_once 'db/config.php'; + enforceChannelLimit($channel_id); + } } header('Location: index.php?server_id=' . $server_id . '&channel_id=' . $channel_id); exit; diff --git a/api_v1_messages.php b/api_v1_messages.php index 2c3794b..5294835 100644 --- a/api_v1_messages.php +++ b/api_v1_messages.php @@ -232,26 +232,7 @@ try { $last_id = db()->lastInsertId(); // Enforce message limit if set - $stmt = db()->prepare("SELECT message_limit FROM channels WHERE id = ?"); - $stmt->execute([$channel_id]); - $channel = $stmt->fetch(); - if ($channel && !empty($channel['message_limit'])) { - $limit = (int)$channel['message_limit']; - // Delete oldest messages that exceed the limit - $stmt = db()->prepare(" - DELETE FROM messages - WHERE channel_id = ? - AND id NOT IN ( - SELECT id FROM ( - SELECT id FROM messages - WHERE channel_id = ? - ORDER BY created_at DESC, id DESC - LIMIT " . $limit . " - ) as tmp - ) - "); - $stmt->execute([$channel_id, $channel_id]); - } + enforceChannelLimit($channel_id); // Get server_id for the channel $stmt = db()->prepare("SELECT server_id FROM channels WHERE id = ?"); diff --git a/api_v1_rss.php b/api_v1_rss.php index eba4996..ac132b0 100644 --- a/api_v1_rss.php +++ b/api_v1_rss.php @@ -78,7 +78,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $items = $xml->entry; } + $feed_items = []; foreach ($items as $item) { + $feed_items[] = $item; + } + $feed_items = array_reverse($feed_items); + + foreach ($feed_items as $item) { $guid = (string)($item->guid ?? ($item->id ?? $item->link)); if (empty($guid) && isset($item->link['href'])) { $guid = (string)$item->link['href']; @@ -159,6 +165,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $stmt_msg = db()->prepare("INSERT INTO messages (channel_id, user_id, content, metadata, rss_guid) VALUES (?, ?, ?, ?, ?)"); $stmt_msg->execute([$channel_id, $bot_id, $content, $metadata, $guid]); + enforceChannelLimit($channel_id); $new_items_count++; } diff --git a/api_v1_webhook.php b/api_v1_webhook.php index c436214..a4ff9bd 100644 --- a/api_v1_webhook.php +++ b/api_v1_webhook.php @@ -28,6 +28,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_GET['token'])) { try { $stmt = db()->prepare("INSERT INTO messages (channel_id, user_id, content) VALUES (?, ?, ?)"); $stmt->execute([$webhook['channel_id'], 1, $content]); // 1 is system/bot user + enforceChannelLimit($webhook['channel_id']); echo json_encode(['success' => true]); } catch (Exception $e) { http_response_code(500); diff --git a/db/config.php b/db/config.php index a719b78..3876e87 100644 --- a/db/config.php +++ b/db/config.php @@ -15,3 +15,26 @@ function db() { } return $pdo; } + +function enforceChannelLimit($channel_id) { + $stmt = db()->prepare("SELECT message_limit FROM channels WHERE id = ?"); + $stmt->execute([$channel_id]); + $channel = $stmt->fetch(); + if ($channel && !empty($channel['message_limit'])) { + $limit = (int)$channel['message_limit']; + // Delete oldest messages that exceed the limit + $stmt = db()->prepare(" + DELETE FROM messages + WHERE channel_id = ? + AND id NOT IN ( + SELECT id FROM ( + SELECT id FROM messages + WHERE channel_id = ? + ORDER BY created_at DESC, id DESC + LIMIT " . $limit . " + ) as tmp + ) + "); + $stmt->execute([$channel_id, $channel_id]); + } +} diff --git a/requests.log b/requests.log index 5ed9ed9..af17a36 100644 --- a/requests.log +++ b/requests.log @@ -405,3 +405,11 @@ 2026-02-16 23:39:53 - GET /index.php?server_id=1&channel_id=12 - POST: [] 2026-02-16 23:40:18 - GET /index.php?server_id=1&channel_id=17 - POST: [] 2026-02-16 23:40:19 - GET /index.php?server_id=1&channel_id=12 - POST: [] +2026-02-16 23:44:06 - GET /?fl_project=38443 - POST: [] +2026-02-16 23:45:42 - GET /?fl_project=38443 - POST: [] +2026-02-16 23:45:45 - GET /index.php?server_id=1&channel_id=17 - POST: [] +2026-02-16 23:45:46 - GET /index.php?server_id=1&channel_id=12 - POST: [] +2026-02-16 23:46:19 - GET /index.php?server_id=1&channel_id=12 - POST: [] +2026-02-16 23:46:41 - GET /index.php?server_id=1&channel_id=12 - POST: [] +2026-02-16 23:47:02 - GET /index.php?server_id=1&channel_id=12 - POST: [] +2026-02-16 23:48:40 - GET /index.php?server_id=1&channel_id=12 - POST: []