| @@ -8,6 +8,7 @@ use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; |
| 8 | 8 | use std::sync::Arc; |
| 9 | 9 | use std::time::{Duration, Instant}; |
| 10 | 10 | |
| 11 | use futures::{stream::FuturesUnordered, StreamExt}; |
| 11 | 12 | use gethostname::gethostname; |
| 12 | 13 | use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; |
| 13 | 14 | use tokio::task::JoinHandle; |
| @@ -260,19 +261,122 @@ async fn load_insert(room_id: OwnedRoomId, res: MessageFetchResult, store: Async |
| 260 | 261 | } |
| 261 | 262 | } |
| 262 | 263 | |
| 263 | | async fn load_older(client: &Client, store: &AsyncProgramStore) { |
| 264 | async fn load_older(client: &Client, store: &AsyncProgramStore) -> usize { |
| 264 | 265 | let limit = MIN_MSG_LOAD; |
| 265 | | let plan = load_plan(store).await; |
| 266 | 266 | |
| 267 | 267 | // Fetch each room separately, so they don't block each other. |
| 268 | | for (room_id, fetch_id) in plan.into_iter() { |
| 269 | | let client = client.clone(); |
| 270 | | let store = store.clone(); |
| 268 | load_plan(store) |
| 269 | .await |
| 270 | .into_iter() |
| 271 | .map(|(room_id, fetch_id)| { |
| 272 | let client = client.clone(); |
| 273 | let store = store.clone(); |
| 271 | 274 | |
| 272 | | tokio::spawn(async move { |
| 273 | | let res = load_older_one(client, room_id.as_ref(), fetch_id, limit).await; |
| 274 | | load_insert(room_id, res, store).await; |
| 275 | | }); |
| 275 | async move { |
| 276 | let res = load_older_one(client, room_id.as_ref(), fetch_id, limit).await; |
| 277 | load_insert(room_id, res, store).await; |
| 278 | } |
| 279 | }) |
| 280 | .collect::<FuturesUnordered<_>>() |
| 281 | .count() |
| 282 | .await |
| 283 | } |
| 284 | |
| 285 | async fn load_older_forever(client: &Client, store: &AsyncProgramStore) { |
| 286 | // Load older messages every 2 seconds. |
| 287 | let mut interval = tokio::time::interval(Duration::from_secs(2)); |
| 288 | |
| 289 | loop { |
| 290 | interval.tick().await; |
| 291 | load_older(client, store).await; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | async fn refresh_rooms(client: &Client, store: &AsyncProgramStore) { |
| 296 | let mut names = vec![]; |
| 297 | |
| 298 | let mut spaces = vec![]; |
| 299 | let mut rooms = vec![]; |
| 300 | let mut dms = vec![]; |
| 301 | |
| 302 | for room in client.invited_rooms().into_iter() { |
| 303 | let name = room.display_name().await.unwrap_or(DisplayName::Empty).to_string(); |
| 304 | names.push((room.room_id().to_owned(), name)); |
| 305 | |
| 306 | if room.is_direct() { |
| 307 | let tags = room.tags().await.unwrap_or_default(); |
| 308 | |
| 309 | dms.push(Arc::new((room.into(), tags))); |
| 310 | } else if room.is_space() { |
| 311 | spaces.push(room.into()); |
| 312 | } else { |
| 313 | let tags = room.tags().await.unwrap_or_default(); |
| 314 | |
| 315 | rooms.push(Arc::new((room.into(), tags))); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | for room in client.joined_rooms().into_iter() { |
| 320 | let name = room.display_name().await.unwrap_or(DisplayName::Empty).to_string(); |
| 321 | names.push((room.room_id().to_owned(), name)); |
| 322 | |
| 323 | if room.is_direct() { |
| 324 | let tags = room.tags().await.unwrap_or_default(); |
| 325 | |
| 326 | dms.push(Arc::new((room.into(), tags))); |
| 327 | } else if room.is_space() { |
| 328 | spaces.push(room.into()); |
| 329 | } else { |
| 330 | let tags = room.tags().await.unwrap_or_default(); |
| 331 | |
| 332 | rooms.push(Arc::new((room.into(), tags))); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | let mut locked = store.lock().await; |
| 337 | locked.application.sync_info.spaces = spaces; |
| 338 | locked.application.sync_info.rooms = rooms; |
| 339 | locked.application.sync_info.dms = dms; |
| 340 | |
| 341 | for (room_id, name) in names { |
| 342 | locked.application.set_room_name(&room_id, &name); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | async fn refresh_rooms_forever(client: &Client, store: &AsyncProgramStore) { |
| 347 | let mut interval = tokio::time::interval(Duration::from_secs(5)); |
| 348 | |
| 349 | loop { |
| 350 | interval.tick().await; |
| 351 | |
| 352 | refresh_rooms(client, store).await; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | async fn refresh_receipts_forever(client: &Client, store: &AsyncProgramStore) { |
| 357 | // Update the displayed read receipts every 5 seconds. |
| 358 | let mut interval = tokio::time::interval(Duration::from_secs(5)); |
| 359 | let mut sent = HashMap::<OwnedRoomId, OwnedEventId>::default(); |
| 360 | |
| 361 | loop { |
| 362 | interval.tick().await; |
| 363 | let receipts = update_receipts(client).await; |
| 364 | let read = store.lock().await.application.set_receipts(receipts).await; |
| 365 | |
| 366 | for (room_id, read_till) in read.into_iter() { |
| 367 | if let Some(read_sent) = sent.get(&room_id) { |
| 368 | if read_sent == &read_till { |
| 369 | // Skip unchanged receipts. |
| 370 | continue; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if let Some(room) = client.get_joined_room(&room_id) { |
| 375 | if room.read_receipt(&read_till).await.is_ok() { |
| 376 | sent.insert(room_id, read_till); |
| 377 | } |
| 378 | } |
| 379 | } |
| 276 | 380 | } |
| 277 | 381 | } |
| 278 | 382 | |
| @@ -331,8 +435,6 @@ async fn update_receipts(client: &Client) -> Vec<(OwnedRoomId, Receipts)> { |
| 331 | 435 | pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>); |
| 332 | 436 | |
| 333 | 437 | pub enum WorkerTask { |
| 334 | | ActiveRooms(ClientReply<Vec<FetchedRoom>>), |
| 335 | | DirectMessages(ClientReply<Vec<FetchedRoom>>), |
| 336 | 438 | Init(AsyncProgramStore, ClientReply<()>), |
| 337 | 439 | Login(LoginStyle, ClientReply<IambResult<EditInfo>>), |
| 338 | 440 | GetInviter(Invited, ClientReply<IambResult<Option<RoomMember>>>), |
| @@ -340,7 +442,6 @@ pub enum WorkerTask { |
| 340 | 442 | JoinRoom(String, ClientReply<IambResult<OwnedRoomId>>), |
| 341 | 443 | Members(OwnedRoomId, ClientReply<IambResult<Vec<RoomMember>>>), |
| 342 | 444 | SpaceMembers(OwnedRoomId, ClientReply<IambResult<Vec<OwnedRoomId>>>), |
| 343 | | Spaces(ClientReply<Vec<(MatrixRoom, DisplayName)>>), |
| 344 | 445 | TypingNotice(OwnedRoomId), |
| 345 | 446 | Verify(VerifyAction, SasVerification, ClientReply<IambResult<EditInfo>>), |
| 346 | 447 | VerifyRequest(OwnedUserId, ClientReply<IambResult<EditInfo>>), |
| @@ -349,14 +450,6 @@ pub enum WorkerTask { |
| 349 | 450 | impl Debug for WorkerTask { |
| 350 | 451 | fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { |
| 351 | 452 | match self { |
| 352 | | WorkerTask::ActiveRooms(_) => { |
| 353 | | f.debug_tuple("WorkerTask::ActiveRooms").field(&format_args!("_")).finish() |
| 354 | | }, |
| 355 | | WorkerTask::DirectMessages(_) => { |
| 356 | | f.debug_tuple("WorkerTask::DirectMessages") |
| 357 | | .field(&format_args!("_")) |
| 358 | | .finish() |
| 359 | | }, |
| 360 | 453 | WorkerTask::Init(_, _) => { |
| 361 | 454 | f.debug_tuple("WorkerTask::Init") |
| 362 | 455 | .field(&format_args!("_")) |
| @@ -396,9 +489,6 @@ impl Debug for WorkerTask { |
| 396 | 489 | .field(&format_args!("_")) |
| 397 | 490 | .finish() |
| 398 | 491 | }, |
| 399 | | WorkerTask::Spaces(_) => { |
| 400 | | f.debug_tuple("WorkerTask::Spaces").field(&format_args!("_")).finish() |
| 401 | | }, |
| 402 | 492 | WorkerTask::TypingNotice(room_id) => { |
| 403 | 493 | f.debug_tuple("WorkerTask::TypingNotice").field(room_id).finish() |
| 404 | 494 | }, |
| @@ -442,14 +532,6 @@ impl Requester { |
| 442 | 532 | return response.recv(); |
| 443 | 533 | } |
| 444 | 534 | |
| 445 | | pub fn direct_messages(&self) -> Vec<FetchedRoom> { |
| 446 | | let (reply, response) = oneshot(); |
| 447 | | |
| 448 | | self.tx.send(WorkerTask::DirectMessages(reply)).unwrap(); |
| 449 | | |
| 450 | | return response.recv(); |
| 451 | | } |
| 452 | | |
| 453 | 535 | pub fn get_inviter(&self, invite: Invited) -> IambResult<Option<RoomMember>> { |
| 454 | 536 | let (reply, response) = oneshot(); |
| 455 | 537 | |
| @@ -474,14 +556,6 @@ impl Requester { |
| 474 | 556 | return response.recv(); |
| 475 | 557 | } |
| 476 | 558 | |
| 477 | | pub fn active_rooms(&self) -> Vec<FetchedRoom> { |
| 478 | | let (reply, response) = oneshot(); |
| 479 | | |
| 480 | | self.tx.send(WorkerTask::ActiveRooms(reply)).unwrap(); |
| 481 | | |
| 482 | | return response.recv(); |
| 483 | | } |
| 484 | | |
| 485 | 559 | pub fn members(&self, room_id: OwnedRoomId) -> IambResult<Vec<RoomMember>> { |
| 486 | 560 | let (reply, response) = oneshot(); |
| 487 | 561 | |
| @@ -498,14 +572,6 @@ impl Requester { |
| 498 | 572 | return response.recv(); |
| 499 | 573 | } |
| 500 | 574 | |
| 501 | | pub fn spaces(&self) -> Vec<(MatrixRoom, DisplayName)> { |
| 502 | | let (reply, response) = oneshot(); |
| 503 | | |
| 504 | | self.tx.send(WorkerTask::Spaces(reply)).unwrap(); |
| 505 | | |
| 506 | | return response.recv(); |
| 507 | | } |
| 508 | | |
| 509 | 575 | pub fn typing_notice(&self, room_id: OwnedRoomId) { |
| 510 | 576 | self.tx.send(WorkerTask::TypingNotice(room_id)).unwrap(); |
| 511 | 577 | } |
| @@ -532,7 +598,6 @@ pub struct ClientWorker { |
| 532 | 598 | settings: ApplicationSettings, |
| 533 | 599 | client: Client, |
| 534 | 600 | load_handle: Option<JoinHandle<()>>, |
| 535 | | rcpt_handle: Option<JoinHandle<()>>, |
| 536 | 601 | sync_handle: Option<JoinHandle<()>>, |
| 537 | 602 | } |
| 538 | 603 | |
| @@ -571,7 +636,6 @@ impl ClientWorker { |
| 571 | 636 | settings, |
| 572 | 637 | client: client.clone(), |
| 573 | 638 | load_handle: None, |
| 574 | | rcpt_handle: None, |
| 575 | 639 | sync_handle: None, |
| 576 | 640 | }; |
| 577 | 641 | |
| @@ -597,18 +661,10 @@ impl ClientWorker { |
| 597 | 661 | if let Some(handle) = self.sync_handle.take() { |
| 598 | 662 | handle.abort(); |
| 599 | 663 | } |
| 600 | | |
| 601 | | if let Some(handle) = self.rcpt_handle.take() { |
| 602 | | handle.abort(); |
| 603 | | } |
| 604 | 664 | } |
| 605 | 665 | |
| 606 | 666 | async fn run(&mut self, task: WorkerTask) { |
| 607 | 667 | match task { |
| 608 | | WorkerTask::DirectMessages(reply) => { |
| 609 | | assert!(self.initialized); |
| 610 | | reply.send(self.direct_messages().await); |
| 611 | | }, |
| 612 | 668 | WorkerTask::Init(store, reply) => { |
| 613 | 669 | assert_eq!(self.initialized, false); |
| 614 | 670 | self.init(store).await; |
| @@ -626,10 +682,6 @@ impl ClientWorker { |
| 626 | 682 | assert!(self.initialized); |
| 627 | 683 | reply.send(self.get_room(room_id).await); |
| 628 | 684 | }, |
| 629 | | WorkerTask::ActiveRooms(reply) => { |
| 630 | | assert!(self.initialized); |
| 631 | | reply.send(self.active_rooms().await); |
| 632 | | }, |
| 633 | 685 | WorkerTask::Login(style, reply) => { |
| 634 | 686 | assert!(self.initialized); |
| 635 | 687 | reply.send(self.login_and_sync(style).await); |
| @@ -642,10 +694,6 @@ impl ClientWorker { |
| 642 | 694 | assert!(self.initialized); |
| 643 | 695 | reply.send(self.space_members(space).await); |
| 644 | 696 | }, |
| 645 | | WorkerTask::Spaces(reply) => { |
| 646 | | assert!(self.initialized); |
| 647 | | reply.send(self.spaces().await); |
| 648 | | }, |
| 649 | 697 | WorkerTask::TypingNotice(room_id) => { |
| 650 | 698 | assert!(self.initialized); |
| 651 | 699 | self.typing_notice(room_id).await; |
| @@ -903,50 +951,14 @@ impl ClientWorker { |
| 903 | 951 | }, |
| 904 | 952 | ); |
| 905 | 953 | |
| 906 | | self.rcpt_handle = tokio::spawn({ |
| 907 | | let store = store.clone(); |
| 908 | | let client = self.client.clone(); |
| 909 | | let mut sent = HashMap::<OwnedRoomId, OwnedEventId>::default(); |
| 910 | | |
| 911 | | async move { |
| 912 | | // Update the displayed read receipts every 5 seconds. |
| 913 | | let mut interval = tokio::time::interval(Duration::from_secs(5)); |
| 914 | | |
| 915 | | loop { |
| 916 | | interval.tick().await; |
| 917 | | |
| 918 | | let receipts = update_receipts(&client).await; |
| 919 | | let read = store.lock().await.application.set_receipts(receipts).await; |
| 920 | | |
| 921 | | for (room_id, read_till) in read.into_iter() { |
| 922 | | if let Some(read_sent) = sent.get(&room_id) { |
| 923 | | if read_sent == &read_till { |
| 924 | | // Skip unchanged receipts. |
| 925 | | continue; |
| 926 | | } |
| 927 | | } |
| 928 | | |
| 929 | | if let Some(room) = client.get_joined_room(&room_id) { |
| 930 | | if room.read_receipt(&read_till).await.is_ok() { |
| 931 | | sent.insert(room_id, read_till); |
| 932 | | } |
| 933 | | } |
| 934 | | } |
| 935 | | } |
| 936 | | } |
| 937 | | }) |
| 938 | | .into(); |
| 939 | | |
| 940 | 954 | self.load_handle = tokio::spawn({ |
| 941 | 955 | let client = self.client.clone(); |
| 942 | 956 | |
| 943 | 957 | async move { |
| 944 | | // Load older messages every 2 seconds. |
| 945 | | let mut interval = tokio::time::interval(Duration::from_secs(2)); |
| 946 | | loop { |
| 947 | | interval.tick().await; |
| 948 | | load_older(&client, &store).await; |
| 949 | | } |
| 958 | let load = load_older_forever(&client, &store); |
| 959 | let rcpt = refresh_receipts_forever(&client, &store); |
| 960 | let room = refresh_rooms_forever(&client, &store); |
| 961 | let ((), (), ()) = tokio::join!(load, rcpt, room); |
| 950 | 962 | } |
| 951 | 963 | }) |
| 952 | 964 | .into(); |
| @@ -1002,31 +1014,30 @@ impl ClientWorker { |
| 1002 | 1014 | Ok(Some(InfoMessage::from("Successfully logged in!"))) |
| 1003 | 1015 | } |
| 1004 | 1016 | |
| 1005 | | async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<FetchedRoom> { |
| 1006 | | for (room, name, tags) in self.direct_messages().await { |
| 1017 | async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<OwnedRoomId> { |
| 1018 | for room in self.client.rooms() { |
| 1019 | if !room.is_direct() { |
| 1020 | continue; |
| 1021 | } |
| 1022 | |
| 1007 | 1023 | if room.get_member(user.as_ref()).await.map_err(IambError::from)?.is_some() { |
| 1008 | | return Ok((room, name, tags)); |
| 1024 | return Ok(room.room_id().to_owned()); |
| 1009 | 1025 | } |
| 1010 | 1026 | } |
| 1011 | 1027 | |
| 1012 | 1028 | let rt = CreateRoomType::Direct(user.clone()); |
| 1013 | 1029 | let flags = CreateRoomFlags::ENCRYPTED; |
| 1014 | 1030 | |
| 1015 | | match create_room(&self.client, None, rt, flags).await { |
| 1016 | | Ok(room_id) => self.get_room(room_id).await, |
| 1017 | | Err(e) => { |
| 1018 | | error!( |
| 1019 | | user_id = user.as_str(), |
| 1020 | | err = e.to_string(), |
| 1021 | | "Failed to create direct message room" |
| 1022 | | ); |
| 1023 | | |
| 1024 | | let msg = format!("Could not open a room with {user}"); |
| 1025 | | let err = UIError::Failure(msg); |
| 1031 | create_room(&self.client, None, rt, flags).await.map_err(|e| { |
| 1032 | error!( |
| 1033 | user_id = user.as_str(), |
| 1034 | err = e.to_string(), |
| 1035 | "Failed to create direct message room" |
| 1036 | ); |
| 1026 | 1037 | |
| 1027 | | Err(err) |
| 1028 | | }, |
| 1029 | | } |
| 1038 | let msg = format!("Could not open a room with {user}"); |
| 1039 | UIError::Failure(msg) |
| 1040 | }) |
| 1030 | 1041 | } |
| 1031 | 1042 | |
| 1032 | 1043 | async fn get_inviter(&mut self, invited: Invited) -> IambResult<Option<RoomMember>> { |
| @@ -1058,9 +1069,7 @@ impl ClientWorker { |
| 1058 | 1069 | }, |
| 1059 | 1070 | } |
| 1060 | 1071 | } else if let Ok(user) = OwnedUserId::try_from(name.as_str()) { |
| 1061 | | let room = self.direct_message(user).await?.0; |
| 1062 | | |
| 1063 | | return Ok(room.room_id().to_owned()); |
| 1072 | self.direct_message(user).await |
| 1064 | 1073 | } else { |
| 1065 | 1074 | let msg = format!("{:?} is not a valid room or user name", name.as_str()); |
| 1066 | 1075 | let err = UIError::Failure(msg); |
| @@ -1069,62 +1078,6 @@ impl ClientWorker { |
| 1069 | 1078 | } |
| 1070 | 1079 | } |
| 1071 | 1080 | |
| 1072 | | async fn direct_messages(&self) -> Vec<FetchedRoom> { |
| 1073 | | let mut rooms = vec![]; |
| 1074 | | |
| 1075 | | for room in self.client.invited_rooms().into_iter() { |
| 1076 | | if !room.is_direct() { |
| 1077 | | continue; |
| 1078 | | } |
| 1079 | | |
| 1080 | | let name = room.display_name().await.unwrap_or(DisplayName::Empty); |
| 1081 | | let tags = room.tags().await.unwrap_or_default(); |
| 1082 | | |
| 1083 | | rooms.push((room.into(), name, tags)); |
| 1084 | | } |
| 1085 | | |
| 1086 | | for room in self.client.joined_rooms().into_iter() { |
| 1087 | | if !room.is_direct() { |
| 1088 | | continue; |
| 1089 | | } |
| 1090 | | |
| 1091 | | let name = room.display_name().await.unwrap_or(DisplayName::Empty); |
| 1092 | | let tags = room.tags().await.unwrap_or_default(); |
| 1093 | | |
| 1094 | | rooms.push((room.into(), name, tags)); |
| 1095 | | } |
| 1096 | | |
| 1097 | | return rooms; |
| 1098 | | } |
| 1099 | | |
| 1100 | | async fn active_rooms(&self) -> Vec<FetchedRoom> { |
| 1101 | | let mut rooms = vec![]; |
| 1102 | | |
| 1103 | | for room in self.client.invited_rooms().into_iter() { |
| 1104 | | if room.is_space() || room.is_direct() { |
| 1105 | | continue; |
| 1106 | | } |
| 1107 | | |
| 1108 | | let name = room.display_name().await.unwrap_or(DisplayName::Empty); |
| 1109 | | let tags = room.tags().await.unwrap_or_default(); |
| 1110 | | |
| 1111 | | rooms.push((room.into(), name, tags)); |
| 1112 | | } |
| 1113 | | |
| 1114 | | for room in self.client.joined_rooms().into_iter() { |
| 1115 | | if room.is_space() || room.is_direct() { |
| 1116 | | continue; |
| 1117 | | } |
| 1118 | | |
| 1119 | | let name = room.display_name().await.unwrap_or(DisplayName::Empty); |
| 1120 | | let tags = room.tags().await.unwrap_or_default(); |
| 1121 | | |
| 1122 | | rooms.push((room.into(), name, tags)); |
| 1123 | | } |
| 1124 | | |
| 1125 | | return rooms; |
| 1126 | | } |
| 1127 | | |
| 1128 | 1081 | async fn members(&mut self, room_id: OwnedRoomId) -> IambResult<Vec<RoomMember>> { |
| 1129 | 1082 | if let Some(room) = self.client.get_room(room_id.as_ref()) { |
| 1130 | 1083 | Ok(room.active_members().await.map_err(IambError::from)?) |
| @@ -1145,32 +1098,6 @@ impl ClientWorker { |
| 1145 | 1098 | Ok(rooms) |
| 1146 | 1099 | } |
| 1147 | 1100 | |
| 1148 | | async fn spaces(&self) -> Vec<(MatrixRoom, DisplayName)> { |
| 1149 | | let mut spaces = vec![]; |
| 1150 | | |
| 1151 | | for room in self.client.invited_rooms().into_iter() { |
| 1152 | | if !room.is_space() { |
| 1153 | | continue; |
| 1154 | | } |
| 1155 | | |
| 1156 | | let name = room.display_name().await.unwrap_or(DisplayName::Empty); |
| 1157 | | |
| 1158 | | spaces.push((room.into(), name)); |
| 1159 | | } |
| 1160 | | |
| 1161 | | for room in self.client.joined_rooms().into_iter() { |
| 1162 | | if !room.is_space() { |
| 1163 | | continue; |
| 1164 | | } |
| 1165 | | |
| 1166 | | let name = room.display_name().await.unwrap_or(DisplayName::Empty); |
| 1167 | | |
| 1168 | | spaces.push((room.into(), name)); |
| 1169 | | } |
| 1170 | | |
| 1171 | | return spaces; |
| 1172 | | } |
| 1173 | | |
| 1174 | 1101 | async fn typing_notice(&mut self, room_id: OwnedRoomId) { |
| 1175 | 1102 | if let Some(room) = self.client.get_joined_room(room_id.as_ref()) { |
| 1176 | 1103 | let _ = room.typing_notice(true).await; |