From fd5000aef9dbc2d72a6d3d7f4bf680fc748f5427 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 23 Jan 2021 15:04:52 +0100 Subject: [PATCH] Can create friendship notifications --- src/controllers/friends_controller.rs | 9 +++++++-- src/helpers/notifications_helper.rs | 23 +++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/controllers/friends_controller.rs b/src/controllers/friends_controller.rs index 06214be..777adbf 100644 --- a/src/controllers/friends_controller.rs +++ b/src/controllers/friends_controller.rs @@ -6,7 +6,8 @@ use crate::api_data::friend_api::FriendAPI; use crate::api_data::friendship_status_api::FriendshipStatusAPI; use crate::controllers::routes::RequestResult; use crate::data::http_request_handler::HttpRequestHandler; -use crate::helpers::{account_helper, friends_helper, user_helper}; +use crate::data::notification::NotifEventType; +use crate::helpers::{account_helper, friends_helper, notifications_helper, user_helper}; /// Get the list of friends of the current user pub fn get_list(r: &mut HttpRequestHandler) -> RequestResult { @@ -82,7 +83,11 @@ pub fn send_request(r: &mut HttpRequestHandler) -> RequestResult { friends_helper::send_request(&r.user_id()?, &friend_id)?; - // TODO : create a notification + // Create a notification + notifications_helper::create_friends_notification( + r.user_id_ref()?, + &friend_id, + NotifEventType::SENT_FRIEND_REQUEST)?; r.success("The friendship request was successfully sent!") } diff --git a/src/helpers/notifications_helper.rs b/src/helpers/notifications_helper.rs index 5cb8bf7..7d2b6c1 100644 --- a/src/helpers/notifications_helper.rs +++ b/src/helpers/notifications_helper.rs @@ -17,7 +17,6 @@ use crate::utils::date_utils; /// Create post notification pub fn create_post_notification(from_user: &UserID, post_id: u64, action: NotifEventType) -> ResultBoxError { let mut n = PartialNotification::new() - .set_time_create(date_utils::time()) .set_from_user_id(from_user) .set_on_elem_id(post_id) .set_on_elem_type(NotifElemType::POST) @@ -26,8 +25,20 @@ pub fn create_post_notification(from_user: &UserID, post_id: u64, action: NotifE push(&mut n) } +/// Create & push friend notification +pub fn create_friends_notification(from_user: &UserID, dest_user: &UserID, action: NotifEventType) -> ResultBoxError { + let mut n = PartialNotification::new() + .set_from_user_id(from_user) + .set_dest_user_id(dest_user) + .set_on_elem_id(from_user.id()) + .set_on_elem_type(NotifElemType::FRIENDSHIP_REQUEST) + .set_type(action); + + push(&mut n) +} + /// Push a new notification -pub fn push(n: &mut PartialNotification) -> ResultBoxError +fn push(n: &mut PartialNotification) -> ResultBoxError { if n.time_create.is_none() { @@ -100,6 +111,14 @@ pub fn push(n: &mut PartialNotification) -> ResultBoxError else { unimplemented!(); } + } + + // Friendship notification + else if matches!(n.on_elem_type, Some(NotifElemType::FRIENDSHIP_REQUEST)) { + n.container_id = None; + n.container_type = None; + + return push_private(n); } else { unimplemented!(); }