Skip to content

Commit

Permalink
ws::handlers > improve handle_cert_select
Browse files Browse the repository at this point in the history
  • Loading branch information
canewsin committed Jan 22, 2024
1 parent 14b85d4 commit 069f6b5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
25 changes: 17 additions & 8 deletions src/plugins/websocket/handlers/sites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde_json::{json, Value};

use super::{
super::{error::Error, request::Command, response::Message, ZeruWebsocket},
users::get_current_user,
users::{get_current_user, handle_cert_set},
};
use crate::{
environment::SITE_PERMISSIONS_DETAILS,
Expand Down Expand Up @@ -200,18 +200,27 @@ pub fn handle_cert_select(ws: &mut ZeruWebsocket, cmd: &Command) -> Result<Messa
}
});
}
let script = format!(

let _ = ws.cmd(
"notification",
json!(["ask", body]),
Some(Box::new(move |ws, cmd| Some(handle_cert_set(ws, cmd)))),
None,
);
let script = notification_script_template(ws.next_message_id - 1);
cmd.inject_script(ws.next_message_id as isize, script)
}

fn notification_script_template(id: usize) -> String {
format!(
"
$(\".notification .select.cert\").on(\"click\", function() {{
$(\".notification .select\").removeClass('active')
zeroframe.response({}, this.title)
return false
}})
",
ws.next_message_id
);
ws.send_notification(json!(["ask", body])); //TODO!: Need callback for response
cmd.inject_script(script)
}})",
id
)
}

pub fn handle_site_info(ws: &ZeruWebsocket, command: &Command) -> Result<Message, Error> {
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/websocket/handlers/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,16 @@ pub fn _handle_user_show_master_seed(
command.respond(user.get_master_seed())
}

pub fn handle_cert_set(ws: &ZeruWebsocket, command: &Command) -> Result<Message, Error> {
pub fn handle_cert_set(ws: &mut ZeruWebsocket, command: &Command) -> Result<Message, Error> {
trace!("Handling CertSet with command: {:?}", command);
let site = ws.address.address.clone();
let provider = command.params[0].as_str().unwrap().to_string();
let provider = command.params.as_str().unwrap().to_string();
let _ = block_on(ws.user_controller.send(UserSetSiteCertRequest {
user_addr: String::from("current"),
site_addr: site,
provider,
provider: provider.clone(),
}))?;
ws.update_websocket(Some(json!(vec!["cert_changed", &provider])));
command.respond("ok")
}

Expand Down
1 change: 1 addition & 0 deletions src/plugins/websocket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ impl ZeruWebsocket {
cmd: event_name.to_string(),
params: event,
});
self.next_message_id += 1;
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/websocket/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ impl Command {
Ok(resp)
}

pub fn inject_script<T: Serialize>(&self, body: T) -> Result<Message, Error> {
let resp = Message::inject_script(self.id, serde_json::to_value(body)?);
pub fn inject_script<T: Serialize>(&self, id: isize, body: T) -> Result<Message, Error> {
let resp = Message::inject_script(id, serde_json::to_value(body)?);
Ok(resp)
}

Expand Down
12 changes: 9 additions & 3 deletions src/plugins/websocket/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ use serde_json::json;

use crate::utils::is_default;

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
pub struct Message {
cmd: MessageType,
#[serde(skip_serializing_if = "is_default")]
pub id: usize,
#[serde(skip_serializing_if = "is_default")]
to: isize,
#[serde(skip_serializing_if = "is_default")]
result: serde_json::Value,
#[serde(skip_serializing_if = "is_default")]
params: serde_json::Value,
}

#[derive(Serialize, Deserialize, PartialEq)]
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub enum MessageType {
Command,
Expand All @@ -30,15 +33,17 @@ impl Message {
to: id,
result: body,
id: 0,
params: json!(null),
}
}

pub fn inject_script(id: isize, body: serde_json::Value) -> Message {
Message {
cmd: MessageType::InjectScript,
to: id,
result: body,
result: json!(null),
id: 0,
params: body,
}
}

Expand All @@ -51,6 +56,7 @@ impl Message {
cmd: MessageType::Command,
to: 0,
result: json!(null),
params: json!(null),
id: 0,
}
}
Expand Down

0 comments on commit 069f6b5

Please sign in to comment.