mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 09:34:04 +00:00 
			
		
		
		
	Mark the conversation as seen
This commit is contained in:
		@@ -172,10 +172,10 @@ pub fn refresh_list(r: &mut HttpRequestHandler) -> RequestResult {
 | 
			
		||||
                r.forbidden(format!("Your do not belongs to conversation {} !", conv_id))?;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            let msgs_conv = conversations_helper::get_last_messages(conv_id as u64, 10)?;
 | 
			
		||||
            list.insert(conv_id as u64, msgs_conv);
 | 
			
		||||
            let list_conv = conversations_helper::get_last_messages(conv_id as u64, 10)?;
 | 
			
		||||
            list.insert(conv_id as u64, list_conv);
 | 
			
		||||
 | 
			
		||||
            //TODO : mark user seen
 | 
			
		||||
            conversations_helper::mark_user_seen(conv_id as u64, r.user_id()?)?;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // TODO : Check for refresh of already initialized conversations
 | 
			
		||||
 
 | 
			
		||||
@@ -2,15 +2,15 @@
 | 
			
		||||
//!
 | 
			
		||||
//! @author Pierre Hubert
 | 
			
		||||
 | 
			
		||||
use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_USERS_TABLE, CONV_MESSAGES_TABLE};
 | 
			
		||||
use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_MESSAGES_TABLE, CONV_USERS_TABLE};
 | 
			
		||||
use crate::data::conversation::Conversation;
 | 
			
		||||
use crate::data::conversation_message::ConversationMessage;
 | 
			
		||||
use crate::data::error::{ExecError, ResultBoxError};
 | 
			
		||||
use crate::data::new_conversation::NewConversation;
 | 
			
		||||
use crate::data::user::UserID;
 | 
			
		||||
use crate::helpers::database::InsertQuery;
 | 
			
		||||
use crate::helpers::database;
 | 
			
		||||
use crate::helpers::database::InsertQuery;
 | 
			
		||||
use crate::utils::date_utils::time;
 | 
			
		||||
use crate::data::conversation_message::ConversationMessage;
 | 
			
		||||
 | 
			
		||||
/// Create a new conversation. This method returns the ID of the created conversation
 | 
			
		||||
pub fn create(conv: &NewConversation) -> ResultBoxError<u64> {
 | 
			
		||||
@@ -197,7 +197,7 @@ pub fn find_private(user_1: UserID, user_2: UserID) -> ResultBoxError<Vec<u64>>
 | 
			
		||||
        .cond_user_id("t2.user_id", user_2)
 | 
			
		||||
        .set_custom_where(format!("(SELECT COUNT(*) FROM {} WHERE conv_id = t1.conv_id) = 2", CONV_USERS_TABLE).as_ref())
 | 
			
		||||
        .add_field("t1.conv_id AS conv_id")
 | 
			
		||||
        .exec(|f|f.get_u64("conv_id"))
 | 
			
		||||
        .exec(|f| f.get_u64("conv_id"))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get the last messages posted in a conversation
 | 
			
		||||
@@ -208,11 +208,21 @@ pub fn get_last_messages(conv_id: u64, number_of_messages: u64) -> ResultBoxErro
 | 
			
		||||
        .set_order("id DESC")
 | 
			
		||||
        .exec(db_to_conversation_message)
 | 
			
		||||
        .map(|mut l| {
 | 
			
		||||
            l.sort_by(|a,b| a.id.partial_cmp(&b.id).unwrap());
 | 
			
		||||
            l.sort_by(|a, b| a.id.partial_cmp(&b.id).unwrap());
 | 
			
		||||
            l
 | 
			
		||||
        })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// 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)
 | 
			
		||||
        .cond_u64("conv_id", conv_id)
 | 
			
		||||
        .cond_user_id("user_id", user_id)
 | 
			
		||||
        .cond_legacy_bool("saw_last_message", false)
 | 
			
		||||
        .set_legacy_bool("saw_last_message", true)
 | 
			
		||||
        .exec()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Turn a database entry into a ConversationInfo object
 | 
			
		||||
fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError<Conversation> {
 | 
			
		||||
    let conv_id = row.get_u64("id")?;
 | 
			
		||||
@@ -238,6 +248,6 @@ fn db_to_conversation_message(row: &database::RowResult) -> ResultBoxError<Conve
 | 
			
		||||
        conv_id: row.get_u64("conv_id")?,
 | 
			
		||||
        user_id: row.get_user_id("user_id")?,
 | 
			
		||||
        message: row.get_optional_str("message")?,
 | 
			
		||||
        image_path: row.get_optional_str("image_path")?
 | 
			
		||||
        image_path: row.get_optional_str("image_path")?,
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
@@ -1,15 +1,15 @@
 | 
			
		||||
use std::collections;
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
use std::error::Error;
 | 
			
		||||
use std::ops::Add;
 | 
			
		||||
use std::sync::{Arc, Mutex};
 | 
			
		||||
 | 
			
		||||
use chrono::{TimeZone, Utc};
 | 
			
		||||
use mysql::{Binary, Pool, ResultSet, Value};
 | 
			
		||||
use mysql::prelude::Queryable;
 | 
			
		||||
 | 
			
		||||
use crate::data::config::DatabaseConfig;
 | 
			
		||||
use crate::data::error::{ExecError, ResultBoxError};
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
use chrono::{Utc, TimeZone};
 | 
			
		||||
use crate::data::user::UserID;
 | 
			
		||||
 | 
			
		||||
/// Database access helper
 | 
			
		||||
@@ -595,6 +595,17 @@ impl UpdateInfo {
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Filter with a legacy boolean
 | 
			
		||||
    pub fn cond_legacy_bool(mut self, name: &str, val: bool) -> UpdateInfo {
 | 
			
		||||
        let num = match val {
 | 
			
		||||
            true => 1,
 | 
			
		||||
            false => 0
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        self.cond.insert(name.to_string(), Value::Int(num));
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Set an new optional string
 | 
			
		||||
    ///
 | 
			
		||||
    /// None => Empty string
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user