mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-22 21:39:21 +00:00
Can open new conversations
This commit is contained in:
parent
479caa3945
commit
e7de23e995
42
src/api_data/conversation_message_api.rs
Normal file
42
src/api_data/conversation_message_api.rs
Normal file
@ -0,0 +1,42 @@
|
||||
//! # Conversation message API
|
||||
//!
|
||||
//! API representation of a conversation message
|
||||
//!
|
||||
//! @author Pierre HUBERT
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::data::conversation_message::ConversationMessage;
|
||||
use crate::utils::user_data_utils::user_data_url;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[allow(non_snake_case)]
|
||||
pub struct ConversationMessageAPI {
|
||||
ID: u64,
|
||||
convID: u64,
|
||||
ID_user: u64,
|
||||
time_insert: u64,
|
||||
message: String,
|
||||
image_path: Option<String>,
|
||||
}
|
||||
|
||||
impl ConversationMessageAPI {
|
||||
/// Turn a conversation message into an API entry
|
||||
pub fn new(msg: &ConversationMessage) -> ConversationMessageAPI {
|
||||
ConversationMessageAPI {
|
||||
ID: msg.id,
|
||||
convID: msg.conv_id,
|
||||
ID_user: msg.user_id as u64,
|
||||
time_insert: msg.time_sent,
|
||||
message: msg.message.clone().unwrap_or(String::new()),
|
||||
image_path: msg.image_path.as_ref().map(|f| user_data_url(f)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Turn a list of conversation messages into API entries
|
||||
pub fn for_list(l: &Vec<ConversationMessage>) -> Vec<ConversationMessageAPI> {
|
||||
l.iter()
|
||||
.map(|m| ConversationMessageAPI::new(m))
|
||||
.collect()
|
||||
}
|
||||
}
|
37
src/api_data/conversations_refresh_api.rs
Normal file
37
src/api_data/conversations_refresh_api.rs
Normal file
@ -0,0 +1,37 @@
|
||||
//! # Conversation refresh result
|
||||
//!
|
||||
//! Note : this structure is now deprecated and should no longer be used for further developments
|
||||
//!
|
||||
//! @author Pierre Hubert
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::api_data::conversation_message_api::ConversationMessageAPI;
|
||||
use crate::data::conversation_message::ConversationMessage;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[allow(non_snake_case)]
|
||||
pub struct ConversationRefreshResultAPI {
|
||||
#[serde(flatten)]
|
||||
list: HashMap<String, Vec<ConversationMessageAPI>>,
|
||||
}
|
||||
|
||||
impl ConversationRefreshResultAPI {
|
||||
/// Create a new list
|
||||
pub fn new(list: HashMap<u64, Vec<ConversationMessage>>) -> ConversationRefreshResultAPI {
|
||||
let list = list
|
||||
.iter()
|
||||
.map(|v| (
|
||||
format!("conversation-{}", v.0),
|
||||
ConversationMessageAPI::for_list(v.1)
|
||||
))
|
||||
.collect();
|
||||
|
||||
ConversationRefreshResultAPI {
|
||||
list
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,3 +18,5 @@ pub mod res_create_conversation;
|
||||
pub mod conversation_api;
|
||||
mod legacy_api_bool;
|
||||
pub mod res_find_private_conversations;
|
||||
pub mod conversation_message_api;
|
||||
pub mod conversations_refresh_api;
|
@ -2,7 +2,10 @@
|
||||
//!
|
||||
//! @author Pierre Hubert
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::api_data::conversation_api::ConversationAPI;
|
||||
use crate::api_data::conversations_refresh_api::ConversationRefreshResultAPI;
|
||||
use crate::api_data::res_create_conversation::ResCreateConversation;
|
||||
use crate::api_data::res_find_private_conversations::ResFindPrivateConversations;
|
||||
use crate::controllers::routes::RequestResult;
|
||||
@ -160,5 +163,24 @@ pub fn find_private(r: &mut HttpRequestHandler) -> RequestResult {
|
||||
///
|
||||
/// This method was used only by ComunicWeb before the introduction of WebSockets
|
||||
pub fn refresh_list(r: &mut HttpRequestHandler) -> RequestResult {
|
||||
r.success("Can implement")
|
||||
let mut list = HashMap::new();
|
||||
|
||||
// Check for new conversations
|
||||
if r.has_post_parameter("newConversations") {
|
||||
for conv_id in r.post_numbers_list("newConversations", 0)? {
|
||||
if !conversations_helper::does_user_belongs_to(r.user_id()?, conv_id as u64)? {
|
||||
r.forbidden(format!("Your do not belongs to conversation {} !", conv_id))?;
|
||||
}
|
||||
|
||||
let msgs_conv = conversations_helper::get_last_messages(conv_id as u64, 10)?;
|
||||
list.insert(conv_id as u64, msgs_conv);
|
||||
|
||||
//TODO : mark user seen
|
||||
}
|
||||
|
||||
// TODO : Check for refresh of already initialized conversations
|
||||
}
|
||||
|
||||
|
||||
r.set_response(ConversationRefreshResultAPI::new(list))
|
||||
}
|
15
src/data/conversation_message.rs
Normal file
15
src/data/conversation_message.rs
Normal file
@ -0,0 +1,15 @@
|
||||
//! # Conversation message
|
||||
//!
|
||||
//! Information about a single conversation message
|
||||
|
||||
use crate::data::user::UserID;
|
||||
|
||||
/// Information about a single conversation message
|
||||
pub struct ConversationMessage {
|
||||
pub id: u64,
|
||||
pub time_sent: u64,
|
||||
pub conv_id: u64,
|
||||
pub user_id: UserID,
|
||||
pub message: Option<String>,
|
||||
pub image_path: Option<String>,
|
||||
}
|
@ -9,3 +9,4 @@ pub mod user_token;
|
||||
pub mod custom_emoji;
|
||||
pub mod new_conversation;
|
||||
pub mod conversation;
|
||||
pub mod conversation_message;
|
@ -2,7 +2,7 @@
|
||||
//!
|
||||
//! @author Pierre Hubert
|
||||
|
||||
use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_USERS_TABLE};
|
||||
use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_USERS_TABLE, CONV_MESSAGES_TABLE};
|
||||
use crate::data::conversation::Conversation;
|
||||
use crate::data::error::{ExecError, ResultBoxError};
|
||||
use crate::data::new_conversation::NewConversation;
|
||||
@ -10,6 +10,7 @@ use crate::data::user::UserID;
|
||||
use crate::helpers::database::InsertQuery;
|
||||
use crate::helpers::database;
|
||||
use crate::utils::date_utils::time;
|
||||
use crate::data::conversation_message::ConversationMessage;
|
||||
|
||||
/// Create a new conversation. This method returns the ID of the created conversation
|
||||
pub fn create(conv: &NewConversation) -> ResultBoxError<u64> {
|
||||
@ -199,6 +200,19 @@ pub fn find_private(user_1: UserID, user_2: UserID) -> ResultBoxError<Vec<u64>>
|
||||
.exec(|f|f.get_u64("conv_id"))
|
||||
}
|
||||
|
||||
/// Get the last messages posted in a conversation
|
||||
pub fn get_last_messages(conv_id: u64, number_of_messages: u64) -> ResultBoxError<Vec<ConversationMessage>> {
|
||||
database::QueryInfo::new(CONV_MESSAGES_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.set_limit(number_of_messages)
|
||||
.set_order("id DESC")
|
||||
.exec(db_to_conversation_message)
|
||||
.map(|mut l| {
|
||||
l.sort_by(|a,b| a.id.partial_cmp(&b.id).unwrap());
|
||||
l
|
||||
})
|
||||
}
|
||||
|
||||
/// Turn a database entry into a ConversationInfo object
|
||||
fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError<Conversation> {
|
||||
let conv_id = row.get_u64("id")?;
|
||||
@ -214,3 +228,16 @@ fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError<Conversa
|
||||
saw_last_message: row.get_legacy_bool("saw_last_message")?,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/// Turn a database entry into a ConversationMessgae object
|
||||
fn db_to_conversation_message(row: &database::RowResult) -> ResultBoxError<ConversationMessage> {
|
||||
Ok(ConversationMessage {
|
||||
id: row.get_u64("id")?,
|
||||
time_sent: row.get_u64("time_insert")?,
|
||||
conv_id: row.get_u64("conv_id")?,
|
||||
user_id: row.get_user_id("user_id")?,
|
||||
message: row.get_optional_str("message")?,
|
||||
image_path: row.get_optional_str("image_path")?
|
||||
})
|
||||
}
|
@ -150,6 +150,12 @@ impl QueryInfo {
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the limit for the request
|
||||
pub fn set_limit(mut self, value: u64) -> QueryInfo {
|
||||
self.limit = value;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set results ordering
|
||||
pub fn set_order(mut self, order: &str) -> QueryInfo {
|
||||
self.order = Some(order.to_string());
|
||||
|
Loading…
Reference in New Issue
Block a user