| @@ -34,19 +34,20 @@ use matrix_sdk::{ |
| 34 | 34 | MembershipState, |
| 35 | 35 | }, |
| 36 | 36 | message::{MessageEventContent, TextMessageEventContent}, |
| 37 | | name::NameEventContent, |
| 38 | 37 | }, |
| 39 | 38 | AnyMessageEventContent, AnyPossiblyRedactedSyncMessageEvent, |
| 40 | 39 | AnySyncMessageEvent, AnySyncRoomEvent, AnySyncStateEvent, |
| 41 | 40 | SyncStateEvent, |
| 42 | 41 | }, |
| 43 | 42 | identifiers::{RoomId, UserId}, |
| 43 | locks::{RwLock, RwLockReadGuard}, |
| 44 | 44 | uuid::Uuid, |
| 45 | 45 | PossiblyRedactedExt, Room, |
| 46 | 46 | }; |
| 47 | 47 | use url::Url; |
| 48 | 48 | |
| 49 | 49 | use async_trait::async_trait; |
| 50 | use futures::executor::block_on; |
| 50 | 51 | use tracing::{debug, error, trace}; |
| 51 | 52 | |
| 52 | 53 | use crate::{ |
| @@ -55,11 +56,11 @@ use crate::{ |
| 55 | 56 | }; |
| 56 | 57 | use std::{ |
| 57 | 58 | borrow::Cow, |
| 58 | | cell::{Ref, RefCell, RefMut}, |
| 59 | cell::RefCell, |
| 59 | 60 | collections::HashMap, |
| 60 | 61 | convert::TryFrom, |
| 61 | 62 | rc::Rc, |
| 62 | | sync::Mutex, |
| 63 | sync::{Arc, Mutex}, |
| 63 | 64 | time::Instant, |
| 64 | 65 | }; |
| 65 | 66 | use weechat::{ |
| @@ -113,6 +114,7 @@ struct MatrixRoom { |
| 113 | 114 | homeserver: Rc<Url>, |
| 114 | 115 | room_id: Rc<RoomId>, |
| 115 | 116 | own_user_id: Rc<UserId>, |
| 117 | room: Arc<RwLock<Room>>, |
| 116 | 118 | |
| 117 | 119 | connection: Rc<RefCell<Option<Connection>>>, |
| 118 | 120 | |
| @@ -121,7 +123,6 @@ struct MatrixRoom { |
| 121 | 123 | |
| 122 | 124 | local_echo_queue: Rc<RefCell<HashMap<Uuid, MessageEventContent>>>, |
| 123 | 125 | |
| 124 | | room: Rc<RefCell<Room>>, |
| 125 | 126 | members: Rc<RefCell<HashMap<UserId, WeechatRoomMember>>>, |
| 126 | 127 | } |
| 127 | 128 | |
| @@ -236,6 +237,7 @@ impl MatrixRoom { |
| 236 | 237 | impl RoomBuffer { |
| 237 | 238 | pub fn new( |
| 238 | 239 | connection: &Rc<RefCell<Option<Connection>>>, |
| 240 | room: Arc<RwLock<Room>>, |
| 239 | 241 | homeserver: &Url, |
| 240 | 242 | room_id: RoomId, |
| 241 | 243 | own_user_id: &UserId, |
| @@ -246,7 +248,7 @@ impl RoomBuffer { |
| 246 | 248 | connection: connection.clone(), |
| 247 | 249 | typing_notice_time: Rc::new(RefCell::new(None)), |
| 248 | 250 | typing_in_flight: Rc::new(Mutex::new(())), |
| 249 | | room: Rc::new(RefCell::new(Room::new(&room_id, &own_user_id))), |
| 251 | room, |
| 250 | 252 | own_user_id: Rc::new(own_user_id.to_owned()), |
| 251 | 253 | members: Rc::new(RefCell::new(HashMap::new())), |
| 252 | 254 | local_echo_queue: Rc::new(RefCell::new(HashMap::new())), |
| @@ -275,28 +277,34 @@ impl RoomBuffer { |
| 275 | 277 | } |
| 276 | 278 | |
| 277 | 279 | pub fn restore( |
| 278 | | room: Room, |
| 280 | room: Arc<RwLock<Room>>, |
| 279 | 281 | connection: &Rc<RefCell<Option<Connection>>>, |
| 280 | 282 | homeserver: &Url, |
| 281 | 283 | ) -> Self { |
| 282 | | let mut room_buffer = RoomBuffer::new( |
| 284 | let room_clone = room.clone(); |
| 285 | let room_lock = block_on(room.read()); |
| 286 | let room_id = room_lock.room_id.to_owned(); |
| 287 | let own_user_id = &room_lock.own_user_id; |
| 288 | |
| 289 | let room_buffer = RoomBuffer::new( |
| 283 | 290 | connection, |
| 291 | room_clone, |
| 284 | 292 | homeserver, |
| 285 | | room.room_id.clone(), |
| 286 | | &room.own_user_id, |
| 293 | room_id, |
| 294 | own_user_id, |
| 287 | 295 | ); |
| 288 | 296 | |
| 289 | | debug!("Restoring room {}", room.room_id); |
| 297 | debug!("Restoring room {}", room_lock.room_id); |
| 290 | 298 | |
| 291 | | let matrix_members = room |
| 299 | let matrix_members = room_lock |
| 292 | 300 | .joined_members |
| 293 | 301 | .values() |
| 294 | | .chain(room.invited_members.values()); |
| 302 | .chain(room_lock.invited_members.values()); |
| 295 | 303 | |
| 296 | 304 | let mut weechat_members = HashMap::new(); |
| 297 | 305 | |
| 298 | 306 | for member in matrix_members { |
| 299 | | let display_name = room |
| 307 | let display_name = room_lock |
| 300 | 308 | .get_member(&member.user_id) |
| 301 | 309 | .unwrap() |
| 302 | 310 | .display_name |
| @@ -323,7 +331,6 @@ impl RoomBuffer { |
| 323 | 331 | .expect("Can't add nick to nicklist"); |
| 324 | 332 | } |
| 325 | 333 | |
| 326 | | room_buffer.inner.room = Rc::new(RefCell::new(room)); |
| 327 | 334 | room_buffer.inner.members.replace(weechat_members); |
| 328 | 335 | room_buffer.update_buffer_name(); |
| 329 | 336 | room_buffer.restore_messages(); |
| @@ -365,12 +372,12 @@ impl RoomBuffer { |
| 365 | 372 | self.inner.send_message(buffer, content).await |
| 366 | 373 | } |
| 367 | 374 | |
| 368 | | pub fn room_mut(&mut self) -> RefMut<'_, Room> { |
| 369 | | self.inner.room.borrow_mut() |
| 375 | pub fn room(&self) -> RwLockReadGuard<'_, Room> { |
| 376 | block_on(self.inner.room.read()) |
| 370 | 377 | } |
| 371 | 378 | |
| 372 | | pub fn room(&self) -> Ref<'_, Room> { |
| 373 | | self.inner.room.borrow() |
| 379 | pub fn room_id(&self) -> &RoomId { |
| 380 | &self.inner.room_id |
| 374 | 381 | } |
| 375 | 382 | |
| 376 | 383 | /// Retrieve a reference to a Weechat room member by user ID. |
| @@ -587,6 +594,7 @@ impl RoomBuffer { |
| 587 | 594 | /// |
| 588 | 595 | /// Disambiguations are a hashmap of user ID -> bool indicating that this user is either newly |
| 589 | 596 | /// ambiguous (true) or no longer ambiguous (false). |
| 597 | #[allow(dead_code)] |
| 590 | 598 | fn process_disambiguations( |
| 591 | 599 | &mut self, |
| 592 | 600 | disambiguations: &HashMap<UserId, bool>, |
| @@ -648,10 +656,6 @@ impl RoomBuffer { |
| 648 | 656 | return; |
| 649 | 657 | } |
| 650 | 658 | |
| 651 | | // Handle the event in the SDK room model |
| 652 | | let (_, disambiguations) = |
| 653 | | self.room_mut().handle_membership(event, state_event); |
| 654 | | |
| 655 | 659 | let new_nick = self.calculate_user_name(&target_id); |
| 656 | 660 | |
| 657 | 661 | if state_event { |
| @@ -685,7 +689,9 @@ impl RoomBuffer { |
| 685 | 689 | _ => (), |
| 686 | 690 | } |
| 687 | 691 | |
| 688 | | self.process_disambiguations(&disambiguations); |
| 692 | // TODO enable this again once we receive the event via an event |
| 693 | // emitter. |
| 694 | // self.process_disambiguations(&disambiguations); |
| 689 | 695 | |
| 690 | 696 | // Names of rooms without display names can get affected by the member list so we need to |
| 691 | 697 | // update them. |
| @@ -792,7 +798,9 @@ impl RoomBuffer { |
| 792 | 798 | } |
| 793 | 799 | }; |
| 794 | 800 | |
| 795 | | self.process_disambiguations(&disambiguations); |
| 801 | // TODO enable this again once we receive the event via an event |
| 802 | // emitter. |
| 803 | // self.process_disambiguations(&disambiguations); |
| 796 | 804 | |
| 797 | 805 | // Names of rooms without display names can get affected by the member list so we need to |
| 798 | 806 | // update them. |
| @@ -860,11 +868,7 @@ impl RoomBuffer { |
| 860 | 868 | buffer.print_date_tags(timestamp as i64, &[], &message); |
| 861 | 869 | } |
| 862 | 870 | |
| 863 | | pub fn handle_room_name( |
| 864 | | &mut self, |
| 865 | | event: &SyncStateEvent<NameEventContent>, |
| 866 | | ) { |
| 867 | | self.room_mut().handle_room_name(event); |
| 871 | pub fn handle_room_name(&self) { |
| 868 | 872 | self.update_buffer_name(); |
| 869 | 873 | } |
| 870 | 874 | |
| @@ -885,14 +889,11 @@ impl RoomBuffer { |
| 885 | 889 | AnySyncStateEvent::RoomMember(e) => { |
| 886 | 890 | self.handle_membership_event(e, false) |
| 887 | 891 | } |
| 888 | | AnySyncStateEvent::RoomName(n) => self.handle_room_name(n), |
| 892 | AnySyncStateEvent::RoomName(_) => self.handle_room_name(), |
| 889 | 893 | _ => (), |
| 890 | 894 | }, |
| 891 | 895 | |
| 892 | | event => { |
| 893 | | let mut room = self.room_mut(); |
| 894 | | room.receive_timeline_event(event); |
| 895 | | } |
| 896 | _ => (), |
| 896 | 897 | } |
| 897 | 898 | } |
| 898 | 899 | |
| @@ -901,11 +902,8 @@ impl RoomBuffer { |
| 901 | 902 | AnySyncStateEvent::RoomMember(e) => { |
| 902 | 903 | self.handle_membership_event(e, true) |
| 903 | 904 | } |
| 904 | | AnySyncStateEvent::RoomName(n) => self.handle_room_name(n), |
| 905 | | _ => { |
| 906 | | let mut room = self.room_mut(); |
| 907 | | room.receive_state_event(&event); |
| 908 | | } |
| 905 | AnySyncStateEvent::RoomName(_) => self.handle_room_name(), |
| 906 | _ => (), |
| 909 | 907 | } |
| 910 | 908 | } |
| 911 | 909 | } |