1use std::future::Future;
16
17use eyeball::Subscriber;
18use indexmap::IndexMap;
19use matrix_sdk::{
20 Result, Room, SendOutsideWasm,
21 deserialized_responses::TimelineEvent,
22 paginators::{PaginableRoom, thread::PaginableThread},
23};
24use matrix_sdk_base::{RoomInfo, crypto::types::events::CryptoContextInfo};
25use ruma::{
26 EventId, OwnedEventId, OwnedTransactionId, OwnedUserId, RoomId, UserId,
27 events::{
28 AnyMessageLikeEventContent,
29 fully_read::FullyReadEventContent,
30 receipt::{Receipt, ReceiptThread, ReceiptType},
31 },
32 room_version_rules::RoomVersionRules,
33};
34use tracing::error;
35
36use super::{Profile, RedactError, TimelineBuilder};
37use crate::timeline::{
38 self, Timeline, TimelineReadReceiptTracking, latest_event::LatestEventValue,
39 thread_list_service::ThreadListService,
40};
41
42pub trait RoomExt {
43 fn timeline(&self)
51 -> impl Future<Output = Result<Timeline, timeline::Error>> + SendOutsideWasm;
52
53 fn timeline_builder(&self) -> TimelineBuilder;
62
63 fn latest_event(&self) -> impl Future<Output = LatestEventValue>;
65
66 fn thread_list_service(&self) -> ThreadListService;
72}
73
74impl RoomExt for Room {
75 async fn timeline(&self) -> Result<Timeline, timeline::Error> {
76 self.timeline_builder().build().await
77 }
78
79 fn timeline_builder(&self) -> TimelineBuilder {
80 TimelineBuilder::new(self)
81 .track_read_marker_and_receipts(TimelineReadReceiptTracking::AllEvents)
82 }
83
84 async fn latest_event(&self) -> LatestEventValue {
85 LatestEventValue::from_base_latest_event_value(
86 (**self).latest_event(),
87 self,
88 &self.client(),
89 )
90 .await
91 }
92
93 fn thread_list_service(&self) -> ThreadListService {
94 ThreadListService::new(self.clone())
95 }
96}
97
98pub(super) trait RoomDataProvider:
99 Clone + PaginableRoom + PaginableThread + 'static
100{
101 fn room_id(&self) -> &RoomId;
102 fn own_user_id(&self) -> &UserId;
103 fn room_version_rules(&self) -> RoomVersionRules;
104
105 fn crypto_context_info(&self)
106 -> impl Future<Output = CryptoContextInfo> + SendOutsideWasm + '_;
107
108 fn profile_from_user_id<'a>(
109 &'a self,
110 user_id: &'a UserId,
111 ) -> impl Future<Output = Option<Profile>> + SendOutsideWasm + 'a;
112
113 fn load_user_receipt<'a>(
115 &'a self,
116 receipt_type: ReceiptType,
117 thread: ReceiptThread,
118 user_id: &'a UserId,
119 ) -> impl Future<Output = Option<(OwnedEventId, Receipt)>> + SendOutsideWasm + 'a;
120
121 fn load_event_receipts<'a>(
123 &'a self,
124 event_id: &'a EventId,
125 receipt_thread: ReceiptThread,
126 ) -> impl Future<Output = IndexMap<OwnedUserId, Receipt>> + SendOutsideWasm + 'a;
127
128 fn load_fully_read_marker(&self) -> impl Future<Output = Option<OwnedEventId>> + '_;
130
131 fn send(
133 &self,
134 content: AnyMessageLikeEventContent,
135 ) -> impl Future<Output = Result<(), super::Error>> + SendOutsideWasm + '_;
136
137 fn redact<'a>(
139 &'a self,
140 event_id: &'a EventId,
141 reason: Option<&'a str>,
142 transaction_id: Option<OwnedTransactionId>,
143 ) -> impl Future<Output = Result<(), super::Error>> + SendOutsideWasm + 'a;
144
145 fn room_info(&self) -> Subscriber<RoomInfo>;
146
147 fn load_event<'a>(
149 &'a self,
150 event_id: &'a EventId,
151 ) -> impl Future<Output = Result<TimelineEvent>> + SendOutsideWasm + 'a;
152}
153
154impl RoomDataProvider for Room {
155 fn room_id(&self) -> &RoomId {
156 (**self).room_id()
157 }
158
159 fn own_user_id(&self) -> &UserId {
160 (**self).own_user_id()
161 }
162
163 fn room_version_rules(&self) -> RoomVersionRules {
164 (**self).clone_info().room_version_rules_or_default()
165 }
166
167 async fn crypto_context_info(&self) -> CryptoContextInfo {
168 self.crypto_context_info().await
169 }
170
171 async fn profile_from_user_id<'a>(&'a self, user_id: &'a UserId) -> Option<Profile> {
172 Profile::load(self, user_id).await
173 }
174
175 async fn load_user_receipt<'a>(
176 &'a self,
177 receipt_type: ReceiptType,
178 thread: ReceiptThread,
179 user_id: &'a UserId,
180 ) -> Option<(OwnedEventId, Receipt)> {
181 match self.load_user_receipt(receipt_type.clone(), thread.clone(), user_id).await {
182 Ok(receipt) => receipt,
183 Err(e) => {
184 error!(
185 ?receipt_type,
186 ?thread,
187 ?user_id,
188 "Failed to get read receipt for user: {e}"
189 );
190 None
191 }
192 }
193 }
194
195 async fn load_event_receipts<'a>(
196 &'a self,
197 event_id: &'a EventId,
198 receipt_thread: ReceiptThread,
199 ) -> IndexMap<OwnedUserId, Receipt> {
200 match self.load_event_receipts(ReceiptType::Read, receipt_thread.clone(), event_id).await {
201 Ok(receipts) => receipts.into_iter().collect(),
202 Err(e) => {
203 error!(?event_id, ?receipt_thread, "Failed to get read receipts for event: {e}");
204 IndexMap::new()
205 }
206 }
207 }
208
209 async fn load_fully_read_marker(&self) -> Option<OwnedEventId> {
210 match self.account_data_static::<FullyReadEventContent>().await {
211 Ok(Some(fully_read)) => match fully_read.deserialize() {
212 Ok(fully_read) => Some(fully_read.content.event_id),
213 Err(e) => {
214 error!("Failed to deserialize fully-read account data: {e}");
215 None
216 }
217 },
218 Err(e) => {
219 error!("Failed to get fully-read account data from the store: {e}");
220 None
221 }
222 _ => None,
223 }
224 }
225
226 async fn send(&self, content: AnyMessageLikeEventContent) -> Result<(), super::Error> {
227 let _ = self.send_queue().send(content).await?;
228 Ok(())
229 }
230
231 async fn redact<'a>(
232 &'a self,
233 event_id: &'a EventId,
234 reason: Option<&'a str>,
235 transaction_id: Option<OwnedTransactionId>,
236 ) -> Result<(), super::Error> {
237 let _ = self
238 .redact(event_id, reason, transaction_id)
239 .await
240 .map_err(RedactError::HttpError)
241 .map_err(super::Error::RedactError)?;
242 Ok(())
243 }
244
245 fn room_info(&self) -> Subscriber<RoomInfo> {
246 self.subscribe_info()
247 }
248
249 async fn load_event<'a>(&'a self, event_id: &'a EventId) -> Result<TimelineEvent> {
250 self.load_or_fetch_event(event_id, None).await
251 }
252}