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

This commit is contained in:
Ulyssa 2023-03-20 16:01:02 -07:00
parent 14fe916d94
commit 0bc4ff07b0
No known key found for this signature in database
GPG key ID: 1B3965A3D18B9B64
2 changed files with 38 additions and 18 deletions

View file

@ -1,4 +1,5 @@
use std::cmp::{Ord, Ordering, PartialOrd}; use std::cmp::{Ord, Ordering, PartialOrd};
use std::time::{Duration, Instant};
use matrix_sdk::{ use matrix_sdk::{
encryption::verification::{format_emojis, SasVerification}, encryption::verification::{format_emojis, SasVerification},
@ -77,6 +78,8 @@ use self::{room::RoomState, welcome::WelcomeState};
pub mod room; pub mod room;
pub mod welcome; pub mod welcome;
const MEMBER_FETCH_DEBOUNCE: Duration = Duration::from_secs(5);
#[inline] #[inline]
fn bold_style() -> Style { fn bold_style() -> Style {
Style::default().add_modifier(StyleModifier::BOLD) Style::default().add_modifier(StyleModifier::BOLD)
@ -211,7 +214,7 @@ macro_rules! delegate {
match $s { match $s {
IambWindow::Room($id) => $e, IambWindow::Room($id) => $e,
IambWindow::DirectList($id) => $e, IambWindow::DirectList($id) => $e,
IambWindow::MemberList($id, _) => $e, IambWindow::MemberList($id, _, _) => $e,
IambWindow::RoomList($id) => $e, IambWindow::RoomList($id) => $e,
IambWindow::SpaceList($id) => $e, IambWindow::SpaceList($id) => $e,
IambWindow::VerifyList($id) => $e, IambWindow::VerifyList($id) => $e,
@ -222,7 +225,7 @@ macro_rules! delegate {
pub enum IambWindow { pub enum IambWindow {
DirectList(DirectListState), DirectList(DirectListState),
MemberList(MemberListState, OwnedRoomId), MemberList(MemberListState, OwnedRoomId, Option<Instant>),
Room(RoomState), Room(RoomState),
VerifyList(VerifyListState), VerifyList(VerifyListState),
RoomList(RoomListState), RoomList(RoomListState),
@ -392,10 +395,18 @@ impl WindowOps<IambInfo> for IambWindow {
.focus(focused) .focus(focused)
.render(area, buf, state); .render(area, buf, state);
}, },
IambWindow::MemberList(state, room_id) => { IambWindow::MemberList(state, room_id, last_fetch) => {
if let Ok(mems) = store.application.worker.members(room_id.clone()) { let need_fetch = match last_fetch {
let items = mems.into_iter().map(MemberItem::new); Some(i) => i.elapsed() >= MEMBER_FETCH_DEBOUNCE,
state.set(items.collect()); None => true,
};
if need_fetch {
if let Ok(mems) = store.application.worker.members(room_id.clone()) {
let items = mems.into_iter().map(MemberItem::new);
state.set(items.collect());
*last_fetch = Some(Instant::now());
}
} }
List::new(store) List::new(store)
@ -456,8 +467,8 @@ impl WindowOps<IambInfo> for IambWindow {
match self { match self {
IambWindow::Room(w) => w.dup(store).into(), IambWindow::Room(w) => w.dup(store).into(),
IambWindow::DirectList(w) => w.dup(store).into(), IambWindow::DirectList(w) => w.dup(store).into(),
IambWindow::MemberList(w, room_id) => { IambWindow::MemberList(w, room_id, last_fetch) => {
IambWindow::MemberList(w.dup(store), room_id.clone()) IambWindow::MemberList(w.dup(store), room_id.clone(), *last_fetch)
}, },
IambWindow::RoomList(w) => w.dup(store).into(), IambWindow::RoomList(w) => w.dup(store).into(),
IambWindow::SpaceList(w) => w.dup(store).into(), IambWindow::SpaceList(w) => w.dup(store).into(),
@ -497,7 +508,7 @@ impl Window<IambInfo> for IambWindow {
match self { match self {
IambWindow::Room(room) => IambId::Room(room.id().to_owned()), IambWindow::Room(room) => IambId::Room(room.id().to_owned()),
IambWindow::DirectList(_) => IambId::DirectList, IambWindow::DirectList(_) => IambId::DirectList,
IambWindow::MemberList(_, room_id) => IambId::MemberList(room_id.clone()), IambWindow::MemberList(_, room_id, _) => IambId::MemberList(room_id.clone()),
IambWindow::RoomList(_) => IambId::RoomList, IambWindow::RoomList(_) => IambId::RoomList,
IambWindow::SpaceList(_) => IambId::SpaceList, IambWindow::SpaceList(_) => IambId::SpaceList,
IambWindow::VerifyList(_) => IambId::VerifyList, IambWindow::VerifyList(_) => IambId::VerifyList,
@ -518,7 +529,7 @@ impl Window<IambInfo> for IambWindow {
Spans::from(title) Spans::from(title)
}, },
IambWindow::MemberList(_, room_id) => { IambWindow::MemberList(_, room_id, _) => {
let title = store.application.get_room_title(room_id.as_ref()); let title = store.application.get_room_title(room_id.as_ref());
Spans(vec![bold_span("Room Members: "), title.into()]) Spans(vec![bold_span("Room Members: "), title.into()])
@ -535,7 +546,7 @@ impl Window<IambInfo> for IambWindow {
IambWindow::Welcome(_) => bold_spans("Welcome to iamb"), IambWindow::Welcome(_) => bold_spans("Welcome to iamb"),
IambWindow::Room(w) => w.get_title(store), IambWindow::Room(w) => w.get_title(store),
IambWindow::MemberList(_, room_id) => { IambWindow::MemberList(_, room_id, _) => {
let title = store.application.get_room_title(room_id.as_ref()); let title = store.application.get_room_title(room_id.as_ref());
Spans(vec![bold_span("Room Members: "), title.into()]) Spans(vec![bold_span("Room Members: "), title.into()])
@ -559,7 +570,7 @@ impl Window<IambInfo> for IambWindow {
IambId::MemberList(room_id) => { IambId::MemberList(room_id) => {
let id = IambBufferId::MemberList(room_id.clone()); let id = IambBufferId::MemberList(room_id.clone());
let list = MemberListState::new(id, vec![]); let list = MemberListState::new(id, vec![]);
let win = IambWindow::MemberList(list, room_id); let win = IambWindow::MemberList(list, room_id, None);
return Ok(win); return Ok(win);
}, },

View file

@ -14,13 +14,14 @@ use tokio::task::JoinHandle;
use tracing::{error, warn}; use tracing::{error, warn};
use matrix_sdk::{ use matrix_sdk::{
config::{RequestConfig, StoreConfig, SyncSettings}, config::{RequestConfig, SyncSettings},
encryption::verification::{SasVerification, Verification}, encryption::verification::{SasVerification, Verification},
event_handler::Ctx, event_handler::Ctx,
reqwest, reqwest,
room::{Invited, Messages, MessagesOptions, Room as MatrixRoom, RoomMember}, room::{Invited, Messages, MessagesOptions, Room as MatrixRoom, RoomMember},
ruma::{ ruma::{
api::client::{ api::client::{
filter::{FilterDefinition, LazyLoadOptions, RoomEventFilter, RoomFilter},
room::create_room::v3::{CreationContent, Request as CreateRoomRequest, RoomPreset}, room::create_room::v3::{CreationContent, Request as CreateRoomRequest, RoomPreset},
room::Visibility, room::Visibility,
space::get_hierarchy::v1::Request as SpaceHierarchyRequest, space::get_hierarchy::v1::Request as SpaceHierarchyRequest,
@ -557,7 +558,6 @@ impl ClientWorker {
let client = Client::builder() let client = Client::builder()
.http_client(Arc::new(http)) .http_client(Arc::new(http))
.homeserver_url(account.url.clone()) .homeserver_url(account.url.clone())
.store_config(StoreConfig::default())
.sled_store(settings.matrix_dir.as_path(), None) .sled_store(settings.matrix_dir.as_path(), None)
.expect("Failed to setup up sled store for Matrix SDK") .expect("Failed to setup up sled store for Matrix SDK")
.request_config(req_config) .request_config(req_config)
@ -967,10 +967,19 @@ impl ClientWorker {
self.sync_handle = Some(handle); self.sync_handle = Some(handle);
self.client // Perform an initial, lazily-loaded sync.
.sync_once(SyncSettings::default()) let mut room = RoomEventFilter::default();
.await room.lazy_load_options = LazyLoadOptions::Enabled { include_redundant_members: false };
.map_err(IambError::from)?;
let mut room_ev = RoomFilter::default();
room_ev.state = room;
let mut filter = FilterDefinition::default();
filter.room = room_ev;
let settings = SyncSettings::new().filter(filter.into());
self.client.sync_once(settings).await.map_err(IambError::from)?;
Ok(Some(InfoMessage::from("Successfully logged in!"))) Ok(Some(InfoMessage::from("Successfully logged in!")))
} }