ruma_events/
beacon_info.rs1use ruma_common::{MilliSecondsSinceUnixEpoch, OwnedUserId};
7use ruma_macros::EventContent;
8use serde::{Deserialize, Serialize};
9use web_time::{Duration, SystemTime};
10
11use crate::location::AssetContent;
12
13#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
15#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
16#[ruma_event(
17 type = "org.matrix.msc3672.beacon_info", alias = "m.beacon_info", kind = State, state_key_type = OwnedUserId
18)]
19pub struct BeaconInfoEventContent {
20 #[serde(skip_serializing_if = "Option::is_none")]
24 pub description: Option<String>,
25
26 pub live: bool,
28
29 #[serde(rename = "org.matrix.msc3488.ts")]
31 pub ts: MilliSecondsSinceUnixEpoch,
32
33 #[serde(default, with = "ruma_common::serde::duration::ms")]
37 pub timeout: Duration,
38
39 #[serde(default, rename = "org.matrix.msc3488.asset")]
41 pub asset: AssetContent,
42}
43
44impl BeaconInfoEventContent {
45 pub fn new(
48 description: Option<String>,
49 timeout: Duration,
50 live: bool,
51 ts: Option<MilliSecondsSinceUnixEpoch>,
52 ) -> Self {
53 Self {
54 description,
55 live,
56 ts: ts.unwrap_or_else(MilliSecondsSinceUnixEpoch::now),
57 timeout,
58 asset: Default::default(),
59 }
60 }
61
62 pub fn start(&mut self) {
64 self.live = true;
65 }
66
67 pub fn stop(&mut self) {
69 self.live = false;
70 }
71
72 pub fn is_live(&self) -> bool {
75 self.live
76 && self
77 .ts
78 .to_system_time()
79 .and_then(|t| t.checked_add(self.timeout))
80 .is_some_and(|t| t > SystemTime::now())
81 }
82}