mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 01:24:04 +00:00 
			
		
		
		
	Notify about notifications number update
This commit is contained in:
		@@ -6,11 +6,15 @@ use crate::api_data::notification_api::NotificationAPI;
 | 
				
			|||||||
use crate::api_data::res_count_all_unreads::ResCountAllUnread;
 | 
					use crate::api_data::res_count_all_unreads::ResCountAllUnread;
 | 
				
			||||||
use crate::api_data::res_number_unread_notifications::ResNumberUnreadNotifications;
 | 
					use crate::api_data::res_number_unread_notifications::ResNumberUnreadNotifications;
 | 
				
			||||||
use crate::controllers::routes::RequestResult;
 | 
					use crate::controllers::routes::RequestResult;
 | 
				
			||||||
 | 
					use crate::controllers::user_ws_controller;
 | 
				
			||||||
use crate::data::base_request_handler::BaseRequestHandler;
 | 
					use crate::data::base_request_handler::BaseRequestHandler;
 | 
				
			||||||
use crate::data::error::ResultBoxError;
 | 
					use crate::data::error::{Res, ResultBoxError};
 | 
				
			||||||
use crate::data::http_request_handler::HttpRequestHandler;
 | 
					use crate::data::http_request_handler::HttpRequestHandler;
 | 
				
			||||||
use crate::data::notification::Notification;
 | 
					use crate::data::notification::Notification;
 | 
				
			||||||
use crate::helpers::{conversations_helper, friends_helper, notifications_helper};
 | 
					use crate::data::user::UserID;
 | 
				
			||||||
 | 
					use crate::data::user_ws_message::UserWsMessage;
 | 
				
			||||||
 | 
					use crate::helpers::{conversations_helper, events_helper, friends_helper, notifications_helper};
 | 
				
			||||||
 | 
					use crate::helpers::events_helper::Event;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl HttpRequestHandler {
 | 
					impl HttpRequestHandler {
 | 
				
			||||||
    /// Get the id of a notification included in the request
 | 
					    /// Get the id of a notification included in the request
 | 
				
			||||||
@@ -79,4 +83,30 @@ pub fn delete_all(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			|||||||
    notifications_helper::delete_all_user(r.user_id_ref()?)?;
 | 
					    notifications_helper::delete_all_user(r.user_id_ref()?)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    r.success("Notifications deleted.")
 | 
					    r.success("Notifications deleted.")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Send new notifications number to a specific user
 | 
				
			||||||
 | 
					pub fn send_new_notifications_number(user_id: &UserID) -> Res {
 | 
				
			||||||
 | 
					    if !user_ws_controller::is_user_connected(user_id) {
 | 
				
			||||||
 | 
					        return Ok(());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    user_ws_controller::send_message_to_user(
 | 
				
			||||||
 | 
					        &UserWsMessage::no_id_message("number_notifs", notifications_helper::count_unread(user_id)?)?,
 | 
				
			||||||
 | 
					        user_id,
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Events handler
 | 
				
			||||||
 | 
					pub fn handle_event(e: &events_helper::Event) -> Res {
 | 
				
			||||||
 | 
					    match e {
 | 
				
			||||||
 | 
					        Event::UpdatedNotificationsNumber(users) => {
 | 
				
			||||||
 | 
					            for user in users.iter() {
 | 
				
			||||||
 | 
					                send_new_notifications_number(user)?;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        _ => {}
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -486,6 +486,23 @@ pub fn send_message_to_users(msg: &UserWsMessage, users: &Vec<UserID>) -> Res {
 | 
				
			|||||||
    Ok(())
 | 
					    Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Send a message to a specific user
 | 
				
			||||||
 | 
					pub fn send_message_to_user(msg: &UserWsMessage, user: &UserID) -> Res {
 | 
				
			||||||
 | 
					    let connections = get_ws_connections_list()
 | 
				
			||||||
 | 
					        .lock()
 | 
				
			||||||
 | 
					        .unwrap()
 | 
				
			||||||
 | 
					        .iter()
 | 
				
			||||||
 | 
					        .filter(|f| user == &f.user_id)
 | 
				
			||||||
 | 
					        .map(|f| f.session.clone())
 | 
				
			||||||
 | 
					        .collect::<Vec<Addr<WsSession>>>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for con in connections {
 | 
				
			||||||
 | 
					        send_message(con, msg)?;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Ok(())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Send a message to specific users
 | 
					/// Send a message to specific users
 | 
				
			||||||
pub fn send_message_to_specific_connections<F, M, A>(filter: F, msg_generator: M, after_send: Option<A>) -> Res
 | 
					pub fn send_message_to_specific_connections<F, M, A>(filter: F, msg_generator: M, after_send: Option<A>) -> Res
 | 
				
			||||||
    where F: Fn(&WsConnection) -> bool,
 | 
					    where F: Fn(&WsConnection) -> bool,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,12 +4,15 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::data::error::Res;
 | 
					use crate::controllers::{comments_controller, conversations_controller, notifications_controller};
 | 
				
			||||||
use crate::data::conversation_message::ConversationMessage;
 | 
					 | 
				
			||||||
use crate::controllers::{conversations_controller, comments_controller};
 | 
					 | 
				
			||||||
use crate::data::comment::Comment;
 | 
					use crate::data::comment::Comment;
 | 
				
			||||||
 | 
					use crate::data::conversation_message::ConversationMessage;
 | 
				
			||||||
 | 
					use crate::data::error::Res;
 | 
				
			||||||
 | 
					use crate::data::user::UserID;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub enum Event<'a> {
 | 
					pub enum Event<'a> {
 | 
				
			||||||
 | 
					    /// Updated the number of notifications of one of multiple users user
 | 
				
			||||||
 | 
					    UpdatedNotificationsNumber(&'a Vec<UserID>),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Created a new conversation message
 | 
					    /// Created a new conversation message
 | 
				
			||||||
    NewConversationMessage(&'a ConversationMessage),
 | 
					    NewConversationMessage(&'a ConversationMessage),
 | 
				
			||||||
@@ -37,5 +40,6 @@ pub enum Event<'a> {
 | 
				
			|||||||
pub fn propagate_event(e: &Event) -> Res {
 | 
					pub fn propagate_event(e: &Event) -> Res {
 | 
				
			||||||
    conversations_controller::handle_event(e)?;
 | 
					    conversations_controller::handle_event(e)?;
 | 
				
			||||||
    comments_controller::handle_event(e)?;
 | 
					    comments_controller::handle_event(e)?;
 | 
				
			||||||
 | 
					    notifications_controller::handle_event(e)?;
 | 
				
			||||||
    Ok(())
 | 
					    Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -5,12 +5,13 @@
 | 
				
			|||||||
use std::collections::HashMap;
 | 
					use std::collections::HashMap;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::constants::database_tables_names::NOTIFICATIONS_TABLE;
 | 
					use crate::constants::database_tables_names::NOTIFICATIONS_TABLE;
 | 
				
			||||||
use crate::data::error::ResultBoxError;
 | 
					use crate::data::error::{ExecError, ResultBoxError};
 | 
				
			||||||
use crate::data::group_id::GroupID;
 | 
					use crate::data::group_id::GroupID;
 | 
				
			||||||
use crate::data::notification::{NotifElemType, NotifEventType, NotifEventVisibility, Notification, PartialNotification};
 | 
					use crate::data::notification::{NotifElemType, NotifEventType, NotifEventVisibility, Notification, PartialNotification};
 | 
				
			||||||
use crate::data::post::{PostID, PostPageKind, PostVisibilityLevel};
 | 
					use crate::data::post::{PostID, PostPageKind, PostVisibilityLevel};
 | 
				
			||||||
use crate::data::user::UserID;
 | 
					use crate::data::user::UserID;
 | 
				
			||||||
use crate::helpers::{database, groups_helper, posts_helper};
 | 
					use crate::helpers::{database, events_helper, groups_helper, posts_helper};
 | 
				
			||||||
 | 
					use crate::helpers::events_helper::Event;
 | 
				
			||||||
use crate::helpers::friends_helper::GetFriendsQuery;
 | 
					use crate::helpers::friends_helper::GetFriendsQuery;
 | 
				
			||||||
use crate::utils::date_utils;
 | 
					use crate::utils::date_utils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -218,11 +219,17 @@ fn push_private(n: &mut PartialNotification) -> ResultBoxError {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/// Create a new notification
 | 
					/// Create a new notification
 | 
				
			||||||
fn create(n: &PartialNotification) -> ResultBoxError {
 | 
					fn create(n: &PartialNotification) -> ResultBoxError {
 | 
				
			||||||
 | 
					    if n.dest_user_id.is_none() || n.from_user_id.is_none()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return Err(ExecError::boxed_new("Trying to send a notification without a source or a destination!"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    database::InsertQuery::new(NOTIFICATIONS_TABLE)
 | 
					    database::InsertQuery::new(NOTIFICATIONS_TABLE)
 | 
				
			||||||
        .add_values(notif_to_db(n, true))
 | 
					        .add_values(notif_to_db(n, true))
 | 
				
			||||||
        .insert_drop_result()?;
 | 
					        .insert_drop_result()?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO : Send a notification (updated_number_conversations)
 | 
					    // Send a notification (updated_number_conversations)
 | 
				
			||||||
 | 
					    events_helper::propagate_event(&Event::UpdatedNotificationsNumber(&vec![n.dest_user_id.clone().unwrap()]))?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Ok(())
 | 
					    Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -232,12 +239,19 @@ fn create(n: &PartialNotification) -> ResultBoxError {
 | 
				
			|||||||
pub fn delete(notification: &PartialNotification) -> ResultBoxError {
 | 
					pub fn delete(notification: &PartialNotification) -> ResultBoxError {
 | 
				
			||||||
    let conditions = notif_to_db(notification, false);
 | 
					    let conditions = notif_to_db(notification, false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Get the list of affected users
 | 
				
			||||||
 | 
					    let users = database::QueryInfo::new(NOTIFICATIONS_TABLE)
 | 
				
			||||||
 | 
					        .add_conditions(&conditions)
 | 
				
			||||||
 | 
					        .add_field("dest_user_id")
 | 
				
			||||||
 | 
					        .exec(|r| r.get_user_id("dest_user_id"))?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Delete the notifications
 | 
					    // Delete the notifications
 | 
				
			||||||
    database::DeleteQuery::new(NOTIFICATIONS_TABLE)
 | 
					    database::DeleteQuery::new(NOTIFICATIONS_TABLE)
 | 
				
			||||||
        .add_conditions(conditions)
 | 
					        .add_conditions(conditions)
 | 
				
			||||||
        .exec()?;
 | 
					        .exec()?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO : Send a notification (updated_number_conversations)
 | 
					    // Send a notification (updated_number_conversations)
 | 
				
			||||||
 | 
					    events_helper::propagate_event(&Event::UpdatedNotificationsNumber(&users))?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Ok(())
 | 
					    Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user