1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Get the list of unread conversations

This commit is contained in:
Pierre HUBERT 2020-06-22 18:55:24 +02:00
parent 744111f416
commit 93a52d083f
6 changed files with 79 additions and 3 deletions

View File

@ -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<UnreadConversation>) -> Vec<UnreadConversationAPI> {
l.iter()
.map(|row| Self::new(row))
.collect()
}
}

View File

@ -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;
pub mod res_count_unread_conversations;
pub mod list_unread_conversations_api;

View File

@ -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))
}

View File

@ -10,4 +10,5 @@ pub mod custom_emoji;
pub mod new_conversation;
pub mod conversation;
pub mod conversation_message;
pub mod new_conversation_message;
pub mod new_conversation_message;
pub mod unread_conversation;

View File

@ -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<String>,
pub last_active: u64,
pub user_id: UserID,
pub message: String,
}

View File

@ -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<u64> {
@ -285,6 +286,29 @@ pub fn count_unread_for_user(user_id: UserID) -> ResultBoxError<usize> {
.exec_count()
}
/// Get the list of unread conversations of a user
pub fn get_list_unread(user_id: UserID) -> ResultBoxError<Vec<UnreadConversation>> {
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)