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.
///
/// This is the new primary type introduced in [MSC3488] and can be sent in rooms with a version
/// that doesn't support extensible events. See the documentation of the [`message`] module for more
/// information.
/// This is the new primary type introduced in [MSC3488] and should only be sent in rooms with a
/// version that supports it. See the documentation of the [`message`] module for more information.
///
/// [MSC3488]: https://github.com/matrix-org/matrix-spec-proposals/pull/3488
/// [`message`]: super::message
@ -39,6 +38,15 @@ pub struct LocationEventContent {
#[serde(rename = "m.ts", skip_serializing_if = "Option::is_none")]
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.
#[serde(
flatten,
@ -51,17 +59,27 @@ pub struct LocationEventContent {
impl LocationEventContent {
/// Creates a new `LocationEventContent` with the given fallback representation and location.
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
/// 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 {
text: TextContentBlock::plain(text),
text: TextContentBlock::plain(plain_text),
location,
asset: Default::default(),
ts: None,
#[cfg(feature = "unstable-msc3955")]
automated: false,
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]
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",
LocationContent::new("geo:51.5008,0.1247;u=35".to_owned()),
);