Skip to content

Commit

Permalink
Simplify TimelineEvent construction
Browse files Browse the repository at this point in the history
jplatte committed Mar 13, 2023
1 parent 7121179 commit ee6c0d9
Showing 5 changed files with 25 additions and 38 deletions.
19 changes: 12 additions & 7 deletions crates/matrix-sdk-common/src/deserialized_responses.rs
Original file line number Diff line number Diff line change
@@ -86,8 +86,8 @@ impl From<TimelineEvent> for SyncTimelineEvent {
// this way, we simply cause the `room_id` field in the json to be
// ignored by a subsequent deserialization.
Self {
encryption_info: o.encryption_info,
event: o.event.cast(),
encryption_info: o.encryption_info,
push_actions: o.push_actions,
}
}
@@ -104,6 +104,16 @@ pub struct TimelineEvent {
pub push_actions: Vec<Action>,
}

impl TimelineEvent {
/// Create a new `TimelineEvent` from the given raw event.
///
/// This is a convenience constructor for when you don't need to set
/// `encryption_info` or `push_action`, for example inside a test.
pub fn new(event: Raw<AnyTimelineEvent>) -> Self {
Self { event, encryption_info: None, push_actions: vec![] }
}
}

#[cfg(test)]
mod tests {
use ruma::{
@@ -125,12 +135,7 @@ mod tests {
"sender": "@carl:example.com",
});

let room_event = TimelineEvent {
event: Raw::new(&event).unwrap().cast(),
encryption_info: None,
push_actions: Vec::default(),
};

let room_event = TimelineEvent::new(Raw::new(&event).unwrap().cast());
let converted_room_event: SyncTimelineEvent = room_event.into();

let converted_event: AnySyncTimelineEvent =
4 changes: 2 additions & 2 deletions crates/matrix-sdk-crypto/src/machine.rs
Original file line number Diff line number Diff line change
@@ -1179,9 +1179,9 @@ impl OlmMachine {
let encryption_info = self.get_encryption_info(&session, &event.sender).await?;

Ok(TimelineEvent {
encryption_info: Some(encryption_info),
event: decrypted_event,
push_actions: Vec::default(),
encryption_info: Some(encryption_info),
push_actions: vec![],
})
} else {
Err(MegolmError::MissingRoomKey)
24 changes: 6 additions & 18 deletions crates/matrix-sdk/src/room/common.rs
Original file line number Diff line number Diff line change
@@ -208,15 +208,7 @@ impl Common {
start: http_response.start,
end: http_response.end,
#[cfg(not(feature = "e2e-encryption"))]
chunk: http_response
.chunk
.into_iter()
.map(|event| TimelineEvent {
event,
encryption_info: None,
push_actions: Vec::default(),
})
.collect(),
chunk: http_response.chunk.into_iter().map(TimelineEvent::new).collect(),
#[cfg(feature = "e2e-encryption")]
chunk: Vec::with_capacity(http_response.chunk.len()),
state: http_response.state,
@@ -232,20 +224,16 @@ impl Common {
if let Ok(event) = machine.decrypt_room_event(event.cast_ref(), room_id).await {
event
} else {
TimelineEvent { event, encryption_info: None, push_actions: Vec::default() }
TimelineEvent::new(event)
}
} else {
TimelineEvent { event, encryption_info: None, push_actions: Vec::default() }
TimelineEvent::new(event)
};

response.chunk.push(decrypted_event);
}
} else {
response.chunk.extend(http_response.chunk.into_iter().map(|event| TimelineEvent {
event,
encryption_info: None,
push_actions: Vec::default(),
}));
response.chunk.extend(http_response.chunk.into_iter().map(TimelineEvent::new));
}

Ok(response)
@@ -294,11 +282,11 @@ impl Common {
return Ok(event);
}
}
Ok(TimelineEvent { event, encryption_info: None, push_actions: Vec::default() })
Ok(TimelineEvent::new(event))
}

#[cfg(not(feature = "e2e-encryption"))]
Ok(TimelineEvent { event, encryption_info: None, push_actions: Vec::default() })
Ok(TimelineEvent::new(event))
}

pub(crate) async fn request_members(&self) -> Result<Option<MembersResponse>> {
6 changes: 1 addition & 5 deletions crates/matrix-sdk/src/room/timeline/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -161,11 +161,7 @@ impl TestTimeline {
}

async fn handle_back_paginated_custom_event(&self, event: JsonValue) {
let timeline_event = TimelineEvent {
event: Raw::new(&event).unwrap().cast(),
encryption_info: None,
push_actions: Vec::default(),
};
let timeline_event = TimelineEvent::new(Raw::new(&event).unwrap().cast());
self.inner.handle_back_paginated_event(timeline_event).await;
}

10 changes: 4 additions & 6 deletions crates/matrix-sdk/src/sliding_sync/room.rs
Original file line number Diff line number Diff line change
@@ -368,8 +368,8 @@ mod tests {
room_id: <&RoomId>::try_from("!29fhd83h92h0:example.com").unwrap().to_owned(),
inner: v4::SlidingSyncRoom::default(),
prev_batch: Some("let it go!".to_owned()),
timeline_queue: vector![TimelineEvent {
event: Raw::new(&json! ({
timeline_queue: vector![TimelineEvent::new(
Raw::new(&json! ({
"content": RoomMessageEventContent::text_plain("let it gooo!"),
"type": "m.room.message",
"event_id": "$xxxxx:example.org",
@@ -378,10 +378,8 @@ mod tests {
"sender": "@bob:example.com",
}))
.unwrap()
.cast(),
encryption_info: None,
push_actions: Vec::default(),
}
.cast()
)
.into()],
};

0 comments on commit ee6c0d9

Please sign in to comment.