Skip to content

Decrypt messages from room::Common::messages() #448

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

Merged
merged 2 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/matrix-sdk/src/encryption/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ impl Client {
Ok(olm.import_keys(import, false, |_, _| {}).await?)
}

/// Tries to decrypt a `AnyRoomEvent`. Returns unencrypted room event when
/// Tries to decrypt a `AnyRoomEvent`. Returns undecrypted room event when
/// decryption fails.
#[cfg(feature = "encryption")]
pub(crate) async fn decrypt_room_event(
Expand Down
53 changes: 47 additions & 6 deletions crates/matrix-sdk/src/room/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use ruma::{
message::get_message_events,
room::get_room_event,
},
events::{room::history_visibility::HistoryVisibility, AnySyncStateEvent, EventType},
events::{
room::history_visibility::HistoryVisibility, AnyStateEvent, AnySyncStateEvent, EventType,
},
serde::Raw,
UserId,
};

use crate::{
error::HttpResult,
media::{MediaFormat, MediaRequest, MediaType},
room::RoomType,
BaseRoom, Client, Result, RoomMember,
Expand All @@ -36,6 +37,25 @@ impl Deref for Common {
}
}

/// The result of a `Room::messages` call.
///
/// In short, this is a possibly decrypted version of the response of a
/// `room/messages` api call.
#[derive(Debug)]
pub struct Messages {
/// The token the pagination starts from.
pub start: Option<String>,

/// The token the pagination ends at.
pub end: Option<String>,

/// A list of room events.
pub chunk: Vec<RoomEvent>,

/// A list of state events relevant to showing the `chunk`.
pub state: Vec<Raw<AnyStateEvent>>,
}

impl Common {
/// Create a new `room::Common`
///
Expand Down Expand Up @@ -109,8 +129,8 @@ impl Common {
}

/// Sends a request to `/_matrix/client/r0/rooms/{room_id}/messages` and
/// returns a `get_message_events::Response` that contains a chunk of
/// room and state events (`AnyRoomEvent` and `AnyStateEvent`).
/// returns a `Messages` struct that contains a chunk of room and state
/// events (`RoomEvent` and `AnyStateEvent`).
///
/// # Arguments
///
Expand Down Expand Up @@ -144,9 +164,30 @@ impl Common {
pub async fn messages(
&self,
request: impl Into<get_message_events::Request<'_>>,
) -> HttpResult<get_message_events::Response> {
) -> Result<Messages> {
let request = request.into();
self.client.send(request, None).await
let http_response = self.client.send(request, None).await?;

let mut response = Messages {
start: http_response.start,
end: http_response.end,
chunk: Vec::with_capacity(http_response.chunk.len()),
state: http_response.state,
};

for event in &http_response.chunk {
let event = event.deserialize()?;

#[cfg(feature = "encryption")]
let event = self.client.decrypt_room_event(&event).await?;

#[cfg(not(feature = "encryption"))]
let event = RoomEvent { event: Raw::new(&event)?, encryption_info: None };

response.chunk.push(event);
}

Ok(response)
}

/// Sends a request to `/_matrix/client/r0/rooms/{roomId}/event/{eventId}`
Expand Down
7 changes: 6 additions & 1 deletion crates/matrix-sdk/src/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ mod invited;
mod joined;
mod left;

pub use self::{common::Common, invited::Invited, joined::Joined, left::Left};
pub use self::{
common::{Common, Messages},
invited::Invited,
joined::Joined,
left::Left,
};

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