use actix::Message as ActixMessage;
use std::str::FromStr;
use crate::websocket::webchat::input::subscribes::SubscribeChannel;
use crate::websocket::webchat::ConnectId;
#[derive(ActixMessage)]
#[rtype(result = "()")]
pub struct SubscribeAction {
pub id: ConnectId,
pub channel: SubscribeChannel,
}
impl SubscribeAction {
pub fn parse(connection_id: &str, data: &serde_json::Value) -> Result<SubscribeAction, String> {
match crate::commons::get_value_as_string(data, "channel") {
Ok(Some(c)) => match SubscribeChannel::from_str(&c) {
Ok(channel) => Ok(SubscribeAction {
id: connection_id.to_owned(),
channel,
}),
Err(_) => Err(format!("invalid channel: {}", c)),
},
Ok(None) => Err("channel is required".to_string()),
Err(err) => Err(format!("invalid channel: error={}", err)),
}
}
}