From 385a800b7a01d9177b281502e53144a380d06f0a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 7 Mar 2021 15:06:44 +0100 Subject: [PATCH] Can set conversation image --- src/api_data/conversation_api.rs | 3 ++- src/controllers/conversations_controller.rs | 21 +++++++++++++++++--- src/data/base_request_handler.rs | 12 +++++++++++ src/helpers/conversations_helper.rs | 22 +++++++++++++++++++++ src/routes.rs | 1 + 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/api_data/conversation_api.rs b/src/api_data/conversation_api.rs index 9e849c3..8fb15f0 100644 --- a/src/api_data/conversation_api.rs +++ b/src/api_data/conversation_api.rs @@ -6,6 +6,7 @@ use serde::Serialize; use crate::controllers::calls_controller; use crate::data::conversation::{Conversation, ConversationMember}; use crate::helpers::calls_helper; +use crate::utils::user_data_utils::user_data_url; #[derive(Serialize)] struct ConversationMembersAPI { @@ -53,7 +54,7 @@ impl ConversationAPI { members: conv.members.iter().map(ConversationMembersAPI::new).collect(), can_everyone_add_members: conv.can_everyone_add_members, color: conv.color.clone(), - logo: conv.logo.clone(), + logo: conv.logo.as_ref().map(|s| user_data_url(s)), group_id: conv.group_id.as_ref().map(|i| i.id()), can_have_call: calls_helper::can_have_call(conv), diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 9b93de5..34890c0 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -2,6 +2,8 @@ //! //! @author Pierre Hubert +use std::collections::HashSet; + use crate::api_data::conversation_api::ConversationAPI; use crate::api_data::conversation_message_api::ConversationMessageAPI; use crate::api_data::list_unread_conversations_api::UnreadConversationAPI; @@ -24,7 +26,6 @@ use crate::helpers::events_helper::Event; use crate::routes::RequestResult; use crate::utils::string_utils::remove_html_nodes; use crate::utils::user_data_utils::{delete_user_data_file_if_exists, user_data_path}; -use std::collections::HashSet; /// Create a new conversation pub fn create(r: &mut HttpRequestHandler) -> RequestResult { @@ -106,6 +107,20 @@ pub fn update_settings(r: &mut HttpRequestHandler) -> RequestResult { r.success("Conversation information successfully updated!") } +/// Change conversation image +pub fn change_image(r: &mut HttpRequestHandler) -> RequestResult { + let conv_membership = r.post_conv_admin("convID")?; + let conv = conversations_helper::get_single(conv_membership.conv_id)?; + + let new_image = r.save_post_image("file", "conv-image", 200, 200)?; + + conversations_helper::remove_conversation_image(&conv)?; + + conversations_helper::set_conversation_image(&conv, &new_image)?; + + r.ok() +} + /// Add a new member to a conversation pub fn add_member(r: &mut HttpRequestHandler) -> RequestResult { let conv_membership = r.post_conv("convID")?; @@ -132,7 +147,7 @@ pub fn add_member(r: &mut HttpRequestHandler) -> RequestResult { /// Update admin status of a user pub fn set_admin(r: &mut HttpRequestHandler) -> RequestResult { - let conv_membership = r.post_conv("convID")?; + let conv_membership = r.post_conv_admin("convID")?; let conv = conversations_helper::get_single(conv_membership.conv_id)?; let user_to_update = r.post_user_id("userID")?; let set_admin = r.post_bool("setAdmin")?; @@ -156,7 +171,7 @@ pub fn set_admin(r: &mut HttpRequestHandler) -> RequestResult { /// Remove a member from a conversation pub fn remove_member(r: &mut HttpRequestHandler) -> RequestResult { - let conv_membership = r.post_conv("convID")?; + let conv_membership = r.post_conv_admin("convID")?; let conv = conversations_helper::get_single(conv_membership.conv_id)?; let user_to_remove = r.post_user_id("userID")?; diff --git a/src/data/base_request_handler.rs b/src/data/base_request_handler.rs index 45dd43c..6469892 100644 --- a/src/data/base_request_handler.rs +++ b/src/data/base_request_handler.rs @@ -615,6 +615,18 @@ pub trait BaseRequestHandler { Ok(membership) } + /// Get information about a conversation included in the request. The user must be an admin + /// of the target conversation + fn post_conv_admin(&mut self, name: &str) -> ResultBoxError { + let conv = self.post_conv(name)?; + + if !conv.is_admin { + self.forbidden("You are not an administrator of this conversation!".to_string())?; + } + + Ok(conv) + } + /// Get the ID fn post_group_id(&mut self, name: &str) -> ResultBoxError { let group_id = GroupID::new(self.post_u64(name)?); diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 4a6537f..30d3626 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -462,6 +462,28 @@ pub fn is_message_owner(user_id: &UserID, message_id: u64) -> ResultBoxError 0) } +/// Remove conversation image +pub fn remove_conversation_image(conv: &Conversation) -> Res { + if let Some(image) = &conv.logo { + delete_user_data_file_if_exists(image)?; + + database::UpdateInfo::new(CONV_LIST_TABLE) + .cond_conv_id("id", conv.id) + .set_opt_str("logo", None) + .exec()?; + } + + Ok(()) +} + +/// Set a new conversation image +pub fn set_conversation_image(conv: &Conversation, new_image: &str) -> Res { + database::UpdateInfo::new(CONV_LIST_TABLE) + .cond_conv_id("id", conv.id) + .set_opt_str("logo", Some(new_image.to_string())) + .exec() +} + /// Turn a database entry into a ConversationInfo object fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError { let conv_id = row.get_conv_id("id")?; diff --git a/src/routes.rs b/src/routes.rs index ed6427c..d96cce1 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -194,6 +194,7 @@ pub fn get_routes() -> Vec { Route::post("/conversations/getList", Box::new(conversations_controller::get_list)), Route::post("/conversations/get_single", Box::new(conversations_controller::get_single)), Route::post("/conversations/updateSettings", Box::new(conversations_controller::update_settings)), + Route::post("/conversations/change_image", Box::new(conversations_controller::change_image)), Route::post("/conversations/addMember", Box::new(conversations_controller::add_member)), Route::post("/conversations/setAdmin", Box::new(conversations_controller::set_admin)), Route::post("/conversations/removeMember", Box::new(conversations_controller::remove_member)),