diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 56b707b..e0edd60 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -55,5 +55,7 @@ pub fn get_list(r: &mut HttpRequestHandler) -> RequestResult { /// Get information about a single conversation pub fn get_single(r: &mut HttpRequestHandler) -> RequestResult { - r.success("Implement id") + let conversation_id = r.post_conv_id("conversationID")?; + + r.success("do it") } \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index f70ff85..c7dea90 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -12,7 +12,7 @@ use crate::data::api_client::APIClient; use crate::data::config::conf; use crate::data::error::{ExecError, ResultBoxError}; use crate::data::user::UserID; -use crate::helpers::{account_helper, api_helper, user_helper}; +use crate::helpers::{account_helper, api_helper, user_helper, conversations_helper}; use crate::utils::virtual_directories_utils::check_virtual_directory; /// Http request handler @@ -264,6 +264,10 @@ impl HttpRequestHandler { Ok(self.post_string(name)?.parse::()?) } + pub fn post_u64(&mut self, name: &str) -> ResultBoxError { + 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")) @@ -343,4 +347,15 @@ impl HttpRequestHandler { Ok(dir) } + + /// Get & return the ID of the conversation included in the POST request + pub fn post_conv_id(&mut self, name: &str) -> ResultBoxError { + let conv_id = self.post_u64(name)?; + + if !conversations_helper::does_user_belongs_to(self.user_id()?, conv_id)? { + self.forbidden(format!("You do not belong to conversation {} !", conv_id))?; + } + + Ok(conv_id) + } } \ No newline at end of file diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 8567604..dbd9493 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -77,7 +77,15 @@ pub fn get_list_members(conv_id: u64) -> ResultBoxError> { database::QueryInfo::new(CONV_USERS_TABLE) .cond_u64("conv_id", conv_id) .add_field("user_id") - .exec(|res|res.get_user_id("user_id")) + .exec(|res| res.get_user_id("user_id")) +} + +/// Check if a user belongs to a conversation or not +pub fn does_user_belongs_to(user_id: UserID, conv_id: u64) -> ResultBoxError { + Ok(database::QueryInfo::new(CONV_USERS_TABLE) + .cond_u64("conv_id", conv_id) + .cond_user_id("user_id", user_id) + .exec_count()? > 0) } /// Turn a database entry into a ConversationInfo object