mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-10-30 23:24:42 +00:00 
			
		
		
		
	Can get older conversation messages
This commit is contained in:
		| @@ -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")?; | ||||
|   | ||||
| @@ -104,6 +104,8 @@ pub fn get_routes() -> Vec<Route> { | ||||
|  | ||||
|         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)), | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -224,6 +224,25 @@ pub fn get_new_messages(conv_id: u64, last_message_id: u64) -> ResultBoxError<Ve | ||||
|         .exec(db_to_conversation_message) | ||||
| } | ||||
|  | ||||
| /// Get older messages of a conversation | ||||
| /// | ||||
| /// `conv_id` contains the ID of the target conversation | ||||
| /// `start_id` contains the ID from wich the research start | ||||
| /// `limit` Maximum number of messages to get | ||||
| pub fn get_older_messages(conv_id: u64, start_id: u64, limit: u64) -> ResultBoxError<Vec<ConversationMessage>> { | ||||
|     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(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user