[PR #6991] [MERGED] fix(tgbot): restore current ban log delivery and skip empty files #6564

Closed
opened 2026-11-02 04:09:40 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6991
Author: @HamedSanaei
Created: 10/28/2026
Status: ✅ Merged
Merged: 11/2/2026
Merged by: @MHSanaei

Base: main ← Head: fix/tgbot-ban-logs-6990


📝 Commits (1)

  • 7585272 fix(tgbot): restore ban log delivery and skip empty files

📊 Changes

2 files changed (+260 additions, -16 deletions)

View changed files

➕ internal/web/service/tgbot/ban_logs_test.go (+222 -0)
📝 internal/web/service/tgbot/screen_views.go (+38 -16)

📄 Description

Summary

  • Restore delivery of both banned-IP logs, previous first and current second, as separate documents.
  • Skip missing, inaccessible, empty, or nonregular files and log the reason.
  • Send the existing localized tgbot.noResult notice only when neither file is available to upload. Log upload failures separately and still attempt the second file after a failure of the first.
  • Add regression coverage using an httptest.Server that inspects actual Telegram document uploads.

Root Cause

The Telegram bot rewrite in #6760 replaced v3.10.2's sendBanLogs with sendBanLogsFile, retaining only GetIPLimitBannedPrevLogPath() and dropping the zero-byte check. ClearLogsJob rotates the current banned-IP log into the previous log daily, so the bot stopped delivering today's bans until that rotation, and an empty previous log was submitted to Telegram as a document.

The fix checks both paths in order. Each file and its 60-second upload context are cleaned up before processing the next file. A nonempty file counts as available before the upload, so a Telegram error cannot produce a misleading no-results notice.

Testing

Executed with Go 1.27.3:

  • Regression tests were run before the implementation and failed on omitted current-log uploads, empty-file uploads, missing no-results notices, and failure to attempt the second upload. They pass with the fix.
  • gofmt -w internal/web/service/tgbot/screen_views.go internal/web/service/tgbot/ban_logs_test.go — completed.
  • go test ./internal/web/service/tgbot/ -count=1 — passed on Windows.
  • go vet ./internal/web/service/tgbot/... — passed.
  • go test -race ./internal/web/service/tgbot/ -count=1 — passed in the Linux golang:1.27.3-bookworm container.
  • golangci-lint run ./internal/web/service/tgbot/... — passed, 0 issues (v2.15.0).
  • git diff --check and git diff --cached --check — passed.

The regression table covers 13 scenarios: both files nonempty; either file alone; either file empty; both empty; both missing; mixed empty/missing files; a directory at the previous log path; failure of the first of two uploads; and failure of either sole nonempty file. An additional test covers an unavailable bot. Assertions check document order, filenames and bytes, localized notices, upload-error logging, chat/topic routing, hide buttons, notification behavior, unchanged file contents, and file-handle cleanup on Windows. All new API tests use temporary logs and a local HTTP server, with no live Telegram credentials or external network access.

Compatibility

This restores the previous/current delivery order from v3.10.2 while preserving the existing v3.11.0 notice mechanism, chat ID, adminTopic, hide buttons, and notification settings. Daily rotation and log contents are unchanged. No dependencies, configuration changes, or database migrations are required.

Live Telegram delivery and the full repository make verify were not run; the Telegram API is simulated, including upload rejection.

Fixes #6990


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6991 **Author:** [@HamedSanaei](https://github.com/HamedSanaei) **Created:** 10/28/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/tgbot-ban-logs-6990` --- ### 📝 Commits (1) - [`7585272`](https://github.com/MHSanaei/3x-ui/commit/758527295b7fcc82e3b74aaa8dfc0492c4ed8450) fix(tgbot): restore ban log delivery and skip empty files ### 📊 Changes **2 files changed** (+260 additions, -16 deletions) <details> <summary>View changed files</summary> ➕ `internal/web/service/tgbot/ban_logs_test.go` (+222 -0) 📝 `internal/web/service/tgbot/screen_views.go` (+38 -16) </details> ### 📄 Description ## Summary - Restore delivery of both banned-IP logs, previous first and current second, as separate documents. - Skip missing, inaccessible, empty, or nonregular files and log the reason. - Send the existing localized `tgbot.noResult` notice only when neither file is available to upload. Log upload failures separately and still attempt the second file after a failure of the first. - Add regression coverage using an `httptest.Server` that inspects actual Telegram document uploads. ## Root Cause The Telegram bot rewrite in #6760 replaced v3.10.2's `sendBanLogs` with `sendBanLogsFile`, retaining only `GetIPLimitBannedPrevLogPath()` and dropping the zero-byte check. `ClearLogsJob` rotates the current banned-IP log into the previous log daily, so the bot stopped delivering today's bans until that rotation, and an empty previous log was submitted to Telegram as a document. The fix checks both paths in order. Each file and its 60-second upload context are cleaned up before processing the next file. A nonempty file counts as available before the upload, so a Telegram error cannot produce a misleading no-results notice. ## Testing Executed with Go 1.27.3: - Regression tests were run before the implementation and failed on omitted current-log uploads, empty-file uploads, missing no-results notices, and failure to attempt the second upload. They pass with the fix. - `gofmt -w internal/web/service/tgbot/screen_views.go internal/web/service/tgbot/ban_logs_test.go` — completed. - `go test ./internal/web/service/tgbot/ -count=1` — passed on Windows. - `go vet ./internal/web/service/tgbot/...` — passed. - `go test -race ./internal/web/service/tgbot/ -count=1` — passed in the Linux `golang:1.27.3-bookworm` container. - `golangci-lint run ./internal/web/service/tgbot/...` — passed, 0 issues (v2.15.0). - `git diff --check` and `git diff --cached --check` — passed. The regression table covers 13 scenarios: both files nonempty; either file alone; either file empty; both empty; both missing; mixed empty/missing files; a directory at the previous log path; failure of the first of two uploads; and failure of either sole nonempty file. An additional test covers an unavailable bot. Assertions check document order, filenames and bytes, localized notices, upload-error logging, chat/topic routing, hide buttons, notification behavior, unchanged file contents, and file-handle cleanup on Windows. All new API tests use temporary logs and a local HTTP server, with no live Telegram credentials or external network access. ## Compatibility This restores the previous/current delivery order from v3.10.2 while preserving the existing v3.11.0 notice mechanism, chat ID, `adminTopic`, hide buttons, and notification settings. Daily rotation and log contents are unchanged. No dependencies, configuration changes, or database migrations are required. Live Telegram delivery and the full repository `make verify` were not run; the Telegram API is simulated, including upload rejection. ## Related Issue Fixes #6990 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi changed title from [PR #6991] fix(tgbot): restore current ban log delivery and skip empty files to [PR #6991] [MERGED] fix(tgbot): restore current ban log delivery and skip empty files 2026-11-07 14:34:00 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6564
No description provided.