1
0
mirror of https://github.com/pcvolkmer/arsnova-client.git synced 2025-04-19 19:16:51 +00:00

refactor: add type to hold CreateFeedback websocket message

This commit is contained in:
Paul-Christian Volkmer 2023-12-22 13:27:36 +01:00
parent 9a950917b6
commit 60fe72c8b5

View File

@ -18,7 +18,7 @@
*/ */
use std::error; use std::error;
use std::fmt::{Display, Formatter}; use std::fmt::{Debug, Display, Formatter};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::time::Duration; use std::time::Duration;
@ -127,6 +127,43 @@ impl WsFeedbackPayload {
} }
} }
#[derive(Debug)]
struct WsCreateFeedbackMessage {
room_id: String,
user_id: String,
value: u8,
}
impl WsCreateFeedbackMessage {
fn new(room_id: &str, user_id: &str, value: FeedbackValue) -> WsCreateFeedbackMessage {
WsCreateFeedbackMessage {
room_id: room_id.into(),
user_id: user_id.into(),
value: value.into_u8(),
}
}
}
impl Display for WsCreateFeedbackMessage {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let payload = json!({
"type": "CreateFeedback",
"payload": {
"roomId": self.room_id,
"userId": self.user_id,
"value": self.value
}
})
.to_string();
write!(f,
"SEND\ndestination:/queue/feedback.command\ncontent-type:application/json\ncontent-length:{}\n\n{}\0",
payload.chars().count(),
payload,
)
}
}
impl Display for WsSubscribeMessage { impl Display for WsSubscribeMessage {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let str = format!( let str = format!(
@ -420,22 +457,9 @@ impl Client<LoggedIn> {
{ {
Ok(_) => loop { Ok(_) => loop {
if let Some(value) = receiver.recv().await { if let Some(value) = receiver.recv().await {
let payload = json!({ let msg = WsCreateFeedbackMessage::new(&room_info.id, &user_id, value)
"type": "CreateFeedback",
"payload": {
"roomId": room_info.id,
"userId": user_id,
"value": value.into_u8()
}
})
.to_string(); .to_string();
let _ = write.send(Message::Text(msg)).await;
let _ = write
.send(Message::Text(format!(
"SEND\ndestination:/queue/feedback.command\ncontent-type:application/json\ncontent-length:{}\n\n{}\0",
payload.chars().count(),
payload,
))).await;
}; };
}, },
Err(_) => Err(ConnectionError), Err(_) => Err(ConnectionError),