diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 0136bbb..5bd2e6b 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -12,9 +12,9 @@ use crate::api_data::res_find_private_conversations::ResFindPrivateConversations use crate::controllers::routes::RequestResult; use crate::data::http_request_handler::HttpRequestHandler; use crate::data::new_conversation::NewConversation; +use crate::data::new_conversation_message::NewConversationMessage; use crate::helpers::{conversations_helper, user_helper}; use crate::utils::string_utils::remove_html_nodes; -use crate::data::new_conversation_message::NewConversationMessage; /// Create a new conversation pub fn create(r: &mut HttpRequestHandler) -> RequestResult { @@ -232,6 +232,24 @@ pub fn refresh_single(r: &mut HttpRequestHandler) -> RequestResult { r.set_response(ConversationMessageAPI::for_list(&messages)) } +/// Get older messages of a conversation +pub fn get_older_messages(r: &mut HttpRequestHandler) -> RequestResult { + let conv_id = r.post_conv_id("conversationID")?; + let max_id = r.post_u64("oldest_message_id")? - 1; + + // Determine limit + let limit = r.post_u64("limit")?; + let limit = match limit { + 0 => 1, + 1..=60 => limit, + _ => 60 + }; + + let messages = conversations_helper::get_older_messages(conv_id, max_id, limit)?; + + r.set_response(ConversationMessageAPI::for_list(&messages)) +} + /// Send a new message pub fn send_message(r: &mut HttpRequestHandler) -> RequestResult { let conv_id = r.post_conv_id("conversationID")?; diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 9fbcea1..b41ab26 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -104,6 +104,8 @@ pub fn get_routes() -> Vec { Route::post("/conversations/refresh_single", Box::new(conversations_controller::refresh_single)), + Route::post("/conversations/get_older_messages", Box::new(conversations_controller::get_older_messages)), + Route::post("/conversations/sendMessage", Box::new(conversations_controller::send_message)), diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index c64d2ac..56451fc 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -224,6 +224,25 @@ pub fn get_new_messages(conv_id: u64, last_message_id: u64) -> ResultBoxError ResultBoxError> { + database::QueryInfo::new(CONV_MESSAGES_TABLE) + .cond_u64("conv_id", conv_id) + .set_custom_where("ID <= ?") + .add_custom_where_argument_u64(start_id) + .set_order("id DESC") + .set_limit(limit) + .exec(db_to_conversation_message) + .map(|mut l| { + l.sort_by(|a, b| a.id.partial_cmp(&b.id).unwrap()); + l + }) +} + /// Send a new conversation message pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> { let t = time();