2020-06-03 11:36:19 +00:00
|
|
|
//! # Conversations controller
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
2020-06-15 12:42:02 +00:00
|
|
|
use crate::api_data::conversation_api::ConversationAPI;
|
|
|
|
use crate::api_data::res_create_conversation::ResCreateConversation;
|
2020-06-03 11:36:19 +00:00
|
|
|
use crate::controllers::routes::RequestResult;
|
2020-06-15 12:42:02 +00:00
|
|
|
use crate::data::http_request_handler::HttpRequestHandler;
|
2020-06-03 16:34:01 +00:00
|
|
|
use crate::data::new_conversation::NewConversation;
|
2020-06-15 12:42:02 +00:00
|
|
|
use crate::helpers::{conversations_helper, user_helper};
|
|
|
|
use crate::utils::string_utils::remove_html_nodes;
|
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-12 06:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get information about a single conversation
|
|
|
|
pub fn get_single(r: &mut HttpRequestHandler) -> RequestResult {
|
2020-06-12 07:04:43 +00:00
|
|
|
let conversation_id = r.post_conv_id("conversationID")?;
|
2020-06-12 07:20:32 +00:00
|
|
|
let conv = conversations_helper::get_single(conversation_id, r.user_id()?)?;
|
2020-06-12 07:04:43 +00:00
|
|
|
|
2020-06-12 07:20:32 +00:00
|
|
|
r.set_response(ConversationAPI::new(&conv))
|
2020-06-12 07:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Update the settings of a conversation
|
|
|
|
pub fn update_settings(r: &mut HttpRequestHandler) -> RequestResult {
|
2020-06-12 09:26:12 +00:00
|
|
|
let conv_id = r.post_conv_id("conversationID")?;
|
|
|
|
let is_moderator = conversations_helper::is_user_moderator(r.user_id()?, conv_id)?;
|
|
|
|
|
2020-06-15 06:23:23 +00:00
|
|
|
// Update following state, if required
|
|
|
|
if r.has_post_parameter("following") {
|
|
|
|
conversations_helper::set_following(
|
|
|
|
r.user_id()?,
|
|
|
|
conv_id,
|
2020-06-15 08:46:10 +00:00
|
|
|
r.post_bool("following")?,
|
2020-06-15 06:23:23 +00:00
|
|
|
)?;
|
|
|
|
}
|
2020-06-12 09:26:12 +00:00
|
|
|
|
2020-06-15 07:29:38 +00:00
|
|
|
// Update members list
|
|
|
|
if r.has_post_parameter("members") {
|
2020-06-15 08:46:10 +00:00
|
|
|
let mut members = r.post_numbers_list("members", 1)?;
|
2020-06-15 07:29:38 +00:00
|
|
|
let can_everyone_add_members = conversations_helper::can_everyone_add_members(conv_id)?;
|
|
|
|
|
|
|
|
if !is_moderator && !can_everyone_add_members {
|
|
|
|
r.forbidden("You can not update the list of members of this conversation!".to_string())?;
|
|
|
|
}
|
2020-06-15 08:46:10 +00:00
|
|
|
|
2020-06-15 09:09:16 +00:00
|
|
|
// Check if the members of the conversation really exists
|
|
|
|
for member in &members {
|
|
|
|
if !user_helper::exists(member.clone())? {
|
|
|
|
r.not_found(format!("User {} not found!", member))?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:46:10 +00:00
|
|
|
if !members.contains(&r.user_id()?) {
|
|
|
|
members.push(r.user_id()?);
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:53:50 +00:00
|
|
|
conversations_helper::set_members(conv_id, &members, is_moderator)?;
|
2020-06-15 07:29:38 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 12:30:01 +00:00
|
|
|
// Change moderator settings
|
|
|
|
if r.has_post_parameter("name") || r.has_post_parameter("canEveryoneAddMembers") {
|
|
|
|
if !is_moderator {
|
|
|
|
r.forbidden("You are not allowed to perform changes on this conversation!".to_string())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change conversation name
|
|
|
|
if r.has_post_parameter("name") {
|
|
|
|
let name = r.post_string_opt("name", 0, true)?;
|
|
|
|
let name = if name.eq("false") || name.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2020-06-15 12:42:02 +00:00
|
|
|
Some(remove_html_nodes(&name))
|
2020-06-15 12:30:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
conversations_helper::set_name(conv_id, name)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 06:23:23 +00:00
|
|
|
r.success("Conversation information successfully updated!")
|
2020-06-03 11:36:19 +00:00
|
|
|
}
|