From 3e4d2872ea11941e80954eafdc520ae28e77dfc2 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 6 Feb 2021 11:37:56 +0100 Subject: [PATCH] Propagate comments update events --- src/controllers/comments_controller.rs | 50 ++++++++++++++++++++++++-- src/helpers/comments_helper.rs | 12 ++++--- src/helpers/events_helper.rs | 13 ++++++- 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/controllers/comments_controller.rs b/src/controllers/comments_controller.rs index 49dcbe8..3766943 100644 --- a/src/controllers/comments_controller.rs +++ b/src/controllers/comments_controller.rs @@ -6,14 +6,18 @@ use crate::api_data::comment_api::CommentAPI; use crate::api_data::res_create_comment::ResCreateComment; use crate::constants::PATH_COMMENTS_IMAGES; use crate::controllers::routes::RequestResult; +use crate::controllers::user_ws_controller; +use crate::data::base_request_handler::BaseRequestHandler; use crate::data::comment::Comment; +use crate::data::error::Res; use crate::data::http_request_handler::HttpRequestHandler; use crate::data::notification::NotifEventType; use crate::data::post::PostAccessLevel; -use crate::helpers::{comments_helper, notifications_helper, posts_helper}; +use crate::data::user_ws_message::UserWsMessage; +use crate::helpers::{comments_helper, events_helper, notifications_helper, posts_helper}; +use crate::helpers::events_helper::Event; use crate::utils::date_utils::time; use crate::utils::string_utils::remove_html_nodes; -use crate::data::base_request_handler::BaseRequestHandler; /// Create a new comment pub fn create(r: &mut HttpRequestHandler) -> RequestResult { @@ -80,4 +84,46 @@ pub fn delete(r: &mut HttpRequestHandler) -> RequestResult { comments_helper::delete(&comment)?; r.success("Comment deleted.") +} + +/// Events handler +pub fn handle_event(e: &events_helper::Event) -> Res { + match e { + Event::NewComment(comment) => { + user_ws_controller::send_message_to_specific_connections( + |c| c.posts.contains(&comment.post_id), + |c| UserWsMessage::no_id_message( + "new_comment", + CommentAPI::new(comment, &Some(c.user_id.clone()))?, + ), + None:: _>, + )?; + } + + Event::UpdatedComment(comment) => { + user_ws_controller::send_message_to_specific_connections( + |c| c.posts.contains(&comment.post_id), + |c| UserWsMessage::no_id_message( + "comment_updated", + CommentAPI::new(comment, &Some(c.user_id.clone()))?, + ), + None:: _>, + )?; + } + + Event::DeletedComment(comment) => { + user_ws_controller::send_message_to_specific_connections( + |c| c.posts.contains(&comment.post_id), + |_| UserWsMessage::no_id_message( + "comment_deleted", + comment.id.clone(), + ), + None:: _>, + )?; + } + + _ => {} + } + + Ok(()) } \ No newline at end of file diff --git a/src/helpers/comments_helper.rs b/src/helpers/comments_helper.rs index 1d71302..f6d3446 100644 --- a/src/helpers/comments_helper.rs +++ b/src/helpers/comments_helper.rs @@ -6,10 +6,11 @@ use crate::constants::database_tables_names::COMMENTS_TABLE; use crate::data::comment::Comment; use crate::data::error::{ExecError, ResultBoxError}; use crate::data::user::UserID; -use crate::helpers::{database, likes_helper}; +use crate::helpers::{database, likes_helper, events_helper}; use crate::helpers::likes_helper::LikeType; use crate::utils::date_utils::mysql_date; use crate::utils::user_data_utils::user_data_path; +use crate::helpers::events_helper::Event; /// Create a new comment. In case of success, this function returns the ID of the created comment pub fn create(c: &Comment) -> ResultBoxError { @@ -23,7 +24,8 @@ pub fn create(c: &Comment) -> ResultBoxError { .insert()? .ok_or(ExecError::new("No ID returned after comment creation!"))?; - // TODO : emit an event + // Emit an event + events_helper::propagate_event(&Event::NewComment(&get_single(comment_id)?))?; Ok(comment_id) } @@ -71,7 +73,8 @@ pub fn edit(comment_id: u64, new_content: &str) -> ResultBoxError { .set_str("commentaire", new_content) .exec()?; - // TODO : emit an event + // Emit an event + events_helper::propagate_event(&Event::UpdatedComment(&get_single(comment_id)?))?; Ok(()) } @@ -94,7 +97,8 @@ pub fn delete(c: &Comment) -> ResultBoxError { .cond_u64("ID", c.id) .exec()?; - // TODO : emit an event + // Emit an event + events_helper::propagate_event(&Event::DeletedComment(c))?; Ok(()) } diff --git a/src/helpers/events_helper.rs b/src/helpers/events_helper.rs index b7e8a98..d5f3a71 100644 --- a/src/helpers/events_helper.rs +++ b/src/helpers/events_helper.rs @@ -6,7 +6,8 @@ use crate::data::error::Res; use crate::data::conversation_message::ConversationMessage; -use crate::controllers::conversations_controller; +use crate::controllers::{conversations_controller, comments_controller}; +use crate::data::comment::Comment; pub enum Event<'a> { @@ -19,6 +20,15 @@ pub enum Event<'a> { /// Deleted a conversation message DeleteConversationMessage(&'a ConversationMessage), + /// Created a new comment + NewComment(&'a Comment), + + /// Updated a comment + UpdatedComment(&'a Comment), + + /// Deleted a comment + DeletedComment(&'a Comment), + /// No event None, } @@ -26,5 +36,6 @@ pub enum Event<'a> { /// Propagate an event through the different components of the application pub fn propagate_event(e: &Event) -> Res { conversations_controller::handle_event(e)?; + comments_controller::handle_event(e)?; Ok(()) } \ No newline at end of file