diff --git a/src/actions.rs b/src/actions.rs index 5014a04..e3d7462 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -12,6 +12,7 @@ pub fn handle_subscription(db: &Pool, data: &HashMap) -> bool { let mode = data.get("hub.mode").expect("Mode not provided"); let req_callback = data.get("hub.callback").expect("Callback not provided"); let req_topic = data.get("hub.topic").expect("Topic not provided"); + let timestamp = SystemTime::now() .duration_since(UNIX_EPOCH) .expect("Invalid Time") @@ -21,11 +22,6 @@ pub fn handle_subscription(db: &Pool, data: &HashMap) -> bool { .expect("Unable to calculate time"); let conn = db.get().expect("Unable to grab a DB connection"); - debug!( - log, - "Mode: {}, Callback: {}, topic: {}", mode, req_callback, req_topic - ); - if mode == &"subscribe" { let subscription = NewSubscription { callback: req_callback, @@ -46,7 +42,6 @@ pub fn handle_subscription(db: &Pool, data: &HashMap) -> bool { subscription.callback, subscription.topic ); - return true; } else if mode == &"unsubscribe" { let sub = subscriptions .filter(callback.eq(req_callback)) @@ -58,11 +53,17 @@ pub fn handle_subscription(db: &Pool, data: &HashMap) -> bool { log, "Subscription removed. Callback {}. Topic {}", req_callback, req_topic ); - return true; } else { debug!(log, "Wrong method."); return false; } + + return true; +} + +pub fn handle_publication(db: &Pool, data: &HashMap) -> bool { + // Implement later + true } #[cfg(test)] @@ -80,6 +81,6 @@ mod tests { .expect("Failed to create pool."); let hashmap = HashMap::new(); - handle_subscription(&pool, &hashmap); + assert_eq!(handle_subscription(&pool, &hashmap), false); } } diff --git a/src/controllers.rs b/src/controllers.rs index 35316e8..ef3752f 100644 --- a/src/controllers.rs +++ b/src/controllers.rs @@ -1,4 +1,4 @@ -use actions::handle_subscription; +use actions::{handle_publication, handle_subscription}; use actix_web::{http, web, HttpRequest, HttpResponse}; use askama::Template; use std::collections::HashMap; @@ -36,7 +36,11 @@ pub fn hub(state: web::Data, _req: HttpRequest, params: String) -> Htt } } - handle_subscription(db, ¶meters); + if parameters.get("hub.mode").expect("Mode not provided") == &"publish" { + handle_publication(db, ¶meters); + } else { + handle_subscription(db, ¶meters); + } return HttpResponse::Ok() .status(http::StatusCode::ACCEPTED) .finish(); diff --git a/src/utils.rs b/src/utils.rs index f21f7c9..fc19be3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -23,35 +23,46 @@ pub fn validate_parsed_data(parameters: &HashMap) -> Result<(), let callback; let mode; let topic; - - match parameters.get("hub.callback") { - Some(value) => callback = value, - None => return Err("No hub.callback specified".to_owned()), - }; + let url; match parameters.get("hub.mode") { Some(value) => mode = value, - None => return Err("No hub.mode specified".to_owned()), + None => return Err("No hub.mode specified".to_string()), }; - match parameters.get("hub.topic") { - Some(value) => topic = value, - None => return Err("No hub.topicspecified".to_owned()), - }; + if mode == &"publish" { + match parameters.get("hub.url") { + Some(value) => url = value, + None => return Err("No hub.url specified".to_string()), + }; - if mode != &"subscribe" && mode != &"unsubscribe" { + match Url::parse(url) { + Ok(value) => debug!(setup_logging(), "Valid URL: {}", value), + Err(_) => return Err("hub.url is not a valid URL".to_string()), + }; + } else if mode == &"subscribe" || mode == &"unsubscribe" { + match parameters.get("hub.callback") { + Some(value) => callback = value, + None => return Err("No hub.callback specified".to_string()), + }; + + match parameters.get("hub.topic") { + Some(value) => topic = value, + None => return Err("No hub.topicspecified".to_string()), + }; + + match Url::parse(callback) { + Ok(value) => debug!(setup_logging(), "Valid Callback: {}", value), + Err(_) => return Err("hub.callback is not a valid URL".to_string()), + }; + + match Url::parse(topic) { + Ok(value) => debug!(setup_logging(), "Valid Topic: {}", value), + Err(_) => return Err("hub.topic is not a valid URL".to_string()), + }; + } else { return Err(format!("Invalid Method: {}", mode)); } - - match Url::parse(callback) { - Ok(value) => debug!(setup_logging(), "Valid Callback: {}", value), - Err(_) => return Err("hub.callback is not a valid URL".to_owned()), - }; - - match Url::parse(topic) { - Ok(value) => debug!(setup_logging(), "Valid Topic: {}", value), - Err(_) => return Err("hub.topic is not a valid URL".to_owned()), - }; Ok(()) }