2020-06-03 11:36:19 +00:00
|
|
|
//! # Conversations controller
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
|
|
|
use crate::data::http_request_handler::HttpRequestHandler;
|
|
|
|
use crate::controllers::routes::RequestResult;
|
2020-06-04 11:36:57 +00:00
|
|
|
use crate::helpers::{user_helper, conversations_helper};
|
2020-06-03 16:34:01 +00:00
|
|
|
use crate::data::new_conversation::NewConversation;
|
2020-06-04 15:47:52 +00:00
|
|
|
use crate::api_data::res_create_conversation::ResCreateConversation;
|
2020-06-04 17:01:35 +00:00
|
|
|
use crate::api_data::conversation_api::ConversationAPI;
|
2020-06-03 11:36:19 +00:00
|
|
|
|
|
|
|
/// Create a new conversation
|
|
|
|
pub fn create(r: &mut HttpRequestHandler) -> RequestResult {
|
2020-06-03 16:34:01 +00:00
|
|
|
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,
|
2020-06-04 15:47:52 +00:00
|
|
|
can_everyone_add_members: r.post_bool_opt("canEveryoneAddMembers", true),
|
2020-06-03 16:34:01 +00:00
|
|
|
};
|
|
|
|
|
2020-06-04 15:47:52 +00:00
|
|
|
// Create the conversation
|
2020-06-04 11:36:57 +00:00
|
|
|
let conv_id = conversations_helper::create(&conv)?;
|
2020-06-04 15:47:52 +00:00
|
|
|
r.set_response(ResCreateConversation::new(conv_id))
|
2020-06-04 15:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the list of conversations of a user
|
|
|
|
pub fn get_list(r: &mut HttpRequestHandler) -> RequestResult {
|
2020-06-04 17:01:35 +00:00
|
|
|
|
|
|
|
let list = conversations_helper::get_list_user(r.user_id()?)?;
|
|
|
|
|
|
|
|
r.set_response(list.iter().map(|c| ConversationAPI::new(c)).collect::<Vec<ConversationAPI>>())
|
2020-06-03 11:36:19 +00:00
|
|
|
}
|