Commit 0bc4ff07b0

0bc4ff07b0f58f70317e42dfe5bac92663dc01d0

parent: 14fe916d94

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-03-20 23:01 UTC

Lazy load room state events on initial sync (#62)

Layout: unified · split

src/windows/mod.rs +23 −12
@@ -1,4 +1,5 @@
1use std::cmp::{Ord, Ordering, PartialOrd}; 1use std::cmp::{Ord, Ordering, PartialOrd};
2use std::time::{Duration, Instant};
2 3
3use matrix_sdk::{ 4use matrix_sdk::{
4 encryption::verification::{format_emojis, SasVerification}, 5 encryption::verification::{format_emojis, SasVerification},
@@ -77,6 +78,8 @@ use self::{room::RoomState, welcome::WelcomeState};
77pub mod room; 78pub mod room;
78pub mod welcome; 79pub mod welcome;
79 80
81const MEMBER_FETCH_DEBOUNCE: Duration = Duration::from_secs(5);
82
80#[inline] 83#[inline]
81fn bold_style() -> Style { 84fn bold_style() -> Style {
82 Style::default().add_modifier(StyleModifier::BOLD) 85 Style::default().add_modifier(StyleModifier::BOLD)
@@ -211,7 +214,7 @@ macro_rules! delegate {
211 match $s { 214 match $s {
212 IambWindow::Room($id) => $e, 215 IambWindow::Room($id) => $e,
213 IambWindow::DirectList($id) => $e, 216 IambWindow::DirectList($id) => $e,
214 IambWindow::MemberList($id, _) => $e, 217 IambWindow::MemberList($id, _, _) => $e,
215 IambWindow::RoomList($id) => $e, 218 IambWindow::RoomList($id) => $e,
216 IambWindow::SpaceList($id) => $e, 219 IambWindow::SpaceList($id) => $e,
217 IambWindow::VerifyList($id) => $e, 220 IambWindow::VerifyList($id) => $e,
@@ -222,7 +225,7 @@ macro_rules! delegate {
222 225
223pub enum IambWindow { 226pub enum IambWindow {
224 DirectList(DirectListState), 227 DirectList(DirectListState),
225 MemberList(MemberListState, OwnedRoomId), 228 MemberList(MemberListState, OwnedRoomId, Option<Instant>),
226 Room(RoomState), 229 Room(RoomState),
227 VerifyList(VerifyListState), 230 VerifyList(VerifyListState),
228 RoomList(RoomListState), 231 RoomList(RoomListState),
@@ -392,10 +395,18 @@ impl WindowOps<IambInfo> for IambWindow {
392 .focus(focused) 395 .focus(focused)
393 .render(area, buf, state); 396 .render(area, buf, state);
394 }, 397 },
395 IambWindow::MemberList(state, room_id) => { 398 IambWindow::MemberList(state, room_id, last_fetch) => {
396 if let Ok(mems) = store.application.worker.members(room_id.clone()) { 399 let need_fetch = match last_fetch {
397 let items = mems.into_iter().map(MemberItem::new); 400 Some(i) => i.elapsed() >= MEMBER_FETCH_DEBOUNCE,
398 state.set(items.collect()); 401 None => true,
402 };
403
404 if need_fetch {
405 if let Ok(mems) = store.application.worker.members(room_id.clone()) {
406 let items = mems.into_iter().map(MemberItem::new);
407 state.set(items.collect());
408 *last_fetch = Some(Instant::now());
409 }
399 } 410 }
400 411
401 List::new(store) 412 List::new(store)
@@ -456,8 +467,8 @@ impl WindowOps<IambInfo> for IambWindow {
456 match self { 467 match self {
457 IambWindow::Room(w) => w.dup(store).into(), 468 IambWindow::Room(w) => w.dup(store).into(),
458 IambWindow::DirectList(w) => w.dup(store).into(), 469 IambWindow::DirectList(w) => w.dup(store).into(),
459 IambWindow::MemberList(w, room_id) => { 470 IambWindow::MemberList(w, room_id, last_fetch) => {
460 IambWindow::MemberList(w.dup(store), room_id.clone()) 471 IambWindow::MemberList(w.dup(store), room_id.clone(), *last_fetch)
461 }, 472 },
462 IambWindow::RoomList(w) => w.dup(store).into(), 473 IambWindow::RoomList(w) => w.dup(store).into(),
463 IambWindow::SpaceList(w) => w.dup(store).into(), 474 IambWindow::SpaceList(w) => w.dup(store).into(),
@@ -497,7 +508,7 @@ impl Window<IambInfo> for IambWindow {
497 match self { 508 match self {
498 IambWindow::Room(room) => IambId::Room(room.id().to_owned()), 509 IambWindow::Room(room) => IambId::Room(room.id().to_owned()),
499 IambWindow::DirectList(_) => IambId::DirectList, 510 IambWindow::DirectList(_) => IambId::DirectList,
500 IambWindow::MemberList(_, room_id) => IambId::MemberList(room_id.clone()), 511 IambWindow::MemberList(_, room_id, _) => IambId::MemberList(room_id.clone()),
501 IambWindow::RoomList(_) => IambId::RoomList, 512 IambWindow::RoomList(_) => IambId::RoomList,
502 IambWindow::SpaceList(_) => IambId::SpaceList, 513 IambWindow::SpaceList(_) => IambId::SpaceList,
503 IambWindow::VerifyList(_) => IambId::VerifyList, 514 IambWindow::VerifyList(_) => IambId::VerifyList,
@@ -518,7 +529,7 @@ impl Window<IambInfo> for IambWindow {
518 529
519 Spans::from(title) 530 Spans::from(title)
520 }, 531 },
521 IambWindow::MemberList(_, room_id) => { 532 IambWindow::MemberList(_, room_id, _) => {
522 let title = store.application.get_room_title(room_id.as_ref()); 533 let title = store.application.get_room_title(room_id.as_ref());
523 534
524 Spans(vec![bold_span("Room Members: "), title.into()]) 535 Spans(vec![bold_span("Room Members: "), title.into()])
@@ -535,7 +546,7 @@ impl Window<IambInfo> for IambWindow {
535 IambWindow::Welcome(_) => bold_spans("Welcome to iamb"), 546 IambWindow::Welcome(_) => bold_spans("Welcome to iamb"),
536 547
537 IambWindow::Room(w) => w.get_title(store), 548 IambWindow::Room(w) => w.get_title(store),
538 IambWindow::MemberList(_, room_id) => { 549 IambWindow::MemberList(_, room_id, _) => {
539 let title = store.application.get_room_title(room_id.as_ref()); 550 let title = store.application.get_room_title(room_id.as_ref());
540 551
541 Spans(vec![bold_span("Room Members: "), title.into()]) 552 Spans(vec![bold_span("Room Members: "), title.into()])
@@ -559,7 +570,7 @@ impl Window<IambInfo> for IambWindow {
559 IambId::MemberList(room_id) => { 570 IambId::MemberList(room_id) => {
560 let id = IambBufferId::MemberList(room_id.clone()); 571 let id = IambBufferId::MemberList(room_id.clone());
561 let list = MemberListState::new(id, vec![]); 572 let list = MemberListState::new(id, vec![]);
562 let win = IambWindow::MemberList(list, room_id); 573 let win = IambWindow::MemberList(list, room_id, None);
563 574
564 return Ok(win); 575 return Ok(win);
565 }, 576 },
src/worker.rs +15 −6
@@ -14,13 +14,14 @@ use tokio::task::JoinHandle;
14use tracing::{error, warn}; 14use tracing::{error, warn};
15 15
16use matrix_sdk::{ 16use matrix_sdk::{
17 config::{RequestConfig, StoreConfig, SyncSettings}, 17 config::{RequestConfig, SyncSettings},
18 encryption::verification::{SasVerification, Verification}, 18 encryption::verification::{SasVerification, Verification},
19 event_handler::Ctx, 19 event_handler::Ctx,
20 reqwest, 20 reqwest,
21 room::{Invited, Messages, MessagesOptions, Room as MatrixRoom, RoomMember}, 21 room::{Invited, Messages, MessagesOptions, Room as MatrixRoom, RoomMember},
22 ruma::{ 22 ruma::{
23 api::client::{ 23 api::client::{
24 filter::{FilterDefinition, LazyLoadOptions, RoomEventFilter, RoomFilter},
24 room::create_room::v3::{CreationContent, Request as CreateRoomRequest, RoomPreset}, 25 room::create_room::v3::{CreationContent, Request as CreateRoomRequest, RoomPreset},
25 room::Visibility, 26 room::Visibility,
26 space::get_hierarchy::v1::Request as SpaceHierarchyRequest, 27 space::get_hierarchy::v1::Request as SpaceHierarchyRequest,
@@ -557,7 +558,6 @@ impl ClientWorker {
557 let client = Client::builder() 558 let client = Client::builder()
558 .http_client(Arc::new(http)) 559 .http_client(Arc::new(http))
559 .homeserver_url(account.url.clone()) 560 .homeserver_url(account.url.clone())
560 .store_config(StoreConfig::default())
561 .sled_store(settings.matrix_dir.as_path(), None) 561 .sled_store(settings.matrix_dir.as_path(), None)
562 .expect("Failed to setup up sled store for Matrix SDK") 562 .expect("Failed to setup up sled store for Matrix SDK")
563 .request_config(req_config) 563 .request_config(req_config)
@@ -967,10 +967,19 @@ impl ClientWorker {
967 967
968 self.sync_handle = Some(handle); 968 self.sync_handle = Some(handle);
969 969
970 self.client 970 // Perform an initial, lazily-loaded sync.
971 .sync_once(SyncSettings::default()) 971 let mut room = RoomEventFilter::default();
972 .await 972 room.lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: false };
973 .map_err(IambError::from)?; 973
974 let mut room_ev = RoomFilter::default();
975 room_ev.state = room;
976
977 let mut filter = FilterDefinition::default();
978 filter.room = room_ev;
979
980 let settings = SyncSettings::new().filter(filter.into());
981
982 self.client.sync_once(settings).await.map_err(IambError::from)?;
974 983
975 Ok(Some(InfoMessage::from("Successfully logged in!"))) 984 Ok(Some(InfoMessage::from("Successfully logged in!")))
976 } 985 }