events: Updates location event types

Even if the MSC hasn't been updated, refactor a bit to match other
extensible events types.
This commit is contained in:
Kévin Commaille 2023-02-04 15:20:05 +01:00 committed by Kévin Commaille
parent 61c23491c6
commit cc47a7268b
2 changed files with 25 additions and 7 deletions

View File

@ -13,9 +13,8 @@ use crate::{MilliSecondsSinceUnixEpoch, PrivOwnedStr};
/// The payload for an extensible location message. /// The payload for an extensible location message.
/// ///
/// This is the new primary type introduced in [MSC3488] and can be sent in rooms with a version /// This is the new primary type introduced in [MSC3488] and should only be sent in rooms with a
/// that doesn't support extensible events. See the documentation of the [`message`] module for more /// version that supports it. See the documentation of the [`message`] module for more information.
/// information.
/// ///
/// [MSC3488]: https://github.com/matrix-org/matrix-spec-proposals/pull/3488 /// [MSC3488]: https://github.com/matrix-org/matrix-spec-proposals/pull/3488
/// [`message`]: super::message /// [`message`]: super::message
@ -39,6 +38,15 @@ pub struct LocationEventContent {
#[serde(rename = "m.ts", skip_serializing_if = "Option::is_none")] #[serde(rename = "m.ts", skip_serializing_if = "Option::is_none")]
pub ts: Option<MilliSecondsSinceUnixEpoch>, pub ts: Option<MilliSecondsSinceUnixEpoch>,
/// Whether this message is automated.
#[cfg(feature = "unstable-msc3955")]
#[serde(
default,
skip_serializing_if = "crate::serde::is_default",
rename = "org.matrix.msc1767.automated"
)]
pub automated: bool,
/// Information about related messages. /// Information about related messages.
#[serde( #[serde(
flatten, flatten,
@ -51,17 +59,27 @@ pub struct LocationEventContent {
impl LocationEventContent { impl LocationEventContent {
/// Creates a new `LocationEventContent` with the given fallback representation and location. /// Creates a new `LocationEventContent` with the given fallback representation and location.
pub fn new(text: TextContentBlock, location: LocationContent) -> Self { pub fn new(text: TextContentBlock, location: LocationContent) -> Self {
Self { text, location, asset: Default::default(), ts: None, relates_to: None } Self {
text,
location,
asset: Default::default(),
ts: None,
#[cfg(feature = "unstable-msc3955")]
automated: false,
relates_to: None,
}
} }
/// Creates a new `LocationEventContent` with the given plain text fallback representation and /// Creates a new `LocationEventContent` with the given plain text fallback representation and
/// location. /// location.
pub fn plain(text: impl Into<String>, location: LocationContent) -> Self { pub fn with_plain_text(plain_text: impl Into<String>, location: LocationContent) -> Self {
Self { Self {
text: TextContentBlock::plain(text), text: TextContentBlock::plain(plain_text),
location, location,
asset: Default::default(), asset: Default::default(),
ts: None, ts: None,
#[cfg(feature = "unstable-msc3955")]
automated: false,
relates_to: None, relates_to: None,
} }
} }

View File

@ -20,7 +20,7 @@ use serde_json::{from_value as from_json_value, json, to_value as to_json_value}
#[test] #[test]
fn plain_content_serialization() { fn plain_content_serialization() {
let event_content = LocationEventContent::plain( let event_content = LocationEventContent::with_plain_text(
"Alice was at geo:51.5008,0.1247;u=35", "Alice was at geo:51.5008,0.1247;u=35",
LocationContent::new("geo:51.5008,0.1247;u=35".to_owned()), LocationContent::new("geo:51.5008,0.1247;u=35".to_owned()),
); );