events: Remove Serialize impls for all event types except ToDeviceEvent
This commit is contained in:
parent
80733a8cee
commit
cdabe8e52d
@ -51,19 +51,15 @@ mod tests {
|
||||
let mut content = DirectEventContent(BTreeMap::new());
|
||||
let server_name = server_name!("ruma.io");
|
||||
let alice = UserId::new(server_name);
|
||||
let room = vec![RoomId::new(server_name)];
|
||||
let rooms = vec![RoomId::new(server_name)];
|
||||
|
||||
content.insert(alice.clone(), room.clone());
|
||||
content.insert(alice.clone(), rooms.clone());
|
||||
|
||||
let event = DirectEvent { content };
|
||||
let json_data = json!({
|
||||
"content": {
|
||||
alice.to_string(): vec![room[0].to_string()],
|
||||
},
|
||||
"type": "m.direct"
|
||||
alice.to_string(): rooms,
|
||||
});
|
||||
|
||||
assert_eq!(to_json_value(&event).unwrap(), json_data);
|
||||
assert_eq!(to_json_value(&content).unwrap(), json_data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -55,29 +55,20 @@ mod tests {
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
use super::IgnoredUserListEventContent;
|
||||
use crate::{
|
||||
events::{AnyGlobalAccountDataEvent, GlobalAccountDataEvent},
|
||||
user_id,
|
||||
};
|
||||
use crate::{events::AnyGlobalAccountDataEvent, user_id};
|
||||
|
||||
#[test]
|
||||
fn serialization() {
|
||||
let ignored_user_list_event = GlobalAccountDataEvent {
|
||||
content: IgnoredUserListEventContent::users(vec![
|
||||
user_id!("@carl:example.com").to_owned()
|
||||
]),
|
||||
};
|
||||
let ignored_user_list =
|
||||
IgnoredUserListEventContent::users(vec![user_id!("@carl:example.com").to_owned()]);
|
||||
|
||||
let json = json!({
|
||||
"content": {
|
||||
"ignored_users": {
|
||||
"@carl:example.com": {}
|
||||
}
|
||||
},
|
||||
"type": "m.ignored_user_list"
|
||||
});
|
||||
|
||||
assert_eq!(to_json_value(ignored_user_list_event).unwrap(), json);
|
||||
assert_eq!(to_json_value(ignored_user_list).unwrap(), json);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -177,7 +177,6 @@ mod tests {
|
||||
event_id,
|
||||
events::{relation::Reference, ToDeviceEvent},
|
||||
serde::Base64,
|
||||
user_id,
|
||||
};
|
||||
|
||||
#[test]
|
||||
@ -193,10 +192,7 @@ mod tests {
|
||||
}),
|
||||
};
|
||||
|
||||
let sender = user_id!("@example:localhost").to_owned();
|
||||
|
||||
let json_data = json!({
|
||||
"content": {
|
||||
"transaction_id": "456",
|
||||
"method": "m.sas.v1",
|
||||
"commitment": "aGVsbG8",
|
||||
@ -204,26 +200,14 @@ mod tests {
|
||||
"hash": "sha256",
|
||||
"message_authentication_code": "hkdf-hmac-sha256",
|
||||
"short_authentication_string": ["decimal"]
|
||||
},
|
||||
"sender": sender,
|
||||
"type": "m.key.verification.accept"
|
||||
});
|
||||
|
||||
let key_verification_accept =
|
||||
ToDeviceEvent { sender, content: key_verification_accept_content };
|
||||
|
||||
assert_eq!(to_json_value(&key_verification_accept).unwrap(), json_data);
|
||||
|
||||
let sender = user_id!("@example:localhost").to_owned();
|
||||
assert_eq!(to_json_value(&key_verification_accept_content).unwrap(), json_data);
|
||||
|
||||
let json_data = json!({
|
||||
"content": {
|
||||
"transaction_id": "456",
|
||||
"method": "m.sas.custom",
|
||||
"test": "field",
|
||||
},
|
||||
"sender": sender,
|
||||
"type": "m.key.verification.accept"
|
||||
});
|
||||
|
||||
let key_verification_accept_content = ToDeviceKeyVerificationAcceptEventContent {
|
||||
@ -236,10 +220,7 @@ mod tests {
|
||||
}),
|
||||
};
|
||||
|
||||
let key_verification_accept =
|
||||
ToDeviceEvent { sender, content: key_verification_accept_content };
|
||||
|
||||
assert_eq!(to_json_value(&key_verification_accept).unwrap(), json_data);
|
||||
assert_eq!(to_json_value(&key_verification_accept_content).unwrap(), json_data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -219,7 +219,6 @@ mod tests {
|
||||
event_id,
|
||||
events::{relation::Reference, ToDeviceEvent},
|
||||
serde::Base64,
|
||||
user_id,
|
||||
};
|
||||
|
||||
#[test]
|
||||
@ -238,10 +237,7 @@ mod tests {
|
||||
),
|
||||
};
|
||||
|
||||
let sender = user_id!("@example:localhost").to_owned();
|
||||
|
||||
let json_data = json!({
|
||||
"content": {
|
||||
"from_device": "123",
|
||||
"transaction_id": "456",
|
||||
"method": "m.sas.v1",
|
||||
@ -249,27 +245,15 @@ mod tests {
|
||||
"hashes": ["sha256"],
|
||||
"message_authentication_codes": ["hkdf-hmac-sha256"],
|
||||
"short_authentication_string": ["decimal"]
|
||||
},
|
||||
"type": "m.key.verification.start",
|
||||
"sender": sender
|
||||
});
|
||||
|
||||
let key_verification_start =
|
||||
ToDeviceEvent { sender, content: key_verification_start_content };
|
||||
|
||||
assert_eq!(to_json_value(&key_verification_start).unwrap(), json_data);
|
||||
|
||||
let sender = user_id!("@example:localhost").to_owned();
|
||||
assert_eq!(to_json_value(&key_verification_start_content).unwrap(), json_data);
|
||||
|
||||
let json_data = json!({
|
||||
"content": {
|
||||
"from_device": "123",
|
||||
"transaction_id": "456",
|
||||
"method": "m.sas.custom",
|
||||
"test": "field",
|
||||
},
|
||||
"type": "m.key.verification.start",
|
||||
"sender": sender
|
||||
});
|
||||
|
||||
let key_verification_start_content = ToDeviceKeyVerificationStartEventContent {
|
||||
@ -283,10 +267,7 @@ mod tests {
|
||||
}),
|
||||
};
|
||||
|
||||
let key_verification_start =
|
||||
ToDeviceEvent { sender, content: key_verification_start_content };
|
||||
|
||||
assert_eq!(to_json_value(&key_verification_start).unwrap(), json_data);
|
||||
assert_eq!(to_json_value(&key_verification_start_content).unwrap(), json_data);
|
||||
|
||||
{
|
||||
let secret = Base64::new(b"This is a secret to everybody".to_vec());
|
||||
|
@ -1,7 +1,7 @@
|
||||
#![allow(clippy::exhaustive_structs)]
|
||||
|
||||
use ruma_macros::Event;
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize};
|
||||
use serde_json::value::RawValue as RawJsonValue;
|
||||
|
||||
use super::{
|
||||
@ -146,8 +146,7 @@ pub struct RedactedSyncMessageLikeEvent<C: RedactedMessageLikeEventContent> {
|
||||
/// `MessageLikeEvent` implements the comparison traits using only the `event_id` field, a sorted
|
||||
/// list would be sorted lexicographically based on the event's `EventId`.
|
||||
#[allow(clippy::exhaustive_enums)]
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(untagged)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum MessageLikeEvent<C: MessageLikeEventContent + RedactContent>
|
||||
where
|
||||
C::Redacted: RedactedMessageLikeEventContent,
|
||||
@ -164,8 +163,7 @@ where
|
||||
/// `SyncMessageLikeEvent` implements the comparison traits using only the `event_id` field, a
|
||||
/// sorted list would be sorted lexicographically based on the event's `EventId`.
|
||||
#[allow(clippy::exhaustive_enums)]
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(untagged)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum SyncMessageLikeEvent<C: MessageLikeEventContent + RedactContent>
|
||||
where
|
||||
C::Redacted: RedactedMessageLikeEventContent,
|
||||
@ -331,8 +329,7 @@ pub struct RedactedSyncStateEvent<C: RedactedStateEventContent> {
|
||||
/// `StateEvent` implements the comparison traits using only the `event_id` field, a sorted list
|
||||
/// would be sorted lexicographically based on the event's `EventId`.
|
||||
#[allow(clippy::exhaustive_enums)]
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(untagged)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum StateEvent<C: StateEventContent + RedactContent>
|
||||
where
|
||||
C::Redacted: RedactedStateEventContent,
|
||||
@ -349,8 +346,7 @@ where
|
||||
/// `SyncStateEvent` implements the comparison traits using only the `event_id` field, a sorted list
|
||||
/// would be sorted lexicographically based on the event's `EventId`.
|
||||
#[allow(clippy::exhaustive_enums)]
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(untagged)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum SyncStateEvent<C: StateEventContent + RedactContent>
|
||||
where
|
||||
C::Redacted: RedactedStateEventContent,
|
||||
@ -372,6 +368,19 @@ pub struct ToDeviceEvent<C: ToDeviceEventContent> {
|
||||
pub sender: OwnedUserId,
|
||||
}
|
||||
|
||||
impl<C: ToDeviceEventContent> Serialize for ToDeviceEvent<C> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
let mut state = serializer.serialize_struct("ToDeviceEvent", 3)?;
|
||||
state.serialize_field("type", &self.content.event_type())?;
|
||||
state.serialize_field("content", &self.content)?;
|
||||
state.serialize_field("sender", &self.sender)?;
|
||||
state.end()
|
||||
}
|
||||
}
|
||||
|
||||
/// The decrypted payload of an `m.olm.v1.curve25519-aes-sha2` event.
|
||||
#[derive(Clone, Debug, Event)]
|
||||
pub struct DecryptedOlmV1Event<C: MessageLikeEventContent> {
|
||||
|
@ -17,55 +17,29 @@ pub struct PolicyRuleRoomEventContent(pub PolicyRuleEventContent);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use js_int::int;
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
use super::{OriginalPolicyRuleRoomEvent, PolicyRuleRoomEventContent};
|
||||
use crate::{
|
||||
event_id,
|
||||
events::{
|
||||
policy::rule::{PolicyRuleEventContent, Recommendation},
|
||||
StateUnsigned,
|
||||
},
|
||||
room_id,
|
||||
events::policy::rule::{PolicyRuleEventContent, Recommendation},
|
||||
serde::Raw,
|
||||
user_id, MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn serialization() {
|
||||
let room_event = OriginalPolicyRuleRoomEvent {
|
||||
event_id: event_id!("$143273582443PhrSn:example.org").to_owned(),
|
||||
sender: user_id!("@example:example.org").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(1_432_735_824_653_u64.try_into().unwrap()),
|
||||
room_id: room_id!("!jEsUZKDJdhlrceRyVU:example.org").to_owned(),
|
||||
state_key: "rule:#*:example.org".into(),
|
||||
unsigned: StateUnsigned { age: Some(int!(1234)), ..StateUnsigned::default() },
|
||||
content: PolicyRuleRoomEventContent(PolicyRuleEventContent {
|
||||
let content = PolicyRuleRoomEventContent(PolicyRuleEventContent {
|
||||
entity: "#*:example.org".into(),
|
||||
reason: "undesirable content".into(),
|
||||
recommendation: Recommendation::Ban,
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
let json = json!({
|
||||
"content": {
|
||||
"entity": "#*:example.org",
|
||||
"reason": "undesirable content",
|
||||
"recommendation": "m.ban"
|
||||
},
|
||||
"event_id": "$143273582443PhrSn:example.org",
|
||||
"origin_server_ts": 1_432_735_824_653_u64,
|
||||
"room_id": "!jEsUZKDJdhlrceRyVU:example.org",
|
||||
"sender": "@example:example.org",
|
||||
"state_key": "rule:#*:example.org",
|
||||
"type": "m.policy.rule.room",
|
||||
"unsigned": {
|
||||
"age": 1234
|
||||
}
|
||||
});
|
||||
|
||||
assert_eq!(to_json_value(room_event).unwrap(), json);
|
||||
assert_eq!(to_json_value(content).unwrap(), json);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -83,35 +83,28 @@ mod tests {
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
use super::{PresenceEvent, PresenceEventContent};
|
||||
use crate::{mxc_uri, presence::PresenceState, user_id};
|
||||
use crate::{mxc_uri, presence::PresenceState};
|
||||
|
||||
#[test]
|
||||
fn serialization() {
|
||||
let event = PresenceEvent {
|
||||
content: PresenceEventContent {
|
||||
let content = PresenceEventContent {
|
||||
avatar_url: Some(mxc_uri!("mxc://localhost/wefuiwegh8742w").to_owned()),
|
||||
currently_active: Some(false),
|
||||
displayname: None,
|
||||
last_active_ago: Some(uint!(2_478_593)),
|
||||
presence: PresenceState::Online,
|
||||
status_msg: Some("Making cupcakes".into()),
|
||||
},
|
||||
sender: user_id!("@example:localhost").to_owned(),
|
||||
};
|
||||
|
||||
let json = json!({
|
||||
"content": {
|
||||
"avatar_url": "mxc://localhost/wefuiwegh8742w",
|
||||
"currently_active": false,
|
||||
"last_active_ago": 2_478_593,
|
||||
"presence": "online",
|
||||
"status_msg": "Making cupcakes"
|
||||
},
|
||||
"sender": "@example:localhost",
|
||||
"type": "m.presence"
|
||||
});
|
||||
|
||||
assert_eq!(to_json_value(&event).unwrap(), json);
|
||||
assert_eq!(to_json_value(&content).unwrap(), json);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -39,42 +39,21 @@ impl RoomCanonicalAliasEventContent {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use js_int::uint;
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
use super::RoomCanonicalAliasEventContent;
|
||||
use crate::{
|
||||
event_id,
|
||||
events::{EmptyStateKey, OriginalStateEvent, StateUnsigned},
|
||||
room_alias_id, room_id, user_id, MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use crate::{events::OriginalStateEvent, room_alias_id};
|
||||
|
||||
#[test]
|
||||
fn serialization_with_optional_fields_as_none() {
|
||||
let canonical_alias_event = OriginalStateEvent {
|
||||
content: RoomCanonicalAliasEventContent {
|
||||
let content = RoomCanonicalAliasEventContent {
|
||||
alias: Some(room_alias_id!("#somewhere:localhost").to_owned()),
|
||||
alt_aliases: Vec::new(),
|
||||
},
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
room_id: room_id!("!dummy:example.com").to_owned(),
|
||||
sender: user_id!("@carl:example.com").to_owned(),
|
||||
state_key: EmptyStateKey,
|
||||
unsigned: StateUnsigned::default(),
|
||||
};
|
||||
|
||||
let actual = to_json_value(&canonical_alias_event).unwrap();
|
||||
let actual = to_json_value(&content).unwrap();
|
||||
let expected = json!({
|
||||
"content": {
|
||||
"alias": "#somewhere:localhost"
|
||||
},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
"origin_server_ts": 1,
|
||||
"room_id": "!dummy:example.com",
|
||||
"sender": "@carl:example.com",
|
||||
"state_key": "",
|
||||
"type": "m.room.canonical_alias"
|
||||
"alias": "#somewhere:localhost",
|
||||
});
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
|
@ -30,41 +30,18 @@ impl RoomNameEventContent {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use assert_matches::assert_matches;
|
||||
use js_int::{int, uint};
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
use super::RoomNameEventContent;
|
||||
use crate::{
|
||||
event_id,
|
||||
events::{EmptyStateKey, OriginalStateEvent, StateUnsigned},
|
||||
room_id,
|
||||
serde::Raw,
|
||||
user_id, MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use crate::{events::OriginalStateEvent, serde::Raw};
|
||||
|
||||
#[test]
|
||||
fn serialization_with_optional_fields_as_none() {
|
||||
let name_event = OriginalStateEvent {
|
||||
content: RoomNameEventContent { name: Some("The room name".to_owned()) },
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
room_id: room_id!("!n8f893n9:example.com").to_owned(),
|
||||
sender: user_id!("@carl:example.com").to_owned(),
|
||||
state_key: EmptyStateKey,
|
||||
unsigned: StateUnsigned::default(),
|
||||
};
|
||||
let content = RoomNameEventContent { name: Some("The room name".to_owned()) };
|
||||
|
||||
let actual = to_json_value(&name_event).unwrap();
|
||||
let actual = to_json_value(&content).unwrap();
|
||||
let expected = json!({
|
||||
"content": {
|
||||
"name": "The room name"
|
||||
},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
"origin_server_ts": 1,
|
||||
"room_id": "!n8f893n9:example.com",
|
||||
"sender": "@carl:example.com",
|
||||
"state_key": "",
|
||||
"type": "m.room.name"
|
||||
"name": "The room name",
|
||||
});
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
@ -72,35 +49,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn serialization_with_all_fields() {
|
||||
let name_event = OriginalStateEvent {
|
||||
content: RoomNameEventContent { name: Some("The room name".to_owned()) },
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
room_id: room_id!("!n8f893n9:example.com").to_owned(),
|
||||
sender: user_id!("@carl:example.com").to_owned(),
|
||||
state_key: EmptyStateKey,
|
||||
unsigned: StateUnsigned {
|
||||
age: Some(int!(100)),
|
||||
prev_content: Some(RoomNameEventContent { name: Some("The old name".to_owned()) }),
|
||||
..StateUnsigned::default()
|
||||
},
|
||||
};
|
||||
let content = RoomNameEventContent { name: Some("The room name".to_owned()) };
|
||||
|
||||
let actual = to_json_value(&name_event).unwrap();
|
||||
let actual = to_json_value(&content).unwrap();
|
||||
let expected = json!({
|
||||
"content": {
|
||||
"name": "The room name"
|
||||
},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
"origin_server_ts": 1,
|
||||
"room_id": "!n8f893n9:example.com",
|
||||
"sender": "@carl:example.com",
|
||||
"state_key": "",
|
||||
"type": "m.room.name",
|
||||
"unsigned": {
|
||||
"age": 100,
|
||||
"prev_content": { "name": "The old name" },
|
||||
}
|
||||
"name": "The room name",
|
||||
});
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
|
@ -28,10 +28,7 @@ impl RoomPinnedEventsEventContent {
|
||||
#[cfg(all(test, feature = "rand"))]
|
||||
mod tests {
|
||||
use super::RoomPinnedEventsEventContent;
|
||||
use crate::{
|
||||
events::{EmptyStateKey, OriginalStateEvent, StateUnsigned},
|
||||
server_name, EventId, MilliSecondsSinceUnixEpoch, RoomId, UserId,
|
||||
};
|
||||
use crate::{server_name, EventId};
|
||||
|
||||
#[test]
|
||||
fn serialization_deserialization() {
|
||||
@ -42,28 +39,10 @@ mod tests {
|
||||
content.pinned.push(EventId::new(server_name));
|
||||
content.pinned.push(EventId::new(server_name));
|
||||
|
||||
let event = OriginalStateEvent {
|
||||
content: content.clone(),
|
||||
event_id: EventId::new(server_name),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(1_432_804_485_886_u64.try_into().unwrap()),
|
||||
room_id: RoomId::new(server_name),
|
||||
sender: UserId::new(server_name),
|
||||
state_key: EmptyStateKey,
|
||||
unsigned: StateUnsigned::default(),
|
||||
};
|
||||
let serialized = serde_json::to_string(&content).unwrap();
|
||||
let parsed_content: RoomPinnedEventsEventContent =
|
||||
serde_json::from_str(&serialized).unwrap();
|
||||
|
||||
let serialized_event = serde_json::to_string(&event).unwrap();
|
||||
let parsed_event: OriginalStateEvent<RoomPinnedEventsEventContent> =
|
||||
serde_json::from_str(&serialized_event).unwrap();
|
||||
|
||||
assert_eq!(parsed_event.event_id, event.event_id);
|
||||
assert_eq!(parsed_event.room_id, event.room_id);
|
||||
assert_eq!(parsed_event.sender, event.sender);
|
||||
assert_eq!(parsed_event.state_key, event.state_key);
|
||||
assert_eq!(parsed_event.origin_server_ts, event.origin_server_ts);
|
||||
|
||||
assert_eq!(parsed_event.content.pinned, event.content.pinned);
|
||||
assert_eq!(parsed_event.content.pinned[0], content.pinned[0]);
|
||||
assert_eq!(parsed_event.content.pinned[1], content.pinned[1]);
|
||||
assert_eq!(parsed_content.pinned, content.pinned);
|
||||
}
|
||||
}
|
||||
|
@ -351,23 +351,18 @@ mod tests {
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use assign::assign;
|
||||
use js_int::{int, uint};
|
||||
use js_int::int;
|
||||
use maplit::btreemap;
|
||||
use serde_json::{json, to_value as to_json_value};
|
||||
|
||||
use super::{default_power_level, NotificationPowerLevels, RoomPowerLevelsEventContent};
|
||||
use crate::{
|
||||
event_id,
|
||||
events::{EmptyStateKey, OriginalStateEvent, StateUnsigned},
|
||||
room_id, user_id, MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use crate::user_id;
|
||||
|
||||
#[test]
|
||||
fn serialization_with_optional_fields_as_none() {
|
||||
let default = default_power_level();
|
||||
|
||||
let power_levels_event = OriginalStateEvent {
|
||||
content: RoomPowerLevelsEventContent {
|
||||
let power_levels = RoomPowerLevelsEventContent {
|
||||
ban: default,
|
||||
events: BTreeMap::new(),
|
||||
events_default: int!(0),
|
||||
@ -378,25 +373,10 @@ mod tests {
|
||||
users: BTreeMap::new(),
|
||||
users_default: int!(0),
|
||||
notifications: NotificationPowerLevels::default(),
|
||||
},
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
room_id: room_id!("!n8f893n9:example.com").to_owned(),
|
||||
unsigned: StateUnsigned::default(),
|
||||
sender: user_id!("@carl:example.com").to_owned(),
|
||||
state_key: EmptyStateKey,
|
||||
};
|
||||
|
||||
let actual = to_json_value(&power_levels_event).unwrap();
|
||||
let expected = json!({
|
||||
"content": {},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
"origin_server_ts": 1,
|
||||
"room_id": "!n8f893n9:example.com",
|
||||
"sender": "@carl:example.com",
|
||||
"state_key": "",
|
||||
"type": "m.room.power_levels"
|
||||
});
|
||||
let actual = to_json_value(&power_levels).unwrap();
|
||||
let expected = json!({});
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
@ -404,8 +384,7 @@ mod tests {
|
||||
#[test]
|
||||
fn serialization_with_all_fields() {
|
||||
let user = user_id!("@carl:example.com");
|
||||
let power_levels_event = OriginalStateEvent {
|
||||
content: RoomPowerLevelsEventContent {
|
||||
let power_levels_event = RoomPowerLevelsEventContent {
|
||||
ban: int!(23),
|
||||
events: btreemap! {
|
||||
"m.dummy".into() => int!(23)
|
||||
@ -420,39 +399,10 @@ mod tests {
|
||||
},
|
||||
users_default: int!(23),
|
||||
notifications: assign!(NotificationPowerLevels::new(), { room: int!(23) }),
|
||||
},
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
room_id: room_id!("!n8f893n9:example.com").to_owned(),
|
||||
unsigned: StateUnsigned {
|
||||
age: Some(int!(100)),
|
||||
prev_content: Some(RoomPowerLevelsEventContent {
|
||||
// Make just one field different so we at least know they're two different
|
||||
// objects.
|
||||
ban: int!(42),
|
||||
events: btreemap! {
|
||||
"m.dummy".into() => int!(42)
|
||||
},
|
||||
events_default: int!(42),
|
||||
invite: int!(42),
|
||||
kick: int!(42),
|
||||
redact: int!(42),
|
||||
state_default: int!(42),
|
||||
users: btreemap! {
|
||||
user.to_owned() => int!(42)
|
||||
},
|
||||
users_default: int!(42),
|
||||
notifications: assign!(NotificationPowerLevels::new(), { room: int!(42) }),
|
||||
}),
|
||||
..StateUnsigned::default()
|
||||
},
|
||||
sender: user.to_owned(),
|
||||
state_key: EmptyStateKey,
|
||||
};
|
||||
|
||||
let actual = to_json_value(&power_levels_event).unwrap();
|
||||
let expected = json!({
|
||||
"content": {
|
||||
"ban": 23,
|
||||
"events": {
|
||||
"m.dummy": 23
|
||||
@ -468,35 +418,7 @@ mod tests {
|
||||
"users_default": 23,
|
||||
"notifications": {
|
||||
"room": 23
|
||||
}
|
||||
},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
"origin_server_ts": 1,
|
||||
"room_id": "!n8f893n9:example.com",
|
||||
"sender": "@carl:example.com",
|
||||
"state_key": "",
|
||||
"type": "m.room.power_levels",
|
||||
"unsigned": {
|
||||
"age": 100,
|
||||
"prev_content": {
|
||||
"ban": 42,
|
||||
"events": {
|
||||
"m.dummy": 42
|
||||
},
|
||||
"events_default": 42,
|
||||
"invite": 42,
|
||||
"kick": 42,
|
||||
"redact": 42,
|
||||
"state_default": 42,
|
||||
"users": {
|
||||
"@carl:example.com": 42
|
||||
},
|
||||
"users_default": 42,
|
||||
"notifications": {
|
||||
"room": 42
|
||||
},
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
|
@ -17,8 +17,7 @@ use crate::{
|
||||
|
||||
/// A possibly-redacted redaction event.
|
||||
#[allow(clippy::exhaustive_enums)]
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(untagged)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum RoomRedactionEvent {
|
||||
/// Original, unredacted form of the event.
|
||||
Original(OriginalRoomRedactionEvent),
|
||||
@ -29,8 +28,7 @@ pub enum RoomRedactionEvent {
|
||||
|
||||
/// A possibly-redacted redaction event without a `room_id`.
|
||||
#[allow(clippy::exhaustive_enums)]
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(untagged)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum SyncRoomRedactionEvent {
|
||||
/// Original, unredacted form of the event.
|
||||
Original(OriginalSyncRoomRedactionEvent),
|
||||
|
@ -44,35 +44,27 @@ impl ToDeviceRoomKeyEventContent {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{room_id, user_id, EventEncryptionAlgorithm};
|
||||
use crate::{room_id, EventEncryptionAlgorithm};
|
||||
use serde_json::{json, to_value as to_json_value};
|
||||
|
||||
use super::ToDeviceRoomKeyEventContent;
|
||||
use crate::events::ToDeviceEvent;
|
||||
|
||||
#[test]
|
||||
fn serialization() {
|
||||
let ev = ToDeviceEvent {
|
||||
content: ToDeviceRoomKeyEventContent {
|
||||
let content = ToDeviceRoomKeyEventContent {
|
||||
algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
|
||||
room_id: room_id!("!testroomid:example.org").to_owned(),
|
||||
session_id: "SessId".into(),
|
||||
session_key: "SessKey".into(),
|
||||
},
|
||||
sender: user_id!("@user:example.org").to_owned(),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(ev).unwrap(),
|
||||
to_json_value(content).unwrap(),
|
||||
json!({
|
||||
"type": "m.room_key",
|
||||
"content": {
|
||||
"algorithm": "m.megolm.v1.aes-sha2",
|
||||
"room_id": "!testroomid:example.org",
|
||||
"session_id": "SessId",
|
||||
"session_key": "SessKey",
|
||||
},
|
||||
"sender": "@user:example.org",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -245,19 +245,15 @@ mod tests {
|
||||
},
|
||||
);
|
||||
content.name = Some("my_key".to_owned());
|
||||
let event = GlobalAccountDataEvent { content };
|
||||
|
||||
let json = json!({
|
||||
"type": "m.secret_storage.key.my_key_id",
|
||||
"content": {
|
||||
"name": "my_key",
|
||||
"algorithm": "m.secret_storage.v1.aes-hmac-sha2",
|
||||
"iv": "YWJjZGVmZ2hpamtsbW5vcA",
|
||||
"mac": "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U"
|
||||
}
|
||||
});
|
||||
|
||||
assert_eq!(to_json_value(&event).unwrap(), json);
|
||||
assert_eq!(to_json_value(&content).unwrap(), json);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -75,7 +75,7 @@ mod tests {
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
use super::{HierarchySpaceChildEvent, SpaceChildEventContent};
|
||||
use crate::{server_name, user_id, MilliSecondsSinceUnixEpoch};
|
||||
use crate::{server_name, MilliSecondsSinceUnixEpoch};
|
||||
|
||||
#[test]
|
||||
fn space_child_serialization() {
|
||||
@ -105,29 +105,18 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hierarchy_space_child_serialization() {
|
||||
let event = HierarchySpaceChildEvent {
|
||||
content: SpaceChildEventContent {
|
||||
let content = SpaceChildEventContent {
|
||||
via: Some(vec![server_name!("example.com").to_owned()]),
|
||||
order: Some("uwu".to_owned()),
|
||||
suggested: None,
|
||||
},
|
||||
sender: user_id!("@example:localhost").to_owned(),
|
||||
state_key: "!child:localhost".to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1_629_413_349)),
|
||||
};
|
||||
|
||||
let json = json!({
|
||||
"content": {
|
||||
"via": ["example.com"],
|
||||
"order": "uwu",
|
||||
},
|
||||
"sender": "@example:localhost",
|
||||
"state_key": "!child:localhost",
|
||||
"origin_server_ts": 1_629_413_349,
|
||||
"type": "m.space.child",
|
||||
});
|
||||
|
||||
assert_eq!(to_json_value(&event).unwrap(), json);
|
||||
assert_eq!(to_json_value(&content).unwrap(), json);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -143,7 +143,7 @@ impl<C: StateEventContent> Default for StateUnsigned<C> {
|
||||
}
|
||||
|
||||
/// Extra information about a redacted event that is not incorporated into the event's hash.
|
||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||
#[derive(Clone, Debug, Default, Deserialize)]
|
||||
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
||||
pub struct RedactedUnsigned {
|
||||
/// The event that redacted this event, if any.
|
||||
|
@ -16,11 +16,11 @@ use ruma_common::{
|
||||
message::{AudioMessageEventContent, MessageType, Relation, RoomMessageEventContent},
|
||||
JsonWebKeyInit, MediaSource,
|
||||
},
|
||||
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
|
||||
AnyMessageLikeEvent, MessageLikeEvent,
|
||||
},
|
||||
mxc_uri, room_id,
|
||||
mxc_uri,
|
||||
serde::{Base64, CanBeEmpty},
|
||||
user_id, MilliSecondsSinceUnixEpoch,
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
@ -129,8 +129,7 @@ fn encrypted_content_serialization() {
|
||||
|
||||
#[test]
|
||||
fn event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: assign!(
|
||||
let content = assign!(
|
||||
AudioEventContent::with_message(
|
||||
MessageContent::html(
|
||||
"Upload: my_mix.mp3",
|
||||
@ -159,18 +158,11 @@ fn event_serialization() {
|
||||
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
|
||||
}),
|
||||
}
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"org.matrix.msc1767.html": "Upload: <strong>my_mix.mp3</strong>",
|
||||
"org.matrix.msc1767.text": "Upload: my_mix.mp3",
|
||||
"m.file": {
|
||||
@ -186,13 +178,7 @@ fn event_serialization() {
|
||||
"m.in_reply_to": {
|
||||
"event_id": "$replyevent:example.com"
|
||||
}
|
||||
}
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.audio",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ use assert_matches::assert_matches;
|
||||
use assign::assign;
|
||||
use js_int::uint;
|
||||
use ruma_common::{
|
||||
event_id,
|
||||
events::{
|
||||
call::{
|
||||
answer::CallAnswerEventContent,
|
||||
@ -17,9 +16,9 @@ use ruma_common::{
|
||||
AnswerSessionDescription, CallCapabilities, OfferSessionDescription,
|
||||
SessionDescription, SessionDescriptionType,
|
||||
},
|
||||
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
|
||||
AnyMessageLikeEvent, MessageLikeEvent,
|
||||
},
|
||||
room_id, user_id, MilliSecondsSinceUnixEpoch, VoipVersionId,
|
||||
VoipVersionId,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
@ -47,25 +46,17 @@ fn invite_content_serialization() {
|
||||
|
||||
#[test]
|
||||
fn invite_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: CallInviteEventContent::version_1(
|
||||
let content = CallInviteEventContent::version_1(
|
||||
"abcdef".into(),
|
||||
"9876".into(),
|
||||
uint!(60000),
|
||||
OfferSessionDescription::new("not a real sdp".to_owned()),
|
||||
CallCapabilities::new(),
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"call_id": "abcdef",
|
||||
"party_id": "9876",
|
||||
"lifetime": 60000,
|
||||
@ -74,12 +65,6 @@ fn invite_event_serialization() {
|
||||
"type": "offer",
|
||||
"sdp": "not a real sdp",
|
||||
},
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.call.invite",
|
||||
})
|
||||
);
|
||||
}
|
||||
@ -140,24 +125,16 @@ fn answer_content_serialization() {
|
||||
|
||||
#[test]
|
||||
fn answer_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: CallAnswerEventContent::version_1(
|
||||
let content = CallAnswerEventContent::version_1(
|
||||
AnswerSessionDescription::new("not a real sdp".to_owned()),
|
||||
"abcdef".into(),
|
||||
"9876".into(),
|
||||
assign!(CallCapabilities::new(), { dtmf: true }),
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"call_id": "abcdef",
|
||||
"party_id": "9876",
|
||||
"version": "1",
|
||||
@ -168,12 +145,6 @@ fn answer_event_serialization() {
|
||||
"capabilities": {
|
||||
"m.call.dtmf": true,
|
||||
},
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.call.answer",
|
||||
})
|
||||
);
|
||||
}
|
||||
@ -238,26 +209,18 @@ fn candidates_content_serialization() {
|
||||
|
||||
#[test]
|
||||
fn candidates_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: CallCandidatesEventContent::version_1(
|
||||
let content = CallCandidatesEventContent::version_1(
|
||||
"abcdef".into(),
|
||||
"9876".into(),
|
||||
vec![
|
||||
Candidate::new("not a real candidate".to_owned(), "0".to_owned(), uint!(0)),
|
||||
Candidate::new("another fake candidate".to_owned(), "0".to_owned(), uint!(1)),
|
||||
],
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"call_id": "abcdef",
|
||||
"party_id": "9876",
|
||||
"version": "1",
|
||||
@ -273,12 +236,6 @@ fn candidates_event_serialization() {
|
||||
"sdpMLineIndex": 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.call.candidates",
|
||||
})
|
||||
);
|
||||
}
|
||||
@ -344,33 +301,16 @@ fn hangup_content_serialization() {
|
||||
|
||||
#[test]
|
||||
fn hangup_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: CallHangupEventContent::version_1(
|
||||
"abcdef".into(),
|
||||
"9876".into(),
|
||||
Reason::IceFailed,
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
let content =
|
||||
CallHangupEventContent::version_1("abcdef".into(), "9876".into(), Reason::IceFailed);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"call_id": "abcdef",
|
||||
"party_id": "9876",
|
||||
"version": "1",
|
||||
"reason": "ice_failed",
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.call.hangup",
|
||||
})
|
||||
);
|
||||
}
|
||||
@ -404,37 +344,23 @@ fn hangup_event_deserialization() {
|
||||
|
||||
#[test]
|
||||
fn negotiate_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: CallNegotiateEventContent::new(
|
||||
let content = CallNegotiateEventContent::new(
|
||||
"abcdef".into(),
|
||||
"9876".into(),
|
||||
uint!(30000),
|
||||
SessionDescription::new(SessionDescriptionType::Offer, "not a real sdp".to_owned()),
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"call_id": "abcdef",
|
||||
"party_id": "9876",
|
||||
"lifetime": 30000,
|
||||
"description": {
|
||||
"type": "offer",
|
||||
"sdp": "not a real sdp",
|
||||
}
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.call.negotiate",
|
||||
})
|
||||
);
|
||||
}
|
||||
@ -473,28 +399,14 @@ fn negotiate_event_deserialization() {
|
||||
|
||||
#[test]
|
||||
fn reject_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: CallRejectEventContent::version_1("abcdef".into(), "9876".into()),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
let content = CallRejectEventContent::version_1("abcdef".into(), "9876".into());
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"call_id": "abcdef",
|
||||
"party_id": "9876",
|
||||
"version": "1",
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.call.reject",
|
||||
})
|
||||
);
|
||||
}
|
||||
@ -527,33 +439,16 @@ fn reject_event_deserialization() {
|
||||
|
||||
#[test]
|
||||
fn select_answer_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: CallSelectAnswerEventContent::version_1(
|
||||
"abcdef".into(),
|
||||
"9876".into(),
|
||||
"6336".into(),
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
let content =
|
||||
CallSelectAnswerEventContent::version_1("abcdef".into(), "9876".into(), "6336".into());
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"call_id": "abcdef",
|
||||
"party_id": "9876",
|
||||
"selected_party_id": "6336",
|
||||
"version": "1",
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.call.select_answer",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -1,16 +1,13 @@
|
||||
use assert_matches::assert_matches;
|
||||
use js_int::{int, uint};
|
||||
use js_int::int;
|
||||
use ruma_common::{
|
||||
event_id,
|
||||
events::{MessageLikeEvent, StateEvent, SyncMessageLikeEvent, SyncStateEvent},
|
||||
room_alias_id, room_id,
|
||||
room_alias_id,
|
||||
serde::test::serde_json_eq,
|
||||
user_id,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, json, Value as JsonValue};
|
||||
|
||||
use ruma_common::{
|
||||
events::{
|
||||
use ruma_common::events::{
|
||||
room::{
|
||||
aliases::RoomAliasesEventContent,
|
||||
message::{MessageType, RoomMessageEventContent},
|
||||
@ -18,11 +15,9 @@ use ruma_common::{
|
||||
},
|
||||
AnyEphemeralRoomEvent, AnyMessageLikeEvent, AnyStateEvent, AnySyncMessageLikeEvent,
|
||||
AnySyncStateEvent, AnySyncTimelineEvent, AnyTimelineEvent, EphemeralRoomEventType,
|
||||
GlobalAccountDataEventType, MessageLikeEventType, MessageLikeUnsigned,
|
||||
OriginalMessageLikeEvent, OriginalStateEvent, OriginalSyncMessageLikeEvent,
|
||||
OriginalSyncStateEvent, RoomAccountDataEventType, StateEventType, ToDeviceEventType,
|
||||
},
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
GlobalAccountDataEventType, MessageLikeEventType, OriginalMessageLikeEvent, OriginalStateEvent,
|
||||
OriginalSyncMessageLikeEvent, OriginalSyncStateEvent, RoomAccountDataEventType, StateEventType,
|
||||
ToDeviceEventType,
|
||||
};
|
||||
|
||||
fn message_event() -> JsonValue {
|
||||
@ -205,25 +200,18 @@ fn message_room_event_deserialization() {
|
||||
|
||||
#[test]
|
||||
fn message_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: RoomMessageEventContent::text_plain("test"),
|
||||
event_id: event_id!("$1234:example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(0)),
|
||||
room_id: room_id!("!roomid:example.com").to_owned(),
|
||||
sender: user_id!("@test:example.com").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
let content = RoomMessageEventContent::text_plain("test");
|
||||
|
||||
#[cfg(not(feature = "unstable-msc1767"))]
|
||||
assert_eq!(
|
||||
serde_json::to_string(&event).expect("Failed to serialize message event"),
|
||||
r#"{"type":"m.room.message","content":{"msgtype":"m.text","body":"test"},"event_id":"$1234:example.com","sender":"@test:example.com","origin_server_ts":0,"room_id":"!roomid:example.com"}"#
|
||||
serde_json::to_string(&content).expect("Failed to serialize message event content"),
|
||||
r#"{"msgtype":"m.text","body":"test"}"#
|
||||
);
|
||||
|
||||
#[cfg(feature = "unstable-msc1767")]
|
||||
assert_eq!(
|
||||
serde_json::to_string(&event).expect("Failed to serialize message event"),
|
||||
r#"{"type":"m.room.message","content":{"msgtype":"m.text","body":"test","org.matrix.msc1767.text":"test"},"event_id":"$1234:example.com","sender":"@test:example.com","origin_server_ts":0,"room_id":"!roomid:example.com"}"#
|
||||
serde_json::to_string(&content).expect("Failed to serialize message event content"),
|
||||
r#"{"msgtype":"m.text","body":"test","org.matrix.msc1767.text":"test"}"#
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1,31 +1,22 @@
|
||||
use assert_matches::assert_matches;
|
||||
use js_int::uint;
|
||||
use maplit::btreemap;
|
||||
use ruma_common::{
|
||||
event_id, events::receipt::ReceiptType, room_id, user_id, MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use ruma_common::{event_id, events::receipt::ReceiptType, user_id, MilliSecondsSinceUnixEpoch};
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
use ruma_common::events::{
|
||||
receipt::{Receipt, ReceiptEventContent},
|
||||
typing::TypingEventContent,
|
||||
AnyEphemeralRoomEvent, EphemeralRoomEvent,
|
||||
AnyEphemeralRoomEvent,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn ephemeral_serialize_typing() {
|
||||
let aliases_event = EphemeralRoomEvent {
|
||||
content: TypingEventContent::new(vec![user_id!("@carl:example.com").to_owned()]),
|
||||
room_id: room_id!("!roomid:room.com").to_owned(),
|
||||
};
|
||||
let content = TypingEventContent::new(vec![user_id!("@carl:example.com").to_owned()]);
|
||||
|
||||
let actual = to_json_value(&aliases_event).unwrap();
|
||||
let actual = to_json_value(&content).unwrap();
|
||||
let expected = json!({
|
||||
"content": {
|
||||
"user_ids": [ "@carl:example.com" ]
|
||||
},
|
||||
"room_id": "!roomid:room.com",
|
||||
"type": "m.typing",
|
||||
"user_ids": ["@carl:example.com"],
|
||||
});
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
@ -55,28 +46,21 @@ fn ephemeral_serialize_receipt() {
|
||||
let event_id = event_id!("$h29iv0s8:example.com").to_owned();
|
||||
let user_id = user_id!("@carl:example.com").to_owned();
|
||||
|
||||
let aliases_event = EphemeralRoomEvent {
|
||||
content: ReceiptEventContent(btreemap! {
|
||||
let content = ReceiptEventContent(btreemap! {
|
||||
event_id => btreemap! {
|
||||
ReceiptType::Read => btreemap! {
|
||||
user_id => Receipt::new(MilliSecondsSinceUnixEpoch(uint!(1))),
|
||||
},
|
||||
},
|
||||
}),
|
||||
room_id: room_id!("!roomid:room.com").to_owned(),
|
||||
};
|
||||
});
|
||||
|
||||
let actual = to_json_value(&aliases_event).unwrap();
|
||||
let actual = to_json_value(&content).unwrap();
|
||||
let expected = json!({
|
||||
"content": {
|
||||
"$h29iv0s8:example.com": {
|
||||
"m.read": {
|
||||
"@carl:example.com": { "ts": 1 }
|
||||
}
|
||||
}
|
||||
},
|
||||
"room_id": "!roomid:room.com",
|
||||
"type": "m.receipt"
|
||||
});
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
|
@ -13,11 +13,11 @@ use ruma_common::{
|
||||
message::{FileMessageEventContent, MessageType, Relation, RoomMessageEventContent},
|
||||
EncryptedFileInit, JsonWebKeyInit, MediaSource,
|
||||
},
|
||||
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
|
||||
AnyMessageLikeEvent, MessageLikeEvent,
|
||||
},
|
||||
mxc_uri, room_id,
|
||||
mxc_uri,
|
||||
serde::{Base64, CanBeEmpty},
|
||||
user_id, MilliSecondsSinceUnixEpoch,
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
@ -91,8 +91,7 @@ fn encrypted_content_serialization() {
|
||||
|
||||
#[test]
|
||||
fn file_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: assign!(
|
||||
let content = assign!(
|
||||
FileEventContent::plain_message(
|
||||
MessageContent::html(
|
||||
"Upload: my_file.txt",
|
||||
@ -113,18 +112,11 @@ fn file_event_serialization() {
|
||||
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
|
||||
}),
|
||||
}
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"org.matrix.msc1767.html": "Upload: <strong>my_file.txt</strong>",
|
||||
"org.matrix.msc1767.text": "Upload: my_file.txt",
|
||||
"m.file": {
|
||||
@ -137,13 +129,7 @@ fn file_event_serialization() {
|
||||
"m.in_reply_to": {
|
||||
"event_id": "$replyevent:example.com"
|
||||
}
|
||||
}
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.file",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -17,11 +17,11 @@ use ruma_common::{
|
||||
message::{ImageMessageEventContent, MessageType, Relation, RoomMessageEventContent},
|
||||
JsonWebKeyInit, MediaSource,
|
||||
},
|
||||
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
|
||||
AnyMessageLikeEvent, MessageLikeEvent,
|
||||
},
|
||||
mxc_uri, room_id,
|
||||
mxc_uri,
|
||||
serde::{Base64, CanBeEmpty},
|
||||
user_id, MilliSecondsSinceUnixEpoch,
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
@ -98,8 +98,7 @@ fn encrypted_content_serialization() {
|
||||
|
||||
#[test]
|
||||
fn image_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: assign!(
|
||||
let content = assign!(
|
||||
ImageEventContent::with_message(
|
||||
MessageContent::html(
|
||||
"Upload: my_house.jpg",
|
||||
@ -134,18 +133,11 @@ fn image_event_serialization() {
|
||||
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
|
||||
}),
|
||||
}
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"org.matrix.msc1767.html": "Upload: <strong>my_house.jpg</strong>",
|
||||
"org.matrix.msc1767.text": "Upload: my_house.jpg",
|
||||
"m.file": {
|
||||
@ -175,13 +167,7 @@ fn image_event_serialization() {
|
||||
"m.in_reply_to": {
|
||||
"event_id": "$replyevent:example.com"
|
||||
}
|
||||
}
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.image",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ use ruma_common::{
|
||||
room::message::{
|
||||
LocationMessageEventContent, MessageType, Relation, RoomMessageEventContent,
|
||||
},
|
||||
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
|
||||
AnyMessageLikeEvent, MessageLikeEvent,
|
||||
},
|
||||
room_id,
|
||||
serde::CanBeEmpty,
|
||||
@ -40,8 +40,7 @@ fn plain_content_serialization() {
|
||||
|
||||
#[test]
|
||||
fn event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: assign!(
|
||||
let content = assign!(
|
||||
LocationEventContent::with_message(
|
||||
MessageContent::html(
|
||||
"Alice was at geo:51.5008,0.1247;u=35 as of Sat Nov 13 18:50:58 2021",
|
||||
@ -61,18 +60,11 @@ fn event_serialization() {
|
||||
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
|
||||
}),
|
||||
}
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"org.matrix.msc1767.html": "Alice was at <strong>geo:51.5008,0.1247;u=35</strong> as of <em>Sat Nov 13 18:50:58 2021</em>",
|
||||
"org.matrix.msc1767.text": "Alice was at geo:51.5008,0.1247;u=35 as of Sat Nov 13 18:50:58 2021",
|
||||
"m.location": {
|
||||
@ -86,12 +78,6 @@ fn event_serialization() {
|
||||
"event_id": "$replyevent:example.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.location",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -11,11 +11,10 @@ use ruma_common::{
|
||||
notice::NoticeEventContent,
|
||||
relation::InReplyTo,
|
||||
room::message::{EmoteMessageEventContent, MessageType, Relation, RoomMessageEventContent},
|
||||
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
|
||||
AnyMessageLikeEvent, MessageLikeEvent,
|
||||
},
|
||||
room_id,
|
||||
serde::CanBeEmpty,
|
||||
user_id, MilliSecondsSinceUnixEpoch,
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
@ -146,27 +145,10 @@ fn relates_to_content_serialization() {
|
||||
|
||||
#[test]
|
||||
fn message_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: MessageEventContent::plain("Hello, World!"),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
|
||||
let content = MessageEventContent::plain("Hello, World!");
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"org.matrix.msc1767.text": "Hello, World!",
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.message",
|
||||
})
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({ "org.matrix.msc1767.text": "Hello, World!" })
|
||||
);
|
||||
}
|
||||
|
||||
@ -469,27 +451,10 @@ fn room_message_message_unstable_deserialization() {
|
||||
|
||||
#[test]
|
||||
fn notice_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: NoticeEventContent::plain("Hello, I'm a robot!"),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
|
||||
let content = NoticeEventContent::plain("Hello, I'm a robot!");
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"org.matrix.msc1767.text": "Hello, I'm a robot!",
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.notice",
|
||||
})
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({ "org.matrix.msc1767.text": "Hello, I'm a robot!" })
|
||||
);
|
||||
}
|
||||
|
||||
@ -616,30 +581,14 @@ fn room_message_notice_unstable_deserialization() {
|
||||
|
||||
#[test]
|
||||
fn emote_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: EmoteEventContent::html(
|
||||
"is testing some code…",
|
||||
"is testing some <code>code</code>…",
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
let content =
|
||||
EmoteEventContent::html("is testing some code…", "is testing some <code>code</code>…");
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"org.matrix.msc1767.html": "is testing some <code>code</code>…",
|
||||
"org.matrix.msc1767.text": "is testing some code…",
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.emote",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -2,23 +2,21 @@ use assert_matches::assert_matches;
|
||||
use assign::assign;
|
||||
use js_int::{uint, UInt};
|
||||
use ruma_common::{
|
||||
event_id,
|
||||
events::{
|
||||
room::{ImageInfo, MediaSource, ThumbnailInfo},
|
||||
sticker::StickerEventContent,
|
||||
AnyMessageLikeEvent, AnyMessageLikeEventContent, AnySyncMessageLikeEvent, MessageLikeEvent,
|
||||
MessageLikeEventType, MessageLikeUnsigned, OriginalMessageLikeEvent,
|
||||
MessageLikeEventType,
|
||||
},
|
||||
mxc_uri, room_id,
|
||||
serde::{CanBeEmpty, Raw},
|
||||
user_id, MilliSecondsSinceUnixEpoch, VoipVersionId,
|
||||
MilliSecondsSinceUnixEpoch, VoipVersionId,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
#[test]
|
||||
fn message_serialize_sticker() {
|
||||
let aliases_event = OriginalMessageLikeEvent {
|
||||
content: StickerEventContent::new(
|
||||
let content = StickerEventContent::new(
|
||||
"Hello".into(),
|
||||
assign!(ImageInfo::new(), {
|
||||
height: UInt::new(423),
|
||||
@ -34,19 +32,12 @@ fn message_serialize_sticker() {
|
||||
thumbnail_source: Some(MediaSource::Plain(mxc_uri!("mxc://matrix.org/irsns989Rrsn").to_owned())),
|
||||
}),
|
||||
mxc_uri!("mxc://matrix.org/rnsldl8srs98IRrs").to_owned(),
|
||||
),
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
room_id: room_id!("!roomid:room.com").to_owned(),
|
||||
sender: user_id!("@carl:example.com").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
);
|
||||
|
||||
let actual = to_json_value(&aliases_event).unwrap();
|
||||
let actual = to_json_value(&content).unwrap();
|
||||
|
||||
#[cfg(not(feature = "unstable-msc3552"))]
|
||||
let expected = json!({
|
||||
"content": {
|
||||
"body": "Hello",
|
||||
"info": {
|
||||
"h": 423,
|
||||
@ -61,18 +52,11 @@ fn message_serialize_sticker() {
|
||||
"thumbnail_url": "mxc://matrix.org/irsns989Rrsn",
|
||||
"w": 1011
|
||||
},
|
||||
"url": "mxc://matrix.org/rnsldl8srs98IRrs"
|
||||
},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
"origin_server_ts": 1,
|
||||
"room_id": "!roomid:room.com",
|
||||
"sender": "@carl:example.com",
|
||||
"type": "m.sticker",
|
||||
"url": "mxc://matrix.org/rnsldl8srs98IRrs",
|
||||
});
|
||||
|
||||
#[cfg(feature = "unstable-msc3552")]
|
||||
let expected = json!({
|
||||
"content": {
|
||||
"body": "Hello",
|
||||
"info": {
|
||||
"h": 423,
|
||||
@ -107,12 +91,6 @@ fn message_serialize_sticker() {
|
||||
"width": 800,
|
||||
}
|
||||
],
|
||||
},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
"origin_server_ts": 1,
|
||||
"room_id": "!roomid:room.com",
|
||||
"sender": "@carl:example.com",
|
||||
"type": "m.sticker",
|
||||
});
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
|
@ -16,9 +16,8 @@ use ruma_common::{
|
||||
},
|
||||
},
|
||||
relation::Reference,
|
||||
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
|
||||
AnyMessageLikeEvent, MessageLikeEvent,
|
||||
},
|
||||
room_id, user_id, MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
@ -105,8 +104,7 @@ fn start_content_serialization() {
|
||||
|
||||
#[test]
|
||||
fn start_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: PollStartEventContent::new(assign!(
|
||||
let content = PollStartEventContent::new(assign!(
|
||||
PollStartContent::new(
|
||||
MessageContent::plain("How's the weather?"),
|
||||
PollKind::Disclosed,
|
||||
@ -119,18 +117,11 @@ fn start_event_serialization() {
|
||||
.unwrap(),
|
||||
),
|
||||
{ max_selections: uint!(2) }
|
||||
)),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"org.matrix.msc3381.poll.start": {
|
||||
"question": { "org.matrix.msc1767.text": "How's the weather?" },
|
||||
"kind": "org.matrix.msc3381.poll.disclosed",
|
||||
@ -141,12 +132,6 @@ fn start_event_serialization() {
|
||||
{ "id": "amazing", "org.matrix.msc1767.text": "Amazing!"},
|
||||
]
|
||||
},
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "org.matrix.msc3381.poll.start",
|
||||
})
|
||||
);
|
||||
}
|
||||
@ -255,35 +240,21 @@ fn response_content_serialization() {
|
||||
|
||||
#[test]
|
||||
fn response_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: PollResponseEventContent::new(
|
||||
let content = PollResponseEventContent::new(
|
||||
PollResponseContent::new(vec!["first-answer".to_owned(), "second-answer".to_owned()]),
|
||||
event_id!("$related_event:notareal.hs").to_owned(),
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"org.matrix.msc3381.poll.response": {
|
||||
"answers": ["first-answer", "second-answer"],
|
||||
},
|
||||
"m.relates_to": {
|
||||
"rel_type": "m.reference",
|
||||
"event_id": "$related_event:notareal.hs",
|
||||
}
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "org.matrix.msc3381.poll.response",
|
||||
})
|
||||
);
|
||||
}
|
||||
@ -380,33 +351,19 @@ fn end_content_serialization() {
|
||||
|
||||
#[test]
|
||||
fn end_event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: PollEndEventContent::new(
|
||||
let content = PollEndEventContent::new(
|
||||
PollEndContent::new(),
|
||||
event_id!("$related_event:notareal.hs").to_owned(),
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"org.matrix.msc3381.poll.end": {},
|
||||
"m.relates_to": {
|
||||
"rel_type": "m.reference",
|
||||
"event_id": "$related_event:notareal.hs",
|
||||
}
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "org.matrix.msc3381.poll.end",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -1,117 +1,55 @@
|
||||
use assert_matches::assert_matches;
|
||||
use js_int::uint;
|
||||
use ruma_common::{
|
||||
event_id,
|
||||
events::{
|
||||
room::{
|
||||
aliases::RedactedRoomAliasesEventContent,
|
||||
create::{RedactedRoomCreateEventContent, RoomCreateEventContent},
|
||||
message::{RedactedRoomMessageEventContent, RoomMessageEventContent},
|
||||
redaction::{
|
||||
OriginalSyncRoomRedactionEvent, RoomRedactionEventContent, SyncRoomRedactionEvent,
|
||||
},
|
||||
redaction::RoomRedactionEventContent,
|
||||
},
|
||||
AnyMessageLikeEvent, AnySyncMessageLikeEvent, AnySyncStateEvent, AnySyncTimelineEvent,
|
||||
AnyTimelineEvent, EventContent, MessageLikeEvent, MessageLikeUnsigned, RedactContent,
|
||||
RedactedSyncMessageLikeEvent, RedactedSyncStateEvent, RedactedUnsigned,
|
||||
SyncMessageLikeEvent, SyncStateEvent,
|
||||
AnyTimelineEvent, EventContent, MessageLikeEvent, RedactContent, SyncMessageLikeEvent,
|
||||
SyncStateEvent,
|
||||
},
|
||||
server_name, user_id, MilliSecondsSinceUnixEpoch, RoomVersionId,
|
||||
RoomVersionId,
|
||||
};
|
||||
use serde_json::{
|
||||
from_value as from_json_value, json, to_value as to_json_value,
|
||||
value::to_raw_value as to_raw_json_value,
|
||||
value::to_raw_value as to_raw_json_value, Value as JsonValue,
|
||||
};
|
||||
|
||||
fn unsigned() -> RedactedUnsigned {
|
||||
let mut unsigned = RedactedUnsigned::default();
|
||||
unsigned.redacted_because =
|
||||
Some(Box::new(SyncRoomRedactionEvent::Original(OriginalSyncRoomRedactionEvent {
|
||||
content: RoomRedactionEventContent::with_reason("redacted because".into()),
|
||||
redacts: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
sender: user_id!("@carl:example.com").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
})));
|
||||
|
||||
unsigned
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redacted_message_event_serialize() {
|
||||
let redacted = RedactedSyncMessageLikeEvent {
|
||||
content: RedactedRoomMessageEventContent::new(),
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
sender: user_id!("@carl:example.com").to_owned(),
|
||||
unsigned: RedactedUnsigned::default(),
|
||||
};
|
||||
|
||||
let expected = json!({
|
||||
"content": {},
|
||||
fn unsigned() -> JsonValue {
|
||||
json!({
|
||||
"redacted_because": {
|
||||
"type": "m.room.redaction",
|
||||
"content": RoomRedactionEventContent::with_reason("redacted because".into()),
|
||||
"redacts": "$h29iv0s8:example.com",
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
"origin_server_ts": 1,
|
||||
"sender": "@carl:example.com",
|
||||
"type": "m.room.message",
|
||||
});
|
||||
|
||||
let actual = to_json_value(&redacted).unwrap();
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redacted_aliases_event_serialize_empty_content() {
|
||||
let redacted = RedactedSyncStateEvent {
|
||||
content: RedactedRoomAliasesEventContent::default(),
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
state_key: server_name!("example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
sender: user_id!("@carl:example.com").to_owned(),
|
||||
unsigned: RedactedUnsigned::default(),
|
||||
};
|
||||
fn serialize_redacted_message_event_content() {
|
||||
assert_eq!(to_json_value(RedactedRoomMessageEventContent::new()).unwrap(), json!({}));
|
||||
}
|
||||
|
||||
let expected = json!({
|
||||
"content": {},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
"state_key": "example.com",
|
||||
"origin_server_ts": 1,
|
||||
"sender": "@carl:example.com",
|
||||
"type": "m.room.aliases",
|
||||
});
|
||||
|
||||
let actual = to_json_value(&redacted).unwrap();
|
||||
assert_eq!(actual, expected);
|
||||
#[test]
|
||||
fn serialize_empty_redacted_aliases_event_content() {
|
||||
assert_eq!(to_json_value(&RedactedRoomAliasesEventContent::default()).unwrap(), json!({}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redacted_aliases_event_serialize_with_content() {
|
||||
let redacted = RedactedSyncStateEvent {
|
||||
content: RedactedRoomAliasesEventContent::new_v1(vec![]),
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
state_key: server_name!("example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
sender: user_id!("@carl:example.com").to_owned(),
|
||||
unsigned: RedactedUnsigned::default(),
|
||||
};
|
||||
|
||||
let expected = json!({
|
||||
"content": {
|
||||
"aliases": []
|
||||
},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
"state_key": "example.com",
|
||||
"origin_server_ts": 1,
|
||||
"sender": "@carl:example.com",
|
||||
"type": "m.room.aliases",
|
||||
});
|
||||
|
||||
let actual = to_json_value(&redacted).unwrap();
|
||||
let expected = json!({ "aliases": [] });
|
||||
let actual = to_json_value(&RedactedRoomAliasesEventContent::new_v1(vec![])).unwrap();
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redacted_aliases_deserialize() {
|
||||
fn deserialize_redacted_aliases() {
|
||||
let redacted = json!({
|
||||
"content": {},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
@ -135,7 +73,7 @@ fn redacted_aliases_deserialize() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redacted_deserialize_any_room() {
|
||||
fn deserialize_redacted_any_room() {
|
||||
let redacted = json!({
|
||||
"content": {},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
@ -159,27 +97,13 @@ fn redacted_deserialize_any_room() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redacted_deserialize_any_room_sync() {
|
||||
let mut unsigned = RedactedUnsigned::default();
|
||||
// The presence of `redacted_because` triggers the event enum (AnySyncTimelineEvent in this
|
||||
// case) to return early with `RedactedContent` instead of failing to deserialize according
|
||||
// to the event type string.
|
||||
unsigned.redacted_because =
|
||||
Some(Box::new(SyncRoomRedactionEvent::Original(OriginalSyncRoomRedactionEvent {
|
||||
content: RoomRedactionEventContent::with_reason("redacted because".into()),
|
||||
redacts: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
sender: user_id!("@carl:example.com").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
})));
|
||||
|
||||
fn deserialize_redacted_any_room_sync() {
|
||||
let redacted = json!({
|
||||
"content": {},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
"origin_server_ts": 1,
|
||||
"sender": "@carl:example.com",
|
||||
"unsigned": unsigned,
|
||||
"unsigned": unsigned(),
|
||||
"type": "m.room.message",
|
||||
});
|
||||
|
||||
@ -195,7 +119,7 @@ fn redacted_deserialize_any_room_sync() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redacted_state_event_deserialize() {
|
||||
fn deserialize_redacted_state_event() {
|
||||
let redacted = json!({
|
||||
"content": {
|
||||
"creator": "@carl:example.com",
|
||||
@ -220,7 +144,7 @@ fn redacted_state_event_deserialize() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redacted_custom_event_deserialize() {
|
||||
fn deserialize_redacted_custom_event() {
|
||||
let redacted = json!({
|
||||
"content": {},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
|
@ -1,25 +1,32 @@
|
||||
use assert_matches::assert_matches;
|
||||
use js_int::uint;
|
||||
use ruma_common::{
|
||||
event_id,
|
||||
events::{
|
||||
room::redaction::{
|
||||
OriginalRoomRedactionEvent, RoomRedactionEvent, RoomRedactionEventContent,
|
||||
room::redaction::{RoomRedactionEvent, RoomRedactionEventContent},
|
||||
AnyMessageLikeEvent,
|
||||
},
|
||||
AnyMessageLikeEvent, MessageLikeUnsigned,
|
||||
},
|
||||
room_id,
|
||||
serde::CanBeEmpty,
|
||||
user_id, MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use serde_json::{
|
||||
from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue,
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
fn redaction() -> JsonValue {
|
||||
json!({
|
||||
#[test]
|
||||
fn serialize_redaction_content() {
|
||||
let content = RoomRedactionEventContent::with_reason("being very unfriendly".into());
|
||||
|
||||
let actual = to_json_value(content).unwrap();
|
||||
let expected = json!({
|
||||
"reason": "being very unfriendly"
|
||||
});
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_redaction() {
|
||||
let json_data = json!({
|
||||
"content": {
|
||||
"reason": "being a turd"
|
||||
"reason": "being very unfriendly"
|
||||
},
|
||||
"redacts": "$nomore:example.com",
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
@ -27,36 +34,13 @@ fn redaction() -> JsonValue {
|
||||
"origin_server_ts": 1,
|
||||
"room_id": "!roomid:room.com",
|
||||
"type": "m.room.redaction"
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize_redaction() {
|
||||
let aliases_event = OriginalRoomRedactionEvent {
|
||||
content: RoomRedactionEventContent::with_reason("being a turd".into()),
|
||||
redacts: event_id!("$nomore:example.com").to_owned(),
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
room_id: room_id!("!roomid:room.com").to_owned(),
|
||||
sender: user_id!("@carl:example.com").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
|
||||
let actual = to_json_value(&aliases_event).unwrap();
|
||||
let expected = redaction();
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_redaction() {
|
||||
let json_data = redaction();
|
||||
});
|
||||
|
||||
let ev = assert_matches!(
|
||||
from_json_value::<AnyMessageLikeEvent>(json_data),
|
||||
Ok(AnyMessageLikeEvent::RoomRedaction(RoomRedactionEvent::Original(ev))) => ev
|
||||
);
|
||||
assert_eq!(ev.content.reason.as_deref(), Some("being a turd"));
|
||||
assert_eq!(ev.content.reason.as_deref(), Some("being very unfriendly"));
|
||||
assert_eq!(ev.event_id, "$h29iv0s8:example.com");
|
||||
assert_eq!(ev.redacts, "$nomore:example.com");
|
||||
assert_eq!(ev.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(1)));
|
||||
|
@ -34,46 +34,27 @@ macro_rules! json_object {
|
||||
|
||||
#[test]
|
||||
fn serialization() {
|
||||
let ev = OriginalRoomMessageEvent {
|
||||
content: RoomMessageEventContent::new(MessageType::Audio(AudioMessageEventContent::plain(
|
||||
let content =
|
||||
RoomMessageEventContent::new(MessageType::Audio(AudioMessageEventContent::plain(
|
||||
"test".into(),
|
||||
mxc_uri!("mxc://example.org/ffed755USFFxlgbQYZGtryd").to_owned(),
|
||||
None,
|
||||
))),
|
||||
event_id: event_id!("$143273582443PhrSn:example.org").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(10_000)),
|
||||
room_id: room_id!("!testroomid:example.org").to_owned(),
|
||||
sender: user_id!("@user:example.org").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
)));
|
||||
|
||||
#[cfg(not(feature = "unstable-msc3246"))]
|
||||
assert_eq!(
|
||||
to_json_value(ev).unwrap(),
|
||||
to_json_value(content).unwrap(),
|
||||
json!({
|
||||
"type": "m.room.message",
|
||||
"event_id": "$143273582443PhrSn:example.org",
|
||||
"origin_server_ts": 10_000,
|
||||
"room_id": "!testroomid:example.org",
|
||||
"sender": "@user:example.org",
|
||||
"content": {
|
||||
"body": "test",
|
||||
"msgtype": "m.audio",
|
||||
"url": "mxc://example.org/ffed755USFFxlgbQYZGtryd",
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
#[cfg(feature = "unstable-msc3246")]
|
||||
assert_eq!(
|
||||
to_json_value(ev).unwrap(),
|
||||
to_json_value(content).unwrap(),
|
||||
json!({
|
||||
"type": "m.room.message",
|
||||
"event_id": "$143273582443PhrSn:example.org",
|
||||
"origin_server_ts": 10_000,
|
||||
"room_id": "!testroomid:example.org",
|
||||
"sender": "@user:example.org",
|
||||
"content": {
|
||||
"body": "test",
|
||||
"msgtype": "m.audio",
|
||||
"url": "mxc://example.org/ffed755USFFxlgbQYZGtryd",
|
||||
@ -82,7 +63,6 @@ fn serialization() {
|
||||
"url": "mxc://example.org/ffed755USFFxlgbQYZGtryd",
|
||||
},
|
||||
"org.matrix.msc1767.audio": {},
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -1,20 +1,15 @@
|
||||
use assert_matches::assert_matches;
|
||||
use assign::assign;
|
||||
use js_int::uint;
|
||||
use ruma_common::{
|
||||
event_id,
|
||||
events::{
|
||||
room::aliases::RoomAliasesEventContent, AnyStateEvent, AnyStateEventContent,
|
||||
AnySyncStateEvent, AnyTimelineEvent, OriginalStateEvent, StateEvent, StateEventType,
|
||||
StateUnsigned, SyncStateEvent,
|
||||
AnyStateEvent, AnyStateEventContent, AnySyncStateEvent, AnyTimelineEvent, StateEvent,
|
||||
StateEventType, SyncStateEvent,
|
||||
},
|
||||
mxc_uri, room_alias_id, room_id,
|
||||
mxc_uri, room_alias_id,
|
||||
serde::{CanBeEmpty, Raw},
|
||||
server_name, user_id, MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use serde_json::{
|
||||
from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue,
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, json, Value as JsonValue};
|
||||
|
||||
fn aliases_event_with_prev_content() -> JsonValue {
|
||||
json!({
|
||||
@ -35,65 +30,10 @@ fn aliases_event_with_prev_content() -> JsonValue {
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize_aliases_with_prev_content() {
|
||||
let aliases_event = OriginalStateEvent {
|
||||
content: RoomAliasesEventContent::new(vec![
|
||||
room_alias_id!("#somewhere:localhost").to_owned()
|
||||
]),
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
room_id: room_id!("!roomid:room.com").to_owned(),
|
||||
sender: user_id!("@carl:example.com").to_owned(),
|
||||
state_key: server_name!("room.com").to_owned(),
|
||||
unsigned: assign!(StateUnsigned::default(), {
|
||||
prev_content: Some(RoomAliasesEventContent::new(vec![room_alias_id!(
|
||||
"#inner:localhost"
|
||||
)
|
||||
.to_owned()])),
|
||||
}),
|
||||
};
|
||||
|
||||
let actual = to_json_value(&aliases_event).unwrap();
|
||||
let expected = aliases_event_with_prev_content();
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize_aliases_without_prev_content() {
|
||||
let aliases_event = OriginalStateEvent {
|
||||
content: RoomAliasesEventContent::new(vec![
|
||||
room_alias_id!("#somewhere:localhost").to_owned()
|
||||
]),
|
||||
event_id: event_id!("$h29iv0s8:example.com").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
|
||||
room_id: room_id!("!roomid:room.com").to_owned(),
|
||||
sender: user_id!("@carl:example.com").to_owned(),
|
||||
state_key: server_name!("example.com").to_owned(),
|
||||
unsigned: StateUnsigned::default(),
|
||||
};
|
||||
|
||||
let actual = to_json_value(&aliases_event).unwrap();
|
||||
let expected = json!({
|
||||
"content": {
|
||||
"aliases": [ "#somewhere:localhost" ]
|
||||
},
|
||||
"event_id": "$h29iv0s8:example.com",
|
||||
"origin_server_ts": 1,
|
||||
"room_id": "!roomid:room.com",
|
||||
"sender": "@carl:example.com",
|
||||
"state_key": "example.com",
|
||||
"type": "m.room.aliases",
|
||||
});
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_aliases_content() {
|
||||
let json_data = json!({
|
||||
"aliases": [ "#somewhere:localhost" ]
|
||||
"aliases": ["#somewhere:localhost"],
|
||||
});
|
||||
|
||||
let content = assert_matches!(
|
||||
|
@ -3,30 +3,18 @@ use js_int::uint;
|
||||
use ruma_common::{
|
||||
events::{
|
||||
room::{join_rules::JoinRule, topic::RoomTopicEventContent},
|
||||
AnyStrippedStateEvent, EmptyStateKey, StrippedStateEvent,
|
||||
AnyStrippedStateEvent,
|
||||
},
|
||||
mxc_uri, user_id,
|
||||
mxc_uri,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
#[test]
|
||||
fn serialize_stripped_state_event_any_content() {
|
||||
let event = StrippedStateEvent {
|
||||
content: RoomTopicEventContent::new("Testing room".into()),
|
||||
state_key: EmptyStateKey,
|
||||
sender: user_id!("@example:localhost").to_owned(),
|
||||
};
|
||||
|
||||
let json_data = json!({
|
||||
"content": {
|
||||
"topic": "Testing room"
|
||||
},
|
||||
"type": "m.room.topic",
|
||||
"state_key": "",
|
||||
"sender": "@example:localhost"
|
||||
});
|
||||
|
||||
assert_eq!(to_json_value(&event).unwrap(), json_data);
|
||||
assert_eq!(
|
||||
to_json_value(RoomTopicEventContent::new("Testing room".into())).unwrap(),
|
||||
json!({ "topic": "Testing room" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1,32 +1,24 @@
|
||||
use ruma_common::{
|
||||
events::{room_key::ToDeviceRoomKeyEventContent, ToDeviceEvent},
|
||||
room_id, user_id, EventEncryptionAlgorithm,
|
||||
events::room_key::ToDeviceRoomKeyEventContent, room_id, EventEncryptionAlgorithm,
|
||||
};
|
||||
use serde_json::{json, to_value as to_json_value};
|
||||
|
||||
#[test]
|
||||
fn serialization() {
|
||||
let ev = ToDeviceEvent {
|
||||
sender: user_id!("@example:example.org").to_owned(),
|
||||
content: ToDeviceRoomKeyEventContent::new(
|
||||
let content = ToDeviceRoomKeyEventContent::new(
|
||||
EventEncryptionAlgorithm::MegolmV1AesSha2,
|
||||
room_id!("!testroomid:example.org").to_owned(),
|
||||
"SessId".into(),
|
||||
"SessKey".into(),
|
||||
),
|
||||
};
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&ev).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"type": "m.room_key",
|
||||
"sender": "@example:example.org",
|
||||
"content": {
|
||||
"algorithm": "m.megolm.v1.aes-sha2",
|
||||
"room_id": "!testroomid:example.org",
|
||||
"session_id": "SessId",
|
||||
"session_key": "SessKey",
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -17,11 +17,11 @@ use ruma_common::{
|
||||
JsonWebKeyInit, MediaSource,
|
||||
},
|
||||
video::{VideoContent, VideoEventContent},
|
||||
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
|
||||
AnyMessageLikeEvent, MessageLikeEvent,
|
||||
},
|
||||
mxc_uri, room_id,
|
||||
mxc_uri,
|
||||
serde::{Base64, CanBeEmpty},
|
||||
user_id, MilliSecondsSinceUnixEpoch,
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
@ -98,8 +98,7 @@ fn encrypted_content_serialization() {
|
||||
|
||||
#[test]
|
||||
fn event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: assign!(
|
||||
let content = assign!(
|
||||
VideoEventContent::with_message(
|
||||
MessageContent::html(
|
||||
"Upload: my_lava_lamp.webm",
|
||||
@ -141,18 +140,11 @@ fn event_serialization() {
|
||||
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
|
||||
}),
|
||||
}
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"org.matrix.msc1767.html": "Upload: <strong>my_lava_lamp.webm</strong>",
|
||||
"org.matrix.msc1767.text": "Upload: my_lava_lamp.webm",
|
||||
"m.file": {
|
||||
@ -184,12 +176,6 @@ fn event_serialization() {
|
||||
"event_id": "$replyevent:example.com"
|
||||
}
|
||||
}
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.video",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -16,18 +16,17 @@ use ruma_common::{
|
||||
MediaSource,
|
||||
},
|
||||
voice::{VoiceContent, VoiceEventContent},
|
||||
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
|
||||
AnyMessageLikeEvent, MessageLikeEvent,
|
||||
},
|
||||
mxc_uri, room_id,
|
||||
mxc_uri,
|
||||
serde::CanBeEmpty,
|
||||
user_id, MilliSecondsSinceUnixEpoch,
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||
|
||||
#[test]
|
||||
fn event_serialization() {
|
||||
let event = OriginalMessageLikeEvent {
|
||||
content: assign!(
|
||||
let content = assign!(
|
||||
VoiceEventContent::plain(
|
||||
"Voice message",
|
||||
FileContent::plain(
|
||||
@ -53,18 +52,11 @@ fn event_serialization() {
|
||||
in_reply_to: InReplyTo::new(event_id!("$replyevent:example.com").to_owned()),
|
||||
}),
|
||||
}
|
||||
),
|
||||
event_id: event_id!("$event:notareal.hs").to_owned(),
|
||||
sender: user_id!("@user:notareal.hs").to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
|
||||
room_id: room_id!("!roomid:notareal.hs").to_owned(),
|
||||
unsigned: MessageLikeUnsigned::default(),
|
||||
};
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&event).unwrap(),
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"content": {
|
||||
"org.matrix.msc1767.text": "Voice message",
|
||||
"m.file": {
|
||||
"url": "mxc://notareal.hs/abcdef",
|
||||
@ -80,13 +72,7 @@ fn event_serialization() {
|
||||
"m.in_reply_to": {
|
||||
"event_id": "$replyevent:example.com"
|
||||
}
|
||||
}
|
||||
},
|
||||
"event_id": "$event:notareal.hs",
|
||||
"origin_server_ts": 134_829_848,
|
||||
"room_id": "!roomid:notareal.hs",
|
||||
"sender": "@user:notareal.hs",
|
||||
"type": "m.voice",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
@ -41,7 +41,6 @@ pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||
|
||||
let mut res = TokenStream::new();
|
||||
|
||||
res.extend(expand_serialize_event(&input, var, &fields, &ruma_common));
|
||||
res.extend(
|
||||
expand_deserialize_event(&input, kind, var, &fields, &ruma_common)
|
||||
.unwrap_or_else(syn::Error::into_compile_error),
|
||||
@ -58,72 +57,6 @@ pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn expand_serialize_event(
|
||||
input: &DeriveInput,
|
||||
var: EventKindVariation,
|
||||
fields: &[Field],
|
||||
ruma_common: &TokenStream,
|
||||
) -> TokenStream {
|
||||
let serde = quote! { #ruma_common::exports::serde };
|
||||
|
||||
let ident = &input.ident;
|
||||
let (impl_gen, ty_gen, where_clause) = input.generics.split_for_impl();
|
||||
let serialize_fields: Vec<_> = fields
|
||||
.iter()
|
||||
.map(|field| {
|
||||
let name = field.ident.as_ref().unwrap();
|
||||
if name == "content" && var.is_redacted() {
|
||||
quote! {
|
||||
state.serialize_field("content", &self.content)?;
|
||||
}
|
||||
} else if name == "unsigned" {
|
||||
quote! {
|
||||
if !#ruma_common::serde::is_empty(&self.unsigned) {
|
||||
state.serialize_field("unsigned", &self.unsigned)?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let name_s = name.to_string();
|
||||
match &field.ty {
|
||||
syn::Type::Path(syn::TypePath { path: syn::Path { segments, .. }, .. })
|
||||
if segments.last().unwrap().ident == "Option" =>
|
||||
{
|
||||
quote! {
|
||||
if let Some(content) = self.#name.as_ref() {
|
||||
state.serialize_field(#name_s, content)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => quote! {
|
||||
state.serialize_field(#name_s, &self.#name)?;
|
||||
},
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
quote! {
|
||||
#[automatically_derived]
|
||||
impl #impl_gen #serde::ser::Serialize for #ident #ty_gen #where_clause {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: #serde::ser::Serializer,
|
||||
{
|
||||
use #serde::ser::{SerializeStruct as _, Error as _};
|
||||
|
||||
let mut state = serializer.serialize_struct(stringify!(#ident), 7)?;
|
||||
|
||||
let event_type = #ruma_common::events::EventContent::event_type(&self.content);
|
||||
state.serialize_field("type", &event_type)?;
|
||||
|
||||
#( #serialize_fields )*
|
||||
|
||||
state.end()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_deserialize_event(
|
||||
input: &DeriveInput,
|
||||
kind: EventKind,
|
||||
|
Loading…
x
Reference in New Issue
Block a user