Skip to content

Commit c2888e5

Browse files
committed
Use a custom options type for room::Common::messages
With the previous API, one would have to supply the room's ID manually, despite the method already being called on a room.
1 parent c79e62d commit c2888e5

File tree

2 files changed

+76
-23
lines changed

2 files changed

+76
-23
lines changed

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

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ use matrix_sdk_base::deserialized_responses::{MembersResponse, RoomEvent};
44
use matrix_sdk_common::locks::Mutex;
55
use ruma::{
66
api::client::r0::{
7+
filter::RoomEventFilter,
78
membership::{get_member_events, join_room_by_id, leave_room},
8-
message::get_message_events,
9+
message::get_message_events::{self, Direction},
910
room::get_room_event,
1011
},
12+
assign,
1113
events::{
1214
room::history_visibility::HistoryVisibility, AnyStateEvent, AnySyncStateEvent, EventType,
1315
},
1416
serde::Raw,
15-
EventId, UserId,
17+
uint, EventId, RoomId, UInt, UserId,
1618
};
1719

1820
use crate::{
@@ -136,40 +138,30 @@ impl Common {
136138
/// decryption fails for an individual message, that message is returned
137139
/// undecrypted.
138140
///
139-
/// # Arguments
140-
///
141-
/// * `request` - The easiest way to create this request is using the
142-
/// `get_message_events::Request` itself.
143-
///
144141
/// # Examples
145142
/// ```no_run
146143
/// # use std::convert::TryFrom;
147-
/// use matrix_sdk::Client;
148-
/// # use matrix_sdk::ruma::room_id;
149-
/// # use matrix_sdk::ruma::api::client::r0::{
150-
/// # filter::RoomEventFilter,
151-
/// # message::get_message_events::Request as MessagesRequest,
144+
/// use matrix_sdk::{room::MessagesOptions, Client};
145+
/// # use matrix_sdk::ruma::{
146+
/// # api::client::r0::filter::RoomEventFilter,
147+
/// # room_id,
152148
/// # };
153149
/// # use url::Url;
154150
///
155151
/// # let homeserver = Url::parse("http://example.com").unwrap();
156-
/// let room_id = room_id!("!roomid:example.com");
157-
/// let request = MessagesRequest::backward(&room_id, "t47429-4392820_219380_26003_2265");
152+
/// let request = MessagesOptions::backward("t47429-4392820_219380_26003_2265");
158153
///
159154
/// let mut client = Client::new(homeserver).unwrap();
160-
/// # let room = client
161-
/// # .get_joined_room(&room_id)
162-
/// # .unwrap();
155+
/// let room = client
156+
/// .get_joined_room(room_id!("!roomid:example.com"))
157+
/// .unwrap();
163158
/// # use futures::executor::block_on;
164159
/// # block_on(async {
165160
/// assert!(room.messages(request).await.is_ok());
166161
/// # });
167162
/// ```
168-
pub async fn messages(
169-
&self,
170-
request: impl Into<get_message_events::Request<'_>>,
171-
) -> Result<Messages> {
172-
let request = request.into();
163+
pub async fn messages(&self, options: MessagesOptions<'_>) -> Result<Messages> {
164+
let request = options.into_request(self.inner.room_id());
173165
let http_response = self.client.send(request, None).await?;
174166

175167
let mut response = Messages {
@@ -445,3 +437,64 @@ impl Common {
445437
Ok(true)
446438
}
447439
}
440+
441+
/// Options for [`messages`][Common::messages].
442+
///
443+
/// See that method for details.
444+
#[derive(Debug)]
445+
#[non_exhaustive]
446+
pub struct MessagesOptions<'a> {
447+
/// The token to start returning events from.
448+
///
449+
/// This token can be obtained from a `prev_batch` token returned for each
450+
/// room from the sync API, or from a start or end token returned by a
451+
/// previous `messages` call.
452+
pub from: &'a str,
453+
454+
/// The token to stop returning events at.
455+
///
456+
/// This token can be obtained from a `prev_batch` token returned for each
457+
/// room by the sync API, or from a start or end token returned by a
458+
/// previous `messages` call.
459+
pub to: Option<&'a str>,
460+
461+
/// The direction to return events in.
462+
pub dir: Direction,
463+
464+
/// The maximum number of events to return.
465+
///
466+
/// Default: 10.
467+
pub limit: UInt,
468+
469+
/// A [`RoomEventFilter`] to filter returned events with.
470+
pub filter: Option<RoomEventFilter<'a>>,
471+
}
472+
473+
impl<'a> MessagesOptions<'a> {
474+
/// Creates `MessagesOptions` with the given start token and direction.
475+
///
476+
/// All other parameters will be defaulted.
477+
pub fn new(from: &'a str, dir: Direction) -> Self {
478+
Self { from, to: None, dir, limit: uint!(10), filter: None }
479+
}
480+
481+
/// Creates `MessagesOptions` with the given start token, and `dir` set to
482+
/// `Backward`.
483+
pub fn backward(from: &'a str) -> Self {
484+
Self::new(from, Direction::Backward)
485+
}
486+
487+
/// Creates `MessagesOptions` with the given start token, and `dir` set to
488+
/// `Forward`.
489+
pub fn forward(from: &'a str) -> Self {
490+
Self::new(from, Direction::Forward)
491+
}
492+
493+
fn into_request(self, room_id: &'a RoomId) -> get_message_events::Request {
494+
assign!(get_message_events::Request::new(room_id, self.from, self.dir), {
495+
to: self.to,
496+
limit: self.limit,
497+
filter: self.filter,
498+
})
499+
}
500+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod joined;
88
mod left;
99

1010
pub use self::{
11-
common::{Common, Messages},
11+
common::{Common, Messages, MessagesOptions},
1212
invited::Invited,
1313
joined::Joined,
1414
left::Left,

0 commit comments

Comments
 (0)