Skip to content

[Fixes #170] allow users of mdk to choose whether to sort by createdAt or processedAt first#171

Merged
erskingardner merged 2 commits intomasterfrom
feat/custom-message-sort-order
Feb 10, 2026
Merged

[Fixes #170] allow users of mdk to choose whether to sort by createdAt or processedAt first#171
erskingardner merged 2 commits intomasterfrom
feat/custom-message-sort-order

Conversation

@dannym-arx
Copy link
Contributor

@dannym-arx dannym-arx commented Feb 9, 2026

I don't know if this is the right approach honestly, or if this is even a good idea in the first place

This PR adds configurable message sort order across the mdk stack and a matching "last message" retrieval that respects the chosen ordering, so clients can choose to order messages by created_at (default) or by processed_at (local reception time) and get a consistent last-message value. The change propagates a new MessageSortOrder enum and a sort_order field on Pagination through storage traits, storage implementations, core APIs, and UniFFI bindings, with tests and a SQLite index to support processed-at-first queries.

What changed:

  • Introduced MessageSortOrder enum (CreatedAtFirst default, ProcessedAtFirst) and added sort_order: Option to Pagination in mdk-storage-traits.
  • Added Message::processed_at_order_cmp and compare_processed_at_keys helpers for processed-at-first ordering.
  • Added GroupStorage::last_message(group_id, sort_order) trait method and implemented it in memory and SQLite storages to return the most recent message according to the requested sort order.
  • Updated messages() implementations in mdk-memory-storage and mdk-sqlite-storage to respect Pagination.sort_order and apply the appropriate three-key ordering (processed_at, created_at, id) for ProcessedAtFirst.
  • Added SQLite migration V003 creating idx_messages_sorting_processed_at (mls_group_id, processed_at DESC, created_at DESC, id DESC) to optimize ProcessedAtFirst queries.
  • Added MDK::get_last_message(...) in mdk-core and exposed sort_order parameter and get_last_message in mdk-uniffi, including parsing for "created_at_first" and "processed_at_first".
  • Updated changelogs across affected crates and added/updated tests exercising sort-order behavior and pagination.

Security impact:

  • No cryptographic, key management, identity, SQLCipher, or file-permission changes detected.

Protocol changes:

  • No changes to the MLS protocol, Nostr integrations, or MIP behavior are introduced.

API surface:

  • Breaking/non-backward API changes: Pagination now includes sort_order: Option and new constructor with_sort_order; callers constructing Pagination directly may be affected.
  • New public enum: MessageSortOrder in mdk-storage-traits.
  • New public trait method: GroupStorage::last_message(&self, group_id: &GroupId, sort_order: MessageSortOrder) -> Result<Option, GroupError>.
  • New public APIs: MDK::get_last_message(...) in mdk-core and corresponding UniFFI binding Mdk::get_last_message(...); get_messages() now accepts an optional sort_order parameter at the UniFFI boundary.
  • Storage schema change: new SQLite index idx_messages_sorting_processed_at to support ProcessedAtFirst queries.

Testing:

  • Added unit/integration tests in mdk-storage-traits (shared group_tests and test_utils) validating CreatedAtFirst vs ProcessedAtFirst ordering, pagination slicing, and last_message behavior.
  • Added corresponding tests for memory-storage and sqlite-storage to exercise the new behavior.

@dannym-arx dannym-arx added the enhancement New feature or request label Feb 9, 2026
@dannym-arx dannym-arx linked an issue Feb 9, 2026 that may be closed by this pull request
@github-actions
Copy link

github-actions bot commented Feb 9, 2026

❌ Coverage: 90.99% → 90.5% (-0.49%)

@dannym-arx
Copy link
Contributor Author

@coderabbitai no reviews?

@coderabbitai
Copy link

coderabbitai bot commented Feb 9, 2026

@dannym-arx, I'll review the changes in this PR right away.

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai
Copy link

coderabbitai bot commented Feb 9, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Adds configurable message sort order (CreatedAtFirst, ProcessedAtFirst), threads sort_order through Pagination, implements sort-aware messages() and new last_message() across storage layers, adds a DB index for ProcessedAtFirst, and exposes sort-order-aware APIs in core and UniFFI; tests updated for sorting and pagination.

Changes

Cohort / File(s) Summary
Storage Traits API
crates/mdk-storage-traits/src/groups/mod.rs, crates/mdk-storage-traits/src/messages/types.rs
Adds MessageSortOrder enum, Pagination.sort_order + constructors/accessor, comparison helpers for processed-at ordering, and GroupStorage::last_message() trait method.
Core MDK
crates/mdk-core/src/messages/mod.rs
Adds MDK::get_last_message(mls_group_id, sort_order) delegating to storage.last_message and mapping errors. Imports MessageSortOrder.
Memory Storage
crates/mdk-memory-storage/src/groups.rs
messages() now respects Pagination.sort_order and uses appropriate comparators; implements last_message() that returns the last message per requested sort order.
SQLite Storage
crates/mdk-sqlite-storage/src/groups.rs, crates/mdk-sqlite-storage/migrations/V003__add_processed_at_first_sort_index.sql
Conditional SQL queries/ORDER BY depending on MessageSortOrder; adds last_message() using ORDER BY ... LIMIT 1; adds composite index (mls_group_id, processed_at DESC, created_at DESC, id DESC).
UniFFI bindings
crates/mdk-uniffi/src/lib.rs
Parses string sort_order, wires it into get_messages() pagination, and adds get_last_message() UniFFI API.
Tests & Test Utilities
crates/mdk-storage-traits/src/test_utils.rs, crates/mdk-storage-traits/tests/shared/group_tests.rs, crates/mdk-storage-traits/tests/{memory_storage,sqlite_storage}.rs
Adds shared tests for sort order, pagination under ProcessedAtFirst, and last_message behavior; integrates tests into memory and sqlite test suites.
Changelogs
**/CHANGELOG.md (multiple crates)
Updates changelogs to document new sort-order support, last_message by sort order, and DB migration.

Sequence Diagram(s)

sequenceDiagram
  participant Client as UniFFI Client
  participant Core as MDK Core
  participant Storage as GroupStorage impl
  participant DB as Database

  Client->>Core: get_last_message(group_id, sort_order)
  Core->>Storage: last_message(group_id, sort_order)
  Storage->>DB: query (ORDER BY based on sort_order LIMIT 1)
  DB-->>Storage: row / none
  Storage-->>Core: Message | None
  Core-->>Client: Message | None
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

breaking-change, storage

Suggested reviewers

  • jgmontoya
  • mubarakcoded
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly describes the main feature added in this pull request: allowing users to choose between sorting messages by createdAt or processedAt first.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
No Sensitive Identifier Leakage ✅ Passed The pull request's modifications contain no sensitive identifier leakage. The new get_last_message method handles errors generically without exposing parameters, and the Debug trait implementation properly redacts mls_group_id fields.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/custom-message-sort-order

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

Copy link
Contributor

@jgmontoya jgmontoya left a comment

Choose a reason for hiding this comment

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

LGTM!

@github-actions
Copy link

❌ Coverage: 90.95% → 90.46% (-0.49%)

@erskingardner erskingardner merged commit 5ef0c60 into master Feb 10, 2026
13 of 15 checks passed
@erskingardner erskingardner deleted the feat/custom-message-sort-order branch February 10, 2026 16:27
@coderabbitai coderabbitai bot mentioned this pull request Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow specifying custom sort order for messages in mdk.get_messages

4 participants