events: Use CanBeEmpty trait for skipping unsigned serialization

This commit is contained in:
Jonas Platte 2022-09-12 11:44:05 +02:00
parent 24d0b2e2bb
commit 674137c41d
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C
14 changed files with 35 additions and 17 deletions

View File

@ -460,8 +460,8 @@ mod tests {
use super::{MembershipState, RoomMemberEventContent};
use crate::{
events::OriginalStateEvent, mxc_uri, server_name, server_signing_key_id, user_id,
MilliSecondsSinceUnixEpoch,
events::OriginalStateEvent, mxc_uri, serde::CanBeEmpty, server_name, server_signing_key_id,
user_id, MilliSecondsSinceUnixEpoch,
};
#[test]

View File

@ -3,7 +3,10 @@ use serde::{Deserialize, Serialize};
use serde_json::{from_str as from_json_str, value::RawValue as RawJsonValue};
use super::{relation::Relations, room::redaction::SyncRoomRedactionEvent, StateEventContent};
use crate::{serde::Raw, OwnedTransactionId};
use crate::{
serde::{CanBeEmpty, Raw},
OwnedTransactionId,
};
/// Extra information about a message event that is not incorporated into the event's hash.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
@ -34,13 +37,15 @@ impl MessageLikeUnsigned {
pub fn new() -> Self {
Self::default()
}
}
impl CanBeEmpty for MessageLikeUnsigned {
/// Whether this unsigned data is empty (all fields are `None`).
///
/// This method is used to determine whether to skip serializing the `unsigned` field in room
/// events. Do not use it to determine whether an incoming `unsigned` field was present - it
/// could still have been present but contained none of the known fields.
pub fn is_empty(&self) -> bool {
fn is_empty(&self) -> bool {
self.age.is_none() && self.transaction_id.is_none() && self.relations.is_none()
}
}
@ -78,13 +83,15 @@ impl<C: StateEventContent> StateUnsigned<C> {
pub fn new() -> Self {
Self { age: None, transaction_id: None, prev_content: None, relations: None }
}
}
impl<C: StateEventContent> CanBeEmpty for StateUnsigned<C> {
/// Whether this unsigned data is empty (all fields are `None`).
///
/// This method is used to determine whether to skip serializing the `unsigned` field in room
/// events. Do not use it to determine whether an incoming `unsigned` field was present - it
/// could still have been present but contained none of the known fields.
pub fn is_empty(&self) -> bool {
fn is_empty(&self) -> bool {
self.age.is_none()
&& self.transaction_id.is_none()
&& self.prev_content.is_none()
@ -160,13 +167,15 @@ impl RedactedUnsigned {
pub fn new_because(redacted_because: Box<SyncRoomRedactionEvent>) -> Self {
Self { redacted_because: Some(redacted_because) }
}
}
impl CanBeEmpty for RedactedUnsigned {
/// Whether this unsigned data is empty (`redacted_because` is `None`).
///
/// This method is used to determine whether to skip serializing the `unsigned` field in
/// redacted room events. Do not use it to determine whether an incoming `unsigned` field
/// was present - it could still have been present but contained none of the known fields.
pub fn is_empty(&self) -> bool {
fn is_empty(&self) -> bool {
self.redacted_because.is_none()
}
}

View File

@ -20,7 +20,7 @@ use ruma_common::{
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
},
mxc_uri, room_id,
serde::Base64,
serde::{Base64, CanBeEmpty},
user_id, MilliSecondsSinceUnixEpoch,
};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};

View File

@ -2,6 +2,7 @@ use assert_matches::assert_matches;
use js_int::uint;
use ruma_common::{
events::{AnyMessageLikeEvent, MessageLikeEvent},
serde::CanBeEmpty,
MilliSecondsSinceUnixEpoch, VoipVersionId,
};
use serde_json::{from_value as from_json_value, json};

View File

@ -17,7 +17,7 @@ use ruma_common::{
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
},
mxc_uri, room_id,
serde::Base64,
serde::{Base64, CanBeEmpty},
user_id, MilliSecondsSinceUnixEpoch,
};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};

View File

@ -21,7 +21,7 @@ use ruma_common::{
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
},
mxc_uri, room_id,
serde::Base64,
serde::{Base64, CanBeEmpty},
user_id, MilliSecondsSinceUnixEpoch,
};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};

View File

@ -13,7 +13,9 @@ use ruma_common::{
},
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
},
room_id, user_id, MilliSecondsSinceUnixEpoch,
room_id,
serde::CanBeEmpty,
user_id, MilliSecondsSinceUnixEpoch,
};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};

View File

@ -14,7 +14,9 @@ use ruma_common::{
},
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
},
room_id, user_id, MilliSecondsSinceUnixEpoch,
room_id,
serde::CanBeEmpty,
user_id, MilliSecondsSinceUnixEpoch,
};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};

View File

@ -10,7 +10,7 @@ use ruma_common::{
MessageLikeEventType, MessageLikeUnsigned, OriginalMessageLikeEvent,
},
mxc_uri, room_id,
serde::Raw,
serde::{CanBeEmpty, Raw},
user_id, MilliSecondsSinceUnixEpoch, VoipVersionId,
};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};

View File

@ -8,7 +8,9 @@ use ruma_common::{
},
AnyMessageLikeEvent, MessageLikeUnsigned,
},
room_id, user_id, MilliSecondsSinceUnixEpoch,
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,

View File

@ -9,7 +9,7 @@ use ruma_common::{
StateUnsigned, SyncStateEvent,
},
mxc_uri, room_alias_id, room_id,
serde::Raw,
serde::{CanBeEmpty, Raw},
server_name, user_id, MilliSecondsSinceUnixEpoch,
};
use serde_json::{

View File

@ -21,7 +21,7 @@ use ruma_common::{
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
},
mxc_uri, room_id,
serde::Base64,
serde::{Base64, CanBeEmpty},
user_id, MilliSecondsSinceUnixEpoch,
};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};

View File

@ -19,7 +19,9 @@ use ruma_common::{
voice::{VoiceContent, VoiceEventContent},
AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent,
},
mxc_uri, room_id, user_id, MilliSecondsSinceUnixEpoch,
mxc_uri, room_id,
serde::CanBeEmpty,
user_id, MilliSecondsSinceUnixEpoch,
};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};

View File

@ -86,7 +86,7 @@ fn expand_serialize_event(
}
} else if name == "unsigned" {
quote! {
if !self.unsigned.is_empty() {
if !#ruma_common::serde::is_empty(&self.unsigned) {
state.serialize_field("unsigned", &self.unsigned)?;
}
}