message: feature flag message queue storage in Room struct
parent
c90eb6a938
commit
9939efe0e8
|
@ -12,6 +12,7 @@ version = "0.1.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["encryption", "sqlite-cryptostore"]
|
default = ["encryption", "sqlite-cryptostore"]
|
||||||
|
messages = []
|
||||||
encryption = ["matrix-sdk-crypto"]
|
encryption = ["matrix-sdk-crypto"]
|
||||||
sqlite-cryptostore = ["matrix-sdk-crypto/sqlite-cryptostore"]
|
sqlite-cryptostore = ["matrix-sdk-crypto/sqlite-cryptostore"]
|
||||||
|
|
||||||
|
|
|
@ -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::cmp::Ordering;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::vec::IntoIter;
|
use std::vec::IntoIter;
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
mod event_deser;
|
mod event_deser;
|
||||||
|
#[cfg(feature = "messages")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "messages")))]
|
||||||
mod message;
|
mod message;
|
||||||
mod room;
|
mod room;
|
||||||
mod room_member;
|
mod room_member;
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
use std::collections::{BTreeMap, HashMap};
|
use std::collections::{BTreeMap, HashMap};
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
#[cfg(feature = "messages")]
|
||||||
use super::message::MessageQueue;
|
use super::message::MessageQueue;
|
||||||
use super::RoomMember;
|
use super::RoomMember;
|
||||||
|
|
||||||
|
@ -27,12 +28,15 @@ use crate::events::room::{
|
||||||
canonical_alias::CanonicalAliasEvent,
|
canonical_alias::CanonicalAliasEvent,
|
||||||
encryption::EncryptionEvent,
|
encryption::EncryptionEvent,
|
||||||
member::{MemberEvent, MembershipChange},
|
member::{MemberEvent, MembershipChange},
|
||||||
message::MessageEvent,
|
|
||||||
name::NameEvent,
|
name::NameEvent,
|
||||||
power_levels::{NotificationPowerLevels, PowerLevelsEvent, PowerLevelsEventContent},
|
power_levels::{NotificationPowerLevels, PowerLevelsEvent, PowerLevelsEventContent},
|
||||||
tombstone::TombstoneEvent,
|
tombstone::TombstoneEvent,
|
||||||
};
|
};
|
||||||
use crate::events::EventType;
|
use crate::events::EventType;
|
||||||
|
|
||||||
|
#[cfg(feature = "messages")]
|
||||||
|
use crate::events::room::message::MessageEvent;
|
||||||
|
|
||||||
use crate::identifiers::{RoomAliasId, RoomId, UserId};
|
use crate::identifiers::{RoomAliasId, RoomId, UserId};
|
||||||
|
|
||||||
use crate::js_int::{Int, UInt};
|
use crate::js_int::{Int, UInt};
|
||||||
|
@ -114,6 +118,8 @@ pub struct Room {
|
||||||
///
|
///
|
||||||
/// This is helpful when using a `StateStore` to avoid multiple requests
|
/// This is helpful when using a `StateStore` to avoid multiple requests
|
||||||
/// to the server for messages.
|
/// to the server for messages.
|
||||||
|
#[cfg(feature = "messages")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "messages")))]
|
||||||
#[serde(with = "super::message::ser_deser")]
|
#[serde(with = "super::message::ser_deser")]
|
||||||
pub messages: MessageQueue,
|
pub messages: MessageQueue,
|
||||||
/// A list of users that are currently typing.
|
/// A list of users that are currently typing.
|
||||||
|
@ -219,6 +225,7 @@ impl Room {
|
||||||
own_user_id: own_user_id.clone(),
|
own_user_id: own_user_id.clone(),
|
||||||
creator: None,
|
creator: None,
|
||||||
members: HashMap::new(),
|
members: HashMap::new(),
|
||||||
|
#[cfg(feature = "messages")]
|
||||||
messages: MessageQueue::new(),
|
messages: MessageQueue::new(),
|
||||||
typing_users: Vec::new(),
|
typing_users: Vec::new(),
|
||||||
power_levels: None,
|
power_levels: None,
|
||||||
|
@ -336,6 +343,8 @@ impl Room {
|
||||||
/// Handle a room.message event and update the `MessageQueue` if necessary.
|
/// Handle a room.message event and update the `MessageQueue` if necessary.
|
||||||
///
|
///
|
||||||
/// Returns true if `MessageQueue` was added to.
|
/// 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 {
|
pub fn handle_message(&mut self, event: &MessageEvent) -> bool {
|
||||||
self.messages.push(event.clone())
|
self.messages.push(event.clone())
|
||||||
}
|
}
|
||||||
|
@ -425,6 +434,7 @@ impl Room {
|
||||||
RoomEvent::RoomPowerLevels(power) => self.handle_power_level(power),
|
RoomEvent::RoomPowerLevels(power) => self.handle_power_level(power),
|
||||||
RoomEvent::RoomTombstone(tomb) => self.handle_tombstone(tomb),
|
RoomEvent::RoomTombstone(tomb) => self.handle_tombstone(tomb),
|
||||||
RoomEvent::RoomEncryption(encrypt) => self.handle_encryption_event(encrypt),
|
RoomEvent::RoomEncryption(encrypt) => self.handle_encryption_event(encrypt),
|
||||||
|
#[cfg(feature = "messages")]
|
||||||
RoomEvent::RoomMessage(msg) => self.handle_message(msg),
|
RoomEvent::RoomMessage(msg) => self.handle_message(msg),
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,6 +107,35 @@ mod test {
|
||||||
|
|
||||||
let mut joined_rooms = HashMap::new();
|
let mut joined_rooms = HashMap::new();
|
||||||
joined_rooms.insert(id, room);
|
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!(
|
assert_eq!(
|
||||||
r#"{
|
r#"{
|
||||||
"!roomid:example.com": {
|
"!roomid:example.com": {
|
||||||
|
|
Loading…
Reference in New Issue