1use mumble_protocol::control::msgs;
2use serde::{Deserialize, Serialize};
3
4#[derive(Clone, Debug, Deserialize, Serialize)]
5pub struct User {
6 channel: u32,
7 comment: Option<String>,
8 hash: Option<String>,
9 name: String,
10 priority_speaker: bool,
11 recording: bool,
12
13 suppress: bool, self_mute: bool, self_deaf: bool, mute: bool, deaf: bool, }
19
20impl User {
21 pub fn new(mut msg: msgs::UserState) -> Self {
22 Self {
23 channel: msg.get_channel_id(),
24 comment: msg.has_comment().then(|| msg.take_comment()),
25 hash: msg.has_hash().then(|| msg.take_hash()),
26 name: msg.take_name(),
27 priority_speaker: msg.has_priority_speaker() && msg.get_priority_speaker(),
28 recording: msg.has_recording() && msg.get_recording(),
29 suppress: msg.has_suppress() && msg.get_suppress(),
30 self_mute: msg.has_self_mute() && msg.get_self_mute(),
31 self_deaf: msg.has_self_deaf() && msg.get_self_deaf(),
32 mute: msg.has_mute() && msg.get_mute(),
33 deaf: msg.has_deaf() && msg.get_deaf(),
34 }
35 }
36
37 pub fn parse_user_state(&mut self, mut msg: msgs::UserState) {
38 if msg.has_channel_id() {
39 self.channel = msg.get_channel_id();
40 }
41 if msg.has_comment() {
42 self.comment = Some(msg.take_comment());
43 }
44 if msg.has_hash() {
45 self.hash = Some(msg.take_hash());
46 }
47 if msg.has_name() {
48 self.name = msg.take_name();
49 }
50 if msg.has_priority_speaker() {
51 self.priority_speaker = msg.get_priority_speaker();
52 }
53 if msg.has_recording() {
54 self.recording = msg.get_recording();
55 }
56 if msg.has_suppress() {
57 self.suppress = msg.get_suppress();
58 }
59 if msg.has_self_mute() {
60 self.self_mute = msg.get_self_mute();
61 }
62 if msg.has_self_deaf() {
63 self.self_deaf = msg.get_self_deaf();
64 }
65 if msg.has_mute() {
66 self.mute = msg.get_mute();
67 }
68 if msg.has_deaf() {
69 self.deaf = msg.get_deaf();
70 }
71 }
72
73 pub fn apply_user_diff(&mut self, diff: &UserDiff) {
74 if let Some(comment) = diff.comment.clone() {
75 self.comment = Some(comment);
76 }
77 if let Some(hash) = diff.hash.clone() {
78 self.hash = Some(hash);
79 }
80 if let Some(name) = diff.name.clone() {
81 self.name = name;
82 }
83 if let Some(priority_speaker) = diff.priority_speaker {
84 self.priority_speaker = priority_speaker;
85 }
86 if let Some(recording) = diff.recording {
87 self.recording = recording;
88 }
89 if let Some(suppress) = diff.suppress {
90 self.suppress = suppress;
91 }
92 if let Some(self_mute) = diff.self_mute {
93 self.self_mute = self_mute;
94 }
95 if let Some(self_deaf) = diff.self_deaf {
96 self.self_deaf = self_deaf;
97 }
98 if let Some(mute) = diff.mute {
99 self.mute = mute;
100 }
101 if let Some(deaf) = diff.deaf {
102 self.deaf = deaf;
103 }
104 if let Some(channel_id) = diff.channel_id {
105 self.channel = channel_id;
106 }
107 }
108
109 pub fn name(&self) -> &str {
110 &self.name
111 }
112
113 pub fn channel(&self) -> u32 {
114 self.channel
115 }
116
117 pub fn self_mute(&self) -> bool {
118 self.self_mute
119 }
120
121 pub fn self_deaf(&self) -> bool {
122 self.self_deaf
123 }
124
125 pub fn suppressed(&self) -> bool {
126 self.suppress
127 }
128
129 pub fn set_suppressed(&mut self, value: bool) {
130 self.suppress = value;
131 }
132}
133
134impl From<&User> for mumlib::state::User {
135 fn from(user: &User) -> Self {
136 mumlib::state::User {
137 comment: user.comment.clone(),
138 hash: user.hash.clone(),
139 name: user.name.clone(),
140 priority_speaker: user.priority_speaker,
141 recording: user.recording,
142 suppress: user.suppress,
143 self_mute: user.self_mute,
144 self_deaf: user.self_deaf,
145 mute: user.mute,
146 deaf: user.deaf,
147 }
148 }
149}
150
151#[derive(Debug, Default)]
152pub struct UserDiff {
153 pub comment: Option<String>,
154 pub hash: Option<String>,
155 pub name: Option<String>,
156 pub priority_speaker: Option<bool>,
157 pub recording: Option<bool>,
158
159 pub suppress: Option<bool>, pub self_mute: Option<bool>, pub self_deaf: Option<bool>, pub mute: Option<bool>, pub deaf: Option<bool>, pub channel_id: Option<u32>,
166}
167
168impl UserDiff {
169 pub fn new() -> Self {
170 UserDiff::default()
171 }
172}
173
174impl From<msgs::UserState> for UserDiff {
175 fn from(mut msg: msgs::UserState) -> Self {
176 let mut ud = UserDiff::new();
177 if msg.has_comment() {
178 ud.comment = Some(msg.take_comment());
179 }
180 if msg.has_hash() {
181 ud.hash = Some(msg.take_hash());
182 }
183 if msg.has_name() {
184 ud.name = Some(msg.take_name());
185 }
186 if msg.has_priority_speaker() {
187 ud.priority_speaker = Some(msg.get_priority_speaker());
188 }
189 if msg.has_recording() {
190 ud.recording = Some(msg.get_recording());
191 }
192 if msg.has_suppress() {
193 ud.suppress = Some(msg.get_suppress());
194 }
195 if msg.has_self_mute() {
196 ud.self_mute = Some(msg.get_self_mute());
197 }
198 if msg.has_self_deaf() {
199 ud.self_deaf = Some(msg.get_self_deaf());
200 }
201 if msg.has_mute() {
202 ud.mute = Some(msg.get_mute());
203 }
204 if msg.has_deaf() {
205 ud.deaf = Some(msg.get_deaf());
206 }
207 if msg.has_channel_id() {
208 ud.channel_id = Some(msg.get_channel_id());
209 }
210 ud
211 }
212}