1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-28 14:38:52 +00:00

Can get a conversation ID from a POST request

This commit is contained in:
Pierre HUBERT 2020-06-12 09:04:43 +02:00
parent bfd7cc0630
commit 619fc171e1
3 changed files with 28 additions and 3 deletions

View File

@ -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")
}

View File

@ -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::<i64>()?)
}
pub fn post_u64(&mut self, name: &str) -> ResultBoxError<u64> {
Ok(self.post_string(name)?.parse::<u64>()?)
}
/// Get a boolean included in a POST request
pub fn post_bool(&mut self, name: &str) -> ResultBoxError<bool> {
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<u64> {
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)
}
}

View File

@ -77,7 +77,15 @@ pub fn get_list_members(conv_id: u64) -> ResultBoxError<Vec<UserID>> {
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<bool> {
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