1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 07:19:22 +00:00

Propagate comments update events

This commit is contained in:
Pierre HUBERT 2021-02-06 11:37:56 +01:00
parent e2acb3813e
commit 3e4d2872ea
3 changed files with 68 additions and 7 deletions

View File

@ -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::<fn(&_) -> _>,
)?;
}
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::<fn(&_) -> _>,
)?;
}
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::<fn(&_) -> _>,
)?;
}
_ => {}
}
Ok(())
}

View File

@ -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<u64> {
@ -23,7 +24,8 @@ pub fn create(c: &Comment) -> ResultBoxError<u64> {
.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(())
}

View File

@ -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(())
}