mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-25 23:09:22 +00:00
Can set conversation image
This commit is contained in:
parent
b43453651f
commit
385a800b7a
@ -6,6 +6,7 @@ use serde::Serialize;
|
|||||||
use crate::controllers::calls_controller;
|
use crate::controllers::calls_controller;
|
||||||
use crate::data::conversation::{Conversation, ConversationMember};
|
use crate::data::conversation::{Conversation, ConversationMember};
|
||||||
use crate::helpers::calls_helper;
|
use crate::helpers::calls_helper;
|
||||||
|
use crate::utils::user_data_utils::user_data_url;
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct ConversationMembersAPI {
|
struct ConversationMembersAPI {
|
||||||
@ -53,7 +54,7 @@ impl ConversationAPI {
|
|||||||
members: conv.members.iter().map(ConversationMembersAPI::new).collect(),
|
members: conv.members.iter().map(ConversationMembersAPI::new).collect(),
|
||||||
can_everyone_add_members: conv.can_everyone_add_members,
|
can_everyone_add_members: conv.can_everyone_add_members,
|
||||||
color: conv.color.clone(),
|
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()),
|
group_id: conv.group_id.as_ref().map(|i| i.id()),
|
||||||
|
|
||||||
can_have_call: calls_helper::can_have_call(conv),
|
can_have_call: calls_helper::can_have_call(conv),
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
//!
|
//!
|
||||||
//! @author Pierre Hubert
|
//! @author Pierre Hubert
|
||||||
|
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use crate::api_data::conversation_api::ConversationAPI;
|
use crate::api_data::conversation_api::ConversationAPI;
|
||||||
use crate::api_data::conversation_message_api::ConversationMessageAPI;
|
use crate::api_data::conversation_message_api::ConversationMessageAPI;
|
||||||
use crate::api_data::list_unread_conversations_api::UnreadConversationAPI;
|
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::routes::RequestResult;
|
||||||
use crate::utils::string_utils::remove_html_nodes;
|
use crate::utils::string_utils::remove_html_nodes;
|
||||||
use crate::utils::user_data_utils::{delete_user_data_file_if_exists, user_data_path};
|
use crate::utils::user_data_utils::{delete_user_data_file_if_exists, user_data_path};
|
||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
/// Create a new conversation
|
/// Create a new conversation
|
||||||
pub fn create(r: &mut HttpRequestHandler) -> RequestResult {
|
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!")
|
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
|
/// Add a new member to a conversation
|
||||||
pub fn add_member(r: &mut HttpRequestHandler) -> RequestResult {
|
pub fn add_member(r: &mut HttpRequestHandler) -> RequestResult {
|
||||||
let conv_membership = r.post_conv("convID")?;
|
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
|
/// Update admin status of a user
|
||||||
pub fn set_admin(r: &mut HttpRequestHandler) -> RequestResult {
|
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 conv = conversations_helper::get_single(conv_membership.conv_id)?;
|
||||||
let user_to_update = r.post_user_id("userID")?;
|
let user_to_update = r.post_user_id("userID")?;
|
||||||
let set_admin = r.post_bool("setAdmin")?;
|
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
|
/// Remove a member from a conversation
|
||||||
pub fn remove_member(r: &mut HttpRequestHandler) -> RequestResult {
|
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 conv = conversations_helper::get_single(conv_membership.conv_id)?;
|
||||||
|
|
||||||
let user_to_remove = r.post_user_id("userID")?;
|
let user_to_remove = r.post_user_id("userID")?;
|
||||||
|
@ -615,6 +615,18 @@ pub trait BaseRequestHandler {
|
|||||||
Ok(membership)
|
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<ConversationMember> {
|
||||||
|
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
|
/// Get the ID
|
||||||
fn post_group_id(&mut self, name: &str) -> ResultBoxError<GroupID> {
|
fn post_group_id(&mut self, name: &str) -> ResultBoxError<GroupID> {
|
||||||
let group_id = GroupID::new(self.post_u64(name)?);
|
let group_id = GroupID::new(self.post_u64(name)?);
|
||||||
|
@ -462,6 +462,28 @@ pub fn is_message_owner(user_id: &UserID, message_id: u64) -> ResultBoxError<boo
|
|||||||
.map(|r| r > 0)
|
.map(|r| r > 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
|
/// Turn a database entry into a ConversationInfo object
|
||||||
fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError<Conversation> {
|
fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError<Conversation> {
|
||||||
let conv_id = row.get_conv_id("id")?;
|
let conv_id = row.get_conv_id("id")?;
|
||||||
|
@ -194,6 +194,7 @@ pub fn get_routes() -> Vec<Route> {
|
|||||||
Route::post("/conversations/getList", Box::new(conversations_controller::get_list)),
|
Route::post("/conversations/getList", Box::new(conversations_controller::get_list)),
|
||||||
Route::post("/conversations/get_single", Box::new(conversations_controller::get_single)),
|
Route::post("/conversations/get_single", Box::new(conversations_controller::get_single)),
|
||||||
Route::post("/conversations/updateSettings", Box::new(conversations_controller::update_settings)),
|
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/addMember", Box::new(conversations_controller::add_member)),
|
||||||
Route::post("/conversations/setAdmin", Box::new(conversations_controller::set_admin)),
|
Route::post("/conversations/setAdmin", Box::new(conversations_controller::set_admin)),
|
||||||
Route::post("/conversations/removeMember", Box::new(conversations_controller::remove_member)),
|
Route::post("/conversations/removeMember", Box::new(conversations_controller::remove_member)),
|
||||||
|
Loading…
Reference in New Issue
Block a user