sdk-base: Remove MessageWrapper and do not order messages in queue

master
Devin Ragotzy 2020-07-31 09:15:18 -04:00
parent 3472614649
commit 79e661d1d9
6 changed files with 27 additions and 81 deletions

View File

@ -41,7 +41,7 @@ pub use matrix_sdk_base::Error as BaseError;
pub use matrix_sdk_base::JsonStore; pub use matrix_sdk_base::JsonStore;
pub use matrix_sdk_base::{CustomOrRawEvent, EventEmitter, Room, Session, SyncRoom}; pub use matrix_sdk_base::{CustomOrRawEvent, EventEmitter, Room, Session, SyncRoom};
#[cfg(feature = "messages")] #[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_base::{RoomState, StateStore};
pub use matrix_sdk_common::*; pub use matrix_sdk_common::*;
pub use reqwest::header::InvalidHeaderValue; pub use reqwest::header::InvalidHeaderValue;

View File

@ -2397,7 +2397,7 @@ mod test {
let queue = &room.read().await.messages; let queue = &room.read().await.messages;
if let crate::events::AnyPossiblyRedactedSyncMessageEvent::Redacted( if let crate::events::AnyPossiblyRedactedSyncMessageEvent::Redacted(
crate::events::AnyRedactedSyncMessageEvent::RoomMessage(event), crate::events::AnyRedactedSyncMessageEvent::RoomMessage(event),
) = &queue.msgs[0].deref() ) = &queue.msgs[0]
{ {
// this is the id from the message event in the sync response // this is the id from the message event in the sync response
assert_eq!( assert_eq!(
@ -2425,7 +2425,7 @@ mod test {
let queue = &room.read().await.messages; let queue = &room.read().await.messages;
if let crate::events::AnyPossiblyRedactedSyncMessageEvent::Redacted( if let crate::events::AnyPossiblyRedactedSyncMessageEvent::Redacted(
crate::events::AnyRedactedSyncMessageEvent::RoomMessage(event), crate::events::AnyRedactedSyncMessageEvent::RoomMessage(event),
) = &queue.msgs[0].deref() ) = &queue.msgs[0]
{ {
// this is the id from the message event in the sync response // this is the id from the message event in the sync response
assert_eq!( assert_eq!(

View File

@ -55,7 +55,7 @@ pub use matrix_sdk_crypto::{Device, TrustState};
#[cfg(feature = "messages")] #[cfg(feature = "messages")]
#[cfg_attr(docsrs, doc(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"))] #[cfg(not(target_arch = "wasm32"))]
pub use state::JsonStore; pub use state::JsonStore;

View File

@ -3,12 +3,7 @@
//! The `Room` struct optionally holds a `MessageQueue` if the "messages" //! The `Room` struct optionally holds a `MessageQueue` if the "messages"
//! feature is enabled. //! feature is enabled.
use std::{ use std::{time::SystemTime, vec::IntoIter};
cmp::Ordering,
ops::{Deref, DerefMut},
time::SystemTime,
vec::IntoIter,
};
use matrix_sdk_common::identifiers::{EventId, UserId}; use matrix_sdk_common::identifiers::{EventId, UserId};
use serde::{de, ser, Serialize}; 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. /// A queue that holds the 35 most recent messages received from the server.
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct MessageQueue { pub struct MessageQueue {
pub(crate) msgs: Vec<MessageWrapper>, pub(crate) msgs: Vec<AnyPossiblyRedactedSyncMessageEvent>,
}
/// 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<Ordering> {
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())
}
} }
impl MessageQueue { impl MessageQueue {
@ -134,34 +76,38 @@ impl MessageQueue {
} }
} }
let message = MessageWrapper(msg); if self.msgs.iter().all(|old| old.event_id() != msg.event_id()) {
match self.msgs.binary_search_by(|m| m.cmp(&message)) { self.msgs.push(msg)
Ok(pos) => {
if self.msgs[pos] != message {
self.msgs.insert(pos, message)
}
}
Err(pos) => self.msgs.insert(pos, message),
} }
if self.msgs.len() > MESSAGE_QUEUE_CAP { if self.msgs.len() > MESSAGE_QUEUE_CAP {
self.msgs.remove(0); self.msgs.pop();
} }
true true
} }
/// Iterate over the messages in the queue. /// Iterate over the messages in the queue.
pub fn iter(&self) -> impl Iterator<Item = &MessageWrapper> { pub fn iter(&self) -> impl Iterator<Item = &AnyPossiblyRedactedSyncMessageEvent> {
self.msgs.iter() self.msgs.iter()
} }
/// Iterate over each message mutably. /// Iterate over each message mutably.
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut MessageWrapper> { pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut AnyPossiblyRedactedSyncMessageEvent> {
self.msgs.iter_mut() 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 { impl IntoIterator for MessageQueue {
type Item = MessageWrapper; type Item = AnyPossiblyRedactedSyncMessageEvent;
type IntoIter = IntoIter<Self::Item>; type IntoIter = IntoIter<Self::Item>;
fn into_iter(self) -> Self::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)); let mut msgs = Vec::with_capacity(access.size_hint().unwrap_or(0));
while let Some(msg) = access.next_element::<AnyPossiblyRedactedSyncMessageEvent>()? { while let Some(msg) = access.next_element::<AnyPossiblyRedactedSyncMessageEvent>()? {
msgs.push(MessageWrapper(msg)); msgs.push(msg);
} }
Ok(MessageQueue { msgs }) Ok(MessageQueue { msgs })

View File

@ -6,6 +6,6 @@ mod room_member;
#[cfg(feature = "messages")] #[cfg(feature = "messages")]
#[cfg_attr(docsrs, doc(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::{Room, RoomName};
pub use room_member::RoomMember; pub use room_member::RoomMember;

View File

@ -673,14 +673,14 @@ impl Room {
use crate::identifiers::RoomVersionId; use crate::identifiers::RoomVersionId;
use crate::models::message::PossiblyRedactedExt; use crate::models::message::PossiblyRedactedExt;
if let Some(msg) = self if let Some(mut msg) = self
.messages .messages
.iter_mut() .iter_mut()
.find(|msg| &redacted_event.redacts == msg.event_id()) .find(|msg| &redacted_event.redacts == msg.event_id())
{ {
match msg.deref_mut() { match msg.deref_mut() {
AnyPossiblyRedactedSyncMessageEvent::Regular(event) => { AnyPossiblyRedactedSyncMessageEvent::Regular(event) => {
msg.0 = AnyPossiblyRedactedSyncMessageEvent::Redacted( *msg = AnyPossiblyRedactedSyncMessageEvent::Redacted(
event event
.clone() .clone()
.redact(redacted_event.clone(), RoomVersionId::Version6), .redact(redacted_event.clone(), RoomVersionId::Version6),
@ -1655,7 +1655,7 @@ mod test {
let queue = &room.read().await.messages; let queue = &room.read().await.messages;
if let crate::events::AnyPossiblyRedactedSyncMessageEvent::Redacted( if let crate::events::AnyPossiblyRedactedSyncMessageEvent::Redacted(
crate::events::AnyRedactedSyncMessageEvent::RoomMessage(event), crate::events::AnyRedactedSyncMessageEvent::RoomMessage(event),
) = &queue.msgs[0].deref() ) = &queue.msgs[0]
{ {
// this is the id from the message event in the sync response // this is the id from the message event in the sync response
assert_eq!( assert_eq!(