@@ -4,15 +4,17 @@ use matrix_sdk_base::deserialized_responses::{MembersResponse, RoomEvent};
4
4
use matrix_sdk_common:: locks:: Mutex ;
5
5
use ruma:: {
6
6
api:: client:: r0:: {
7
+ filter:: RoomEventFilter ,
7
8
membership:: { get_member_events, join_room_by_id, leave_room} ,
8
- message:: get_message_events,
9
+ message:: get_message_events:: { self , Direction } ,
9
10
room:: get_room_event,
10
11
} ,
12
+ assign,
11
13
events:: {
12
14
room:: history_visibility:: HistoryVisibility , AnyStateEvent , AnySyncStateEvent , EventType ,
13
15
} ,
14
16
serde:: Raw ,
15
- EventId , UserId ,
17
+ uint , EventId , RoomId , UInt , UserId ,
16
18
} ;
17
19
18
20
use crate :: {
@@ -136,40 +138,30 @@ impl Common {
136
138
/// decryption fails for an individual message, that message is returned
137
139
/// undecrypted.
138
140
///
139
- /// # Arguments
140
- ///
141
- /// * `request` - The easiest way to create this request is using the
142
- /// `get_message_events::Request` itself.
143
- ///
144
141
/// # Examples
145
142
/// ```no_run
146
143
/// # 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,
152
148
/// # };
153
149
/// # use url::Url;
154
150
///
155
151
/// # 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");
158
153
///
159
154
/// 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();
163
158
/// # use futures::executor::block_on;
164
159
/// # block_on(async {
165
160
/// assert!(room.messages(request).await.is_ok());
166
161
/// # });
167
162
/// ```
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 ( ) ) ;
173
165
let http_response = self . client . send ( request, None ) . await ?;
174
166
175
167
let mut response = Messages {
@@ -445,3 +437,64 @@ impl Common {
445
437
Ok ( true )
446
438
}
447
439
}
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
+ }
0 commit comments