1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00
This commit is contained in:
Pierre HUBERT 2021-03-05 11:58:31 +01:00
parent fbf4728347
commit 0ae6ffb814
5 changed files with 50 additions and 73 deletions

View File

@ -1,33 +1,34 @@
//! # List of unread conversations
use serde::{Serialize};
use crate::data::unread_conversation::UnreadConversation;
use serde::Serialize;
use crate::api_data::conversation_api::ConversationAPI;
use crate::api_data::conversation_message_api::ConversationMessageAPI;
use crate::data::conversation::ConvID;
use crate::data::error::Res;
use crate::helpers::conversations_helper;
#[derive(Serialize)]
#[allow(non_snake_case)]
pub struct UnreadConversationAPI {
id: u64,
conv_name: String,
last_active: u64,
userID: u64,
message: String
conv: ConversationAPI,
message: ConversationMessageAPI,
}
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.id(),
message: conv.message.clone()
}
pub fn new(conv: ConvID) -> Res<UnreadConversationAPI> {
Ok(UnreadConversationAPI {
conv: ConversationAPI::new(&conversations_helper::get_single(conv)?),
message: conversations_helper::get_last_messages(conv, 1)?
.first()
.map(ConversationMessageAPI::new)
.unwrap(),
})
}
/// Turn a list of unread conversation into API conversations
pub fn for_list(l: &Vec<UnreadConversation>) -> Vec<UnreadConversationAPI> {
pub fn for_list(l: &Vec<ConvID>) -> Res<Vec<UnreadConversationAPI>> {
l.iter()
.map(|row| Self::new(row))
.map(|row| Self::new(row.clone()))
.collect()
}
}

View File

@ -93,7 +93,7 @@ pub fn update_settings(r: &mut HttpRequestHandler) -> RequestResult {
if r.has_post_parameter("following") {
conversations_helper::set_following(
&r.user_id()?,
conv_membership.member_id,
conv_membership.conv_id,
r.post_bool("following")?,
)?;
}
@ -208,12 +208,20 @@ pub fn refresh_single(r: &mut HttpRequestHandler) -> RequestResult {
let messages = match last_message_id {
// Get latest messages of the conversation
0 => conversations_helper::get_last_messages(conv.member_id, 10)?,
0 => conversations_helper::get_last_messages(conv.conv_id, 10)?,
// Get new messages
_ => conversations_helper::get_new_messages(conv.member_id, last_message_id)?,
_ => conversations_helper::get_new_messages(conv.conv_id, last_message_id)?,
};
if messages.len() > 0 {
conversations_helper::mark_user_seen(
conv.conv_id,
r.user_id_ref()?,
messages.iter().map(|m| m.id).max().unwrap(),
)?;
}
r.set_response(ConversationMessageAPI::for_list(&messages))
}
@ -230,7 +238,7 @@ pub fn get_older_messages(r: &mut HttpRequestHandler) -> RequestResult {
_ => 60
};
let messages = conversations_helper::get_older_messages(conv.member_id, max_id, limit)?;
let messages = conversations_helper::get_older_messages(conv.conv_id, max_id, limit)?;
r.set_response(ConversationMessageAPI::for_list(&messages))
}
@ -284,7 +292,7 @@ pub fn count_unread(r: &mut HttpRequestHandler) -> RequestResult {
pub fn list_unread(r: &mut HttpRequestHandler) -> RequestResult {
let list = conversations_helper::get_list_unread(&r.user_id()?)?;
r.set_response(UnreadConversationAPI::for_list(&list))
r.set_response(UnreadConversationAPI::for_list(&list)?)
}
/// Delete a conversation

View File

@ -13,7 +13,6 @@ pub mod new_conversation;
pub mod conversation;
pub mod conversation_message;
pub mod new_conversation_message;
pub mod unread_conversation;
pub mod group_id;
pub mod group;
pub mod new_group;

View File

@ -1,14 +0,0 @@
//! # 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

@ -8,7 +8,6 @@ use crate::data::conversation_message::{ConversationMessage, ConversationMessage
use crate::data::error::{ExecError, Res, ResultBoxError};
use crate::data::new_conversation::NewConversation;
use crate::data::new_conversation_message::NewConversationMessage;
use crate::data::unread_conversation::UnreadConversation;
use crate::data::user::{User, UserID};
use crate::helpers::{database, events_helper};
use crate::helpers::database::{InsertQuery, UpdateInfo};
@ -140,9 +139,9 @@ pub fn can_everyone_add_members(conv_id: ConvID) -> ResultBoxError<bool> {
}
/// Set whether a user is following a conversation or not
pub fn set_following(user_id: &UserID, conv_id: u64, following: bool) -> ResultBoxError<()> {
pub fn set_following(user_id: &UserID, conv_id: ConvID, following: bool) -> ResultBoxError<()> {
database::UpdateInfo::new(CONV_MEMBERS_TABLE)
.cond_u64("conv_id", conv_id)
.cond_conv_id("conv_id", conv_id)
.cond_user_id("user_id", user_id)
.set_legacy_bool("following", following)
.exec()
@ -205,9 +204,9 @@ pub fn find_private(user_1: &UserID, user_2: &UserID) -> ResultBoxError<Vec<Conv
}
/// Get the last messages posted in a conversation
pub fn get_last_messages(conv_id: u64, number_of_messages: u64) -> ResultBoxError<Vec<ConversationMessage>> {
pub fn get_last_messages(conv_id: ConvID, number_of_messages: u64) -> ResultBoxError<Vec<ConversationMessage>> {
database::QueryInfo::new(CONV_MESSAGES_TABLE)
.cond_u64("conv_id", conv_id)
.cond_conv_id("conv_id", conv_id)
.set_limit(number_of_messages)
.set_order("id DESC")
.exec(db_to_conversation_message)
@ -218,9 +217,9 @@ pub fn get_last_messages(conv_id: u64, number_of_messages: u64) -> ResultBoxErro
}
/// Get the new messages of a conversation
pub fn get_new_messages(conv_id: u64, last_message_id: u64) -> ResultBoxError<Vec<ConversationMessage>> {
pub fn get_new_messages(conv_id: ConvID, last_message_id: u64) -> ResultBoxError<Vec<ConversationMessage>> {
database::QueryInfo::new(CONV_MESSAGES_TABLE)
.cond_u64("conv_id", conv_id)
.cond_conv_id("conv_id", conv_id)
.set_custom_where("id > ?")
.add_custom_where_argument_u64(last_message_id)
.set_order("id")
@ -232,9 +231,9 @@ pub fn get_new_messages(conv_id: u64, last_message_id: u64) -> ResultBoxError<Ve
/// `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>> {
pub fn get_older_messages(conv_id: ConvID, start_id: u64, limit: u64) -> ResultBoxError<Vec<ConversationMessage>> {
database::QueryInfo::new(CONV_MESSAGES_TABLE)
.cond_u64("conv_id", conv_id)
.cond_conv_id("conv_id", conv_id)
.set_custom_where("ID <= ?")
.add_custom_where_argument_u64(start_id)
.set_order("id DESC")
@ -409,40 +408,24 @@ pub fn delete_message_by_id(id: u64) -> ResultBoxError<()> {
/// Count the number of unread conversation for a specified user
pub fn count_unread_for_user(user_id: &UserID) -> ResultBoxError<usize> {
database::QueryInfo::new(CONV_MEMBERS_TABLE)
.cond_user_id("user_id", user_id)
.cond_legacy_bool("saw_last_message", false)
.cond_legacy_bool("following", true)
.exec_count()
get_list_unread(user_id).map(|l| l.len())
}
/// Get the list of unread conversations of a user
pub fn get_list_unread(user_id: &UserID) -> ResultBoxError<Vec<UnreadConversation>> {
pub fn get_list_unread(user_id: &UserID) -> ResultBoxError<Vec<ConvID>> {
// First, get the ID of unread conversation
database::QueryInfo::new(CONV_MEMBERS_TABLE)
.alias("users")
.join(CONV_LIST_TABLE, "list", "users.conv_id = list.id")
.join(CONV_MESSAGES_TABLE, "messages", "messages.conv_id = users.conv_id")
.alias("mem")
.join(CONV_MESSAGES_TABLE, "mess", "mem.conv_id = mess.conv_id")
.cond_user_id("users.user_id", user_id)
.cond_legacy_bool("users.following", true)
.cond_user_id("mem.user_id", user_id)
.cond_legacy_bool("mem.following", true)
.set_custom_where("list.last_activity = messages.time_insert AND user.last_message_seen < messages.id")
.set_custom_where("mem.last_message_seen < mess.id")
.set_order("list.last_activity DESC")
.add_field("distinct mem.conv_id")
.add_field("messages.conv_id")
.add_field("name")
.add_field("last_activity")
.add_field("messages.user_id")
.add_field("message")
.exec(|res| Ok(UnreadConversation {
id: res.get_u64("conv_id")?,
name: res.get_optional_str("name")?,
last_active: res.get_u64("last_activity")?,
user_id: res.get_user_id("user_id")?,
message: res.get_str("message")?,
}))
.exec(|r| r.get_conv_id("conv_id"))
}
/// Indicate that a user has seen the last messages of a conversation