mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 01:24:04 +00:00 
			
		
		
		
	Start to implement the mark_seen method
This commit is contained in:
		@@ -75,7 +75,7 @@ pub struct QueryInfo {
 | 
			
		||||
    joins: Vec<QueryJoin>,
 | 
			
		||||
 | 
			
		||||
    /// Query limits
 | 
			
		||||
    pub conditions: collections::HashMap<String, String>,
 | 
			
		||||
    pub conditions: collections::HashMap<String, mysql::Value>,
 | 
			
		||||
 | 
			
		||||
    /// Custom WHERE condition
 | 
			
		||||
    pub custom_where: Option<String>,
 | 
			
		||||
@@ -137,43 +137,50 @@ impl QueryInfo {
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn add_conditions(mut self, map: &HashMap<String, mysql::Value>) -> QueryInfo {
 | 
			
		||||
        for (k,v) in map {
 | 
			
		||||
            self.conditions.insert(k.to_string(), v.clone());
 | 
			
		||||
        }
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn cond(mut self, key: &str, val: &str) -> QueryInfo {
 | 
			
		||||
        self.conditions.insert(key.to_string(), val.to_string());
 | 
			
		||||
        self.conditions.insert(key.to_string(), mysql::Value::from(val));
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn cond_u32(mut self, key: &str, val: u32) -> QueryInfo {
 | 
			
		||||
        self.conditions.insert(key.to_string(), val.to_string());
 | 
			
		||||
        self.conditions.insert(key.to_string(), mysql::Value::from(val));
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn cond_u64(mut self, key: &str, val: u64) -> QueryInfo {
 | 
			
		||||
        self.conditions.insert(key.to_string(), val.to_string());
 | 
			
		||||
        self.conditions.insert(key.to_string(), mysql::Value::from(val));
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn cond_i64(mut self, key: &str, val: i64) -> QueryInfo {
 | 
			
		||||
        self.conditions.insert(key.to_string(), val.to_string());
 | 
			
		||||
        self.conditions.insert(key.to_string(), mysql::Value::from(val));
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn cond_user_id(mut self, key: &str, val: &UserID) -> QueryInfo {
 | 
			
		||||
        self.conditions.insert(key.to_string(), val.id().to_string());
 | 
			
		||||
        self.conditions.insert(key.to_string(), mysql::Value::from(val.id()));
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn cond_group_id(mut self, key: &str, val: &GroupID) -> QueryInfo {
 | 
			
		||||
        self.conditions.insert(key.to_string(), val.id().to_string());
 | 
			
		||||
        self.conditions.insert(key.to_string(), mysql::Value::from(val.id()));
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn cond_legacy_bool(mut self, key: &str, val: bool) -> QueryInfo {
 | 
			
		||||
        let val = match val {
 | 
			
		||||
            true => "1".to_string(),
 | 
			
		||||
            false => "0".to_string()
 | 
			
		||||
            true => 1,
 | 
			
		||||
            false => 0
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        self.conditions.insert(key.to_string(), val);
 | 
			
		||||
        self.conditions.insert(key.to_string(), mysql::Value::from(val));
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -451,7 +458,7 @@ pub fn query<E, F: Fn(&RowResult) -> ProcessRowResult<E>>(info: QueryInfo, proce
 | 
			
		||||
 | 
			
		||||
        for (k, v) in &info.conditions {
 | 
			
		||||
            where_args.push(format!("{} = ?", k));
 | 
			
		||||
            params.push(mysql::Value::from(v));
 | 
			
		||||
            params.push(v.clone());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        let where_args = format!(" WHERE {} ", where_args.join(" AND "));
 | 
			
		||||
 
 | 
			
		||||
@@ -2,12 +2,22 @@
 | 
			
		||||
//!
 | 
			
		||||
//! @author Pierre Hubert
 | 
			
		||||
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
 | 
			
		||||
use crate::constants::database_tables_names::NOTIFICATIONS_TABLE;
 | 
			
		||||
use crate::data::error::ResultBoxError;
 | 
			
		||||
use crate::data::notification::{NotifElemType, NotifEventType, NotifEventVisibility, Notification};
 | 
			
		||||
use crate::data::notification::{NotifElemType, NotifEventType, NotifEventVisibility, Notification, PartialNotification};
 | 
			
		||||
use crate::data::user::UserID;
 | 
			
		||||
use crate::helpers::database;
 | 
			
		||||
 | 
			
		||||
/// check out whether a similar notification exists for given specifications
 | 
			
		||||
pub fn similar_exists(n: &PartialNotification) -> ResultBoxError<bool> {
 | 
			
		||||
    database::QueryInfo::new(NOTIFICATIONS_TABLE)
 | 
			
		||||
        .add_conditions(¬if_to_db(n, false))
 | 
			
		||||
        .exec_count()
 | 
			
		||||
        .map(|f| f > 0)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Count the number of unread notifications
 | 
			
		||||
pub fn count_unread(user_id: &UserID) -> ResultBoxError<u64> {
 | 
			
		||||
    database::QueryInfo::new(NOTIFICATIONS_TABLE)
 | 
			
		||||
@@ -42,4 +52,62 @@ fn db_to_notif(row: &database::RowResult) -> ResultBoxError<Notification> {
 | 
			
		||||
        container_type: row.get_optional_str("from_container_type")?
 | 
			
		||||
            .map(|s| NotifElemType::from_db(&s)),
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Turn a notification into a database entry
 | 
			
		||||
fn notif_to_db(n: &PartialNotification, complete_information: bool) -> HashMap<String, mysql::Value> {
 | 
			
		||||
    let mut map = HashMap::new();
 | 
			
		||||
 | 
			
		||||
    if let Some(id) = n.id {
 | 
			
		||||
        map.insert("id".to_string(), mysql::Value::UInt(id));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if let Some(seen) = n.seen {
 | 
			
		||||
        map.insert("seen".to_string(), mysql::Value::Int(match seen {
 | 
			
		||||
            true => 1,
 | 
			
		||||
            false => 0,
 | 
			
		||||
        }));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if let Some(from_user_id) = &n.from_user_id {
 | 
			
		||||
        map.insert("from_user_id".to_string(), mysql::Value::UInt(from_user_id.id()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if let Some(dest_user_id) = &n.dest_user_id {
 | 
			
		||||
        map.insert("dest_user_id".to_string(), mysql::Value::UInt(dest_user_id.id()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if let Some(kind) = &n.kind {
 | 
			
		||||
        map.insert("type".to_string(), mysql::Value::from(kind.to_db()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if let Some(on_elem_id) = n.on_elem_id {
 | 
			
		||||
        map.insert("on_elem_id".to_string(), mysql::Value::from(on_elem_id));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if let Some(on_elem_type) = &n.on_elem_type {
 | 
			
		||||
        map.insert("on_elem_type".to_string(), mysql::Value::from(on_elem_type.to_db()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    if complete_information {
 | 
			
		||||
        if let Some(from_container_id) = n.container_id {
 | 
			
		||||
            map.insert("from_container_id".to_string(), mysql::Value::from(from_container_id));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if let Some(from_container_type) = &n.container_type {
 | 
			
		||||
            map.insert("from_container_type".to_string(), mysql::Value::from(from_container_type.to_db()));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if let Some(time_create) = n.time_create {
 | 
			
		||||
            map.insert("time_create".to_string(), mysql::Value::from(time_create));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if let Some(visibility) = &n.visibility {
 | 
			
		||||
            map.insert("visibility".to_string(), mysql::Value::from(visibility.to_db()));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    map
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user