ruma_events/room/message/
location.rs
1#[cfg(feature = "unstable-msc3488")]
2use ruma_common::MilliSecondsSinceUnixEpoch;
3use serde::{Deserialize, Serialize};
4
5use crate::room::{MediaSource, ThumbnailInfo};
6#[cfg(feature = "unstable-msc3488")]
7use crate::{
8 location::{AssetContent, AssetType, LocationContent},
9 message::{TextContentBlock, TextRepresentation},
10};
11
12#[derive(Clone, Debug, Deserialize, Serialize)]
14#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
15#[cfg_attr(
16 feature = "unstable-msc3488",
17 serde(
18 from = "super::content_serde::msc3488::LocationMessageEventContentSerDeHelper",
19 into = "super::content_serde::msc3488::LocationMessageEventContentSerDeHelper"
20 )
21)]
22pub struct LocationMessageEventContent {
23 pub body: String,
26
27 pub geo_uri: String,
29
30 #[serde(skip_serializing_if = "Option::is_none")]
32 pub info: Option<Box<LocationInfo>>,
33
34 #[cfg(feature = "unstable-msc3488")]
38 pub message: Option<TextContentBlock>,
39
40 #[cfg(feature = "unstable-msc3488")]
44 pub location: Option<LocationContent>,
45
46 #[cfg(feature = "unstable-msc3488")]
48 pub asset: Option<AssetContent>,
49
50 #[cfg(feature = "unstable-msc3488")]
52 pub ts: Option<MilliSecondsSinceUnixEpoch>,
53}
54
55impl LocationMessageEventContent {
56 pub fn new(body: String, geo_uri: String) -> Self {
58 Self {
59 #[cfg(feature = "unstable-msc3488")]
60 message: Some(vec![TextRepresentation::plain(&body)].into()),
61 #[cfg(feature = "unstable-msc3488")]
62 location: Some(LocationContent::new(geo_uri.clone())),
63 #[cfg(feature = "unstable-msc3488")]
64 asset: Some(AssetContent::default()),
65 #[cfg(feature = "unstable-msc3488")]
66 ts: None,
67 body,
68 geo_uri,
69 info: None,
70 }
71 }
72
73 #[cfg(feature = "unstable-msc3488")]
75 pub fn with_asset_type(mut self, asset: AssetType) -> Self {
76 self.asset = Some(AssetContent { type_: asset });
77 self
78 }
79
80 #[cfg(feature = "unstable-msc3488")]
82 pub fn with_ts(mut self, ts: MilliSecondsSinceUnixEpoch) -> Self {
83 self.ts = Some(ts);
84 self
85 }
86
87 pub fn geo_uri(&self) -> &str {
89 #[cfg(feature = "unstable-msc3488")]
90 if let Some(uri) = self.location.as_ref().map(|l| &l.uri) {
91 return uri;
92 }
93
94 &self.geo_uri
95 }
96
97 pub fn plain_text_representation(&self) -> &str {
99 #[cfg(feature = "unstable-msc3488")]
100 if let Some(text) = self.message.as_ref().and_then(|m| m.find_plain()) {
101 return text;
102 }
103
104 &self.body
105 }
106
107 #[cfg(feature = "unstable-msc3488")]
109 pub fn asset_type(&self) -> AssetType {
110 self.asset.as_ref().map(|a| a.type_.clone()).unwrap_or_default()
111 }
112}
113
114#[derive(Clone, Debug, Default, Deserialize, Serialize)]
116#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
117pub struct LocationInfo {
118 #[serde(
120 flatten,
121 with = "crate::room::thumbnail_source_serde",
122 skip_serializing_if = "Option::is_none"
123 )]
124 pub thumbnail_source: Option<MediaSource>,
125
126 #[serde(skip_serializing_if = "Option::is_none")]
128 pub thumbnail_info: Option<Box<ThumbnailInfo>>,
129}
130
131impl LocationInfo {
132 pub fn new() -> Self {
134 Self::default()
135 }
136}