mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 01:24:04 +00:00 
			
		
		
		
	Gather information to create a new conversation
This commit is contained in:
		@@ -4,8 +4,42 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
use crate::data::http_request_handler::HttpRequestHandler;
 | 
					use crate::data::http_request_handler::HttpRequestHandler;
 | 
				
			||||||
use crate::controllers::routes::RequestResult;
 | 
					use crate::controllers::routes::RequestResult;
 | 
				
			||||||
 | 
					use crate::helpers::user_helper;
 | 
				
			||||||
 | 
					use crate::data::new_conversation::NewConversation;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Create a new conversation
 | 
					/// Create a new conversation
 | 
				
			||||||
pub fn create(r: &mut HttpRequestHandler) -> RequestResult {
 | 
					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")
 | 
					    r.success("create")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -264,6 +264,16 @@ impl HttpRequestHandler {
 | 
				
			|||||||
        Ok(self.post_string(name)?.parse::<i64>()?)
 | 
					        Ok(self.post_string(name)?.parse::<i64>()?)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// 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"))
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// 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
 | 
					    /// Get user ID. This function assess that a user ID is available to continue
 | 
				
			||||||
    pub fn user_id(&self) -> ResultBoxError<UserID> {
 | 
					    pub fn user_id(&self) -> ResultBoxError<UserID> {
 | 
				
			||||||
        match self.curr_user_id {
 | 
					        match self.curr_user_id {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,3 +7,4 @@ pub mod api_client;
 | 
				
			|||||||
pub mod user;
 | 
					pub mod user;
 | 
				
			||||||
pub mod user_token;
 | 
					pub mod user_token;
 | 
				
			||||||
pub mod custom_emoji;
 | 
					pub mod custom_emoji;
 | 
				
			||||||
 | 
					pub mod new_conversation;
 | 
				
			||||||
							
								
								
									
										14
									
								
								src/data/new_conversation.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/data/new_conversation.rs
									
									
									
									
									
										Normal file
									
								
							@@ -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<String>,
 | 
				
			||||||
 | 
					    pub owner_following: bool,
 | 
				
			||||||
 | 
					    pub members: Vec<i64>,
 | 
				
			||||||
 | 
					    pub can_everyone_add_members: bool
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user