Skip to content

Commit 906dcfe

Browse files
committed
Merge branch 'messages-param'
2 parents b5ce7e7 + c2888e5 commit 906dcfe

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,18 +4,20 @@ 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
tag::{create_tag, delete_tag},
1112
},
13+
assign,
1214
events::{
1315
room::history_visibility::HistoryVisibility,
1416
tag::{TagInfo, TagName},
1517
AnyStateEvent, AnySyncStateEvent, EventType,
1618
},
1719
serde::Raw,
18-
EventId, UserId,
20+
uint, EventId, RoomId, UInt, UserId,
1921
};
2022

2123
use crate::{
@@ -139,40 +141,30 @@ impl Common {
139141
/// decryption fails for an individual message, that message is returned
140142
/// undecrypted.
141143
///
142-
/// # Arguments
143-
///
144-
/// * `request` - The easiest way to create this request is using the
145-
/// `get_message_events::Request` itself.
146-
///
147144
/// # Examples
148145
/// ```no_run
149146
/// # use std::convert::TryFrom;
150-
/// use matrix_sdk::Client;
151-
/// # use matrix_sdk::ruma::room_id;
152-
/// # use matrix_sdk::ruma::api::client::r0::{
153-
/// # filter::RoomEventFilter,
154-
/// # message::get_message_events::Request as MessagesRequest,
147+
/// use matrix_sdk::{room::MessagesOptions, Client};
148+
/// # use matrix_sdk::ruma::{
149+
/// # api::client::r0::filter::RoomEventFilter,
150+
/// # room_id,
155151
/// # };
156152
/// # use url::Url;
157153
///
158154
/// # let homeserver = Url::parse("http://example.com").unwrap();
159-
/// let room_id = room_id!("!roomid:example.com");
160-
/// let request = MessagesRequest::backward(&room_id, "t47429-4392820_219380_26003_2265");
155+
/// let request = MessagesOptions::backward("t47429-4392820_219380_26003_2265");
161156
///
162157
/// let mut client = Client::new(homeserver).unwrap();
163-
/// # let room = client
164-
/// # .get_joined_room(&room_id)
165-
/// # .unwrap();
158+
/// let room = client
159+
/// .get_joined_room(room_id!("!roomid:example.com"))
160+
/// .unwrap();
166161
/// # use futures::executor::block_on;
167162
/// # block_on(async {
168163
/// assert!(room.messages(request).await.is_ok());
169164
/// # });
170165
/// ```
171-
pub async fn messages(
172-
&self,
173-
request: impl Into<get_message_events::Request<'_>>,
174-
) -> Result<Messages> {
175-
let request = request.into();
166+
pub async fn messages(&self, options: MessagesOptions<'_>) -> Result<Messages> {
167+
let request = options.into_request(self.inner.room_id());
176168
let http_response = self.client.send(request, None).await?;
177169

178170
let mut response = Messages {
@@ -501,3 +493,64 @@ impl Common {
501493
self.client.send(request, None).await
502494
}
503495
}
496+
497+
/// Options for [`messages`][Common::messages].
498+
///
499+
/// See that method for details.
500+
#[derive(Debug)]
501+
#[non_exhaustive]
502+
pub struct MessagesOptions<'a> {
503+
/// The token to start returning events from.
504+
///
505+
/// This token can be obtained from a `prev_batch` token returned for each
506+
/// room from the sync API, or from a start or end token returned by a
507+
/// previous `messages` call.
508+
pub from: &'a str,
509+
510+
/// The token to stop returning events at.
511+
///
512+
/// This token can be obtained from a `prev_batch` token returned for each
513+
/// room by the sync API, or from a start or end token returned by a
514+
/// previous `messages` call.
515+
pub to: Option<&'a str>,
516+
517+
/// The direction to return events in.
518+
pub dir: Direction,
519+
520+
/// The maximum number of events to return.
521+
///
522+
/// Default: 10.
523+
pub limit: UInt,
524+
525+
/// A [`RoomEventFilter`] to filter returned events with.
526+
pub filter: Option<RoomEventFilter<'a>>,
527+
}
528+
529+
impl<'a> MessagesOptions<'a> {
530+
/// Creates `MessagesOptions` with the given start token and direction.
531+
///
532+
/// All other parameters will be defaulted.
533+
pub fn new(from: &'a str, dir: Direction) -> Self {
534+
Self { from, to: None, dir, limit: uint!(10), filter: None }
535+
}
536+
537+
/// Creates `MessagesOptions` with the given start token, and `dir` set to
538+
/// `Backward`.
539+
pub fn backward(from: &'a str) -> Self {
540+
Self::new(from, Direction::Backward)
541+
}
542+
543+
/// Creates `MessagesOptions` with the given start token, and `dir` set to
544+
/// `Forward`.
545+
pub fn forward(from: &'a str) -> Self {
546+
Self::new(from, Direction::Forward)
547+
}
548+
549+
fn into_request(self, room_id: &'a RoomId) -> get_message_events::Request {
550+
assign!(get_message_events::Request::new(room_id, self.from, self.dir), {
551+
to: self.to,
552+
limit: self.limit,
553+
filter: self.filter,
554+
})
555+
}
556+
}

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)