diff --git a/src/api_data/list_unread_conversations_api.rs b/src/api_data/list_unread_conversations_api.rs new file mode 100644 index 0000000..1f55dd3 --- /dev/null +++ b/src/api_data/list_unread_conversations_api.rs @@ -0,0 +1,33 @@ +//! # List of unread conversations +use serde::{Serialize}; +use crate::data::unread_conversation::UnreadConversation; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct UnreadConversationAPI { + id: u64, + conv_name: String, + last_active: u64, + userID: u64, + message: String +} + +impl UnreadConversationAPI { + /// Construct a new instance + pub fn new(conv: &UnreadConversation) -> UnreadConversationAPI { + UnreadConversationAPI { + id: conv.id, + conv_name: conv.name.clone().unwrap_or(String::new()), + last_active: conv.last_active, + userID: conv.user_id as u64, + message: conv.message.clone() + } + } + + /// Turn a list of unread conversation into API conversations + pub fn for_list(l: &Vec) -> Vec { + l.iter() + .map(|row| Self::new(row)) + .collect() + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 7db3c48..60d2bbe 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -20,4 +20,5 @@ mod legacy_api_bool; pub mod res_find_private_conversations; pub mod conversation_message_api; pub mod conversations_refresh_api; -pub mod res_count_unread_conversations; \ No newline at end of file +pub mod res_count_unread_conversations; +pub mod list_unread_conversations_api; \ No newline at end of file diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index cebfd14..e42d1f5 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -16,6 +16,7 @@ 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::api_data::list_unread_conversations_api::UnreadConversationAPI; /// Create a new conversation pub fn create(r: &mut HttpRequestHandler) -> RequestResult { @@ -285,5 +286,7 @@ pub fn count_unread(r: &mut HttpRequestHandler) -> RequestResult { /// Get the list of unread conversations of a user pub fn list_unread(r: &mut HttpRequestHandler) -> RequestResult { - r.success("implement me") + let list = conversations_helper::get_list_unread(r.user_id()?)?; + + r.set_response(UnreadConversationAPI::for_list(&list)) } \ No newline at end of file diff --git a/src/data/mod.rs b/src/data/mod.rs index 6e03249..ed3a21c 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -10,4 +10,5 @@ pub mod custom_emoji; pub mod new_conversation; pub mod conversation; pub mod conversation_message; -pub mod new_conversation_message; \ No newline at end of file +pub mod new_conversation_message; +pub mod unread_conversation; \ No newline at end of file diff --git a/src/data/unread_conversation.rs b/src/data/unread_conversation.rs new file mode 100644 index 0000000..b2e07fc --- /dev/null +++ b/src/data/unread_conversation.rs @@ -0,0 +1,14 @@ +//! # Unread conversation +//! +//! @author Pierre Hubert + +use crate::data::user::UserID; + +/// Unread conversation information +pub struct UnreadConversation { + pub id: u64, + pub name: Option, + pub last_active: u64, + pub user_id: UserID, + pub message: String, +} \ No newline at end of file diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 870b7b5..7667947 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -12,6 +12,7 @@ use crate::data::user::UserID; use crate::helpers::database; use crate::helpers::database::InsertQuery; use crate::utils::date_utils::time; +use crate::data::unread_conversation::UnreadConversation; /// Create a new conversation. This method returns the ID of the created conversation pub fn create(conv: &NewConversation) -> ResultBoxError { @@ -285,6 +286,29 @@ pub fn count_unread_for_user(user_id: UserID) -> ResultBoxError { .exec_count() } +/// Get the list of unread conversations of a user +pub fn get_list_unread(user_id: UserID) -> ResultBoxError> { + database::QueryInfo::new(CONV_USERS_TABLE) + .alias("users") + .join(CONV_LIST_TABLE, "list", "users.conv_id = list.id") + .join(CONV_MESSAGES_TABLE, "messages", "messages.conv_id = users.conv_id") + + .cond_user_id("users.user_id", user_id) + .cond_legacy_bool("users.following", true) + .cond_legacy_bool("users.saw_last_message", false) + + .set_custom_where("list.last_active = messages.time_insert") + + .set_order("list.last_active DESC") + .exec(|res| Ok(UnreadConversation{ + id: res.get_u64("conv_id")?, + name: res.get_optional_str("name")?, + last_active: res.get_u64("last_active")?, + user_id: res.get_user_id("user_id")?, + message: res.get_str("message")? + })) +} + /// Indicate that a user has seen the last messages of a conversation pub fn mark_user_seen(conv_id: u64, user_id: UserID) -> ResultBoxError<()> { database::UpdateInfo::new(CONV_USERS_TABLE)