@@ -4,18 +4,20 @@ 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
tag:: { create_tag, delete_tag} ,
11
12
} ,
13
+ assign,
12
14
events:: {
13
15
room:: history_visibility:: HistoryVisibility ,
14
16
tag:: { TagInfo , TagName } ,
15
17
AnyStateEvent , AnySyncStateEvent , EventType ,
16
18
} ,
17
19
serde:: Raw ,
18
- EventId , UserId ,
20
+ uint , EventId , RoomId , UInt , UserId ,
19
21
} ;
20
22
21
23
use crate :: {
@@ -139,40 +141,30 @@ impl Common {
139
141
/// decryption fails for an individual message, that message is returned
140
142
/// undecrypted.
141
143
///
142
- /// # Arguments
143
- ///
144
- /// * `request` - The easiest way to create this request is using the
145
- /// `get_message_events::Request` itself.
146
- ///
147
144
/// # Examples
148
145
/// ```no_run
149
146
/// # 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,
155
151
/// # };
156
152
/// # use url::Url;
157
153
///
158
154
/// # 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");
161
156
///
162
157
/// 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();
166
161
/// # use futures::executor::block_on;
167
162
/// # block_on(async {
168
163
/// assert!(room.messages(request).await.is_ok());
169
164
/// # });
170
165
/// ```
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 ( ) ) ;
176
168
let http_response = self . client . send ( request, None ) . await ?;
177
169
178
170
let mut response = Messages {
@@ -501,3 +493,64 @@ impl Common {
501
493
self . client . send ( request, None ) . await
502
494
}
503
495
}
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
+ }
0 commit comments