1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-24 06:19:21 +00:00
comunicapiv3/src/controllers/conversations_controller.rs

45 lines
1.2 KiB
Rust
Raw Normal View History

//! # Conversations controller
//!
//! @author Pierre Hubert
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")
}