@@ -16,7 +16,9 @@ use std::collections::{BTreeMap, BTreeSet};
16
16
17
17
use ruma:: {
18
18
OwnedRoomId , OwnedUserId , RoomId ,
19
- api:: client:: sync:: sync_events:: v3:: { InvitedRoom , JoinedRoom , KnockedRoom , LeftRoom , State } ,
19
+ api:: client:: sync:: sync_events:: v3:: {
20
+ InvitedRoom , JoinedRoom , KnockedRoom , LeftRoom , State as RumaState ,
21
+ } ,
20
22
} ;
21
23
use tokio:: sync:: broadcast:: Sender ;
22
24
use tracing:: error;
@@ -29,7 +31,7 @@ use super::{
29
31
} ;
30
32
use crate :: {
31
33
Result , RoomInfoNotableUpdate , RoomState ,
32
- sync:: { InvitedRoomUpdate , JoinedRoomUpdate , KnockedRoomUpdate , LeftRoomUpdate } ,
34
+ sync:: { InvitedRoomUpdate , JoinedRoomUpdate , KnockedRoomUpdate , LeftRoomUpdate , State } ,
33
35
} ;
34
36
35
37
/// Process updates of a joined room.
@@ -64,39 +66,18 @@ pub async fn update_joined_room(
64
66
65
67
let mut new_user_ids = BTreeSet :: new ( ) ;
66
68
67
- let state = match joined_room. state {
68
- State :: Before ( state) => {
69
- let ( raw_state_events, state_events) = state_events:: sync:: collect ( & state. events ) ;
70
- state_events:: sync:: dispatch (
71
- context,
72
- ( & raw_state_events, & state_events) ,
73
- & mut room_info,
74
- ambiguity_cache,
75
- & mut new_user_ids,
76
- state_store,
77
- )
78
- . await ?;
79
-
80
- let ( raw_state_events_from_timeline, state_events_from_timeline) =
81
- state_events:: sync:: collect_from_timeline ( & joined_room. timeline . events ) ;
82
- state_events:: sync:: dispatch (
83
- context,
84
- ( & raw_state_events_from_timeline, & state_events_from_timeline) ,
85
- & mut room_info,
86
- ambiguity_cache,
87
- & mut new_user_ids,
88
- state_store,
89
- )
90
- . await ?;
91
-
92
- state
93
- }
94
- // We shouldn't receive other variants because they are opt-in.
95
- state => {
96
- error ! ( "Unsupported State variant received for joined room: {state:?}" ) ;
97
- Default :: default ( )
98
- }
99
- } ;
69
+ let state = State :: from_sync_v2 ( joined_room. state ) ;
70
+ let ( raw_state_events, state_events) = state. collect ( & joined_room. timeline . events ) ;
71
+
72
+ state_events:: sync:: dispatch (
73
+ context,
74
+ ( & raw_state_events, & state_events) ,
75
+ & mut room_info,
76
+ ambiguity_cache,
77
+ & mut new_user_ids,
78
+ state_store,
79
+ )
80
+ . await ?;
100
81
101
82
ephemeral_events:: dispatch ( context, & joined_room. ephemeral . events , room_id) ;
102
83
@@ -155,7 +136,7 @@ pub async fn update_joined_room(
155
136
156
137
Ok ( JoinedRoomUpdate :: new (
157
138
timeline,
158
- state. events ,
139
+ state,
159
140
joined_room. account_data . events ,
160
141
joined_room. ephemeral . events ,
161
142
notification_count,
@@ -189,39 +170,18 @@ pub async fn update_left_room(
189
170
room_info. mark_state_partially_synced ( ) ;
190
171
room_info. handle_encryption_state ( requested_required_states. for_room ( room_id) ) ;
191
172
192
- let state = match left_room. state {
193
- State :: Before ( state) => {
194
- let ( raw_state_events, state_events) = state_events:: sync:: collect ( & state. events ) ;
195
- state_events:: sync:: dispatch (
196
- context,
197
- ( & raw_state_events, & state_events) ,
198
- & mut room_info,
199
- ambiguity_cache,
200
- & mut ( ) ,
201
- state_store,
202
- )
203
- . await ?;
204
-
205
- let ( raw_state_events_from_timeline, state_events_from_timeline) =
206
- state_events:: sync:: collect_from_timeline ( & left_room. timeline . events ) ;
207
- state_events:: sync:: dispatch (
208
- context,
209
- ( & raw_state_events_from_timeline, & state_events_from_timeline) ,
210
- & mut room_info,
211
- ambiguity_cache,
212
- & mut ( ) ,
213
- state_store,
214
- )
215
- . await ?;
216
-
217
- state
218
- }
219
- // We shouldn't receive other variants because they are opt-in.
220
- state => {
221
- error ! ( "Unsupported State variant received for left room: {state:?}" ) ;
222
- Default :: default ( )
223
- }
224
- } ;
173
+ let state = State :: from_sync_v2 ( left_room. state ) ;
174
+ let ( raw_state_events, state_events) = state. collect ( & left_room. timeline . events ) ;
175
+
176
+ state_events:: sync:: dispatch (
177
+ context,
178
+ ( & raw_state_events, & state_events) ,
179
+ & mut room_info,
180
+ ambiguity_cache,
181
+ & mut ( ) ,
182
+ state_store,
183
+ )
184
+ . await ?;
225
185
226
186
let timeline = timeline:: build (
227
187
context,
@@ -241,12 +201,7 @@ pub async fn update_left_room(
241
201
242
202
let ambiguity_changes = ambiguity_cache. changes . remove ( room_id) . unwrap_or_default ( ) ;
243
203
244
- Ok ( LeftRoomUpdate :: new (
245
- timeline,
246
- state. events ,
247
- left_room. account_data . events ,
248
- ambiguity_changes,
249
- ) )
204
+ Ok ( LeftRoomUpdate :: new ( timeline, state, left_room. account_data . events , ambiguity_changes) )
250
205
}
251
206
252
207
/// Process updates of an invited room.
@@ -320,3 +275,19 @@ pub async fn update_knocked_room(
320
275
321
276
Ok ( knocked_room)
322
277
}
278
+
279
+ impl State {
280
+ /// Construct a [`State`] from the state changes for a joined or left room
281
+ /// from a response of the sync v2 endpoint.
282
+ fn from_sync_v2 ( state : RumaState ) -> Self {
283
+ match state {
284
+ RumaState :: Before ( state) => Self :: Before ( state. events ) ,
285
+ RumaState :: After ( state) => Self :: After ( state. events ) ,
286
+ // We shouldn't receive other variants because they are opt-in.
287
+ state => {
288
+ error ! ( "Unsupported State variant received for joined room: {state:?}" ) ;
289
+ Self :: default ( )
290
+ }
291
+ }
292
+ }
293
+ }
0 commit comments