@@ -8,13 +8,14 @@ use ruma::{
8
8
message:: get_message_events,
9
9
room:: get_room_event,
10
10
} ,
11
- events:: { room:: history_visibility:: HistoryVisibility , AnySyncStateEvent , EventType } ,
11
+ events:: {
12
+ room:: history_visibility:: HistoryVisibility , AnyStateEvent , AnySyncStateEvent , EventType ,
13
+ } ,
12
14
serde:: Raw ,
13
15
UserId ,
14
16
} ;
15
17
16
18
use crate :: {
17
- error:: HttpResult ,
18
19
media:: { MediaFormat , MediaRequest , MediaType } ,
19
20
room:: RoomType ,
20
21
BaseRoom , Client , Result , RoomMember ,
@@ -36,6 +37,25 @@ impl Deref for Common {
36
37
}
37
38
}
38
39
40
+ /// The result of a `Room::messages` call.
41
+ ///
42
+ /// In short, this is a possibly decrypted version of the response of a
43
+ /// `room/messages` api call.
44
+ #[ derive( Debug ) ]
45
+ pub struct Messages {
46
+ /// The token the pagination starts from.
47
+ pub start : Option < String > ,
48
+
49
+ /// The token the pagination ends at.
50
+ pub end : Option < String > ,
51
+
52
+ /// A list of room events.
53
+ pub chunk : Vec < RoomEvent > ,
54
+
55
+ /// A list of state events relevant to showing the `chunk`.
56
+ pub state : Vec < Raw < AnyStateEvent > > ,
57
+ }
58
+
39
59
impl Common {
40
60
/// Create a new `room::Common`
41
61
///
@@ -109,8 +129,8 @@ impl Common {
109
129
}
110
130
111
131
/// Sends a request to `/_matrix/client/r0/rooms/{room_id}/messages` and
112
- /// returns a `get_message_events::Response` that contains a chunk of
113
- /// room and state events (`AnyRoomEvent ` and `AnyStateEvent`).
132
+ /// returns a `Messages` struct that contains a chunk of room and state
133
+ /// events (`RoomEvent ` and `AnyStateEvent`).
114
134
///
115
135
/// # Arguments
116
136
///
@@ -144,9 +164,30 @@ impl Common {
144
164
pub async fn messages (
145
165
& self ,
146
166
request : impl Into < get_message_events:: Request < ' _ > > ,
147
- ) -> HttpResult < get_message_events :: Response > {
167
+ ) -> Result < Messages > {
148
168
let request = request. into ( ) ;
149
- self . client . send ( request, None ) . await
169
+ let http_response = self . client . send ( request, None ) . await ?;
170
+
171
+ let mut response = Messages {
172
+ start : http_response. start ,
173
+ end : http_response. end ,
174
+ chunk : Vec :: with_capacity ( http_response. chunk . len ( ) ) ,
175
+ state : http_response. state ,
176
+ } ;
177
+
178
+ for event in & http_response. chunk {
179
+ let event = event. deserialize ( ) ?;
180
+
181
+ #[ cfg( feature = "encryption" ) ]
182
+ let event = self . client . decrypt_room_event ( & event) . await ?;
183
+
184
+ #[ cfg( not( feature = "encryption" ) ) ]
185
+ let event = RoomEvent { event : Raw :: new ( & event) ?, encryption_info : None } ;
186
+
187
+ response. chunk . push ( event) ;
188
+ }
189
+
190
+ Ok ( response)
150
191
}
151
192
152
193
/// Sends a request to `/_matrix/client/r0/rooms/{roomId}/event/{eventId}`
0 commit comments