Skip to content

🐛(backend) fix threads ordering#617

Merged
jbpenrath merged 1 commit intomainfrom
fix/thread-order-all-messages
Apr 2, 2026
Merged

🐛(backend) fix threads ordering#617
jbpenrath merged 1 commit intomainfrom
fix/thread-order-all-messages

Conversation

@jbpenrath
Copy link
Copy Markdown
Contributor

@jbpenrath jbpenrath commented Apr 2, 2026

Purpose

There was bug in the default thread ordering. Actually, on list that embed active and draft messages, some draft messages might have a messaged_at at None (New message) but when we were sort threads by this field, new draft message was sort at the top of the list because of null attribute. So in this case, we need to first try to sort by messaged_at then by draft_message_at.

Summary by CodeRabbit

  • Bug Fixes

    • Improved thread list ordering so draft-only conversations are positioned correctly.
    • Fixed date display for draft threads to show the most recent draft time when messages have not been sent.
  • Tests

    • Added API tests to verify thread ordering and date behavior across default and filtered views.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 2, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b300e976-c63b-4339-b6f1-8c8c6755b52c

📥 Commits

Reviewing files that changed from the base of the PR and between 07f7826 and 35e297b.

📒 Files selected for processing (3)
  • src/backend/core/api/viewsets/thread.py
  • src/backend/core/tests/api/test_threads_list.py
  • src/frontend/src/features/layouts/components/thread-panel/components/thread-item/index.tsx
✅ Files skipped from review due to trivial changes (1)
  • src/frontend/src/features/layouts/components/thread-panel/components/thread-item/index.tsx
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/backend/core/tests/api/test_threads_list.py
  • src/backend/core/api/viewsets/thread.py

📝 Walkthrough

Walkthrough

Thread listing ordering was changed to build and apply an ordering expression (using Coalesce("messaged_at", "draft_messaged_at").desc() as the default) in the backend, with new tests covering ordering scenarios and a frontend fallback to draft_messaged_at for display when messaged_at is null.

Changes

Cohort / File(s) Summary
Backend Thread Ordering Logic
src/backend/core/api/viewsets/thread.py
Renamed ThreadViewSet._get_order_field()ThreadViewSet._get_order_expression(); return ordering expressions and apply order_by(order_expression, "-created_at"); use Coalesce("messaged_at", "draft_messaged_at").desc() as default to handle messaged_at IS NULL.
Thread List Test Coverage
src/backend/core/tests/api/test_threads_list.py
Added five tests asserting ordering behavior: default ordering by messaged_at, draft-only thread placement, draft-only ordering by draft time, filtered ordering with has_messages=1, and view-specific ordering with has_draft=1.
Frontend Thread Display
src/frontend/src/features/layouts/components/thread-panel/components/thread-item/index.tsx
Adjusted threadDate memoization to fall back to thread.draft_messaged_at when thread.messaged_at is falsy, ensuring draft-only threads have an appropriate display date.

Sequence Diagram(s)

sequenceDiagram
  participant Client as Client
  participant Frontend as Frontend (thread-item)
  participant API as API (ThreadViewSet)
  participant DB as Database

  rect rgba(200,220,255,0.5)
    Client->>Frontend: Request threads list
    Frontend->>API: GET /threads (query params)
    API->>API: _get_order_expression(query_params)
    API->>DB: SELECT ... ORDER BY COALESCE(messaged_at, draft_messaged_at) DESC, created_at DESC
    DB-->>API: Rows (ordered)
    API-->>Frontend: JSON results
    Frontend->>Frontend: compute threadDate (messaged_at || draft_messaged_at)
    Frontend-->>Client: Rendered thread list
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped through rows both old and new,

I nudged NULL dates to peek through,
Coalesce stitched drafts in place,
Now threads keep tidy, timely grace,
— a rabbit's tiny code-time woohoo 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: fixing thread ordering in the backend when handling both active and draft messages.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/thread-order-all-messages

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

There was bug in the default thread ordering. Actually, on list that
embed active and draft messages, some draft messages might have a
messaged_at at None (New message) but when we were sort threads by this
field, new draft message was sort at the top of the list because of null
attribute. So in this case, we need to first try to sort by messaged_at
then by draft_message_at.
@jbpenrath jbpenrath force-pushed the fix/thread-order-all-messages branch from 07f7826 to 35e297b Compare April 2, 2026 14:09
@jbpenrath jbpenrath merged commit d72df6c into main Apr 2, 2026
13 checks passed
@jbpenrath jbpenrath deleted the fix/thread-order-all-messages branch April 2, 2026 14:27
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.

1 participant