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 @@
11use std::cmp::{Ord, Ordering, PartialOrd};
2use std::time::{Duration, Instant};
23
34use matrix_sdk::{
45 encryption::verification::{format_emojis, SasVerification},
@@ -77,6 +78,8 @@ use self::{room::RoomState, welcome::WelcomeState};
7778pub mod room;
7879pub mod welcome;
7980
81const MEMBER_FETCH_DEBOUNCE: Duration = Duration::from_secs(5);
82
8083#[inline]
8184fn bold_style() -> Style {
8285 Style::default().add_modifier(StyleModifier::BOLD)
@@ -211,7 +214,7 @@ macro_rules! delegate {
211214 match $s {
212215 IambWindow::Room($id) => $e,
213216 IambWindow::DirectList($id) => $e,
214 IambWindow::MemberList($id, _) => $e,
217 IambWindow::MemberList($id, _, _) => $e,
215218 IambWindow::RoomList($id) => $e,
216219 IambWindow::SpaceList($id) => $e,
217220 IambWindow::VerifyList($id) => $e,
@@ -222,7 +225,7 @@ macro_rules! delegate {
222225
223226pub enum IambWindow {
224227 DirectList(DirectListState),
225 MemberList(MemberListState, OwnedRoomId),
228 MemberList(MemberListState, OwnedRoomId, Option<Instant>),
226229 Room(RoomState),
227230 VerifyList(VerifyListState),
228231 RoomList(RoomListState),
@@ -392,10 +395,18 @@ impl WindowOps<IambInfo> for IambWindow {
392395 .focus(focused)
393396 .render(area, buf, state);
394397 },
395 IambWindow::MemberList(state, room_id) => {
396 if let Ok(mems) = store.application.worker.members(room_id.clone()) {
397 let items = mems.into_iter().map(MemberItem::new);
398 state.set(items.collect());
398 IambWindow::MemberList(state, room_id, last_fetch) => {
399 let need_fetch = match last_fetch {
400 Some(i) => i.elapsed() >= MEMBER_FETCH_DEBOUNCE,
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 }
399410 }
400411
401412 List::new(store)
@@ -456,8 +467,8 @@ impl WindowOps<IambInfo> for IambWindow {
456467 match self {
457468 IambWindow::Room(w) => w.dup(store).into(),
458469 IambWindow::DirectList(w) => w.dup(store).into(),
459 IambWindow::MemberList(w, room_id) => {
460 IambWindow::MemberList(w.dup(store), room_id.clone())
470 IambWindow::MemberList(w, room_id, last_fetch) => {
471 IambWindow::MemberList(w.dup(store), room_id.clone(), *last_fetch)
461472 },
462473 IambWindow::RoomList(w) => w.dup(store).into(),
463474 IambWindow::SpaceList(w) => w.dup(store).into(),
@@ -497,7 +508,7 @@ impl Window<IambInfo> for IambWindow {
497508 match self {
498509 IambWindow::Room(room) => IambId::Room(room.id().to_owned()),
499510 IambWindow::DirectList(_) => IambId::DirectList,
500 IambWindow::MemberList(_, room_id) => IambId::MemberList(room_id.clone()),
511 IambWindow::MemberList(_, room_id, _) => IambId::MemberList(room_id.clone()),
501512 IambWindow::RoomList(_) => IambId::RoomList,
502513 IambWindow::SpaceList(_) => IambId::SpaceList,
503514 IambWindow::VerifyList(_) => IambId::VerifyList,
@@ -518,7 +529,7 @@ impl Window<IambInfo> for IambWindow {
518529
519530 Spans::from(title)
520531 },
521 IambWindow::MemberList(_, room_id) => {
532 IambWindow::MemberList(_, room_id, _) => {
522533 let title = store.application.get_room_title(room_id.as_ref());
523534
524535 Spans(vec![bold_span("Room Members: "), title.into()])
@@ -535,7 +546,7 @@ impl Window<IambInfo> for IambWindow {
535546 IambWindow::Welcome(_) => bold_spans("Welcome to iamb"),
536547
537548 IambWindow::Room(w) => w.get_title(store),
538 IambWindow::MemberList(_, room_id) => {
549 IambWindow::MemberList(_, room_id, _) => {
539550 let title = store.application.get_room_title(room_id.as_ref());
540551
541552 Spans(vec![bold_span("Room Members: "), title.into()])
@@ -559,7 +570,7 @@ impl Window<IambInfo> for IambWindow {
559570 IambId::MemberList(room_id) => {
560571 let id = IambBufferId::MemberList(room_id.clone());
561572 let list = MemberListState::new(id, vec![]);
562 let win = IambWindow::MemberList(list, room_id);
573 let win = IambWindow::MemberList(list, room_id, None);
563574
564575 return Ok(win);
565576 },
src/worker.rs +15 −6
@@ -14,13 +14,14 @@ use tokio::task::JoinHandle;
1414use tracing::{error, warn};
1515
1616use matrix_sdk::{
17 config::{RequestConfig, StoreConfig, SyncSettings},
17 config::{RequestConfig, SyncSettings},
1818 encryption::verification::{SasVerification, Verification},
1919 event_handler::Ctx,
2020 reqwest,
2121 room::{Invited, Messages, MessagesOptions, Room as MatrixRoom, RoomMember},
2222 ruma::{
2323 api::client::{
24 filter::{FilterDefinition, LazyLoadOptions, RoomEventFilter, RoomFilter},
2425 room::create_room::v3::{CreationContent, Request as CreateRoomRequest, RoomPreset},
2526 room::Visibility,
2627 space::get_hierarchy::v1::Request as SpaceHierarchyRequest,
@@ -557,7 +558,6 @@ impl ClientWorker {
557558 let client = Client::builder()
558559 .http_client(Arc::new(http))
559560 .homeserver_url(account.url.clone())
560 .store_config(StoreConfig::default())
561561 .sled_store(settings.matrix_dir.as_path(), None)
562562 .expect("Failed to setup up sled store for Matrix SDK")
563563 .request_config(req_config)
@@ -967,10 +967,19 @@ impl ClientWorker {
967967
968968 self.sync_handle = Some(handle);
969969
970 self.client
971 .sync_once(SyncSettings::default())
972 .await
973 .map_err(IambError::from)?;
970 // Perform an initial, lazily-loaded sync.
971 let mut room = RoomEventFilter::default();
972 room.lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: false };
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)?;
974983
975984 Ok(Some(InfoMessage::from("Successfully logged in!")))
976985 }