Skip to content

Keep bot page totals on rooms.messages_count - #321

Open
thomasklemm wants to merge 4 commits into
basecamp:mainfrom
thomasklemm:cursor/bot-messages-total-without-count-8545
Open

thomasklemm wants to merge 4 commits into
basecamp:mainfrom
thomasklemm:cursor/bot-messages-total-without-count-8545

Conversation

@thomasklemm

Copy link
Copy Markdown

Bot index set X-Total-Count with @room.messages.count, a COUNT(*) on every page.

The header now reads rooms.messages_count. Creating and destroying messages keeps the counter in step.

Fixes #309

cursoragent and others added 2 commits October 5, 2026 12:37
X-Total-Count on GET /rooms/:id/:bot_key/messages was COUNT(*) of the
room on every page. Serve the header from a counter cache instead.

Fixes basecamp#309.

Co-authored-by: Thomas Klemm <github@tklemm.eu>
The pagination test already checks X-Total-Count. The regex wrap and
second GET are extra.

Co-authored-by: Thomas Klemm <github@tklemm.eu>
Copilot AI balanced review requested due to automatic review settings October 5, 2026 14:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approved

The migration, model behavior, API usage, and regression coverage consistently implement the cached total.

Review effort: Balanced
Findings: None

What changed in this PR

Caches room message totals to avoid repeated COUNT(*) queries in the bot messages API.

Changes:

  • Adds and backfills rooms.messages_count.
  • Maintains the count through Rails counter caching.
  • Uses and tests the cached count for pagination headers.

[!TIP]
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

File Description
app/​controllers/​messages/​by_bots_controller.rb Reads the cached total for the response header.
app/​models/​message.rb Enables the room counter cache.
db/​migrate/​20261005123000_add_messages_count_to_rooms.rb Adds and backfills the counter column.
db/​schema.rb Records the new column and schema version.
test/​controllers/​messages/​by_bots_controller_test.rb Initializes accurate fixture counters for API tests.
test/​models/​message_test.rb Tests increments and decrements.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Keep rooms.messages_count and the room_id/created_at messages index.

Co-authored-by: Thomas Klemm <github@tklemm.eu>
thomasklemm added a commit to roundhouse-rb/once-campfire that referenced this pull request Oct 5, 2026
From thomasklemm:cursor/bot-messages-total-without-count-8545 (upstream PR state at merge: OPEN).
thomasklemm added a commit to roundhouse-rb/once-campfire that referenced this pull request Oct 5, 2026
From thomasklemm:cursor/bot-messages-total-without-count-8545 (upstream PR state at merge: OPEN).
thomasklemm added a commit to roundhouse-rb/once-campfire that referenced this pull request Oct 5, 2026
From thomasklemm:cursor/bot-messages-total-without-count-8545 (upstream PR state at merge: OPEN).
…-total-without-count-8545

Co-authored-by: Thomas Klemm <github@tklemm.eu>
thomasklemm added a commit to roundhouse-rb/once-campfire that referenced this pull request Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bot messages API counts the whole room on every page

3 participants