From 8465f0da655f1f8954afeaef429aeec2b82cdc41 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 3 Jun 2020 18:34:01 +0200 Subject: [PATCH] Gather information to create a new conversation --- src/controllers/conversations_controller.rs | 34 +++++++++++++++++++++ src/data/http_request_handler.rs | 10 ++++++ src/data/mod.rs | 3 +- src/data/new_conversation.rs | 14 +++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/data/new_conversation.rs diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index fbcc75a..a850f4c 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -4,8 +4,42 @@ use crate::data::http_request_handler::HttpRequestHandler; use crate::controllers::routes::RequestResult; +use crate::helpers::user_helper; +use crate::data::new_conversation::NewConversation; /// Create a new conversation pub fn create(r: &mut HttpRequestHandler) -> RequestResult { + let name = r.post_string("name")?; + let mut members = r.post_numbers_list("users", 1)?; + + // Adapt name + let name = match name.as_str() { + "false" => None, + s => Some(s.to_string()) + }; + + // Check if members exists + for user in &members { + if !user_helper::exists(user.clone())? { + r.not_found(format!("User {} not found!", user))?; + } + } + + // Add current user ID if required + let curr_user_id = r.user_id()? as i64; + if !members.contains(&curr_user_id) { + members.push(curr_user_id); + } + + let conv = NewConversation { + owner_id: r.user_id()?, + name, + owner_following: r.post_bool("follow")?, + members, + can_everyone_add_members: r.post_bool_opt("canEveryoneAddMembers", true) + }; + + println!("Conversation to create: {:#?}", conv); + r.success("create") } \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 01f0262..f70ff85 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -264,6 +264,16 @@ impl HttpRequestHandler { Ok(self.post_string(name)?.parse::()?) } + /// Get a boolean included in a POST request + pub fn post_bool(&mut self, name: &str) -> ResultBoxError { + Ok(self.post_string(name)?.eq("true")) + } + + /// Get an optional boolean included in post request + pub fn post_bool_opt(&mut self, name: &str, default: bool) -> bool { + self.post_bool(name).unwrap_or(default) + } + /// Get user ID. This function assess that a user ID is available to continue pub fn user_id(&self) -> ResultBoxError { match self.curr_user_id { diff --git a/src/data/mod.rs b/src/data/mod.rs index ed56c43..1e33f43 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -6,4 +6,5 @@ pub mod api_client; pub mod user; pub mod user_token; -pub mod custom_emoji; \ No newline at end of file +pub mod custom_emoji; +pub mod new_conversation; \ No newline at end of file diff --git a/src/data/new_conversation.rs b/src/data/new_conversation.rs new file mode 100644 index 0000000..7028742 --- /dev/null +++ b/src/data/new_conversation.rs @@ -0,0 +1,14 @@ +//! # New conversation information +//! +//! @author Pierre Huber + +use crate::data::user::UserID; + +#[derive(Debug)] +pub struct NewConversation { + pub owner_id: UserID, + pub name: Option, + pub owner_following: bool, + pub members: Vec, + pub can_everyone_add_members: bool +} \ No newline at end of file