Rebase upstream/master into ruma-mono branch
parent
b1e7bc77a4
commit
68822861d5
|
@ -1,7 +1,9 @@
|
|||
use matrix_sdk::{
|
||||
api::r0::sync::sync_events::Response as SyncResponse,
|
||||
events::room::message::{MessageEventContent, TextMessageEventContent},
|
||||
events::MessageEventStub,
|
||||
events::{
|
||||
room::message::{MessageEventContent, TextMessageEventContent},
|
||||
AnyMessageEventStub, AnyRoomEventStub, MessageEventStub,
|
||||
},
|
||||
identifiers::RoomId,
|
||||
Client, ClientConfig, SyncSettings,
|
||||
};
|
||||
|
@ -43,7 +45,9 @@ impl WasmBot {
|
|||
for (room_id, room) in response.rooms.join {
|
||||
for event in room.timeline.events {
|
||||
if let Ok(event) = event.deserialize() {
|
||||
self.on_room_message(&room_id, event).await
|
||||
if let AnyRoomEventStub::Message(AnyMessageEventStub::RoomMessage(ev)) = event {
|
||||
self.on_room_message(&room_id, ev).await
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1639,8 +1639,8 @@ mod test {
|
|||
client.restore_login(session).await.unwrap();
|
||||
|
||||
let mut response = EventBuilder::default()
|
||||
.add_state_event(EventsFile::Member)
|
||||
.add_state_event(EventsFile::PowerLevels)
|
||||
.add_state_event(EventsJson::Member)
|
||||
.add_state_event(EventsJson::PowerLevels)
|
||||
.build_sync_response();
|
||||
|
||||
client
|
||||
|
@ -2406,6 +2406,8 @@ mod test {
|
|||
base_client.sync_token().await,
|
||||
Some("s526_47314_0_7_1_1_1_11444_1".to_string())
|
||||
);
|
||||
|
||||
// This is commented out because this field is private...
|
||||
// assert_eq!(
|
||||
// *base_client.ignored_users.read().await,
|
||||
// vec![UserId::try_from("@someone:example.org").unwrap()]
|
||||
|
|
|
@ -11,7 +11,10 @@ use matrix_sdk_common::{
|
|||
},
|
||||
uiaa::AuthData,
|
||||
},
|
||||
events::{room::power_levels::PowerLevelsEventContent, EventJson},
|
||||
events::{
|
||||
room::{create::PreviousRoom, power_levels::PowerLevelsEventContent},
|
||||
EventJson,
|
||||
},
|
||||
identifiers::{DeviceId, RoomId, UserId},
|
||||
js_int::UInt,
|
||||
};
|
||||
|
@ -267,7 +270,7 @@ impl Into<get_message_events::Request> for MessagesRequestBuilder {
|
|||
from: self.from,
|
||||
to: self.to,
|
||||
dir: self.direction.unwrap_or(Direction::Backward),
|
||||
limit: self.limit.map(UInt::from),
|
||||
limit: self.limit.map(UInt::from).unwrap_or_default(),
|
||||
filter: self.filter,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,8 +38,8 @@ use crate::session::Session;
|
|||
use crate::state::{AllRooms, ClientState, StateStore};
|
||||
use crate::EventEmitter;
|
||||
use matrix_sdk_common::events::{
|
||||
AnyBasicEvent, AnyEphemeralRoomEvent, AnyMessageEventStub, AnyRoomEventStub, AnyStateEventStub,
|
||||
AnyStrippedStateEventStub, EventJson,
|
||||
AnyBasicEvent, AnyEphemeralRoomEventStub, AnyMessageEventStub, AnyRoomEventStub,
|
||||
AnyStateEventStub, AnyStrippedStateEventStub, EventJson,
|
||||
};
|
||||
|
||||
#[cfg(feature = "encryption")]
|
||||
|
@ -105,7 +105,9 @@ fn hoist_room_event_prev_content(
|
|||
let mut ev = event.deserialize().ok()?;
|
||||
|
||||
match &mut ev {
|
||||
RoomEvent::RoomMember(ref mut member) if member.prev_content.is_none() => {
|
||||
AnyRoomEventStub::State(AnyStateEventStub::RoomMember(ref mut member))
|
||||
if member.prev_content.is_none() =>
|
||||
{
|
||||
if let Ok(prev) = prev_content.deserialize() {
|
||||
member.prev_content = Some(prev)
|
||||
}
|
||||
|
@ -130,9 +132,7 @@ fn hoist_state_event_prev_content(
|
|||
|
||||
let mut ev = event.deserialize().ok()?;
|
||||
match &mut ev {
|
||||
AnyRoomEventStub::State(AnyStateEventStub::RoomMember(ref mut member))
|
||||
if member.prev_content.is_none() =>
|
||||
{
|
||||
AnyStateEventStub::RoomMember(ref mut member) if member.prev_content.is_none() => {
|
||||
member.prev_content = Some(prev_content.deserialize().ok()?);
|
||||
Some(EventJson::from(ev))
|
||||
}
|
||||
|
@ -907,12 +907,12 @@ impl BaseClient {
|
|||
pub async fn receive_ephemeral_event(
|
||||
&self,
|
||||
_room_id: &RoomId,
|
||||
event: &AnyEphemeralRoomEvent,
|
||||
event: &AnyEphemeralRoomEventStub,
|
||||
) -> bool {
|
||||
match &event {
|
||||
AnyEphemeralRoomEvent::FullyRead(_) => {}
|
||||
AnyEphemeralRoomEvent::Receipt(_) => {}
|
||||
AnyEphemeralRoomEvent::Typing(_) => {}
|
||||
AnyEphemeralRoomEventStub::FullyRead(_) => {}
|
||||
AnyEphemeralRoomEventStub::Receipt(_) => {}
|
||||
AnyEphemeralRoomEventStub::Typing(_) => {}
|
||||
_ => {}
|
||||
};
|
||||
false
|
||||
|
@ -1724,7 +1724,7 @@ impl BaseClient {
|
|||
pub(crate) async fn emit_ephemeral_event(
|
||||
&self,
|
||||
room_id: &RoomId,
|
||||
event: &AnyEphemeralRoomEvent,
|
||||
event: &AnyEphemeralRoomEventStub,
|
||||
room_state: RoomStateType,
|
||||
) {
|
||||
let lock = self.event_emitter.read().await;
|
||||
|
@ -1759,13 +1759,13 @@ impl BaseClient {
|
|||
};
|
||||
|
||||
match event {
|
||||
AnyEphemeralRoomEvent::FullyRead(full_read) => {
|
||||
AnyEphemeralRoomEventStub::FullyRead(full_read) => {
|
||||
event_emitter.on_non_room_fully_read(room, &full_read).await
|
||||
}
|
||||
AnyEphemeralRoomEvent::Typing(typing) => {
|
||||
AnyEphemeralRoomEventStub::Typing(typing) => {
|
||||
event_emitter.on_non_room_typing(room, &typing).await
|
||||
}
|
||||
AnyEphemeralRoomEvent::Receipt(receipt) => {
|
||||
AnyEphemeralRoomEventStub::Receipt(receipt) => {
|
||||
event_emitter.on_non_room_receipt(room, &receipt).await
|
||||
}
|
||||
_ => {}
|
||||
|
@ -1887,7 +1887,7 @@ mod test {
|
|||
#[async_test]
|
||||
async fn test_joined_room_creation() {
|
||||
let mut sync_response = EventBuilder::default()
|
||||
.add_state_event(EventsFile::Member)
|
||||
.add_state_event(EventsJson::Member)
|
||||
.build_sync_response();
|
||||
let client = get_client().await;
|
||||
let room_id = get_room_id();
|
||||
|
@ -1907,7 +1907,7 @@ mod test {
|
|||
assert!(room.is_some());
|
||||
|
||||
let mut sync_response = EventBuilder::default()
|
||||
.add_custom_left_event(&room_id, member_event(), AnyRoomEventStub::State)
|
||||
.add_custom_left_event(&room_id, member_event())
|
||||
.build_sync_response();
|
||||
|
||||
sync_response.next_batch = "Hello".to_owned();
|
||||
|
@ -1928,7 +1928,7 @@ mod test {
|
|||
async fn test_left_room_creation() {
|
||||
let room_id = RoomId::try_from("!left_room:localhost").unwrap();
|
||||
let mut sync_response = EventBuilder::default()
|
||||
.add_custom_left_event(&room_id, member_event(), AnyRoomEventStub::State)
|
||||
.add_custom_left_event(&room_id, member_event())
|
||||
.build_sync_response();
|
||||
|
||||
let client = get_client().await;
|
||||
|
@ -1944,8 +1944,10 @@ mod test {
|
|||
let room = client.get_left_room(&room_id).await;
|
||||
assert!(room.is_some());
|
||||
|
||||
let mem = member_event();
|
||||
|
||||
let mut sync_response = EventBuilder::default()
|
||||
.add_custom_joined_event(&room_id, member_event(), AnyRoomEventStub::State)
|
||||
.add_custom_joined_event(&room_id, mem)
|
||||
.build_sync_response();
|
||||
|
||||
sync_response.next_batch = "Hello".to_owned();
|
||||
|
@ -1983,7 +1985,7 @@ mod test {
|
|||
assert!(room.is_some());
|
||||
|
||||
let mut sync_response = EventBuilder::default()
|
||||
.add_custom_joined_event(&room_id, member_event(), AnyRoomEventStub::State)
|
||||
.add_custom_joined_event(&room_id, member_event())
|
||||
.build_sync_response();
|
||||
|
||||
sync_response.next_batch = "Hello".to_owned();
|
||||
|
@ -2355,7 +2357,7 @@ mod test {
|
|||
let room_id = get_room_id();
|
||||
|
||||
let mut sync_response = EventBuilder::default()
|
||||
.add_state_event(EventsFile::Member)
|
||||
.add_state_event(EventsJson::Member)
|
||||
.build_sync_response();
|
||||
|
||||
client
|
||||
|
|
|
@ -7,12 +7,12 @@ use std::cmp::Ordering;
|
|||
use std::ops::{Deref, DerefMut};
|
||||
use std::vec::IntoIter;
|
||||
|
||||
use crate::events::room::message::MessageEventContent;
|
||||
use crate::events::MessageEventStub;
|
||||
use crate::events::AnyMessageEventStub;
|
||||
|
||||
use serde::{de, ser, Serialize};
|
||||
|
||||
type MessageEvent = MessageEventStub<MessageEventContent>;
|
||||
const MESSAGE_QUEUE_CAP: usize = 35;
|
||||
|
||||
/// A queue that holds the 10 most recent messages received from the server.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct MessageQueue {
|
||||
|
@ -20,10 +20,10 @@ pub struct MessageQueue {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct MessageWrapper(MessageEvent);
|
||||
pub struct MessageWrapper(pub AnyMessageEventStub);
|
||||
|
||||
impl Deref for MessageWrapper {
|
||||
type Target = MessageEvent;
|
||||
type Target = AnyMessageEventStub;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
|
@ -38,7 +38,7 @@ impl DerefMut for MessageWrapper {
|
|||
|
||||
impl PartialEq for MessageWrapper {
|
||||
fn eq(&self, other: &MessageWrapper) -> bool {
|
||||
self.0.event_id == other.0.event_id
|
||||
self.0.event_id() == other.0.event_id()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,7 +46,7 @@ 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))
|
||||
Some(self.0.origin_server_ts().cmp(&other.0.origin_server_ts()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,7 +63,7 @@ impl PartialEq for MessageQueue {
|
|||
.msgs
|
||||
.iter()
|
||||
.zip(other.msgs.iter())
|
||||
.all(|(msg_a, msg_b)| msg_a.event_id == msg_b.event_id)
|
||||
.all(|(msg_a, msg_b)| msg_a.event_id() == msg_b.event_id())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -71,17 +71,17 @@ impl MessageQueue {
|
|||
/// Create a new empty `MessageQueue`.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
msgs: Vec::with_capacity(20),
|
||||
msgs: Vec::with_capacity(45),
|
||||
}
|
||||
}
|
||||
|
||||
/// Inserts a `MessageEvent` into `MessageQueue`, sorted by by `origin_server_ts`.
|
||||
///
|
||||
/// Removes the oldest element in the queue if there are more than 10 elements.
|
||||
pub fn push(&mut self, msg: MessageEvent) -> bool {
|
||||
pub fn push(&mut self, msg: AnyMessageEventStub) -> bool {
|
||||
// only push new messages into the queue
|
||||
if let Some(latest) = self.msgs.last() {
|
||||
if msg.origin_server_ts < latest.origin_server_ts && self.msgs.len() >= 10 {
|
||||
if msg.origin_server_ts() < latest.origin_server_ts() && self.msgs.len() >= 10 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -95,7 +95,7 @@ impl MessageQueue {
|
|||
}
|
||||
Err(pos) => self.msgs.insert(pos, message),
|
||||
}
|
||||
if self.msgs.len() > 10 {
|
||||
if self.msgs.len() > MESSAGE_QUEUE_CAP {
|
||||
self.msgs.remove(0);
|
||||
}
|
||||
true
|
||||
|
@ -126,7 +126,7 @@ pub(crate) mod ser_deser {
|
|||
where
|
||||
D: de::Deserializer<'de>,
|
||||
{
|
||||
let messages: Vec<MessageEvent> = de::Deserialize::deserialize(deserializer)?;
|
||||
let messages: Vec<AnyMessageEventStub> = de::Deserialize::deserialize(deserializer)?;
|
||||
|
||||
let mut msgs = vec![];
|
||||
for msg in messages {
|
||||
|
@ -156,7 +156,6 @@ mod test {
|
|||
|
||||
use matrix_sdk_test::test_json;
|
||||
|
||||
use crate::events::{collections::all::RoomEvent, EventJson};
|
||||
use crate::identifiers::{RoomId, UserId};
|
||||
use crate::Room;
|
||||
|
||||
|
@ -168,7 +167,7 @@ mod test {
|
|||
let mut room = Room::new(&id, &user);
|
||||
|
||||
let json: &serde_json::Value = &test_json::MESSAGE_TEXT;
|
||||
let msg = serde_json::from_value::<MessageEvent>(json.clone()).unwrap();
|
||||
let msg = serde_json::from_value::<AnyMessageEventStub>(json.clone()).unwrap();
|
||||
|
||||
let mut msgs = MessageQueue::new();
|
||||
msgs.push(msg.clone());
|
||||
|
@ -214,7 +213,7 @@ mod test {
|
|||
let mut room = Room::new(&id, &user);
|
||||
|
||||
let json: &serde_json::Value = &test_json::MESSAGE_TEXT;
|
||||
let msg = serde_json::from_value::<MessageEvent>(json.clone()).unwrap();
|
||||
let msg = serde_json::from_value::<AnyMessageEventStub>(json.clone()).unwrap();
|
||||
|
||||
let mut msgs = MessageQueue::new();
|
||||
msgs.push(msg.clone());
|
||||
|
|
|
@ -18,7 +18,7 @@ use std::collections::{BTreeMap, HashMap};
|
|||
use std::convert::TryFrom;
|
||||
|
||||
#[cfg(feature = "messages")]
|
||||
use super::message::MessageQueue;
|
||||
use super::message::{MessageQueue, MessageWrapper};
|
||||
use super::RoomMember;
|
||||
|
||||
use crate::api::r0::sync::sync_events::{RoomSummary, UnreadNotificationsCount};
|
||||
|
@ -30,28 +30,23 @@ use crate::events::room::{
|
|||
member::{MemberEventContent, MembershipChange},
|
||||
name::NameEventContent,
|
||||
power_levels::{NotificationPowerLevels, PowerLevelsEventContent},
|
||||
redaction::RedactionEventStub,
|
||||
tombstone::TombstoneEventContent,
|
||||
};
|
||||
|
||||
use crate::events::{
|
||||
Algorithm, AnyMessageEventStub, AnyRoomEventStub, AnyStateEventStub, AnyStrippedStateEventStub,
|
||||
EventType, StateEventStub, StrippedStateEventStub,
|
||||
Algorithm, AnyRoomEventStub, AnyStateEventStub, AnyStrippedStateEventStub, EventType,
|
||||
StateEventStub, StrippedStateEventStub,
|
||||
};
|
||||
|
||||
#[cfg(feature = "messages")]
|
||||
use crate::events::{
|
||||
room::message::{MessageEventContent, TextMessageEventContent},
|
||||
MessageEventStub,
|
||||
};
|
||||
use crate::events::{room::redaction::RedactionEventStub, AnyMessageEventStub};
|
||||
|
||||
use crate::identifiers::{RoomAliasId, RoomId, UserId};
|
||||
|
||||
use crate::js_int::{Int, UInt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
#[cfg_attr(test, derive(Clone))]
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
/// `RoomName` allows the calculation of a text room name.
|
||||
pub struct RoomName {
|
||||
/// The displayed name of the room.
|
||||
|
@ -73,7 +68,7 @@ pub struct RoomName {
|
|||
pub invited_member_count: Option<UInt>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct PowerLevels {
|
||||
/// The level required to ban a user.
|
||||
pub ban: Int,
|
||||
|
@ -99,7 +94,7 @@ pub struct PowerLevels {
|
|||
pub notifications: Int,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
/// Encryption info of the room.
|
||||
pub struct EncryptionInfo {
|
||||
/// The encryption algorithm that should be used to encrypt messages in the
|
||||
|
@ -144,7 +139,7 @@ impl From<&StateEventStub<EncryptionEventContent>> for EncryptionInfo {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Tombstone {
|
||||
/// A server-defined message.
|
||||
body: String,
|
||||
|
@ -158,7 +153,7 @@ enum MemberDirection {
|
|||
Exiting,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
/// A Matrix room.
|
||||
pub struct Room {
|
||||
/// The unique id of the room.
|
||||
|
@ -361,14 +356,15 @@ impl Room {
|
|||
member.name().into()
|
||||
} else {
|
||||
// There is no member with the requested MXID in the room. We still return the MXID.
|
||||
id.as_ref().into()
|
||||
id.as_str().into()
|
||||
}
|
||||
}
|
||||
|
||||
fn add_member(&mut self, event: &StateEventStub<MemberEventContent>, room_id: &RoomId) -> bool {
|
||||
if self
|
||||
.members
|
||||
.contains_key(&UserId::try_from(event.state_key.as_str()).unwrap())
|
||||
let new_member = RoomMember::new(event, room_id);
|
||||
|
||||
if self.joined_members.contains_key(&new_member.user_id)
|
||||
|| self.invited_members.contains_key(&new_member.user_id)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -390,8 +386,12 @@ impl Room {
|
|||
/// Process the member event of a leaving user.
|
||||
///
|
||||
/// Returns true if this made a change to the room's state, false otherwise.
|
||||
fn remove_member(&mut self, event: &StateEventStub<MemberEventContent>) -> bool {
|
||||
let leaving_member = RoomMember::new(event);
|
||||
fn remove_member(
|
||||
&mut self,
|
||||
event: &StateEventStub<MemberEventContent>,
|
||||
room_id: &RoomId,
|
||||
) -> bool {
|
||||
let leaving_member = RoomMember::new(event, room_id);
|
||||
|
||||
// Perform display name disambiguations, if necessary.
|
||||
let disambiguations =
|
||||
|
@ -587,12 +587,12 @@ impl Room {
|
|||
// TODO: This would not be handled correctly as all the MemberEvents have the `prev_content`
|
||||
// inside of `unsigned` field.
|
||||
match event.membership_change() {
|
||||
Invited | Joined => self.add_member(event),
|
||||
Invited | Joined => self.add_member(event, room_id),
|
||||
Kicked | Banned | KickedAndBanned | InvitationRejected | Left => {
|
||||
self.remove_member(event)
|
||||
self.remove_member(event, room_id)
|
||||
}
|
||||
ProfileChanged => {
|
||||
let user = if let Ok(id) = UserId::try_from(event.state_key.as_str()) {
|
||||
ProfileChanged { .. } => {
|
||||
let user_id = if let Ok(id) = UserId::try_from(event.state_key.as_str()) {
|
||||
id
|
||||
} else {
|
||||
return false;
|
||||
|
@ -615,7 +615,7 @@ impl Room {
|
|||
/// Returns true if `MessageQueue` was added to.
|
||||
#[cfg(feature = "messages")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "messages")))]
|
||||
pub fn handle_message(&mut self, event: &MessageEventStub<MessageEventContent>) -> bool {
|
||||
pub fn handle_message(&mut self, event: &AnyMessageEventStub) -> bool {
|
||||
self.messages.push(event.clone())
|
||||
}
|
||||
|
||||
|
@ -628,10 +628,10 @@ impl Room {
|
|||
if let Some(msg) = self
|
||||
.messages
|
||||
.iter_mut()
|
||||
.find(|msg| event.redacts == msg.event_id)
|
||||
.find(|msg| &event.redacts == msg.event_id())
|
||||
{
|
||||
// TODO make msg an enum or use AnyMessageEventStub enum to represent
|
||||
msg.content = MessageEventContent::Text(TextMessageEventContent::new_plain("Redacted"));
|
||||
*msg = MessageWrapper(AnyMessageEventStub::RoomRedaction(event.clone()));
|
||||
true
|
||||
} else {
|
||||
false
|
||||
|
@ -741,7 +741,9 @@ impl Room {
|
|||
},
|
||||
AnyRoomEventStub::Message(event) => match &event {
|
||||
#[cfg(feature = "messages")]
|
||||
AnyMessageEventStub::RoomMessage(event) => self.handle_message(&event),
|
||||
// We ignore this variants event because `handle_message` takes the enum
|
||||
// to store AnyMessageEventStub events in the `MessageQueue`.
|
||||
AnyMessageEventStub::RoomMessage(_) => self.handle_message(&event),
|
||||
#[cfg(feature = "messages")]
|
||||
AnyMessageEventStub::RoomRedaction(event) => self.handle_redaction(&event),
|
||||
_ => false,
|
||||
|
@ -968,23 +970,23 @@ mod test {
|
|||
let mut event_builder = EventBuilder::new();
|
||||
|
||||
let mut member1_join_sync_response = event_builder
|
||||
.add_room_event(EventsJson::Member, RoomEvent::RoomMember)
|
||||
.add_room_event(EventsJson::Member)
|
||||
.build_sync_response();
|
||||
|
||||
let mut member2_join_sync_response = event_builder
|
||||
.add_custom_joined_event(&room_id, member2_join_event, RoomEvent::RoomMember)
|
||||
.add_custom_joined_event(&room_id, member2_join_event)
|
||||
.build_sync_response();
|
||||
|
||||
let mut member3_join_sync_response = event_builder
|
||||
.add_custom_joined_event(&room_id, member3_join_event, RoomEvent::RoomMember)
|
||||
.add_custom_joined_event(&room_id, member3_join_event)
|
||||
.build_sync_response();
|
||||
|
||||
let mut member2_leave_sync_response = event_builder
|
||||
.add_custom_joined_event(&room_id, member2_leave_event, RoomEvent::RoomMember)
|
||||
.add_custom_joined_event(&room_id, member2_leave_event)
|
||||
.build_sync_response();
|
||||
|
||||
let mut member3_leave_sync_response = event_builder
|
||||
.add_custom_joined_event(&room_id, member3_leave_event, RoomEvent::RoomMember)
|
||||
.add_custom_joined_event(&room_id, member3_leave_event)
|
||||
.build_sync_response();
|
||||
|
||||
// First member with display name "example" joins
|
||||
|
@ -1052,8 +1054,8 @@ mod test {
|
|||
let user_id = UserId::try_from("@example:localhost").unwrap();
|
||||
|
||||
let mut response = EventBuilder::default()
|
||||
.add_state_event(EventsFile::Member)
|
||||
.add_state_event(EventsFile::PowerLevels)
|
||||
.add_state_event(EventsJson::Member)
|
||||
.add_state_event(EventsJson::PowerLevels)
|
||||
.build_sync_response();
|
||||
|
||||
client.receive_sync_response(&mut response).await.unwrap();
|
||||
|
@ -1081,7 +1083,7 @@ mod test {
|
|||
let room_id = get_room_id();
|
||||
|
||||
let mut response = EventBuilder::default()
|
||||
.add_state_event(EventsFile::Aliases)
|
||||
.add_state_event(EventsJson::Aliases)
|
||||
.build_sync_response();
|
||||
|
||||
client.receive_sync_response(&mut response).await.unwrap();
|
||||
|
@ -1099,7 +1101,7 @@ mod test {
|
|||
let room_id = get_room_id();
|
||||
|
||||
let mut response = EventBuilder::default()
|
||||
.add_state_event(EventsFile::Alias)
|
||||
.add_state_event(EventsJson::Alias)
|
||||
.build_sync_response();
|
||||
|
||||
client.receive_sync_response(&mut response).await.unwrap();
|
||||
|
@ -1117,7 +1119,7 @@ mod test {
|
|||
let room_id = get_room_id();
|
||||
|
||||
let mut response = EventBuilder::default()
|
||||
.add_state_event(EventsFile::Name)
|
||||
.add_state_event(EventsJson::Name)
|
||||
.build_sync_response();
|
||||
|
||||
client.receive_sync_response(&mut response).await.unwrap();
|
||||
|
|
|
@ -41,7 +41,7 @@ pub struct RoomMember {
|
|||
/// If the user should be considered active.
|
||||
pub currently_active: Option<bool>,
|
||||
/// The unique id of the room.
|
||||
pub room_id: Option<RoomId>,
|
||||
pub room_id: RoomId,
|
||||
/// If the member is typing.
|
||||
pub typing: Option<bool>,
|
||||
/// The presence of the user, if found.
|
||||
|
@ -76,7 +76,7 @@ impl RoomMember {
|
|||
pub fn new(event: &StateEventStub<MemberEventContent>, room_id: &RoomId) -> Self {
|
||||
Self {
|
||||
name: event.state_key.clone(),
|
||||
room_id: Some(room_id.clone()),
|
||||
room_id: room_id.clone(),
|
||||
user_id: UserId::try_from(event.state_key.as_str()).unwrap(),
|
||||
display_name: event.content.displayname.clone(),
|
||||
avatar_url: event.content.avatar_url.clone(),
|
||||
|
@ -117,7 +117,8 @@ impl RoomMember {
|
|||
use MembershipChange::*;
|
||||
|
||||
match event.membership_change() {
|
||||
ProfileChanged => {
|
||||
// we assume that the profile has changed
|
||||
ProfileChanged { .. } => {
|
||||
self.display_name = event.content.displayname.clone();
|
||||
self.avatar_url = event.content.avatar_url.clone();
|
||||
true
|
||||
|
@ -212,7 +213,6 @@ impl RoomMember {
|
|||
mod test {
|
||||
use matrix_sdk_test::{async_test, EventBuilder, EventsJson};
|
||||
|
||||
use crate::events::room::member::MembershipState;
|
||||
use crate::identifiers::{RoomId, UserId};
|
||||
use crate::{BaseClient, Session};
|
||||
|
||||
|
@ -245,8 +245,8 @@ mod test {
|
|||
let room_id = get_room_id();
|
||||
|
||||
let mut response = EventBuilder::default()
|
||||
.add_state_event(EventsFile::Member)
|
||||
.add_state_event(EventsFile::PowerLevels)
|
||||
.add_state_event(EventsJson::Member)
|
||||
.add_state_event(EventsJson::PowerLevels)
|
||||
.build_sync_response();
|
||||
|
||||
client.receive_sync_response(&mut response).await.unwrap();
|
||||
|
@ -268,9 +268,9 @@ mod test {
|
|||
let room_id = get_room_id();
|
||||
|
||||
let mut response = EventBuilder::default()
|
||||
.add_state_event(EventsFile::Member)
|
||||
.add_state_event(EventsFile::PowerLevels)
|
||||
.add_presence_event(EventsFile::Presence)
|
||||
.add_state_event(EventsJson::Member)
|
||||
.add_state_event(EventsJson::PowerLevels)
|
||||
.add_presence_event(EventsJson::Presence)
|
||||
.build_sync_response();
|
||||
|
||||
client.receive_sync_response(&mut response).await.unwrap();
|
||||
|
|
|
@ -13,7 +13,7 @@ version = "0.1.0"
|
|||
[dependencies]
|
||||
js_int = "0.1.5"
|
||||
|
||||
ruma = { git = "https://github.com/ruma/ruma", features = ["client-api", "rand"] }
|
||||
ruma = { git = "https://github.com/ruma/ruma", features = ["client-api"] }
|
||||
|
||||
instant = { version = "0.1.4", features = ["wasm-bindgen", "now"] }
|
||||
|
||||
|
|
|
@ -73,76 +73,53 @@ impl EventBuilder {
|
|||
}
|
||||
|
||||
/// Add an event to the room events `Vec`.
|
||||
pub fn add_ephemeral<Ev: serde::de::DeserializeOwned>(
|
||||
mut self,
|
||||
file: EventsFile,
|
||||
variant: fn(Ev) -> AnyEphemeralRoomEventStub,
|
||||
) -> Self {
|
||||
pub fn add_ephemeral(&mut self, json: EventsJson) -> &mut Self {
|
||||
let val: &JsonValue = match json {
|
||||
EventsJson::Typing => &test_json::TYPING,
|
||||
_ => panic!("unknown ephemeral event {:?}", json),
|
||||
};
|
||||
|
||||
let event = serde_json::from_value::<EventJson<Ev>>(val.clone())
|
||||
.unwrap()
|
||||
.deserialize()
|
||||
.unwrap();
|
||||
|
||||
self.ephemeral.push(variant(event));
|
||||
let event = serde_json::from_value::<AnyEphemeralRoomEventStub>(val.clone()).unwrap();
|
||||
self.ephemeral.push(event);
|
||||
self
|
||||
}
|
||||
|
||||
/// Add an event to the room events `Vec`.
|
||||
#[allow(clippy::match_single_binding, unused)]
|
||||
pub fn add_account<Ev: TryFromRaw>(
|
||||
&mut self,
|
||||
json: EventsJson,
|
||||
variant: fn(Ev) -> Event,
|
||||
) -> &mut Self {
|
||||
pub fn add_account(&mut self, json: EventsJson) -> &mut Self {
|
||||
let val: &JsonValue = match json {
|
||||
_ => panic!("unknown account event {:?}", json),
|
||||
};
|
||||
|
||||
let event = serde_json::from_value::<EventJson<Ev>>(val.clone())
|
||||
.unwrap()
|
||||
.deserialize()
|
||||
.unwrap();
|
||||
self.account_data.push(variant(event));
|
||||
let event = serde_json::from_value::<AnyBasicEvent>(val.clone()).unwrap();
|
||||
self.account_data.push(event);
|
||||
self
|
||||
}
|
||||
|
||||
// /// Add an event to the room events `Vec`.
|
||||
// pub fn add_room_event<Ev: TryFromRaw>(
|
||||
// &mut self,
|
||||
// json: EventsJson,
|
||||
// variant: fn(Ev) -> AnyRoomEventStub,
|
||||
// ) -> &mut Self {
|
||||
// let val: &JsonValue = match json {
|
||||
// EventsJson::Member => &test_json::MEMBER,
|
||||
// EventsJson::PowerLevels => &test_json::POWER_LEVELS,
|
||||
// _ => panic!("unknown room event json {:?}", json),
|
||||
// };
|
||||
/// Add an event to the room events `Vec`.
|
||||
pub fn add_room_event(&mut self, json: EventsJson) -> &mut Self {
|
||||
let val: &JsonValue = match json {
|
||||
EventsJson::Member => &test_json::MEMBER,
|
||||
EventsJson::PowerLevels => &test_json::POWER_LEVELS,
|
||||
_ => panic!("unknown room event json {:?}", json),
|
||||
};
|
||||
|
||||
// let event = serde_json::from_value::<EventJson<Ev>>(val.clone())
|
||||
// .unwrap()
|
||||
// .deserialize()
|
||||
// .unwrap();
|
||||
let event = serde_json::from_value::<AnyRoomEventStub>(val.clone()).unwrap();
|
||||
|
||||
// self.add_joined_event(
|
||||
// &RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap(),
|
||||
// variant(event),
|
||||
// );
|
||||
// self
|
||||
// }
|
||||
self.add_joined_event(
|
||||
&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap(),
|
||||
event,
|
||||
);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_custom_joined_event<Ev: serde::de::DeserializeOwned>(
|
||||
mut self,
|
||||
pub fn add_custom_joined_event(
|
||||
&mut self,
|
||||
room_id: &RoomId,
|
||||
event: serde_json::Value,
|
||||
variant: fn(Ev) -> AnyRoomEventStub,
|
||||
) -> Self {
|
||||
let event = serde_json::from_value::<Ev>(event).unwrap();
|
||||
self.add_joined_event(room_id, variant(event));
|
||||
) -> &mut Self {
|
||||
let event = serde_json::from_value::<AnyRoomEventStub>(event).unwrap();
|
||||
self.add_joined_event(room_id, event);
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -153,7 +130,11 @@ impl EventBuilder {
|
|||
.push(event);
|
||||
}
|
||||
|
||||
pub fn add_custom_invited_event(mut self, room_id: &RoomId, event: serde_json::Value) -> Self {
|
||||
pub fn add_custom_invited_event(
|
||||
&mut self,
|
||||
room_id: &RoomId,
|
||||
event: serde_json::Value,
|
||||
) -> &mut Self {
|
||||
let event = serde_json::from_value::<AnyStateEventStub>(event).unwrap();
|
||||
self.invited_room_events
|
||||
.entry(room_id.clone())
|
||||
|
@ -162,35 +143,32 @@ impl EventBuilder {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn add_custom_left_event<Ev: serde::de::DeserializeOwned>(
|
||||
mut self,
|
||||
pub fn add_custom_left_event(
|
||||
&mut self,
|
||||
room_id: &RoomId,
|
||||
event: serde_json::Value,
|
||||
variant: fn(Ev) -> AnyRoomEventStub,
|
||||
) -> Self {
|
||||
let event = serde_json::from_value::<Ev>(event).unwrap();
|
||||
) -> &mut Self {
|
||||
let event = serde_json::from_value::<AnyRoomEventStub>(event).unwrap();
|
||||
self.left_room_events
|
||||
.entry(room_id.clone())
|
||||
.or_insert_with(Vec::new)
|
||||
.push(variant(event));
|
||||
.push(event);
|
||||
self
|
||||
}
|
||||
|
||||
/// Add a state event to the state events `Vec`.
|
||||
pub fn add_state_event<Ev: TryFromRaw>(
|
||||
&mut self,
|
||||
json: EventsJson,
|
||||
) -> &mut Self {
|
||||
pub fn add_state_event(&mut self, json: EventsJson) -> &mut Self {
|
||||
let val: &JsonValue = match json {
|
||||
EventsJson::Alias => &test_json::ALIAS,
|
||||
EventsJson::Aliases => &test_json::ALIASES,
|
||||
EventsJson::Name => &test_json::NAME,
|
||||
EventsJson::Member => &test_json::MEMBER,
|
||||
EventsJson::PowerLevels => &test_json::POWER_LEVELS,
|
||||
_ => panic!("unknown state event {:?}", json),
|
||||
};
|
||||
|
||||
let event = serde_json::from_value::<AnyStateEventStub>(val.clone())
|
||||
.unwrap();
|
||||
self.state_events.push(variant(event));
|
||||
let event = serde_json::from_value::<AnyStateEventStub>(val.clone()).unwrap();
|
||||
self.state_events.push(event);
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -201,8 +179,7 @@ impl EventBuilder {
|
|||
_ => panic!("unknown presence event {:?}", json),
|
||||
};
|
||||
|
||||
let event = serde_json::from_value::<PresenceEvent>(val.clone())
|
||||
.unwrap();
|
||||
let event = serde_json::from_value::<PresenceEvent>(val.clone()).unwrap();
|
||||
self.presence_events.push(event);
|
||||
self
|
||||
}
|
||||
|
|
|
@ -18,13 +18,6 @@ lazy_static! {
|
|||
"summary": {},
|
||||
"account_data": {
|
||||
"events": [
|
||||
{
|
||||
"content": {
|
||||
"event_id": "$someplace:example.org"
|
||||
},
|
||||
"room_id": "!roomid:room.com",
|
||||
"type": "m.fully_read"
|
||||
},
|
||||
{
|
||||
"content": {
|
||||
"ignored_users": {
|
||||
|
@ -47,7 +40,15 @@ lazy_static! {
|
|||
}
|
||||
}
|
||||
},
|
||||
"room_id": "!SVkFJHzfwvuaIEawgC:localhost",
|
||||
"type": "m.receipt"
|
||||
},
|
||||
{
|
||||
"content": {
|
||||
"event_id": "$someplace:example.org"
|
||||
},
|
||||
"room_id": "!roomid:room.com",
|
||||
"type": "m.fully_read"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -305,13 +306,6 @@ lazy_static! {
|
|||
},
|
||||
"account_data": {
|
||||
"events": [
|
||||
{
|
||||
"content": {
|
||||
"event_id": "$someplace:example.org"
|
||||
},
|
||||
"room_id": "!roomid:room.com",
|
||||
"type": "m.fully_read"
|
||||
},
|
||||
{
|
||||
"content": {
|
||||
"ignored_users": {
|
||||
|
@ -335,6 +329,13 @@ lazy_static! {
|
|||
}
|
||||
},
|
||||
"type": "m.receipt"
|
||||
},
|
||||
{
|
||||
"content": {
|
||||
"event_id": "$someplace:example.org"
|
||||
},
|
||||
"room_id": "!roomid:room.com",
|
||||
"type": "m.fully_read"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -570,6 +571,7 @@ lazy_static! {
|
|||
}
|
||||
}
|
||||
},
|
||||
"room_id": "!jEsUZKDJdhlrceRyVU:example.org",
|
||||
"type": "m.receipt"
|
||||
},
|
||||
{
|
||||
|
@ -771,13 +773,6 @@ lazy_static! {
|
|||
"summary": {},
|
||||
"account_data": {
|
||||
"events": [
|
||||
{
|
||||
"content": {
|
||||
"event_id": "$someplace:example.org"
|
||||
},
|
||||
"room_id": "!roomid:room.com",
|
||||
"type": "m.fully_read"
|
||||
},
|
||||
{
|
||||
"content": {
|
||||
"ignored_users": {
|
||||
|
@ -801,6 +796,13 @@ lazy_static! {
|
|||
}
|
||||
},
|
||||
"type": "m.receipt"
|
||||
},
|
||||
{
|
||||
"content": {
|
||||
"event_id": "$someplace:example.org"
|
||||
},
|
||||
"room_id": "!roomid:room.com",
|
||||
"type": "m.fully_read"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue