Skip to content
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

timeline: factor out inner part of [Sync]TimelineEvent #4082

Merged
merged 9 commits into from
Oct 9, 2024
18 changes: 9 additions & 9 deletions crates/matrix-sdk-base/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ impl BaseClient {
let event: SyncTimelineEvent =
olm.decrypt_room_event(event.cast_ref(), room_id, &decryption_settings).await?.into();

if let Ok(AnySyncTimelineEvent::MessageLike(e)) = event.event.deserialize() {
if let Ok(AnySyncTimelineEvent::MessageLike(e)) = event.raw().deserialize() {
match &e {
AnySyncMessageLikeEvent::RoomMessage(SyncMessageLikeEvent::Original(
original_event,
Expand Down Expand Up @@ -398,7 +398,7 @@ impl BaseClient {
for event in events {
let mut event: SyncTimelineEvent = event.into();

match event.event.deserialize() {
match event.raw().deserialize() {
Ok(e) => {
#[allow(clippy::single_match)]
match &e {
Expand Down Expand Up @@ -432,7 +432,7 @@ impl BaseClient {
}
}

let raw_event: Raw<AnySyncStateEvent> = event.event.clone().cast();
let raw_event: Raw<AnySyncStateEvent> = event.raw().clone().cast();
changes.add_state_event(room.room_id(), s.clone(), raw_event);
}

Expand All @@ -443,8 +443,8 @@ impl BaseClient {
room_info.room_version().unwrap_or(&RoomVersionId::V1);

if let Some(redacts) = r.redacts(room_version) {
room_info.handle_redaction(r, event.event.cast_ref());
let raw_event = event.event.clone().cast();
room_info.handle_redaction(r, event.raw().cast_ref());
let raw_event = event.raw().clone().cast();

changes.add_redaction(room.room_id(), redacts, raw_event);
}
Expand All @@ -456,7 +456,7 @@ impl BaseClient {
SyncMessageLikeEvent::Original(_),
) => {
if let Ok(Some(e)) = Box::pin(
self.decrypt_sync_room_event(&event.event, room.room_id()),
self.decrypt_sync_room_event(event.raw(), room.room_id()),
)
.await
{
Expand Down Expand Up @@ -494,14 +494,14 @@ impl BaseClient {
}

if let Some(context) = &push_context {
let actions = push_rules.get_actions(&event.event, context);
let actions = push_rules.get_actions(event.raw(), context);

if actions.iter().any(Action::should_notify) {
notifications.entry(room.room_id().to_owned()).or_default().push(
Notification {
actions: actions.to_owned(),
event: RawAnySyncOrStrippedTimelineEvent::Sync(
event.event.clone(),
event.raw().clone(),
),
},
);
Expand Down Expand Up @@ -773,7 +773,7 @@ impl BaseClient {

if let Ok(Some(decrypted)) = decrypt_sync_room_event.await {
// We found an event we can decrypt
if let Ok(any_sync_event) = decrypted.event.deserialize() {
if let Ok(any_sync_event) = decrypted.raw().deserialize() {
// We can deserialize it to find its type
match is_suitable_for_latest_event(&any_sync_event) {
PossibleLatestEvent::YesRoomMessage(_)
Expand Down
26 changes: 23 additions & 3 deletions crates/matrix-sdk-base/src/latest_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,12 @@ mod tests {
json!({
"latest_event": {
"event": {
"encryption_info": null,
"event": {
"event_id": "$1"
"kind": {
"PlainText": {
"event": {
"event_id": "$1"
}
}
}
},
}
Expand All @@ -577,6 +580,23 @@ mod tests {
assert!(deserialized.latest_event.sender_name_is_ambiguous.is_none());

// The previous format can also be deserialized.
let serialized = json!({
"latest_event": {
"event": {
"encryption_info": null,
"event": {
"event_id": "$1"
}
},
}
});

let deserialized: TestStruct = serde_json::from_value(serialized).unwrap();
assert_eq!(deserialized.latest_event.event().event_id().unwrap(), "$1");
assert!(deserialized.latest_event.sender_profile.is_none());
assert!(deserialized.latest_event.sender_name_is_ambiguous.is_none());

// The even older format can also be deserialized.
let serialized = json!({
"latest_event": event
});
Expand Down
4 changes: 2 additions & 2 deletions crates/matrix-sdk-base/src/read_receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl RoomReadReceipts {
/// Returns whether a new event triggered a new unread/notification/mention.
#[inline(always)]
fn process_event(&mut self, event: &SyncTimelineEvent, user_id: &UserId) {
if marks_as_unread(&event.event, user_id) {
if marks_as_unread(event.raw(), user_id) {
self.num_unread += 1;
}

Expand Down Expand Up @@ -408,7 +408,7 @@ impl ReceiptSelector {
fn try_match_implicit(&mut self, user_id: &UserId, new_events: &[SyncTimelineEvent]) {
for ev in new_events {
// Get the `sender` field, if any, or skip this event.
let Ok(Some(sender)) = ev.event.get_field::<OwnedUserId>("sender") else { continue };
let Ok(Some(sender)) = ev.raw().get_field::<OwnedUserId>("sender") else { continue };
if sender == user_id {
// Get the event id, if any, or skip this event.
let Some(event_id) = ev.event_id() else { continue };
Expand Down
14 changes: 8 additions & 6 deletions crates/matrix-sdk-base/src/rooms/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use std::{
use bitflags::bitflags;
use eyeball::{SharedObservable, Subscriber};
use futures_util::{Stream, StreamExt};
#[cfg(feature = "experimental-sliding-sync")]
use matrix_sdk_common::deserialized_responses::TimelineEventKind;
#[cfg(all(feature = "e2e-encryption", feature = "experimental-sliding-sync"))]
use matrix_sdk_common::ring_buffer::RingBuffer;
#[cfg(feature = "experimental-sliding-sync")]
Expand Down Expand Up @@ -1261,9 +1263,12 @@ impl RoomInfo {
if let Some(latest_event) = &mut self.latest_event {
tracing::trace!("Checking if redaction applies to latest event");
if latest_event.event_id().as_deref() == Some(redacts) {
match apply_redaction(&latest_event.event().event, _raw, room_version) {
match apply_redaction(latest_event.event().raw(), _raw, room_version) {
Some(redacted) => {
latest_event.event_mut().event = redacted;
// Even if the original event was encrypted, redaction removes all its
// fields so it cannot possibly be successfully decrypted after redaction.
latest_event.event_mut().kind =
TimelineEventKind::PlainText { event: redacted };
debug!("Redacted latest event");
}
None => {
Expand Down Expand Up @@ -1880,10 +1885,7 @@ mod tests {
"encryption_state_synced": true,
"latest_event": {
"event": {
"encryption_info": null,
"event": {
"sender": "@u:i.uk",
},
"kind": {"PlainText": {"event": {"sender": "@u:i.uk"}}},
},
},
"base_info": {
Expand Down
6 changes: 3 additions & 3 deletions crates/matrix-sdk-base/src/sliding_sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ async fn cache_latest_events(
Vec::with_capacity(room.latest_encrypted_events.read().unwrap().capacity());

for event in events.iter().rev() {
if let Ok(timeline_event) = event.event.deserialize() {
if let Ok(timeline_event) = event.raw().deserialize() {
match is_suitable_for_latest_event(&timeline_event) {
PossibleLatestEvent::YesRoomMessage(_)
| PossibleLatestEvent::YesPoll(_)
Expand Down Expand Up @@ -757,7 +757,7 @@ async fn cache_latest_events(
// Check how many encrypted events we have seen. Only store another if we
// haven't already stored the maximum number.
if encrypted_events.len() < encrypted_events.capacity() {
encrypted_events.push(event.event.clone());
encrypted_events.push(event.raw().clone());
}
}
_ => {
Expand Down Expand Up @@ -1683,7 +1683,7 @@ mod tests {

// But it's now redacted
assert_matches!(
latest_event.event().event.deserialize().unwrap(),
latest_event.event().raw().deserialize().unwrap(),
AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(
SyncRoomMessageEvent::Redacted(_)
))
Expand Down
Loading
Loading