| @@ -41,6 +41,7 @@ use matrix_sdk::{ |
| 41 | }, |
41 | }, |
| 42 | presence::PresenceEvent, |
42 | presence::PresenceEvent, |
| 43 | reaction::ReactionEventContent, |
43 | reaction::ReactionEventContent, |
| |
44 | receipt::{ReceiptEventContent, ReceiptType}, |
| 44 | room::{ |
45 | room::{ |
| 45 | encryption::RoomEncryptionEventContent, |
46 | encryption::RoomEncryptionEventContent, |
| 46 | member::OriginalSyncRoomMemberEvent, |
47 | member::OriginalSyncRoomMemberEvent, |
| @@ -55,12 +56,14 @@ use matrix_sdk::{ |
| 55 | AnyTimelineEvent, |
56 | AnyTimelineEvent, |
| 56 | EmptyStateKey, |
57 | EmptyStateKey, |
| 57 | InitialStateEvent, |
58 | InitialStateEvent, |
| |
59 | SyncEphemeralRoomEvent, |
| 58 | SyncMessageLikeEvent, |
60 | SyncMessageLikeEvent, |
| 59 | SyncStateEvent, |
61 | SyncStateEvent, |
| 60 | }, |
62 | }, |
| 61 | room::RoomType, |
63 | room::RoomType, |
| 62 | serde::Raw, |
64 | serde::Raw, |
| 63 | EventEncryptionAlgorithm, |
65 | EventEncryptionAlgorithm, |
| |
66 | EventId, |
| 64 | OwnedEventId, |
67 | OwnedEventId, |
| 65 | OwnedRoomId, |
68 | OwnedRoomId, |
| 66 | OwnedRoomOrAliasId, |
69 | OwnedRoomOrAliasId, |
| @@ -84,8 +87,8 @@ use crate::{ |
| 84 | EventLocation, |
87 | EventLocation, |
| 85 | IambError, |
88 | IambError, |
| 86 | IambResult, |
89 | IambResult, |
| 87 | Receipts, |
| |
| 88 | RoomFetchStatus, |
90 | RoomFetchStatus, |
| |
91 | RoomInfo, |
| 89 | VerifyAction, |
92 | VerifyAction, |
| 90 | }, |
93 | }, |
| 91 | message::MessageFetchResult, |
94 | message::MessageFetchResult, |
| @@ -171,6 +174,19 @@ pub async fn create_room( |
| 171 | return Ok(resp.room_id); |
174 | return Ok(resp.room_id); |
| 172 | } |
175 | } |
| 173 | |
176 | |
| |
177 | async fn update_event_receipts(info: &mut RoomInfo, room: &MatrixRoom, event_id: &EventId) { |
| |
178 | let receipts = match room.event_read_receipts(event_id).await { |
| |
179 | Ok(receipts) => receipts, |
| |
180 | Err(e) => { |
| |
181 | tracing::warn!(?event_id, "failed to get event receipts: {e}"); |
| |
182 | return; |
| |
183 | }, |
| |
184 | }; |
| |
185 | for (user_id, _) in receipts { |
| |
186 | info.set_receipt(user_id, event_id.to_owned()); |
| |
187 | } |
| |
188 | } |
| |
189 | |
| 174 | async fn load_plan(store: &AsyncProgramStore) -> HashMap<OwnedRoomId, Option<String>> { |
190 | async fn load_plan(store: &AsyncProgramStore) -> HashMap<OwnedRoomId, Option<String>> { |
| 175 | let mut locked = store.lock().await; |
191 | let mut locked = store.lock().await; |
| 176 | let ChatStore { need_load, rooms, .. } = &mut locked.application; |
192 | let ChatStore { need_load, rooms, .. } = &mut locked.application; |
| @@ -200,7 +216,7 @@ async fn load_plan(store: &AsyncProgramStore) -> HashMap<OwnedRoomId, Option<Str |
| 200 | } |
216 | } |
| 201 | |
217 | |
| 202 | async fn load_older_one( |
218 | async fn load_older_one( |
| 203 | client: Client, |
219 | client: &Client, |
| 204 | room_id: &RoomId, |
220 | room_id: &RoomId, |
| 205 | fetch_id: Option<String>, |
221 | fetch_id: Option<String>, |
| 206 | limit: u32, |
222 | limit: u32, |
| @@ -228,7 +244,12 @@ async fn load_older_one( |
| 228 | } |
244 | } |
| 229 | } |
245 | } |
| 230 | |
246 | |
| 231 | async fn load_insert(room_id: OwnedRoomId, res: MessageFetchResult, store: AsyncProgramStore) { |
247 | async fn load_insert( |
| |
248 | client: &Client, |
| |
249 | room_id: OwnedRoomId, |
| |
250 | res: MessageFetchResult, |
| |
251 | store: AsyncProgramStore, |
| |
252 | ) { |
| 232 | let mut locked = store.lock().await; |
253 | let mut locked = store.lock().await; |
| 233 | let ChatStore { need_load, presences, rooms, .. } = &mut locked.application; |
254 | let ChatStore { need_load, presences, rooms, .. } = &mut locked.application; |
| 234 | let info = rooms.get_or_default(room_id.clone()); |
255 | let info = rooms.get_or_default(room_id.clone()); |
| @@ -240,6 +261,10 @@ async fn load_insert(room_id: OwnedRoomId, res: MessageFetchResult, store: Async |
| 240 | let sender = msg.sender().to_owned(); |
261 | let sender = msg.sender().to_owned(); |
| 241 | let _ = presences.get_or_default(sender); |
262 | let _ = presences.get_or_default(sender); |
| 242 | |
263 | |
| |
264 | if let Some(room) = client.get_room(&room_id) { |
| |
265 | update_event_receipts(info, &room, msg.event_id()).await; |
| |
266 | } |
| |
267 | |
| 243 | match msg { |
268 | match msg { |
| 244 | AnyMessageLikeEvent::RoomEncrypted(msg) => { |
269 | AnyMessageLikeEvent::RoomEncrypted(msg) => { |
| 245 | info.insert_encrypted(msg); |
270 | info.insert_encrypted(msg); |
| @@ -277,8 +302,8 @@ async fn load_older(client: &Client, store: &AsyncProgramStore) -> usize { |
| 277 | let store = store.clone(); |
302 | let store = store.clone(); |
| 278 | |
303 | |
| 279 | async move { |
304 | async move { |
| 280 | let res = load_older_one(client, room_id.as_ref(), fetch_id, limit).await; |
305 | let res = load_older_one(&client, room_id.as_ref(), fetch_id, limit).await; |
| 281 | load_insert(room_id, res, store).await; |
306 | load_insert(&client, room_id, res, store).await; |
| 282 | } |
307 | } |
| 283 | }) |
308 | }) |
| 284 | .collect::<FuturesUnordered<_>>() |
309 | .collect::<FuturesUnordered<_>>() |
| @@ -357,28 +382,40 @@ async fn refresh_rooms_forever(client: &Client, store: &AsyncProgramStore) { |
| 357 | } |
382 | } |
| 358 | } |
383 | } |
| 359 | |
384 | |
| 360 | async fn refresh_receipts_forever(client: &Client, store: &AsyncProgramStore) { |
385 | async fn send_receipts_forever(client: &Client, store: &AsyncProgramStore) { |
| 361 | // Update the displayed read receipts every 5 seconds. |
386 | let mut interval = tokio::time::interval(Duration::from_secs(2)); |
| 362 | let mut interval = tokio::time::interval(Duration::from_secs(5)); |
| |
| 363 | let mut sent = HashMap::<OwnedRoomId, OwnedEventId>::default(); |
387 | let mut sent = HashMap::<OwnedRoomId, OwnedEventId>::default(); |
| 364 | |
388 | |
| 365 | loop { |
389 | loop { |
| 366 | interval.tick().await; |
390 | interval.tick().await; |
| 367 | let receipts = update_receipts(client).await; |
| |
| 368 | let read = store.lock().await.application.set_receipts(receipts).await; |
| |
| 369 | |
| |
| 370 | for (room_id, read_till) in read.into_iter() { |
| |
| 371 | if let Some(read_sent) = sent.get(&room_id) { |
| |
| 372 | if read_sent == &read_till { |
| |
| 373 | // Skip unchanged receipts. |
| |
| 374 | continue; |
| |
| 375 | } |
| |
| 376 | } |
| |
| 377 | |
391 | |
| 378 | if let Some(room) = client.get_joined_room(&room_id) { |
392 | let locked = store.lock().await; |
| 379 | if room.read_receipt(&read_till).await.is_ok() { |
393 | let updates = client |
| 380 | sent.insert(room_id, read_till); |
394 | .joined_rooms() |
| |
395 | .into_iter() |
| |
396 | .filter_map(|room| { |
| |
397 | let room_id = room.room_id().to_owned(); |
| |
398 | let info = locked.application.rooms.get(&room_id)?; |
| |
399 | let new_receipt = info.read_till.as_ref()?; |
| |
400 | let old_receipt = sent.get(&room_id); |
| |
401 | if Some(new_receipt) != old_receipt { |
| |
402 | Some((room_id, new_receipt.clone())) |
| |
403 | } else { |
| |
404 | None |
| 381 | } |
405 | } |
| |
406 | }) |
| |
407 | .collect::<Vec<_>>(); |
| |
408 | drop(locked); |
| |
409 | |
| |
410 | for (room_id, new_receipt) in updates { |
| |
411 | let Some(room) = client.get_joined_room(&room_id) else { |
| |
412 | continue; |
| |
413 | }; |
| |
414 | match room.read_receipt(&new_receipt).await { |
| |
415 | Ok(()) => { |
| |
416 | sent.insert(room_id, new_receipt); |
| |
417 | }, |
| |
418 | Err(e) => tracing::warn!(?room_id, "Failed to set read receipt: {e}"), |
| 382 | } |
419 | } |
| 383 | } |
420 | } |
| 384 | } |
421 | } |
| @@ -413,29 +450,6 @@ fn oneshot<T>() -> (ClientReply<T>, ClientResponse<T>) { |
| 413 | return (reply, response); |
450 | return (reply, response); |
| 414 | } |
451 | } |
| 415 | |
452 | |
| 416 | async fn update_receipts(client: &Client) -> Vec<(OwnedRoomId, Receipts)> { |
| |
| 417 | let mut rooms = vec![]; |
| |
| 418 | |
| |
| 419 | for room in client.joined_rooms() { |
| |
| 420 | if let Ok(users) = room.active_members_no_sync().await { |
| |
| 421 | let mut receipts = Receipts::new(); |
| |
| 422 | |
| |
| 423 | for member in users { |
| |
| 424 | let res = room.user_read_receipt(member.user_id()).await; |
| |
| 425 | |
| |
| 426 | if let Ok(Some((event_id, _))) = res { |
| |
| 427 | let user_id = member.user_id().to_owned(); |
| |
| 428 | receipts.entry(event_id).or_default().push(user_id); |
| |
| 429 | } |
| |
| 430 | } |
| |
| 431 | |
| |
| 432 | rooms.push((room.room_id().to_owned(), receipts)); |
| |
| 433 | } |
| |
| 434 | } |
| |
| 435 | |
| |
| 436 | return rooms; |
| |
| 437 | } |
| |
| 438 | |
| |
| 439 | pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>); |
453 | pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>); |
| 440 | |
454 | |
| 441 | pub enum WorkerTask { |
455 | pub enum WorkerTask { |
| @@ -787,6 +801,7 @@ impl ClientWorker { |
| 787 | let _ = locked.application.presences.get_or_default(sender); |
801 | let _ = locked.application.presences.get_or_default(sender); |
| 788 | |
802 | |
| 789 | let info = locked.application.get_room_info(room_id.to_owned()); |
803 | let info = locked.application.get_room_info(room_id.to_owned()); |
| |
804 | update_event_receipts(info, &room, ev.event_id()).await; |
| 790 | info.insert(ev.into_full_event(room_id.to_owned())); |
805 | info.insert(ev.into_full_event(room_id.to_owned())); |
| 791 | } |
806 | } |
| 792 | }, |
807 | }, |
| @@ -805,11 +820,34 @@ impl ClientWorker { |
| 805 | let _ = locked.application.presences.get_or_default(sender); |
820 | let _ = locked.application.presences.get_or_default(sender); |
| 806 | |
821 | |
| 807 | let info = locked.application.get_room_info(room_id.to_owned()); |
822 | let info = locked.application.get_room_info(room_id.to_owned()); |
| |
823 | update_event_receipts(info, &room, ev.event_id()).await; |
| 808 | info.insert_reaction(ev.into_full_event(room_id.to_owned())); |
824 | info.insert_reaction(ev.into_full_event(room_id.to_owned())); |
| 809 | } |
825 | } |
| 810 | }, |
826 | }, |
| 811 | ); |
827 | ); |
| 812 | |
828 | |
| |
829 | let _ = self.client.add_event_handler( |
| |
830 | |ev: SyncEphemeralRoomEvent<ReceiptEventContent>, |
| |
831 | room: MatrixRoom, |
| |
832 | store: Ctx<AsyncProgramStore>| { |
| |
833 | async move { |
| |
834 | let room_id = room.room_id(); |
| |
835 | |
| |
836 | let mut locked = store.lock().await; |
| |
837 | |
| |
838 | let info = locked.application.get_room_info(room_id.to_owned()); |
| |
839 | for (event_id, receipts) in ev.content.0.into_iter() { |
| |
840 | let Some(receipts) = receipts.get(&ReceiptType::Read) else { |
| |
841 | continue; |
| |
842 | }; |
| |
843 | for user_id in receipts.keys() { |
| |
844 | info.set_receipt(user_id.to_owned(), event_id.clone()); |
| |
845 | } |
| |
846 | } |
| |
847 | } |
| |
848 | }, |
| |
849 | ); |
| |
850 | |
| 813 | let _ = self.client.add_event_handler( |
851 | let _ = self.client.add_event_handler( |
| 814 | |ev: OriginalSyncRoomRedactionEvent, |
852 | |ev: OriginalSyncRoomRedactionEvent, |
| 815 | room: MatrixRoom, |
853 | room: MatrixRoom, |
| @@ -992,7 +1030,7 @@ impl ClientWorker { |
| 992 | |
1030 | |
| 993 | async move { |
1031 | async move { |
| 994 | let load = load_older_forever(&client, &store); |
1032 | let load = load_older_forever(&client, &store); |
| 995 | let rcpt = refresh_receipts_forever(&client, &store); |
1033 | let rcpt = send_receipts_forever(&client, &store); |
| 996 | let room = refresh_rooms_forever(&client, &store); |
1034 | let room = refresh_rooms_forever(&client, &store); |
| 997 | let ((), (), ()) = tokio::join!(load, rcpt, room); |
1035 | let ((), (), ()) = tokio::join!(load, rcpt, room); |
| 998 | } |
1036 | } |