1use std::{collections::HashSet, sync::Arc};
18
19use matrix_sdk_common::deserialized_responses::TimelineEvent;
20use ruma::{
21 OwnedRoomId, OwnedUserId, RoomId,
22 events::{
23 direct::OwnedDirectUserIdentifier,
24 room::{
25 avatar::PossiblyRedactedRoomAvatarEventContent,
26 canonical_alias::PossiblyRedactedRoomCanonicalAliasEventContent,
27 create::RoomCreateEventContent, encryption::PossiblyRedactedRoomEncryptionEventContent,
28 guest_access::PossiblyRedactedRoomGuestAccessEventContent,
29 history_visibility::PossiblyRedactedRoomHistoryVisibilityEventContent,
30 join_rules::PossiblyRedactedRoomJoinRulesEventContent,
31 name::PossiblyRedactedRoomNameEventContent,
32 tombstone::PossiblyRedactedRoomTombstoneEventContent,
33 topic::PossiblyRedactedRoomTopicEventContent,
34 },
35 },
36};
37use serde::{Deserialize, Serialize};
38
39use crate::{
40 MinimalStateEvent, RoomInfo, RoomState,
41 deserialized_responses::SyncOrStrippedState,
42 latest_event::LatestEventValue,
43 room::{BaseRoomInfo, RoomSummary, SyncInfo},
44 sync::UnreadNotificationsCount,
45};
46
47#[derive(Clone, Debug, Serialize, Deserialize)]
60pub struct RoomInfoV1 {
61 room_id: OwnedRoomId,
62 room_type: RoomState,
63 notification_counts: UnreadNotificationsCount,
64 summary: RoomSummary,
65 members_synced: bool,
66 last_prev_batch: Option<String>,
67 #[serde(default = "sync_info_complete")] sync_info: SyncInfo,
69 #[serde(default = "encryption_state_default")] encryption_state_synced: bool,
71 latest_event: Option<TimelineEvent>,
72 base_info: BaseRoomInfoV1,
73}
74
75impl RoomInfoV1 {
76 pub fn room_id(&self) -> &RoomId {
78 &self.room_id
79 }
80
81 pub fn state(&self) -> RoomState {
83 self.room_type
84 }
85
86 pub fn migrate(self, create: Option<&SyncOrStrippedState<RoomCreateEventContent>>) -> RoomInfo {
89 let RoomInfoV1 {
90 room_id,
91 room_type,
92 notification_counts,
93 summary,
94 members_synced,
95 last_prev_batch,
96 sync_info,
97 encryption_state_synced,
98 latest_event: _, base_info,
100 } = self;
101
102 RoomInfo {
103 data_format_version: 0,
104 room_id,
105 room_state: room_type,
106 notification_counts,
107 summary,
108 members_synced,
109 last_prev_batch,
110 sync_info,
111 encryption_state_synced,
112 latest_event_value: LatestEventValue::None,
113 read_receipts: Default::default(),
114 base_info: base_info.migrate(create),
115 warned_about_unknown_room_version_rules: Arc::new(false.into()),
116 cached_display_name: None,
117 cached_user_defined_notification_mode: None,
118 recency_stamp: None,
119 }
120 }
121}
122
123fn sync_info_complete() -> SyncInfo {
128 SyncInfo::FullySynced
129}
130
131fn encryption_state_default() -> bool {
136 true
137}
138
139#[derive(Clone, Debug, Serialize, Deserialize)]
141struct BaseRoomInfoV1 {
142 avatar: Option<MinimalStateEvent<PossiblyRedactedRoomAvatarEventContent>>,
143 canonical_alias: Option<MinimalStateEvent<PossiblyRedactedRoomCanonicalAliasEventContent>>,
144 dm_targets: HashSet<OwnedUserId>,
145 encryption: Option<PossiblyRedactedRoomEncryptionEventContent>,
146 guest_access: Option<MinimalStateEvent<PossiblyRedactedRoomGuestAccessEventContent>>,
147 history_visibility:
148 Option<MinimalStateEvent<PossiblyRedactedRoomHistoryVisibilityEventContent>>,
149 join_rules: Option<MinimalStateEvent<PossiblyRedactedRoomJoinRulesEventContent>>,
150 max_power_level: i64,
151 name: Option<MinimalStateEvent<PossiblyRedactedRoomNameEventContent>>,
152 tombstone: Option<MinimalStateEvent<PossiblyRedactedRoomTombstoneEventContent>>,
153 topic: Option<MinimalStateEvent<PossiblyRedactedRoomTopicEventContent>>,
154}
155
156impl BaseRoomInfoV1 {
157 fn migrate(
159 self,
160 create: Option<&SyncOrStrippedState<RoomCreateEventContent>>,
161 ) -> Box<BaseRoomInfo> {
162 let BaseRoomInfoV1 {
163 avatar,
164 canonical_alias,
165 dm_targets,
166 encryption,
167 guest_access,
168 history_visibility,
169 join_rules,
170 max_power_level,
171 name,
172 tombstone,
173 topic,
174 } = self;
175
176 let create = create.map(|ev| match ev {
177 SyncOrStrippedState::Sync(e) => e.into(),
178 SyncOrStrippedState::Stripped(e) => e.into(),
179 });
180
181 let mut converted_dm_targets = HashSet::new();
182 for dm_target in dm_targets {
183 converted_dm_targets.insert(OwnedDirectUserIdentifier::from(dm_target));
184 }
185
186 Box::new(BaseRoomInfo {
187 avatar,
188 canonical_alias,
189 create,
190 dm_targets: converted_dm_targets,
191 encryption,
192 guest_access,
193 history_visibility,
194 join_rules,
195 max_power_level,
196 name,
197 tombstone,
198 topic,
199 ..Default::default()
200 })
201 }
202}