Commit afcffbe3d3

afcffbe3d3a918ce29d334779469a40b439978ed

parent: 155878d7b3

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2021-01-02 17:49 UTC

room: Smarter handling for prev_batch

Now that we restore our rooms again we need to be a bit smarter about
our prev_batches. If our buffer is empty we'll need to fetch events in
the froward direction the first time, on the other hand if we receive
a sync response and our buffer is empty we need to set it to the new
prev_batch token.

Layout: unified · split

src/connection.rs +7 −3
@@ -43,7 +43,7 @@ use matrix_sdk::{
4343
4444use weechat::{Task, Weechat};
4545
46use crate::server::{InnerServer, MatrixServer};
46use crate::{room::PrevBatch, server::{InnerServer, MatrixServer}};
4747
4848const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(30);
4949pub const TYPING_NOTICE_TIMEOUT: Duration = Duration::from_secs(4);
@@ -203,12 +203,16 @@ impl Connection {
203203 pub async fn room_messages(
204204 &self,
205205 room_id: RoomId,
206 prev_batch: String,
206 prev_batch: PrevBatch,
207207 ) -> MatrixResult<MessagesResponse> {
208208 let client = self.client.clone();
209209
210210 self.spawn(async move {
211 let request = MessagesRequest::backward(&room_id, &prev_batch);
211 let request = match &prev_batch {
212 PrevBatch::Backwards(t) => MessagesRequest::backward(&room_id, &t),
213 PrevBatch::Forward(t) => MessagesRequest::forward(&room_id, &t),
214 };
215
212216 client.room_messages(request).await
213217 })
214218 .await
src/room/mod.rs +36 −6
@@ -87,6 +87,12 @@ pub struct RoomHandle {
8787 buffer_handle: BufferHandle,
8888}
8989
90#[derive(Debug, Clone)]
91pub enum PrevBatch {
92 Forward(String),
93 Backwards(String),
94}
95
9096impl Deref for RoomHandle {
9197 type Target = MatrixRoom;
9298
@@ -150,7 +156,7 @@ pub struct MatrixRoom {
150156 typing_in_flight: Rc<Mutex<()>>,
151157
152158 messages_in_flight: IntMutex,
153 prev_batch: Rc<RefCell<Option<String>>>,
159 prev_batch: Rc<RefCell<Option<PrevBatch>>>,
154160
155161 outgoing_messages: MessageQueue,
156162
@@ -201,7 +207,9 @@ impl RoomHandle {
201207 typing_notice_time: Rc::new(RefCell::new(None)),
202208 typing_in_flight: Rc::new(Mutex::new(())),
203209 config,
204 prev_batch: Rc::new(RefCell::new(room.last_prev_batch())),
210 prev_batch: Rc::new(RefCell::new(
211 room.last_prev_batch().map(PrevBatch::Backwards),
212 )),
205213 own_user_id: Rc::new(own_user_id.to_owned()),
206214 members: members.clone(),
207215 buffer: members.buffer,
@@ -286,6 +294,7 @@ impl RoomHandle {
286294 let room_clone = room.clone();
287295 let room_id = room.room_id();
288296 let own_user_id = room.own_user_id();
297 let prev_batch = room.last_prev_batch();
289298
290299 let room_buffer = Self::new(
291300 server_name,
@@ -306,6 +315,9 @@ impl RoomHandle {
306315 room_buffer.members.add_or_modify(&user_id).await;
307316 }
308317
318 *room_buffer.prev_batch.borrow_mut() =
319 prev_batch.map(PrevBatch::Forward);
320
309321 room_buffer.update_buffer_name();
310322
311323 room_buffer
@@ -739,11 +751,18 @@ impl MatrixRoom {
739751 self.handle_room_event(&event).await;
740752 }
741753
742 if r.chunk.is_empty() {
743 *self.prev_batch.borrow_mut() = None;
744 } else {
745 *self.prev_batch.borrow_mut() = r.end;
754 let mut prev_batch = self.prev_batch.borrow_mut();
755
756 if let Some(PrevBatch::Forward(t)) = prev_batch.as_ref() {
757 *prev_batch = Some(PrevBatch::Backwards(t.to_owned()));
746758 self.sort_messages();
759 } else {
760 if r.chunk.is_empty() {
761 *prev_batch = None;
762 } else {
763 *prev_batch = r.end.map(PrevBatch::Backwards);
764 self.sort_messages();
765 }
747766 }
748767 }
749768 }
@@ -916,7 +935,18 @@ impl MatrixRoom {
916935 .await
917936 }
918937
938 fn set_prev_batch(&self) {
939 if let Ok(buffer) = self.buffer_handle().upgrade() {
940 if buffer.num_lines() == 0 {
941 *self.prev_batch.borrow_mut() =
942 self.room.last_prev_batch().map(PrevBatch::Backwards);
943 }
944 }
945 }
946
919947 pub async fn handle_sync_room_event(&self, event: AnySyncRoomEvent) {
948 self.set_prev_batch();
949
920950 match &event {
921951 AnySyncRoomEvent::Message(message) => {
922952 self.handle_room_message(message).await