From eff32b9379a4d3f5f7b16276c39acf36e20e8d3a Mon Sep 17 00:00:00 2001 From: torrybr <16907963+torrybr@users.noreply.github.com> Date: Tue, 11 Mar 2025 17:11:42 -0400 Subject: [PATCH 1/2] feat: highlight user on map --- crates/ruma-events/src/beacon_info.rs | 16 +++++++ crates/ruma-events/tests/it/beacon_info.rs | 53 +++++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/crates/ruma-events/src/beacon_info.rs b/crates/ruma-events/src/beacon_info.rs index c7bc528cd1..307ce7bdf5 100644 --- a/crates/ruma-events/src/beacon_info.rs +++ b/crates/ruma-events/src/beacon_info.rs @@ -39,6 +39,10 @@ pub struct BeaconInfoEventContent { /// The asset that this message refers to. #[serde(default, rename = "org.matrix.msc3488.asset")] pub asset: AssetContent, + + /// The color to highlight the beacon_info. + #[serde(skip_serializing_if = "Option::is_none")] + pub highlight_color: Option, } impl BeaconInfoEventContent { @@ -48,6 +52,7 @@ impl BeaconInfoEventContent { description: Option, timeout: Duration, live: bool, + highlight_color: Option, ts: Option, ) -> Self { Self { @@ -56,6 +61,7 @@ impl BeaconInfoEventContent { ts: ts.unwrap_or_else(MilliSecondsSinceUnixEpoch::now), timeout, asset: Default::default(), + highlight_color, } } @@ -69,6 +75,16 @@ impl BeaconInfoEventContent { self.live = false; } + /// Set a new highlight color for the map dot. + pub fn set_highlight_color(&mut self, color: Option) { + self.highlight_color = color + } + + /// Returns whether the beacon_info is highlighted. + pub fn is_highlighted(&self) -> bool { + self.highlight_color.is_some() + } + /// Start time plus its timeout, it returns `false`, indicating that the beacon is not live. /// Otherwise, it returns `true`. pub fn is_live(&self) -> bool { diff --git a/crates/ruma-events/tests/it/beacon_info.rs b/crates/ruma-events/tests/it/beacon_info.rs index 323b554acd..85a19711d8 100644 --- a/crates/ruma-events/tests/it/beacon_info.rs +++ b/crates/ruma-events/tests/it/beacon_info.rs @@ -18,7 +18,7 @@ fn get_beacon_info_event_content( let duration_or = duration.unwrap_or(Duration::from_secs(60)); let ts_or = Some(ts.unwrap_or(MilliSecondsSinceUnixEpoch::now())); - BeaconInfoEventContent::new(description, duration_or, true, ts_or) + BeaconInfoEventContent::new(description, duration_or, true, None, ts_or) } fn get_beacon_info_json() -> serde_json::Value { @@ -78,6 +78,7 @@ fn beacon_info_start_event() { Some("Kylie's live location".to_owned()), Duration::from_secs(60), false, + None, ts, ); @@ -130,6 +131,56 @@ fn beacon_info_start_event_content_deserialization() { assert_eq!(event_content.asset.type_, AssetType::Self_); } +#[test] +fn beacon_info_highlight_event_serialization() { + let ts = Some(MilliSecondsSinceUnixEpoch(1_636_829_458_u64.try_into().unwrap())); + + let event_content = BeaconInfoEventContent::new( + Some("Kylie's live location".to_owned()), + Duration::from_secs(60), + false, + Some("#FF0000".to_owned()), + ts, + ); + + assert_eq!( + to_json_value(&event_content).unwrap(), + json!({ + "org.matrix.msc3488.ts": 1_636_829_458, + "org.matrix.msc3488.asset": { + "type": "m.self" + }, + "highlight_color": "#FF0000", + "timeout": 60_000, + "description": "Kylie's live location", + "live": false + }) + ); +} +#[test] +fn beacon_info_highlight_event_deserialization() { + let json_data = json!({ + "org.matrix.msc3488.ts": 1_636_829_458, + "org.matrix.msc3488.asset": { + "type": "m.self" + }, + "highlight_color": "#FF0000", + "timeout": 60_000, + "description": "Kylie's live location", + "live": false + }); + + let event_content: BeaconInfoEventContent = serde_json::from_value(json_data).unwrap(); + + assert_eq!(event_content.description, Some("Kylie's live location".to_owned())); + assert_eq!(event_content.highlight_color, Some("#FF0000".to_owned())); + assert!(event_content.is_highlighted()); + assert!(!event_content.live); + assert_eq!(event_content.ts, MilliSecondsSinceUnixEpoch(uint!(1_636_829_458))); + assert_eq!(event_content.timeout, Duration::from_secs(60)); + assert_eq!(event_content.asset.type_, AssetType::Self_); +} + #[test] fn state_event_deserialization() { let json_data = json!({ From 0d7b0e52bbe37b26b7a4e035e33b781e5e277f75 Mon Sep 17 00:00:00 2001 From: torrybr <16907963+torrybr@users.noreply.github.com> Date: Tue, 25 Mar 2025 17:15:58 -0400 Subject: [PATCH 2/2] feat: attempt to use function to return color --- crates/ruma-events/src/beacon_info.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/ruma-events/src/beacon_info.rs b/crates/ruma-events/src/beacon_info.rs index 307ce7bdf5..0fc7bf4e70 100644 --- a/crates/ruma-events/src/beacon_info.rs +++ b/crates/ruma-events/src/beacon_info.rs @@ -75,6 +75,10 @@ impl BeaconInfoEventContent { self.live = false; } + pub fn highlight_color_f(&self) -> Option { + self.highlight_color.clone() + } + /// Set a new highlight color for the map dot. pub fn set_highlight_color(&mut self, color: Option) { self.highlight_color = color