From 79e661d1d98ca20c4cde5edcf45dceead0a2a150 Mon Sep 17 00:00:00 2001 From: Devin Ragotzy Date: Fri, 31 Jul 2020 09:15:18 -0400 Subject: [PATCH] sdk-base: Remove MessageWrapper and do not order messages in queue --- matrix_sdk/src/lib.rs | 2 +- matrix_sdk_base/src/client.rs | 4 +- matrix_sdk_base/src/lib.rs | 2 +- matrix_sdk_base/src/models/message.rs | 92 ++++++--------------------- matrix_sdk_base/src/models/mod.rs | 2 +- matrix_sdk_base/src/models/room.rs | 6 +- 6 files changed, 27 insertions(+), 81 deletions(-) diff --git a/matrix_sdk/src/lib.rs b/matrix_sdk/src/lib.rs index 9798c67e..9b72cd43 100644 --- a/matrix_sdk/src/lib.rs +++ b/matrix_sdk/src/lib.rs @@ -41,7 +41,7 @@ pub use matrix_sdk_base::Error as BaseError; pub use matrix_sdk_base::JsonStore; pub use matrix_sdk_base::{CustomOrRawEvent, EventEmitter, Room, Session, SyncRoom}; #[cfg(feature = "messages")] -pub use matrix_sdk_base::{MessageQueue, MessageWrapper, PossiblyRedactedExt}; +pub use matrix_sdk_base::{MessageQueue, PossiblyRedactedExt}; pub use matrix_sdk_base::{RoomState, StateStore}; pub use matrix_sdk_common::*; pub use reqwest::header::InvalidHeaderValue; diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index 375a6195..1f2bc2a2 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -2397,7 +2397,7 @@ mod test { let queue = &room.read().await.messages; if let crate::events::AnyPossiblyRedactedSyncMessageEvent::Redacted( crate::events::AnyRedactedSyncMessageEvent::RoomMessage(event), - ) = &queue.msgs[0].deref() + ) = &queue.msgs[0] { // this is the id from the message event in the sync response assert_eq!( @@ -2425,7 +2425,7 @@ mod test { let queue = &room.read().await.messages; if let crate::events::AnyPossiblyRedactedSyncMessageEvent::Redacted( crate::events::AnyRedactedSyncMessageEvent::RoomMessage(event), - ) = &queue.msgs[0].deref() + ) = &queue.msgs[0] { // this is the id from the message event in the sync response assert_eq!( diff --git a/matrix_sdk_base/src/lib.rs b/matrix_sdk_base/src/lib.rs index dc565c34..f7e07bd2 100644 --- a/matrix_sdk_base/src/lib.rs +++ b/matrix_sdk_base/src/lib.rs @@ -55,7 +55,7 @@ pub use matrix_sdk_crypto::{Device, TrustState}; #[cfg(feature = "messages")] #[cfg_attr(docsrs, doc(cfg(feature = "messages")))] -pub use models::{MessageQueue, MessageWrapper, PossiblyRedactedExt}; +pub use models::{MessageQueue, PossiblyRedactedExt}; #[cfg(not(target_arch = "wasm32"))] pub use state::JsonStore; diff --git a/matrix_sdk_base/src/models/message.rs b/matrix_sdk_base/src/models/message.rs index 4b361bb8..5b496766 100644 --- a/matrix_sdk_base/src/models/message.rs +++ b/matrix_sdk_base/src/models/message.rs @@ -3,12 +3,7 @@ //! The `Room` struct optionally holds a `MessageQueue` if the "messages" //! feature is enabled. -use std::{ - cmp::Ordering, - ops::{Deref, DerefMut}, - time::SystemTime, - vec::IntoIter, -}; +use std::{time::SystemTime, vec::IntoIter}; use matrix_sdk_common::identifiers::{EventId, UserId}; use serde::{de, ser, Serialize}; @@ -59,60 +54,7 @@ const MESSAGE_QUEUE_CAP: usize = 35; /// A queue that holds the 35 most recent messages received from the server. #[derive(Clone, Debug, Default)] pub struct MessageQueue { - pub(crate) msgs: Vec, -} - -/// A wrapper for `ruma_events::AnyPossiblyRedactedSyncMessageEvent` that allows -/// implementation of Eq, Ord and the Partial versions of the traits. -/// -/// `MessageWrapper` also implements Deref and DerefMut so accessing the events -/// contents are simplified. -#[derive(Clone, Debug, Serialize)] -pub struct MessageWrapper(pub AnyPossiblyRedactedSyncMessageEvent); - -impl Deref for MessageWrapper { - type Target = AnyPossiblyRedactedSyncMessageEvent; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl DerefMut for MessageWrapper { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} - -impl PartialEq for MessageWrapper { - fn eq(&self, other: &MessageWrapper) -> bool { - self.0.event_id() == other.0.event_id() - } -} - -impl Eq for MessageWrapper {} - -impl PartialOrd for MessageWrapper { - fn partial_cmp(&self, other: &MessageWrapper) -> Option { - Some(self.0.origin_server_ts().cmp(&other.0.origin_server_ts())) - } -} - -impl Ord for MessageWrapper { - fn cmp(&self, other: &MessageWrapper) -> Ordering { - self.partial_cmp(other).unwrap_or(Ordering::Equal) - } -} - -impl PartialEq for MessageQueue { - fn eq(&self, other: &MessageQueue) -> bool { - self.msgs.len() == other.msgs.len() - && self - .msgs - .iter() - .zip(other.msgs.iter()) - .all(|(msg_a, msg_b)| msg_a.event_id() == msg_b.event_id()) - } + pub(crate) msgs: Vec, } impl MessageQueue { @@ -134,34 +76,38 @@ impl MessageQueue { } } - let message = MessageWrapper(msg); - match self.msgs.binary_search_by(|m| m.cmp(&message)) { - Ok(pos) => { - if self.msgs[pos] != message { - self.msgs.insert(pos, message) - } - } - Err(pos) => self.msgs.insert(pos, message), + if self.msgs.iter().all(|old| old.event_id() != msg.event_id()) { + self.msgs.push(msg) } + if self.msgs.len() > MESSAGE_QUEUE_CAP { - self.msgs.remove(0); + self.msgs.pop(); } true } /// Iterate over the messages in the queue. - pub fn iter(&self) -> impl Iterator { + pub fn iter(&self) -> impl Iterator { self.msgs.iter() } /// Iterate over each message mutably. - pub fn iter_mut(&mut self) -> impl Iterator { + pub fn iter_mut(&mut self) -> impl Iterator { self.msgs.iter_mut() } } +impl PartialEq for MessageQueue { + fn eq(&self, other: &MessageQueue) -> bool { + self.msgs + .iter() + .zip(other.msgs.iter()) + .all(|(a, b)| a.event_id() == b.event_id()) + } +} + impl IntoIterator for MessageQueue { - type Item = MessageWrapper; + type Item = AnyPossiblyRedactedSyncMessageEvent; type IntoIter = IntoIter; fn into_iter(self) -> Self::IntoIter { @@ -190,7 +136,7 @@ pub(crate) mod ser_deser { let mut msgs = Vec::with_capacity(access.size_hint().unwrap_or(0)); while let Some(msg) = access.next_element::()? { - msgs.push(MessageWrapper(msg)); + msgs.push(msg); } Ok(MessageQueue { msgs }) diff --git a/matrix_sdk_base/src/models/mod.rs b/matrix_sdk_base/src/models/mod.rs index 20514724..bf2eefb0 100644 --- a/matrix_sdk_base/src/models/mod.rs +++ b/matrix_sdk_base/src/models/mod.rs @@ -6,6 +6,6 @@ mod room_member; #[cfg(feature = "messages")] #[cfg_attr(docsrs, doc(cfg(feature = "messages")))] -pub use message::{MessageQueue, MessageWrapper, PossiblyRedactedExt}; +pub use message::{MessageQueue, PossiblyRedactedExt}; pub use room::{Room, RoomName}; pub use room_member::RoomMember; diff --git a/matrix_sdk_base/src/models/room.rs b/matrix_sdk_base/src/models/room.rs index e0e194d4..8d9fb2d9 100644 --- a/matrix_sdk_base/src/models/room.rs +++ b/matrix_sdk_base/src/models/room.rs @@ -673,14 +673,14 @@ impl Room { use crate::identifiers::RoomVersionId; use crate::models::message::PossiblyRedactedExt; - if let Some(msg) = self + if let Some(mut msg) = self .messages .iter_mut() .find(|msg| &redacted_event.redacts == msg.event_id()) { match msg.deref_mut() { AnyPossiblyRedactedSyncMessageEvent::Regular(event) => { - msg.0 = AnyPossiblyRedactedSyncMessageEvent::Redacted( + *msg = AnyPossiblyRedactedSyncMessageEvent::Redacted( event .clone() .redact(redacted_event.clone(), RoomVersionId::Version6), @@ -1655,7 +1655,7 @@ mod test { let queue = &room.read().await.messages; if let crate::events::AnyPossiblyRedactedSyncMessageEvent::Redacted( crate::events::AnyRedactedSyncMessageEvent::RoomMessage(event), - ) = &queue.msgs[0].deref() + ) = &queue.msgs[0] { // this is the id from the message event in the sync response assert_eq!(