Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions crates/ruma-events/src/beacon_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

impl BeaconInfoEventContent {
Expand All @@ -48,6 +52,7 @@ impl BeaconInfoEventContent {
description: Option<String>,
timeout: Duration,
live: bool,
highlight_color: Option<String>,
ts: Option<MilliSecondsSinceUnixEpoch>,
) -> Self {
Self {
Expand All @@ -56,6 +61,7 @@ impl BeaconInfoEventContent {
ts: ts.unwrap_or_else(MilliSecondsSinceUnixEpoch::now),
timeout,
asset: Default::default(),
highlight_color,
}
}

Expand All @@ -69,6 +75,20 @@ impl BeaconInfoEventContent {
self.live = false;
}

pub fn highlight_color_f(&self) -> Option<String> {
self.highlight_color.clone()
}

/// Set a new highlight color for the map dot.
pub fn set_highlight_color(&mut self, color: Option<String>) {
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 {
Expand Down
53 changes: 52 additions & 1 deletion crates/ruma-events/tests/it/beacon_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -78,6 +78,7 @@ fn beacon_info_start_event() {
Some("Kylie's live location".to_owned()),
Duration::from_secs(60),
false,
None,
ts,
);

Expand Down Expand Up @@ -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!({
Expand Down