1use serde::{Deserialize, Serialize};
11
12pub use byob_types::channel::{CHANNEL_PHONE, DAILY_CAP};
13
14use crate::ClientMsg;
15
16pub const CAP_PHONE: &str = "phone";
18
19#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
21pub struct ChannelOption {
22 pub key: String,
24 pub plugin: String,
25 pub title: String,
27 #[serde(default)]
29 pub two_way: bool,
30 #[serde(default)]
32 pub buttons: bool,
33 #[serde(default)]
35 pub enabled: bool,
36 #[serde(default)]
38 pub configured: bool,
39 #[serde(default)]
41 pub ok: bool,
42 #[serde(default, skip_serializing_if = "Option::is_none")]
45 pub status: Option<String>,
46 #[serde(default, skip_serializing_if = "Option::is_none")]
48 pub setting: Option<String>,
49 #[serde(default, skip_serializing_if = "Option::is_none")]
51 pub setting_label: Option<String>,
52 #[serde(default, skip_serializing_if = "Option::is_none")]
54 pub setting_help: Option<String>,
55}
56
57#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
59pub struct PhoneView {
60 pub channel: String,
61 pub title: String,
63 #[serde(default)]
65 pub name: String,
66 #[serde(default)]
68 pub paired_at: i64,
69}
70
71#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
73pub struct Pairing {
74 pub channel: String,
75 pub code: String,
77 #[serde(default, skip_serializing_if = "Option::is_none")]
79 pub link: Option<String>,
80 #[serde(default, skip_serializing_if = "Option::is_none")]
82 pub name: Option<String>,
83 #[serde(default)]
85 pub instructions: String,
86 pub expires_at: i64,
88}
89
90#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
92pub struct PhoneState {
93 #[serde(default)]
94 pub channels: Vec<ChannelOption>,
95 #[serde(default, skip_serializing_if = "Option::is_none")]
96 pub phone: Option<PhoneView>,
97 #[serde(default, skip_serializing_if = "Option::is_none")]
98 pub pairing: Option<Pairing>,
99}
100
101pub(crate) fn check_sizes(msg: &ClientMsg) -> Result<(), String> {
103 match msg {
104 ClientMsg::PairPhone { channel } if channel.len() > 200 || byob_types::channel::split_key(channel).is_none() => Err("bad channel".into()),
105 _ => Ok(()),
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112 use crate::ServerMsg;
113
114 #[test]
115 fn phone_messages_round_trip() {
116 for m in [ClientMsg::PairPhone { channel: "telegram:telegram".into() }, ClientMsg::CancelPairing, ClientMsg::UnpairPhone, ClientMsg::TestPhone] {
117 assert!(m.check_sizes().is_ok());
118 assert_eq!(ClientMsg::from_json(&m.to_json()).unwrap(), m);
119 }
120 assert!(ClientMsg::PairPhone { channel: "nope".into() }.check_sizes().is_err());
121 let s = ServerMsg::Phone(PhoneState {
122 channels: vec![ChannelOption {
123 key: "telegram:telegram".into(),
124 plugin: "telegram".into(),
125 title: "Telegram".into(),
126 two_way: true,
127 ..Default::default()
128 }],
129 phone: Some(PhoneView { channel: "telegram:telegram".into(), title: "Telegram".into(), name: "@jesse".into(), paired_at: 5 }),
130 pairing: Some(Pairing { channel: "telegram:telegram".into(), code: "ABCD-EFGH".into(), expires_at: 9, ..Default::default() }),
131 });
132 let json = serde_json::to_string(&s).unwrap();
133 assert_eq!(ServerMsg::from_json(&json).unwrap(), s, "{json}");
134 }
135}