From 9939efe0e84b16b44ee46ae8e3867c22f88fff06 Mon Sep 17 00:00:00 2001 From: Devin R Date: Wed, 6 May 2020 21:17:21 -0400 Subject: [PATCH] message: feature flag message queue storage in Room struct --- matrix_sdk/Cargo.toml | 1 + matrix_sdk/src/models/message.rs | 5 +++++ matrix_sdk/src/models/mod.rs | 2 ++ matrix_sdk/src/models/room.rs | 12 +++++++++++- matrix_sdk/src/state/mod.rs | 29 +++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/matrix_sdk/Cargo.toml b/matrix_sdk/Cargo.toml index a21cde7c..92ea298b 100644 --- a/matrix_sdk/Cargo.toml +++ b/matrix_sdk/Cargo.toml @@ -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"] diff --git a/matrix_sdk/src/models/message.rs b/matrix_sdk/src/models/message.rs index d1526662..2aed0ec5 100644 --- a/matrix_sdk/src/models/message.rs +++ b/matrix_sdk/src/models/message.rs @@ -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; diff --git a/matrix_sdk/src/models/mod.rs b/matrix_sdk/src/models/mod.rs index 41f4f461..6604d61d 100644 --- a/matrix_sdk/src/models/mod.rs +++ b/matrix_sdk/src/models/mod.rs @@ -1,4 +1,6 @@ mod event_deser; +#[cfg(feature = "messages")] +#[cfg_attr(docsrs, doc(cfg(feature = "messages")))] mod message; mod room; mod room_member; diff --git a/matrix_sdk/src/models/room.rs b/matrix_sdk/src/models/room.rs index 40e9924b..197030bf 100644 --- a/matrix_sdk/src/models/room.rs +++ b/matrix_sdk/src/models/room.rs @@ -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, } diff --git a/matrix_sdk/src/state/mod.rs b/matrix_sdk/src/state/mod.rs index 04b45f94..c4befd4d 100644 --- a/matrix_sdk/src/state/mod.rs +++ b/matrix_sdk/src/state/mod.rs @@ -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": {