ruma_events/room/message/
location.rs#[cfg(feature = "unstable-msc3488")]
use ruma_common::MilliSecondsSinceUnixEpoch;
use serde::{Deserialize, Serialize};
use crate::room::{MediaSource, ThumbnailInfo};
#[cfg(feature = "unstable-msc3488")]
use crate::{
location::{AssetContent, AssetType, LocationContent},
message::{TextContentBlock, TextRepresentation},
};
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
#[serde(tag = "msgtype", rename = "m.location")]
#[cfg_attr(
feature = "unstable-msc3488",
serde(
from = "super::content_serde::msc3488::LocationMessageEventContentSerDeHelper",
into = "super::content_serde::msc3488::LocationMessageEventContentSerDeHelper"
)
)]
pub struct LocationMessageEventContent {
pub body: String,
pub geo_uri: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub info: Option<Box<LocationInfo>>,
#[cfg(feature = "unstable-msc3488")]
pub message: Option<TextContentBlock>,
#[cfg(feature = "unstable-msc3488")]
pub location: Option<LocationContent>,
#[cfg(feature = "unstable-msc3488")]
pub asset: Option<AssetContent>,
#[cfg(feature = "unstable-msc3488")]
pub ts: Option<MilliSecondsSinceUnixEpoch>,
}
impl LocationMessageEventContent {
pub fn new(body: String, geo_uri: String) -> Self {
Self {
#[cfg(feature = "unstable-msc3488")]
message: Some(vec![TextRepresentation::plain(&body)].into()),
#[cfg(feature = "unstable-msc3488")]
location: Some(LocationContent::new(geo_uri.clone())),
#[cfg(feature = "unstable-msc3488")]
asset: Some(AssetContent::default()),
#[cfg(feature = "unstable-msc3488")]
ts: None,
body,
geo_uri,
info: None,
}
}
#[cfg(feature = "unstable-msc3488")]
pub fn with_asset_type(mut self, asset: AssetType) -> Self {
self.asset = Some(AssetContent { type_: asset });
self
}
#[cfg(feature = "unstable-msc3488")]
pub fn with_ts(mut self, ts: MilliSecondsSinceUnixEpoch) -> Self {
self.ts = Some(ts);
self
}
pub fn geo_uri(&self) -> &str {
#[cfg(feature = "unstable-msc3488")]
if let Some(uri) = self.location.as_ref().map(|l| &l.uri) {
return uri;
}
&self.geo_uri
}
pub fn plain_text_representation(&self) -> &str {
#[cfg(feature = "unstable-msc3488")]
if let Some(text) = self.message.as_ref().and_then(|m| m.find_plain()) {
return text;
}
&self.body
}
#[cfg(feature = "unstable-msc3488")]
pub fn asset_type(&self) -> AssetType {
self.asset.as_ref().map(|a| a.type_.clone()).unwrap_or_default()
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct LocationInfo {
#[serde(
flatten,
with = "crate::room::thumbnail_source_serde",
skip_serializing_if = "Option::is_none"
)]
pub thumbnail_source: Option<MediaSource>,
#[serde(skip_serializing_if = "Option::is_none")]
pub thumbnail_info: Option<Box<ThumbnailInfo>>,
}
impl LocationInfo {
pub fn new() -> Self {
Self::default()
}
}