1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-21 00:45:18 +00:00

Can open new conversations

This commit is contained in:
2020-06-18 14:15:17 +02:00
parent 479caa3945
commit e7de23e995
8 changed files with 156 additions and 4 deletions

View File

@ -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")?;
@ -213,4 +227,17 @@ fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError<Conversa
following: row.get_legacy_bool("following")?,
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")?
})
}

View File

@ -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());