Skip to content

feat(sdk): LatestEventValue implements Serialize and Deserialize #5561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Hywan
Copy link
Member

@Hywan Hywan commented Aug 20, 2025

The problem is: LatestEventContent cannot be serialized. It's annoying because it means we can't store a LatestEventValue (that wraps a LatestEventContent) in the database.

This patch revisits LatestEventValue. Before we got:

pub enum LatestEventValue {
     None,
     Remote(LatestEventContent),
     LocalIsSending(LatestEventContent),
     LocalCannotBeSent(LatestEventContent),
}

pub enum LatestEventContent {
    RoomMessage(RoomMessageEventContent),
    Sticker(StickerEventContent),
    Poll(UnstablePollStartEventContent),
    CallInvite(CallInviteEventContent),
    CallNotify(CallNotifyEventContent),
    KnockedStateEvent(RoomMemberEventContent),
    Redacted(AnySyncMessageLikeEvent),
}

LatestEventContent::Redacted contains an AnySyncMessageLikeEvent. That's the part that is not serializable.

It appears that LatestEventContent isn't necessary! The only thing we need is to filter the events by their type, no need to find and map. The LatestEventValue can contain the entry event directly (e.g. a TimelineEvent for the event cache). Okay, let's do that.

pub enum LatestEventValue {
    None,
    Remote(RemoteLatestEventValue),
    LocalIsSending(???),
    LocalCannotBeSent(???),
}

type RemoteLatestEventValue = TimelineEvent;

What about the Local* variants? We can't use a TimelineEvent. We need a new type for that:

pub enum LatestEventValue {
    None,
    Remote(RemoteLatestEventValue),
    LocalIsSending(LocalLatestEventValue),
    LocalCannotBeSent(LocalLatestEventValue),
}

pub struct LocalLatestEventValue {
    pub timestamp: MilliSecondsSinceUnixEpoch,
    pub content: Raw<AnyMessageLikeEventContent>,
    pub event_type: String,
}

We don't need the event ID nor the transaction ID in LocalLatestEventValue.

That's the only change. All the other changes are about the tests.


Hywan added 2 commits August 20, 2025 15:54
The problem is: `LatestEventContent` cannot be serialized. It's annoying
because it means we can't store a `LatestEventValue` (that wraps a
`LatestEventContent`) in the database.

This patch revisits `LatestEventValue`. Before we got:

```rust
pub enum LatestEventValue {
     None,
     Remote(LatestEventContent),
     LocalIsSending(LatestEventContent),
     LocalCannotBeSent(LatestEventContent),
}

pub enum LatestEventContent {
    RoomMessage(RoomMessageEventContent),
    Sticker(StickerEventContent),
    Poll(UnstablePollStartEventContent),
    CallInvite(CallInviteEventContent),
    CallNotify(CallNotifyEventContent),
    KnockedStateEvent(RoomMemberEventContent),
    Redacted(AnySyncMessageLikeEvent),
}
```

`LatestEventContent::Redacted` contains an `AnySyncMessageLikeEvent`.
That's the part that is not serializable.

It appears that `LatestEventContent` isn't necessary! The only thing we
need is to _filter_ the events by their type, no need to _find and
map_. The `LatestEventValue` can contain the entry event directly (e.g.
a `TimelineEvent` for the event cache). Okay, let's do that.

```rust
pub enum LatestEventValue {
    None,
    Remote(RemoteLatestEventValue),
    LocalIsSending(???),
    LocalCannotBeSent(???),
}

type RemoteLatestEventValue = TimelineEvent;
```

What about the `Local*` variants? We can't use a `TimelineEvent`. We
need a new type for that:

```rust
pub enum LatestEventValue {
    None,
    Remote(RemoteLatestEventValue),
    LocalIsSending(LocalLatestEventValue),
    LocalCannotBeSent(LocalLatestEventValue),
}

pub struct LocalLatestEventValue {
    pub timestamp: MilliSecondsSinceUnixEpoch,
    pub content: Raw<AnyMessageLikeEventContent>,
    pub event_type: String,
}
```

We don't need the event ID nor the transaction ID in
`LocalLatestEventValue`.

That's the only change. All the other changes are about the tests.
This patch implements `Serialize` and `Deserialize` on
`LatestEventValue`.
@Hywan Hywan force-pushed the fat-sdk-latest-events-content branch from b30e23f to edd32f9 Compare August 20, 2025 13:54
@Hywan Hywan marked this pull request as ready for review August 20, 2025 13:59
@Hywan Hywan requested a review from a team as a code owner August 20, 2025 13:59
@Hywan Hywan requested review from andybalaam and stefanceriu and removed request for a team and andybalaam August 20, 2025 13:59
Copy link

codspeed-hq bot commented Aug 20, 2025

CodSpeed Performance Report

Merging #5561 will not alter performance

Comparing Hywan:fat-sdk-latest-events-content (edd32f9) with main (7adaf7b)

Summary

✅ 27 untouched benchmarks
🆕 4 new benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
🆕 Restore session [SQLite][clear] N/A 703.5 ms N/A
🆕 Restore session [SQLite][encrypted] N/A 1.2 s N/A
🆕 Restore session [memory store] N/A 181 ms N/A
🆕 Create a timeline with initial events[10000 events] N/A 746.7 ms N/A

Copy link

codecov bot commented Aug 20, 2025

Codecov Report

❌ Patch coverage is 82.06278% with 40 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.64%. Comparing base (feeeb53) to head (edd32f9).
⚠️ Report is 3 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...rates/matrix-sdk/src/latest_events/latest_event.rs 81.64% 8 Missing and 30 partials ⚠️
crates/matrix-sdk/src/latest_events/mod.rs 87.50% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5561      +/-   ##
==========================================
- Coverage   88.65%   88.64%   -0.02%     
==========================================
  Files         340      340              
  Lines       95083    95029      -54     
  Branches    95083    95029      -54     
==========================================
- Hits        84292    84234      -58     
+ Misses       6609     6608       -1     
- Partials     4182     4187       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bnjbvr
Copy link
Member

bnjbvr commented Aug 21, 2025

I think that instead of:

    pub content: Raw<AnyMessageLikeEventContent>,
    pub event_type: String,

Could you use SerializableEventContent, which likely encodes the semantics you're interested in?

@stefanceriu stefanceriu requested review from bnjbvr and removed request for stefanceriu August 21, 2025 09:49
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.

2 participants