1use std::{collections::BTreeMap, time::Duration};
8
9use js_int::UInt;
10use js_option::JsOption;
11use ruma_common::{
12 OwnedMxcUri, OwnedRoomId, OwnedUserId,
13 api::{auth_scheme::AccessToken, request, response},
14 metadata,
15 presence::PresenceState,
16 serde::{Raw, duration::opt_ms},
17};
18use ruma_events::{AnyStrippedStateEvent, AnySyncStateEvent, AnySyncTimelineEvent, StateEventType};
19use serde::{Deserialize, Serialize};
20
21metadata! {
22 method: POST,
23 rate_limited: false,
24 authentication: AccessToken,
25 history: {
26 stable("org.matrix.simplified_msc3575.stable") => "/_matrix/client/v4/sync",
27 }
28}
29
30#[request]
32#[derive(Default)]
33pub struct Request {
34 #[serde(skip_serializing_if = "Option::is_none")]
38 pub conn_id: Option<String>,
39
40 #[serde(skip_serializing_if = "Option::is_none")]
44 pub pos: Option<String>,
45
46 #[serde(with = "opt_ms", default, skip_serializing_if = "Option::is_none")]
51 pub timeout: Option<Duration>,
52
53 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
57 pub set_presence: PresenceState,
58
59 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
63 pub lists: BTreeMap<String, request::List>,
64
65 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
72 pub room_subscriptions: BTreeMap<OwnedRoomId, request::RoomSubscription>,
73
74 #[serde(default, skip_serializing_if = "request::Extensions::is_empty")]
76 pub extensions: request::Extensions,
77}
78
79impl Request {
80 pub fn new() -> Self {
82 Default::default()
83 }
84}
85
86pub mod request {
88 use ruma_common::{OwnedRoomId, directory::RoomTypeFilter};
89 use ruma_events::tag::TagName;
90
91 use super::{BTreeMap, Deserialize, Serialize, StateEventType, UInt};
92
93 #[derive(Clone, Debug, Serialize, Deserialize)]
95 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
96 pub struct List {
97 #[serde(skip_serializing_if = "Option::is_none")]
101 pub range: Option<(UInt, UInt)>,
102
103 pub required_state: RequiredState,
105
106 pub timeline_limit: UInt,
108
109 #[serde(default, skip_serializing_if = "ListFilters::is_empty")]
111 pub filters: ListFilters,
112 }
113
114 impl List {
115 pub fn new(required_state: RequiredState, timeline_limit: UInt) -> Self {
117 Self { range: None, required_state, timeline_limit, filters: ListFilters::new() }
118 }
119 }
120
121 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
126 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
127 pub struct ListFilters {
128 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
132 pub is_dm: FilterMode,
133
134 #[serde(default, skip_serializing_if = "Vec::is_empty")]
137 pub spaces: Vec<OwnedRoomId>,
138
139 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
144 pub is_encrypted: FilterMode,
145
146 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
150 pub is_invited: FilterMode,
151
152 #[serde(default, skip_serializing_if = "Vec::is_empty")]
157 pub room_types: Vec<RoomTypeFilter>,
158
159 #[serde(default, skip_serializing_if = "Vec::is_empty")]
164 pub not_room_types: Vec<RoomTypeFilter>,
165
166 #[serde(default, skip_serializing_if = "Vec::is_empty")]
168 pub tags: Vec<TagName>,
169
170 #[serde(default, skip_serializing_if = "Vec::is_empty")]
174 pub not_tags: Vec<TagName>,
175 }
176
177 impl ListFilters {
178 pub fn new() -> Self {
180 Default::default()
181 }
182
183 pub fn is_empty(&self) -> bool {
185 let Self {
186 is_dm,
187 spaces,
188 is_encrypted,
189 is_invited,
190 room_types,
191 not_room_types,
192 tags,
193 not_tags,
194 } = self;
195
196 *is_dm == FilterMode::Both
197 && spaces.is_empty()
198 && *is_encrypted == FilterMode::Both
199 && *is_invited == FilterMode::Both
200 && room_types.is_empty()
201 && not_room_types.is_empty()
202 && tags.is_empty()
203 && not_tags.is_empty()
204 }
205 }
206
207 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
209 #[serde(from = "Option<bool>", into = "Option<bool>")]
210 #[allow(clippy::exhaustive_enums)]
211 pub enum FilterMode {
212 #[default]
214 Both,
215
216 Only,
218
219 Exclude,
221 }
222
223 impl From<Option<bool>> for FilterMode {
224 fn from(value: Option<bool>) -> Self {
225 match value {
226 None => Self::Both,
227 Some(true) => Self::Only,
228 Some(false) => Self::Exclude,
229 }
230 }
231 }
232
233 impl From<FilterMode> for Option<bool> {
234 fn from(value: FilterMode) -> Self {
235 match value {
236 FilterMode::Both => None,
237 FilterMode::Only => Some(true),
238 FilterMode::Exclude => Some(false),
239 }
240 }
241 }
242
243 #[derive(Clone, Debug, Serialize, Deserialize)]
245 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
246 pub struct RoomSubscription {
247 pub required_state: RequiredState,
249
250 pub timeline_limit: UInt,
252 }
253
254 impl RoomSubscription {
255 pub fn new(required_state: RequiredState, timeline_limit: UInt) -> Self {
257 Self { required_state, timeline_limit }
258 }
259 }
260
261 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
263 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
264 pub struct RequiredState {
265 #[serde(default, skip_serializing_if = "Vec::is_empty")]
267 pub include: Vec<RequiredStateElement>,
268
269 #[serde(default, skip_serializing_if = "Vec::is_empty")]
271 pub exclude: Vec<RequiredStateElement>,
272
273 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
277 pub lazy_members: bool,
278 }
279
280 impl RequiredState {
281 pub fn new() -> Self {
283 Default::default()
284 }
285 }
286
287 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
289 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
290 pub struct RequiredStateElement {
291 #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
295 pub event_type: Option<StateEventType>,
296
297 #[serde(skip_serializing_if = "Option::is_none")]
301 pub state_key: Option<String>,
302 }
303
304 impl RequiredStateElement {
305 pub fn new() -> Self {
307 Default::default()
308 }
309
310 pub fn with_event_type(mut self, event_type: StateEventType) -> Self {
312 self.event_type = Some(event_type);
313 self
314 }
315
316 pub fn with_state_key(mut self, state_key: String) -> Self {
318 self.state_key = Some(state_key);
319 self
320 }
321 }
322
323 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
325 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
326 pub struct Extensions {
327 #[serde(default, skip_serializing_if = "ToDevice::is_disabled")]
329 pub to_device: ToDevice,
330
331 #[serde(default, skip_serializing_if = "E2EE::is_disabled")]
333 pub e2ee: E2EE,
334
335 #[serde(default, skip_serializing_if = "AccountData::is_disabled")]
337 pub account_data: AccountData,
338
339 #[serde(default, skip_serializing_if = "Receipts::is_disabled")]
341 pub receipts: Receipts,
342
343 #[serde(default, skip_serializing_if = "Typing::is_disabled")]
345 pub typing: Typing,
346
347 #[cfg(feature = "unstable-msc4308")]
349 #[serde(
350 default,
351 skip_serializing_if = "ThreadSubscriptions::is_disabled",
352 rename = "io.element.msc4308.thread_subscriptions"
353 )]
354 pub thread_subscriptions: ThreadSubscriptions,
355
356 #[cfg(feature = "unstable-msc4360")]
358 #[serde(
359 default,
360 skip_serializing_if = "Threads::is_disabled",
361 rename = "io.element.msc4360.threads"
362 )]
363 pub threads: Threads,
364
365 #[cfg(feature = "unstable-msc4262")]
367 #[serde(
368 default,
369 skip_serializing_if = "Profiles::is_disabled",
370 rename = "org.matrix.msc4262.profiles"
371 )]
372 pub profiles: Profiles,
373
374 #[cfg(feature = "unstable-msc4480")]
376 #[serde(
377 default,
378 skip_serializing_if = "StickyEvents::is_disabled",
379 rename = "org.matrix.msc4354.sticky_events"
380 )]
381 pub sticky_events: StickyEvents,
382
383 #[cfg(feature = "unstable-msc4559")]
385 #[serde(
386 default,
387 skip_serializing_if = "Presence::is_disabled",
388 rename = "org.continuwuity.presence_v2.msc4559.presence"
389 )]
390 pub presence: Presence,
391
392 #[serde(flatten)]
394 pub(crate) other: BTreeMap<String, serde_json::Value>,
395 }
396
397 impl Extensions {
398 pub fn new() -> Self {
400 Default::default()
401 }
402
403 pub fn is_empty(&self) -> bool {
405 let Self {
406 to_device,
407 e2ee,
408 account_data,
409 receipts,
410 typing,
411 #[cfg(feature = "unstable-msc4308")]
412 thread_subscriptions,
413 #[cfg(feature = "unstable-msc4360")]
414 threads,
415 #[cfg(feature = "unstable-msc4262")]
416 profiles,
417 #[cfg(feature = "unstable-msc4480")]
418 sticky_events,
419 #[cfg(feature = "unstable-msc4559")]
420 presence,
421 other,
422 } = self;
423
424 #[allow(unused_mut)]
425 let mut empty = to_device.is_disabled()
426 && e2ee.is_disabled()
427 && account_data.is_disabled()
428 && receipts.is_disabled()
429 && typing.is_disabled()
430 && other.is_empty();
431
432 #[cfg(feature = "unstable-msc4308")]
433 {
434 empty &= thread_subscriptions.is_disabled();
435 }
436
437 #[cfg(feature = "unstable-msc4360")]
438 {
439 empty &= threads.is_disabled();
440 }
441
442 #[cfg(feature = "unstable-msc4262")]
443 {
444 empty &= profiles.is_disabled();
445 }
446
447 #[cfg(feature = "unstable-msc4480")]
448 {
449 empty &= sticky_events.is_disabled();
450 }
451
452 #[cfg(feature = "unstable-msc4559")]
453 {
454 empty &= presence.is_disabled();
455 }
456
457 empty
458 }
459 }
460
461 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
465 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
466 pub struct ToDevice {
467 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
469 pub enabled: bool,
470
471 #[serde(skip_serializing_if = "Option::is_none")]
475 pub limit: Option<UInt>,
476
477 #[serde(skip_serializing_if = "Option::is_none")]
479 pub since: Option<String>,
480 }
481
482 impl ToDevice {
483 pub fn new() -> Self {
485 Default::default()
486 }
487
488 pub fn is_disabled(&self) -> bool {
490 !self.enabled
491 }
492 }
493
494 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
498 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
499 pub struct E2EE {
500 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
502 pub enabled: bool,
503 }
504
505 impl E2EE {
506 pub fn new() -> Self {
508 Default::default()
509 }
510
511 pub fn is_disabled(&self) -> bool {
513 !self.enabled
514 }
515 }
516
517 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
521 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
522 pub struct AccountData {
523 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
525 pub enabled: bool,
526
527 #[serde(skip_serializing_if = "Option::is_none")]
531 pub lists: Option<Vec<String>>,
532
533 #[serde(skip_serializing_if = "Option::is_none")]
537 pub rooms: Option<Vec<OwnedRoomId>>,
538 }
539
540 impl AccountData {
541 pub fn new() -> Self {
543 Default::default()
544 }
545
546 pub fn is_disabled(&self) -> bool {
548 !self.enabled
549 }
550 }
551
552 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
556 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
557 pub struct Receipts {
558 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
560 pub enabled: bool,
561
562 #[serde(skip_serializing_if = "Option::is_none")]
566 pub lists: Option<Vec<String>>,
567
568 #[serde(skip_serializing_if = "Option::is_none")]
572 pub rooms: Option<Vec<OwnedRoomId>>,
573 }
574
575 impl Receipts {
576 pub fn new() -> Self {
578 Default::default()
579 }
580
581 pub fn is_disabled(&self) -> bool {
583 !self.enabled
584 }
585 }
586
587 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
591 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
592 pub struct Typing {
593 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
595 pub enabled: bool,
596
597 #[serde(skip_serializing_if = "Option::is_none")]
601 pub lists: Option<Vec<String>>,
602
603 #[serde(skip_serializing_if = "Option::is_none")]
607 pub rooms: Option<Vec<OwnedRoomId>>,
608 }
609
610 impl Typing {
611 pub fn new() -> Self {
613 Default::default()
614 }
615
616 pub fn is_disabled(&self) -> bool {
618 !self.enabled
619 }
620 }
621
622 #[cfg(feature = "unstable-msc4308")]
626 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
627 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
628 pub struct ThreadSubscriptions {
629 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
631 pub enabled: bool,
632
633 #[serde(skip_serializing_if = "Option::is_none")]
638 pub limit: Option<UInt>,
639 }
640
641 #[cfg(feature = "unstable-msc4308")]
642 impl ThreadSubscriptions {
643 pub fn new() -> Self {
645 Default::default()
646 }
647
648 pub fn is_disabled(&self) -> bool {
650 !self.enabled
651 }
652 }
653
654 #[cfg(feature = "unstable-msc4360")]
658 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
659 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
660 pub struct Threads {
661 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
663 pub enabled: bool,
664
665 pub include_roots: bool,
667
668 #[serde(skip_serializing_if = "Option::is_none")]
673 pub limit: Option<UInt>,
674 }
675
676 #[cfg(feature = "unstable-msc4360")]
677 impl Threads {
678 pub fn new() -> Self {
680 Default::default()
681 }
682
683 pub fn is_disabled(&self) -> bool {
685 !self.enabled
686 }
687 }
688
689 #[cfg(feature = "unstable-msc4262")]
693 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
694 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
695 pub struct Profiles {
696 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
698 pub enabled: bool,
699
700 #[serde(skip_serializing_if = "Option::is_none")]
704 pub fields: Option<Vec<ruma_common::profile::ProfileFieldName>>,
705
706 #[serde(skip_serializing_if = "Option::is_none")]
710 pub lists: Option<Vec<String>>,
711
712 #[serde(skip_serializing_if = "Option::is_none")]
716 pub rooms: Option<Vec<OwnedRoomId>>,
717 }
718
719 #[cfg(feature = "unstable-msc4262")]
720 impl Profiles {
721 pub fn new() -> Self {
723 Default::default()
724 }
725
726 pub fn is_disabled(&self) -> bool {
728 !self.enabled
729 }
730 }
731
732 #[cfg(feature = "unstable-msc4480")]
736 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
737 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
738 pub struct StickyEvents {
739 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
741 pub enabled: bool,
742
743 #[serde(skip_serializing_if = "Option::is_none")]
747 pub limit: Option<UInt>,
748
749 #[serde(skip_serializing_if = "Option::is_none")]
753 pub lists: Option<Vec<String>>,
754
755 #[serde(skip_serializing_if = "Option::is_none")]
759 pub rooms: Option<Vec<OwnedRoomId>>,
760 }
761
762 #[cfg(feature = "unstable-msc4480")]
763 impl StickyEvents {
764 pub fn new() -> Self {
766 Default::default()
767 }
768
769 pub fn is_disabled(&self) -> bool {
771 !self.enabled
772 }
773 }
774
775 #[cfg(feature = "unstable-msc4559")]
779 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
780 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
781 pub struct Presence {
782 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
784 pub enabled: bool,
785
786 #[serde(skip_serializing_if = "Option::is_none")]
790 pub lists: Option<Vec<String>>,
791
792 #[serde(skip_serializing_if = "Option::is_none")]
796 pub rooms: Option<Vec<OwnedRoomId>>,
797 }
798
799 #[cfg(feature = "unstable-msc4559")]
800 impl Presence {
801 pub fn new() -> Self {
803 Default::default()
804 }
805
806 pub fn is_disabled(&self) -> bool {
808 !self.enabled
809 }
810 }
811}
812
813#[response]
815pub struct Response {
816 pub pos: String,
819
820 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
822 pub lists: BTreeMap<String, response::List>,
823
824 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
826 pub rooms: BTreeMap<OwnedRoomId, response::Room>,
827
828 #[serde(default, skip_serializing_if = "response::Extensions::is_empty")]
830 pub extensions: response::Extensions,
831}
832
833impl Response {
834 pub fn new(pos: String) -> Self {
836 Self {
837 pos,
838 lists: Default::default(),
839 rooms: Default::default(),
840 extensions: Default::default(),
841 }
842 }
843}
844
845pub mod response {
847 #[cfg(any(feature = "unstable-msc4308", feature = "unstable-msc4360"))]
848 use ruma_common::OwnedEventId;
849 #[cfg(feature = "unstable-msc4559")]
850 use ruma_common::presence::PresenceStatus;
851 #[cfg(feature = "unstable-msc4262")]
852 use ruma_common::profile::UserProfileUpdate;
853 use ruma_common::{OneTimeKeyAlgorithm, serde::from_raw_json_value};
854 use ruma_events::{
855 AnyGlobalAccountDataEvent, AnyRoomAccountDataEvent, AnyToDeviceEvent,
856 receipt::ReceiptEventContent, room::member::MembershipState, typing::TypingEventContent,
857 };
858 use serde::de::IgnoredAny;
859 use serde_json::value::RawValue as RawJsonValue;
860
861 #[cfg(feature = "unstable-msc4559")]
862 use super::PresenceState;
863 use super::{
864 super::DeviceLists, AnyStrippedStateEvent, AnySyncStateEvent, AnySyncTimelineEvent,
865 BTreeMap, Deserialize, JsOption, OwnedMxcUri, OwnedRoomId, OwnedUserId, Raw, Serialize,
866 StateEventType, UInt,
867 };
868 #[cfg(feature = "unstable-msc4308")]
869 use crate::threads::get_thread_subscriptions_changes::msc4308::{
870 ThreadSubscription, ThreadUnsubscription,
871 };
872
873 #[derive(Clone, Debug, Default, Deserialize, Serialize)]
875 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
876 pub struct List {
877 pub count: UInt,
879 }
880
881 impl List {
882 pub fn new(count: UInt) -> Self {
884 Self { count }
885 }
886 }
887
888 #[derive(Clone, Debug, Serialize)]
890 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
891 pub struct Room {
892 #[serde(skip_serializing_if = "Option::is_none")]
896 pub bump_stamp: Option<UInt>,
897
898 #[serde(skip_serializing_if = "Option::is_none")]
900 pub membership: Option<MembershipState>,
901
902 #[serde(default, skip_serializing_if = "Vec::is_empty")]
904 pub lists: Vec<String>,
905
906 #[serde(flatten)]
908 pub data: RoomData,
909 }
910
911 impl Room {
912 pub fn new(data: RoomData) -> Self {
914 Self { bump_stamp: None, membership: None, lists: Vec::new(), data }
915 }
916 }
917
918 impl<'de> Deserialize<'de> for Room {
919 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
920 where
921 D: serde::Deserializer<'de>,
922 {
923 #[derive(Deserialize)]
924 struct RoomDeHelper {
925 bump_stamp: Option<UInt>,
926 membership: Option<MembershipState>,
927 #[serde(default)]
928 lists: Vec<String>,
929 }
930
931 let json = Box::<RawJsonValue>::deserialize(deserializer)?;
932 let RoomDeHelper { bump_stamp, membership, lists } = from_raw_json_value(&json)?;
933 let data = from_raw_json_value(&json)?;
934
935 Ok(Self { bump_stamp, membership, lists, data })
936 }
937 }
938
939 #[derive(Clone, Debug, Serialize)]
941 #[serde(untagged)]
942 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
943 pub enum RoomData {
944 Full(FullRoomData),
946
947 Stripped(StrippedRoomData),
949 }
950
951 impl<'de> Deserialize<'de> for RoomData {
952 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
953 where
954 D: serde::Deserializer<'de>,
955 {
956 #[derive(Deserialize)]
957 struct StrippedStateDeHelper {
958 stripped_state: Option<IgnoredAny>,
959 }
960
961 let json = Box::<RawJsonValue>::deserialize(deserializer)?;
962 let StrippedStateDeHelper { stripped_state } = from_raw_json_value(&json)?;
963
964 if stripped_state.is_some() {
965 Ok(Self::Stripped(from_raw_json_value(&json)?))
966 } else {
967 Ok(Self::Full(from_raw_json_value(&json)?))
968 }
969 }
970 }
971
972 impl From<FullRoomData> for RoomData {
973 fn from(value: FullRoomData) -> Self {
974 Self::Full(value)
975 }
976 }
977
978 impl From<StrippedRoomData> for RoomData {
979 fn from(value: StrippedRoomData) -> Self {
980 Self::Stripped(value)
981 }
982 }
983
984 #[derive(Clone, Debug, Default, Deserialize, Serialize)]
988 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
989 pub struct FullRoomData {
990 #[serde(default, skip_serializing_if = "JsOption::is_undefined")]
994 pub name: JsOption<String>,
995
996 #[serde(default, skip_serializing_if = "JsOption::is_undefined")]
1000 pub avatar: JsOption<OwnedMxcUri>,
1001
1002 #[serde(skip_serializing_if = "Option::is_none")]
1004 pub heroes: Option<Vec<Hero>>,
1005
1006 #[serde(skip_serializing_if = "Option::is_none")]
1008 pub is_dm: Option<bool>,
1009
1010 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
1013 pub initial: bool,
1014
1015 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
1017 pub expanded_timeline: bool,
1018
1019 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1021 pub required_state: Vec<RequiredStateEntry>,
1022
1023 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1025 pub timeline: Vec<Raw<AnySyncTimelineEvent>>,
1026
1027 #[serde(skip_serializing_if = "Option::is_none")]
1030 pub prev_batch: Option<String>,
1031
1032 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
1035 pub limited: bool,
1036
1037 #[serde(skip_serializing_if = "Option::is_none")]
1040 pub num_live: Option<UInt>,
1041
1042 #[serde(skip_serializing_if = "Option::is_none")]
1045 pub joined_count: Option<UInt>,
1046
1047 #[serde(skip_serializing_if = "Option::is_none")]
1049 pub invited_count: Option<UInt>,
1050 }
1051
1052 impl FullRoomData {
1053 pub fn new() -> Self {
1055 Default::default()
1056 }
1057 }
1058
1059 #[derive(Clone, Debug, Deserialize, Serialize)]
1061 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1062 pub struct StrippedRoomData {
1063 pub stripped_state: Vec<Raw<AnyStrippedStateEvent>>,
1065 }
1066
1067 impl StrippedRoomData {
1068 pub fn new(stripped_state: Vec<Raw<AnyStrippedStateEvent>>) -> Self {
1070 Self { stripped_state }
1071 }
1072 }
1073
1074 #[derive(Clone, Debug, Deserialize, Serialize)]
1076 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1077 pub struct Hero {
1078 pub user_id: OwnedUserId,
1080
1081 #[serde(skip_serializing_if = "Option::is_none")]
1083 pub displayname: Option<String>,
1084
1085 #[serde(skip_serializing_if = "Option::is_none")]
1087 pub avatar_url: Option<OwnedMxcUri>,
1088 }
1089
1090 impl Hero {
1091 pub fn new(user_id: OwnedUserId) -> Self {
1093 Self { user_id, displayname: None, avatar_url: None }
1094 }
1095 }
1096
1097 #[derive(Clone, Debug, Serialize)]
1099 #[serde(untagged)]
1100 #[allow(clippy::exhaustive_enums)]
1101 pub enum RequiredStateEntry {
1102 Event(Raw<AnySyncStateEvent>),
1104
1105 Deleted(StateStub),
1107 }
1108
1109 impl<'de> Deserialize<'de> for RequiredStateEntry {
1110 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1111 where
1112 D: serde::Deserializer<'de>,
1113 {
1114 #[derive(Deserialize)]
1115 struct ContentDeHelper {
1116 #[serde(default)]
1117 content: JsOption<IgnoredAny>,
1118 }
1119
1120 let json = Box::<RawJsonValue>::deserialize(deserializer)?;
1121 let ContentDeHelper { content } = from_raw_json_value(&json)?;
1122
1123 if content.is_undefined() {
1124 Ok(Self::Deleted(from_raw_json_value(&json)?))
1125 } else {
1126 Ok(Self::Event(Raw::from_json(json)))
1127 }
1128 }
1129 }
1130
1131 #[derive(Clone, Debug, Deserialize, Serialize)]
1133 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1134 pub struct StateStub {
1135 #[serde(rename = "type")]
1137 pub event_type: StateEventType,
1138
1139 pub state_key: String,
1141 }
1142
1143 impl StateStub {
1144 pub fn new(event_type: StateEventType, state_key: String) -> Self {
1146 Self { event_type, state_key }
1147 }
1148 }
1149
1150 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
1152 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1153 pub struct Extensions {
1154 #[serde(skip_serializing_if = "Option::is_none")]
1156 pub to_device: Option<ToDevice>,
1157
1158 #[serde(skip_serializing_if = "Option::is_none")]
1160 pub e2ee: Option<E2EE>,
1161
1162 #[serde(default, skip_serializing_if = "AccountData::is_empty")]
1164 pub account_data: AccountData,
1165
1166 #[serde(default, skip_serializing_if = "Receipts::is_empty")]
1168 pub receipts: Receipts,
1169
1170 #[serde(default, skip_serializing_if = "Typing::is_empty")]
1172 pub typing: Typing,
1173
1174 #[cfg(feature = "unstable-msc4308")]
1176 #[serde(
1177 default,
1178 skip_serializing_if = "ThreadSubscriptions::is_empty",
1179 rename = "io.element.msc4308.thread_subscriptions"
1180 )]
1181 pub thread_subscriptions: ThreadSubscriptions,
1182
1183 #[cfg(feature = "unstable-msc4360")]
1185 #[serde(
1186 default,
1187 skip_serializing_if = "Threads::is_empty",
1188 rename = "io.element.msc4360.threads"
1189 )]
1190 pub threads: Threads,
1191
1192 #[cfg(feature = "unstable-msc4262")]
1194 #[serde(
1195 default,
1196 skip_serializing_if = "Profiles::is_empty",
1197 rename = "org.matrix.msc4262.profiles"
1198 )]
1199 pub profiles: Profiles,
1200
1201 #[cfg(feature = "unstable-msc4480")]
1203 #[serde(
1204 default,
1205 skip_serializing_if = "StickyEvents::is_empty",
1206 rename = "org.matrix.msc4354.sticky_events"
1207 )]
1208 pub sticky_events: StickyEvents,
1209
1210 #[cfg(feature = "unstable-msc4559")]
1212 #[serde(
1213 default,
1214 skip_serializing_if = "Presence::is_empty",
1215 rename = "org.continuwuity.presence_v2.msc4559.presence"
1216 )]
1217 pub presence: Presence,
1218 }
1219
1220 impl Extensions {
1221 pub fn new() -> Self {
1223 Default::default()
1224 }
1225
1226 pub fn is_empty(&self) -> bool {
1228 let Self {
1229 to_device,
1230 e2ee,
1231 account_data,
1232 receipts,
1233 typing,
1234 #[cfg(feature = "unstable-msc4308")]
1235 thread_subscriptions,
1236 #[cfg(feature = "unstable-msc4360")]
1237 threads,
1238 #[cfg(feature = "unstable-msc4262")]
1239 profiles,
1240 #[cfg(feature = "unstable-msc4480")]
1241 sticky_events,
1242 #[cfg(feature = "unstable-msc4559")]
1243 presence,
1244 } = self;
1245
1246 #[allow(unused_mut)]
1247 let mut empty = to_device.is_none()
1248 && e2ee.is_none()
1249 && account_data.is_empty()
1250 && receipts.is_empty()
1251 && typing.is_empty();
1252
1253 #[cfg(feature = "unstable-msc4308")]
1254 {
1255 empty &= thread_subscriptions.is_empty();
1256 }
1257
1258 #[cfg(feature = "unstable-msc4360")]
1259 {
1260 empty &= threads.is_empty();
1261 }
1262
1263 #[cfg(feature = "unstable-msc4262")]
1264 {
1265 empty &= profiles.is_empty();
1266 }
1267
1268 #[cfg(feature = "unstable-msc4480")]
1269 {
1270 empty &= sticky_events.is_empty();
1271 }
1272
1273 #[cfg(feature = "unstable-msc4559")]
1274 {
1275 empty &= presence.is_empty();
1276 }
1277
1278 empty
1279 }
1280 }
1281
1282 #[derive(Clone, Debug, Serialize, Deserialize)]
1286 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1287 pub struct ToDevice {
1288 pub next_batch: String,
1290
1291 pub messages: Vec<Raw<AnyToDeviceEvent>>,
1293 }
1294
1295 impl ToDevice {
1296 pub fn new(next_batch: String, messages: Vec<Raw<AnyToDeviceEvent>>) -> Self {
1298 Self { next_batch, messages }
1299 }
1300 }
1301
1302 #[derive(Clone, Debug, Serialize, Deserialize)]
1306 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1307 pub struct E2EE {
1308 #[serde(default, skip_serializing_if = "DeviceLists::is_empty")]
1310 pub device_lists: DeviceLists,
1311
1312 pub device_one_time_keys_count: BTreeMap<OneTimeKeyAlgorithm, UInt>,
1317
1318 pub device_unused_fallback_key_types: Vec<OneTimeKeyAlgorithm>,
1320 }
1321
1322 impl E2EE {
1323 pub fn new(
1325 device_one_time_keys_count: BTreeMap<OneTimeKeyAlgorithm, UInt>,
1326 device_unused_fallback_key_types: Vec<OneTimeKeyAlgorithm>,
1327 ) -> Self {
1328 Self {
1329 device_lists: DeviceLists::new(),
1330 device_one_time_keys_count,
1331 device_unused_fallback_key_types,
1332 }
1333 }
1334 }
1335
1336 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
1340 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1341 pub struct AccountData {
1342 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1344 pub global: Vec<Raw<AnyGlobalAccountDataEvent>>,
1345
1346 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
1348 pub rooms: BTreeMap<OwnedRoomId, Vec<Raw<AnyRoomAccountDataEvent>>>,
1349 }
1350
1351 impl AccountData {
1352 pub fn new() -> Self {
1354 Default::default()
1355 }
1356
1357 pub fn is_empty(&self) -> bool {
1359 let Self { global, rooms } = self;
1360 global.is_empty() && rooms.is_empty()
1361 }
1362 }
1363
1364 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
1368 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1369 pub struct Receipts {
1370 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
1372 pub rooms: BTreeMap<OwnedRoomId, Raw<ReceiptEventContent>>,
1373 }
1374
1375 impl Receipts {
1376 pub fn new() -> Self {
1378 Default::default()
1379 }
1380
1381 pub fn is_empty(&self) -> bool {
1383 let Self { rooms } = self;
1384 rooms.is_empty()
1385 }
1386 }
1387
1388 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
1392 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1393 pub struct Typing {
1394 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
1396 pub rooms: BTreeMap<OwnedRoomId, TypingEventContent>,
1397 }
1398
1399 impl Typing {
1400 pub fn new() -> Self {
1402 Default::default()
1403 }
1404
1405 pub fn is_empty(&self) -> bool {
1407 let Self { rooms } = self;
1408 rooms.is_empty()
1409 }
1410 }
1411
1412 #[cfg(feature = "unstable-msc4308")]
1416 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
1417 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1418 pub struct ThreadSubscriptions {
1419 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
1421 pub subscribed: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, ThreadSubscription>>,
1422
1423 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
1425 pub unsubscribed: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, ThreadUnsubscription>>,
1426
1427 #[serde(skip_serializing_if = "Option::is_none")]
1433 pub prev_batch: Option<String>,
1434 }
1435
1436 #[cfg(feature = "unstable-msc4308")]
1437 impl ThreadSubscriptions {
1438 pub fn new() -> Self {
1440 Default::default()
1441 }
1442
1443 pub fn is_empty(&self) -> bool {
1445 let Self { subscribed, unsubscribed, prev_batch } = self;
1446 subscribed.is_empty() && unsubscribed.is_empty() && prev_batch.is_none()
1447 }
1448 }
1449
1450 #[cfg(feature = "unstable-msc4360")]
1454 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
1455 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1456 pub struct Threads {
1457 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
1459 pub updates: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, ThreadUpdate>>,
1460
1461 #[serde(skip_serializing_if = "Option::is_none")]
1466 pub prev_batch: Option<String>,
1467 }
1468
1469 #[cfg(feature = "unstable-msc4360")]
1470 impl Threads {
1471 pub fn new() -> Self {
1473 Default::default()
1474 }
1475
1476 pub fn is_empty(&self) -> bool {
1478 let Self { updates, prev_batch } = self;
1479 updates.is_empty() && prev_batch.is_none()
1480 }
1481 }
1482
1483 #[cfg(feature = "unstable-msc4360")]
1487 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
1488 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1489 pub struct ThreadUpdate {
1490 #[serde(skip_serializing_if = "Option::is_none")]
1494 pub thread_root: Option<Raw<AnySyncTimelineEvent>>,
1495
1496 #[serde(skip_serializing_if = "Option::is_none")]
1498 pub prev_batch: Option<String>,
1499 }
1500
1501 #[cfg(feature = "unstable-msc4360")]
1502 impl ThreadUpdate {
1503 pub fn new() -> Self {
1505 Default::default()
1506 }
1507 }
1508
1509 #[cfg(feature = "unstable-msc4262")]
1513 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
1514 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1515 pub struct Profiles {
1516 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
1518 pub users: BTreeMap<OwnedUserId, UserProfileUpdate>,
1519 }
1520
1521 #[cfg(feature = "unstable-msc4262")]
1522 impl Profiles {
1523 pub fn new() -> Self {
1525 Default::default()
1526 }
1527
1528 pub fn is_empty(&self) -> bool {
1530 let Self { users } = self;
1531 users.is_empty()
1532 }
1533 }
1534
1535 #[cfg(feature = "unstable-msc4480")]
1539 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
1540 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1541 pub struct StickyEvents {
1542 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
1544 pub rooms: BTreeMap<OwnedRoomId, StickyEventsRoom>,
1545 }
1546
1547 #[cfg(feature = "unstable-msc4480")]
1548 impl StickyEvents {
1549 pub fn new() -> Self {
1551 Default::default()
1552 }
1553
1554 pub fn is_empty(&self) -> bool {
1556 let Self { rooms } = self;
1557 rooms.is_empty()
1558 }
1559 }
1560
1561 #[cfg(feature = "unstable-msc4480")]
1563 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
1564 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1565 pub struct StickyEventsRoom {
1566 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1568 pub events: Vec<Raw<AnySyncTimelineEvent>>,
1569 }
1570
1571 #[cfg(feature = "unstable-msc4480")]
1572 impl StickyEventsRoom {
1573 pub fn new() -> Self {
1575 Default::default()
1576 }
1577 }
1578
1579 #[cfg(feature = "unstable-msc4559")]
1583 #[derive(Clone, Debug, Serialize, Deserialize)]
1584 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1585 pub struct PresenceUpdate {
1586 #[serde(default = "presence_offline", skip_serializing_if = "is_presence_offline")]
1590 pub presence: PresenceState,
1591
1592 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
1596 pub status: PresenceStatus,
1597 }
1598
1599 #[cfg(feature = "unstable-msc4559")]
1600 impl PresenceUpdate {
1601 pub fn new() -> Self {
1603 Self { presence: PresenceState::Offline, status: PresenceStatus::default() }
1604 }
1605 }
1606
1607 #[cfg(feature = "unstable-msc4559")]
1608 impl Default for PresenceUpdate {
1609 fn default() -> Self {
1610 Self::new()
1611 }
1612 }
1613
1614 #[cfg(feature = "unstable-msc4559")]
1615 fn presence_offline() -> PresenceState {
1616 PresenceState::Offline
1617 }
1618
1619 #[cfg(feature = "unstable-msc4559")]
1620 fn is_presence_offline(presence: &PresenceState) -> bool {
1621 *presence == PresenceState::Offline
1622 }
1623
1624 #[cfg(feature = "unstable-msc4559")]
1628 #[derive(Clone, Debug, Default, Serialize, Deserialize)]
1629 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
1630 pub struct Presence {
1631 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
1633 pub user_ids: BTreeMap<OwnedUserId, PresenceUpdate>,
1634 }
1635
1636 #[cfg(feature = "unstable-msc4559")]
1637 impl Presence {
1638 pub fn new() -> Self {
1640 Default::default()
1641 }
1642
1643 pub fn is_empty(&self) -> bool {
1645 let Self { user_ids } = self;
1646 user_ids.is_empty()
1647 }
1648 }
1649}
1650
1651#[cfg(test)]
1652mod tests {
1653 use js_int::uint;
1654 use ruma_common::{assert_to_canonical_json_eq, owned_room_id};
1655 use ruma_events::StateEventType;
1656 use serde_json::{from_value as from_json_value, json};
1657 use strass::assert_let;
1658
1659 use super::{
1660 request::{
1661 Extensions, FilterMode, List, ListFilters, RequiredState, RequiredStateElement,
1662 RoomSubscription,
1663 },
1664 response::{self, RequiredStateEntry, Room, RoomData, StateStub},
1665 };
1666
1667 #[test]
1668 fn serialize_list() {
1669 let mut required_state = RequiredState::new();
1670 required_state.include.push(
1671 RequiredStateElement::new()
1672 .with_event_type(StateEventType::RoomCreate)
1673 .with_state_key(String::new()),
1674 );
1675 required_state.lazy_members = true;
1676
1677 let mut list = List::new(required_state, uint!(10));
1678 list.range = Some((uint!(0), uint!(99)));
1679
1680 assert_to_canonical_json_eq!(
1681 list,
1682 json!({
1683 "range": [0, 99],
1684 "required_state": {
1685 "include": [{ "type": "m.room.create", "state_key": "" }],
1686 "lazy_members": true,
1687 },
1688 "timeline_limit": 10,
1689 })
1690 );
1691 }
1692
1693 #[test]
1694 fn serialize_room_subscription_all_state() {
1695 let mut required_state = RequiredState::new();
1696 required_state.include.push(RequiredStateElement::new());
1697 let subscription = RoomSubscription::new(required_state, uint!(10));
1698
1699 assert_to_canonical_json_eq!(
1700 subscription,
1701 json!({
1702 "required_state": { "include": [{}] },
1703 "timeline_limit": 10,
1704 })
1705 );
1706 }
1707
1708 #[test]
1709 fn list_filters_serde() {
1710 let mut filters = ListFilters::new();
1711 filters.is_dm = FilterMode::Only;
1712 filters.is_invited = FilterMode::Exclude;
1713
1714 assert_to_canonical_json_eq!(filters, json!({ "is_dm": true, "is_invited": false }));
1715
1716 let filters: ListFilters =
1717 from_json_value(json!({ "is_dm": true, "is_invited": false })).unwrap();
1718 assert_eq!(filters.is_dm, FilterMode::Only);
1719 assert_eq!(filters.is_encrypted, FilterMode::Both);
1720 assert_eq!(filters.is_invited, FilterMode::Exclude);
1721 }
1722
1723 #[test]
1724 fn deserialize_full_room() {
1725 let room: Room = from_json_value(json!({
1726 "name": null,
1727 "initial": true,
1728 "membership": "join",
1729 "lists": ["foo-list"],
1730 "required_state": [
1731 {
1732 "content": {},
1733 "event_id": "$create:example.com",
1734 "origin_server_ts": 1,
1735 "sender": "@alice:example.com",
1736 "state_key": "",
1737 "type": "m.room.create",
1738 },
1739 { "type": "m.room.topic", "state_key": "" },
1740 ],
1741 }))
1742 .unwrap();
1743
1744 assert_eq!(room.lists, ["foo-list"]);
1745 assert_let!(RoomData::Full(data) = room.data);
1746 assert!(data.name.is_null());
1747 assert!(data.initial);
1748 assert!(!data.expanded_timeline);
1749 assert_eq!(data.required_state.len(), 2);
1750 assert_let!(RequiredStateEntry::Event(_) = &data.required_state[0]);
1751 assert_let!(RequiredStateEntry::Deleted(stub) = &data.required_state[1]);
1752 assert_eq!(stub.event_type, StateEventType::RoomTopic);
1753 assert_eq!(stub.state_key, "");
1754 }
1755
1756 #[test]
1757 fn deserialize_stripped_room() {
1758 let room: Room = from_json_value(json!({
1759 "bump_stamp": 4,
1760 "membership": "invite",
1761 "stripped_state": [],
1762 }))
1763 .unwrap();
1764
1765 assert_eq!(room.bump_stamp, Some(uint!(4)));
1766 assert_let!(RoomData::Stripped(data) = room.data);
1767 assert_eq!(data.stripped_state.len(), 0);
1768 }
1769
1770 #[test]
1771 fn serialize_extensions() {
1772 let mut extensions = Extensions::new();
1773 extensions.typing.enabled = true;
1774 extensions.typing.lists = Some(vec![]);
1775 extensions.typing.rooms = Some(vec![owned_room_id!("!a:example.com")]);
1776
1777 assert_to_canonical_json_eq!(
1778 extensions,
1779 json!({ "typing": { "enabled": true, "lists": [], "rooms": ["!a:example.com"] } })
1780 );
1781 }
1782
1783 #[test]
1784 fn deserialize_extensions_response() {
1785 let extensions: response::Extensions = from_json_value(json!({
1786 "to_device": { "next_batch": "1", "messages": [] },
1787 "e2ee": {
1788 "device_one_time_keys_count": { "signed_curve25519": 50 },
1789 "device_unused_fallback_key_types": ["signed_curve25519"],
1790 },
1791 "receipts": {
1792 "rooms": {
1793 "!a:example.com": {
1794 "$e:example.com": { "m.read": { "@b:example.com": { "ts": 1 } } },
1795 },
1796 },
1797 },
1798 "typing": { "rooms": { "!a:example.com": { "user_ids": ["@b:example.com"] } } },
1799 }))
1800 .unwrap();
1801
1802 assert_eq!(extensions.to_device.unwrap().next_batch, "1");
1803 assert_eq!(extensions.e2ee.unwrap().device_unused_fallback_key_types.len(), 1);
1804 assert_eq!(extensions.receipts.rooms.len(), 1);
1805 let typing = &extensions.typing.rooms["!a:example.com"];
1806 assert_eq!(typing.user_ids, ["@b:example.com"]);
1807
1808 from_json_value::<response::Extensions>(json!({ "e2ee": {} })).unwrap_err();
1810 }
1811
1812 #[test]
1813 fn deserialize_null_content_as_event() {
1814 let entry: RequiredStateEntry = from_json_value(json!({
1815 "content": null,
1816 "type": "m.room.topic",
1817 "state_key": "",
1818 }))
1819 .unwrap();
1820
1821 assert_let!(RequiredStateEntry::Event(_) = entry);
1822 }
1823
1824 #[test]
1825 fn serialize_deleted_state() {
1826 let entry =
1827 RequiredStateEntry::Deleted(StateStub::new(StateEventType::RoomTopic, String::new()));
1828
1829 assert_to_canonical_json_eq!(entry, json!({ "type": "m.room.topic", "state_key": "" }));
1830 }
1831
1832 #[cfg(feature = "unstable-msc4480")]
1833 #[test]
1834 fn deserialize_sticky_events() {
1835 let sticky_events: response::StickyEvents = from_json_value(json!({
1836 "rooms": {
1837 "!room:example.com": {
1838 "events": [{
1839 "content": { "body": "sticky", "msgtype": "m.text" },
1840 "event_id": "$1:example.com",
1841 "origin_server_ts": 1,
1842 "sender": "@alice:example.com",
1843 "type": "m.room.message",
1844 "sticky": { "duration_ms": 300_000 },
1845 }]
1846 }
1847 }
1848 }))
1849 .unwrap();
1850
1851 assert_eq!(sticky_events.rooms.len(), 1);
1852 let room = sticky_events.rooms.values().next().unwrap();
1853 assert_eq!(room.events.len(), 1);
1854 }
1855
1856 #[cfg(feature = "unstable-msc4360")]
1857 #[test]
1858 fn threads_extension_serde() {
1859 use super::request;
1860
1861 let mut extensions = Extensions::new();
1862 extensions.threads = request::Threads { enabled: true, include_roots: true, limit: None };
1863 assert_to_canonical_json_eq!(
1864 extensions,
1865 json!({ "io.element.msc4360.threads": { "enabled": true, "include_roots": true } })
1866 );
1867
1868 let extensions: response::Extensions = from_json_value(json!({
1869 "io.element.msc4360.threads": {
1870 "updates": {
1871 "!room:example.com": {
1872 "$root:example.com": { "prev_batch": "t1" },
1873 },
1874 },
1875 "prev_batch": "t2",
1876 },
1877 }))
1878 .unwrap();
1879
1880 let threads = extensions.threads;
1881 let update = &threads.updates["!room:example.com"]["$root:example.com"];
1882 assert!(update.thread_root.is_none());
1883 assert_eq!(update.prev_batch.as_deref(), Some("t1"));
1884 assert_eq!(threads.prev_batch.as_deref(), Some("t2"));
1885 }
1886
1887 #[cfg(feature = "unstable-msc4559")]
1888 #[test]
1889 fn presence_update_defaults() {
1890 use ruma_common::presence::PresenceState;
1891
1892 let update: response::PresenceUpdate = from_json_value(json!({})).unwrap();
1893 assert_eq!(update.presence, PresenceState::Offline);
1894 assert_to_canonical_json_eq!(update, json!({}));
1895 }
1896}
1897
1898#[cfg(all(test, feature = "client"))]
1899mod client_tests {
1900 use std::{borrow::Cow, time::Duration};
1901
1902 use ruma_common::{
1903 api::{
1904 FeatureFlag, MatrixVersion, OutgoingRequestExt as _, SupportedVersions,
1905 auth_scheme::SendAccessToken,
1906 },
1907 assert_to_canonical_json_eq,
1908 };
1909 use serde_json::{Value as JsonValue, from_slice as from_json_slice, json};
1910
1911 use super::Request;
1912
1913 #[test]
1914 fn serialize_request() {
1915 let supported = SupportedVersions {
1916 versions: [MatrixVersion::V1_19].into(),
1917 features: [FeatureFlag::Msc4186Stable].into(),
1918 };
1919 let mut request = Request::new();
1920 request.conn_id = Some("main".to_owned());
1921 request.pos = Some("s58_224".to_owned());
1922 request.timeout = Some(Duration::from_millis(30000));
1923
1924 let req: http::Request<Vec<u8>> = request
1925 .try_into_http_request(
1926 "https://homeserver.tld",
1927 SendAccessToken::IfRequired("auth_tok"),
1928 Cow::Owned(supported),
1929 )
1930 .unwrap();
1931
1932 assert_eq!(req.uri().path(), "/_matrix/client/v4/sync");
1933 assert_eq!(req.uri().query(), None);
1934 assert_to_canonical_json_eq!(
1935 from_json_slice::<JsonValue>(req.body()).unwrap(),
1936 json!({ "conn_id": "main", "pos": "s58_224", "timeout": 30000 })
1937 );
1938 }
1939}