message: feature flag message queue storage in Room struct

master
Devin R 2020-05-06 21:17:21 -04:00
parent c90eb6a938
commit 9939efe0e8
5 changed files with 48 additions and 1 deletions

View File

@ -12,6 +12,7 @@ version = "0.1.0"
[features]
default = ["encryption", "sqlite-cryptostore"]
messages = []
encryption = ["matrix-sdk-crypto"]
sqlite-cryptostore = ["matrix-sdk-crypto/sqlite-cryptostore"]

View File

@ -1,3 +1,8 @@
//! A queue that holds at most ten of the most recent messages.
//!
//! The `Room` struct optionally holds a `MessageQueue` if the "messages"
//! feature is enabled.
use std::cmp::Ordering;
use std::ops::Deref;
use std::vec::IntoIter;

View File

@ -1,4 +1,6 @@
mod event_deser;
#[cfg(feature = "messages")]
#[cfg_attr(docsrs, doc(cfg(feature = "messages")))]
mod message;
mod room;
mod room_member;

View File

@ -16,6 +16,7 @@
use std::collections::{BTreeMap, HashMap};
use std::convert::TryFrom;
#[cfg(feature = "messages")]
use super::message::MessageQueue;
use super::RoomMember;
@ -27,12 +28,15 @@ use crate::events::room::{
canonical_alias::CanonicalAliasEvent,
encryption::EncryptionEvent,
member::{MemberEvent, MembershipChange},
message::MessageEvent,
name::NameEvent,
power_levels::{NotificationPowerLevels, PowerLevelsEvent, PowerLevelsEventContent},
tombstone::TombstoneEvent,
};
use crate::events::EventType;
#[cfg(feature = "messages")]
use crate::events::room::message::MessageEvent;
use crate::identifiers::{RoomAliasId, RoomId, UserId};
use crate::js_int::{Int, UInt};
@ -114,6 +118,8 @@ pub struct Room {
///
/// This is helpful when using a `StateStore` to avoid multiple requests
/// to the server for messages.
#[cfg(feature = "messages")]
#[cfg_attr(docsrs, doc(cfg(feature = "messages")))]
#[serde(with = "super::message::ser_deser")]
pub messages: MessageQueue,
/// A list of users that are currently typing.
@ -219,6 +225,7 @@ impl Room {
own_user_id: own_user_id.clone(),
creator: None,
members: HashMap::new(),
#[cfg(feature = "messages")]
messages: MessageQueue::new(),
typing_users: Vec::new(),
power_levels: None,
@ -336,6 +343,8 @@ impl Room {
/// Handle a room.message event and update the `MessageQueue` if necessary.
///
/// Returns true if `MessageQueue` was added to.
#[cfg(feature = "messages")]
#[cfg_attr(docsrs, doc(cfg(feature = "messages")))]
pub fn handle_message(&mut self, event: &MessageEvent) -> bool {
self.messages.push(event.clone())
}
@ -425,6 +434,7 @@ impl Room {
RoomEvent::RoomPowerLevels(power) => self.handle_power_level(power),
RoomEvent::RoomTombstone(tomb) => self.handle_tombstone(tomb),
RoomEvent::RoomEncryption(encrypt) => self.handle_encryption_event(encrypt),
#[cfg(feature = "messages")]
RoomEvent::RoomMessage(msg) => self.handle_message(msg),
_ => false,
}

View File

@ -107,6 +107,35 @@ mod test {
let mut joined_rooms = HashMap::new();
joined_rooms.insert(id, room);
#[cfg(not(feature = "messages"))]
assert_eq!(
r#"{
"!roomid:example.com": {
"room_id": "!roomid:example.com",
"room_name": {
"name": null,
"canonical_alias": null,
"aliases": [],
"heroes": [],
"joined_member_count": null,
"invited_member_count": null
},
"own_user_id": "@example:example.com",
"creator": null,
"members": {},
"typing_users": [],
"power_levels": null,
"encrypted": false,
"unread_highlight": null,
"unread_notifications": null,
"tombstone": null
}
}"#,
serde_json::to_string_pretty(&joined_rooms).unwrap()
);
#[cfg(feature = "messages")]
assert_eq!(
r#"{
"!roomid:example.com": {