add publish mode

This commit is contained in:
Gonçalo Valério 2019-09-03 12:38:08 +01:00
parent d4c3ec39c0
commit ba1435068b
3 changed files with 47 additions and 31 deletions

View File

@ -12,6 +12,7 @@ pub fn handle_subscription(db: &Pool, data: &HashMap<String, String>) -> 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<String, String>) -> 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<String, String>) -> 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<String, String>) -> 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<String, String>) -> 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);
}
}

View File

@ -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<AppState>, _req: HttpRequest, params: String) -> Htt
}
}
handle_subscription(db, &parameters);
if parameters.get("hub.mode").expect("Mode not provided") == &"publish" {
handle_publication(db, &parameters);
} else {
handle_subscription(db, &parameters);
}
return HttpResponse::Ok()
.status(http::StatusCode::ACCEPTED)
.finish();

View File

@ -23,35 +23,46 @@ pub fn validate_parsed_data(parameters: &HashMap<String, String>) -> 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(())
}