Skip to content

Commit 703e965

Browse files
committed
Decrypt messages from room::Common::messages()
Due to additional error possibilities and encryption info for messages, the return type of the method was changed accordingly and a wrapper struct `Messages` was introduced.
1 parent 046bfa9 commit 703e965

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

crates/matrix-sdk/src/room/common.rs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ use ruma::{
88
message::get_message_events,
99
room::get_room_event,
1010
},
11-
events::{room::history_visibility::HistoryVisibility, AnySyncStateEvent, EventType},
11+
events::{
12+
room::history_visibility::HistoryVisibility, AnyStateEvent, AnySyncStateEvent, EventType,
13+
},
1214
serde::Raw,
1315
UserId,
1416
};
1517

1618
use crate::{
17-
error::HttpResult,
1819
media::{MediaFormat, MediaRequest, MediaType},
1920
room::RoomType,
2021
BaseRoom, Client, Result, RoomMember,
@@ -36,6 +37,25 @@ impl Deref for Common {
3637
}
3738
}
3839

40+
/// The result of a `Room::messages` call.
41+
///
42+
/// In short, this is a possibly decrypted version of the response of a
43+
/// `room/messages` api call.
44+
#[derive(Debug)]
45+
pub struct Messages {
46+
/// The token the pagination starts from.
47+
pub start: Option<String>,
48+
49+
/// The token the pagination ends at.
50+
pub end: Option<String>,
51+
52+
/// A list of room events.
53+
pub chunk: Vec<RoomEvent>,
54+
55+
/// A list of state events relevant to showing the `chunk`.
56+
pub state: Vec<Raw<AnyStateEvent>>,
57+
}
58+
3959
impl Common {
4060
/// Create a new `room::Common`
4161
///
@@ -109,8 +129,8 @@ impl Common {
109129
}
110130

111131
/// Sends a request to `/_matrix/client/r0/rooms/{room_id}/messages` and
112-
/// returns a `get_message_events::Response` that contains a chunk of
113-
/// room and state events (`AnyRoomEvent` and `AnyStateEvent`).
132+
/// returns a `Messages` struct that contains a chunk of room and state
133+
/// events (`RoomEvent` and `AnyStateEvent`).
114134
///
115135
/// # Arguments
116136
///
@@ -144,9 +164,30 @@ impl Common {
144164
pub async fn messages(
145165
&self,
146166
request: impl Into<get_message_events::Request<'_>>,
147-
) -> HttpResult<get_message_events::Response> {
167+
) -> Result<Messages> {
148168
let request = request.into();
149-
self.client.send(request, None).await
169+
let http_response = self.client.send(request, None).await?;
170+
171+
let mut response = Messages {
172+
start: http_response.start,
173+
end: http_response.end,
174+
chunk: Vec::with_capacity(http_response.chunk.len()),
175+
state: http_response.state,
176+
};
177+
178+
for event in &http_response.chunk {
179+
let event = event.deserialize()?;
180+
181+
#[cfg(feature = "encryption")]
182+
let event = self.client.decrypt_room_event(&event).await?;
183+
184+
#[cfg(not(feature = "encryption"))]
185+
let event = RoomEvent { event: Raw::new(&event)?, encryption_info: None };
186+
187+
response.chunk.push(event);
188+
}
189+
190+
Ok(response)
150191
}
151192

152193
/// Sends a request to `/_matrix/client/r0/rooms/{roomId}/event/{eventId}`

crates/matrix-sdk/src/room/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ mod invited;
77
mod joined;
88
mod left;
99

10-
pub use self::{common::Common, invited::Invited, joined::Joined, left::Left};
10+
pub use self::{
11+
common::{Common, Messages},
12+
invited::Invited,
13+
joined::Joined,
14+
left::Left,
15+
};
1116

1217
/// An enum that abstracts over the different states a room can be in.
1318
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)