room/message: make docs more clear, deserialize to MessageEvent over RoomEvent

master
Devin R 2020-04-30 06:28:36 -04:00
parent d4b2fd0e1a
commit 9788233771
2 changed files with 4 additions and 12 deletions

View File

@ -2,13 +2,12 @@ use std::cmp::Ordering;
use std::ops::Deref; use std::ops::Deref;
use std::vec::IntoIter; use std::vec::IntoIter;
use crate::events::collections::all::RoomEvent;
use crate::events::room::message::MessageEvent; use crate::events::room::message::MessageEvent;
use crate::events::EventJson; use crate::events::EventJson;
use serde::{de, ser, Serialize}; use serde::{de, ser, Serialize};
/// A queue that holds at most 10 messages received from the server. /// A queue that holds the 10 most recent messages received from the server.
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct MessageQueue { pub struct MessageQueue {
msgs: Vec<MessageWrapper>, msgs: Vec<MessageWrapper>,
@ -104,19 +103,12 @@ pub(crate) mod ser_deser {
where where
D: de::Deserializer<'de>, D: de::Deserializer<'de>,
{ {
let messages: Vec<EventJson<RoomEvent>> = de::Deserialize::deserialize(deserializer)?; let messages: Vec<EventJson<MessageEvent>> = de::Deserialize::deserialize(deserializer)?;
// TODO this should probably bail out if deserialization fails not skip // TODO this should probably bail out if deserialization fails not skip the message
let msgs: Vec<MessageWrapper> = messages let msgs: Vec<MessageWrapper> = messages
.into_iter() .into_iter()
.flat_map(|json| json.deserialize()) .flat_map(|json| json.deserialize())
.flat_map(|ev| {
if let RoomEvent::RoomMessage(msg) = ev {
Some(msg)
} else {
None
}
})
.map(MessageWrapper) .map(MessageWrapper)
.collect(); .collect();

View File

@ -110,7 +110,7 @@ pub struct Room {
pub creator: Option<UserId>, pub creator: Option<UserId>,
/// The map of room members. /// The map of room members.
pub members: HashMap<UserId, RoomMember>, pub members: HashMap<UserId, RoomMember>,
/// A queue of messages no longer than MAX_MSGS /// A queue of messages, holds no more than 10 of the most recent messages.
/// ///
/// 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.