1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-09-25 22:29:45 +00:00

Turned a lot of function to async mode

This commit is contained in:
2022-03-12 07:47:22 +01:00
parent cfaaef68b7
commit 09ce13c554
24 changed files with 368 additions and 357 deletions

View File

@@ -21,7 +21,6 @@ use crate::data::error::Res;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::data::new_conversation::NewConversation;
use crate::data::new_conversation_message::NewConversationMessage;
use crate::data::user_ws_connection::UserWsConnection;
use crate::data::user_ws_message::UserWsMessage;
use crate::data::user_ws_request_handler::UserWsRequestHandler;
use crate::helpers::{conversations_helper, events_helper};
@@ -50,7 +49,7 @@ pub async fn create(r: &mut HttpRequestHandler) -> RequestResult {
};
// Create the conversation
let conv_id = conversations_helper::create(&conv)?;
let conv_id = conversations_helper::create(&conv).await?;
r.set_response(ResCreateConversation::new(conv_id))
}
@@ -147,7 +146,7 @@ pub async fn add_member(r: &mut HttpRequestHandler) -> RequestResult {
r.bad_request("This user is already a member of this conversation!".to_string())?;
}
conversations_helper::add_member(conv.id, &user_to_add, true, false, Some(r.user_id_ref()?))?;
conversations_helper::add_member(conv.id, &user_to_add, true, false, Some(r.user_id_ref()?)).await?;
r.success("The user was added to the conversation!")
}
@@ -195,7 +194,7 @@ pub async fn remove_member(r: &mut HttpRequestHandler) -> RequestResult {
r.bad_request("This user is not a member of this conversation!".to_string())?;
}
conversations_helper::remove_member(&user_to_remove, conv.id, Some(r.user_id_ref()?))?;
conversations_helper::remove_member(&user_to_remove, conv.id, Some(r.user_id_ref()?)).await?;
r.ok()
}
@@ -228,7 +227,7 @@ pub async fn find_private(r: &mut HttpRequestHandler) -> RequestResult {
group_id: None,
group_min_membership_level: None,
};
let conv_id = conversations_helper::create(&new_conv)?;
let conv_id = conversations_helper::create(&new_conv).await?;
list.push(conv_id);
}
@@ -255,7 +254,7 @@ pub async fn refresh_single(r: &mut HttpRequestHandler) -> RequestResult {
conv.conv_id,
r.user_id_ref()?,
&messages.last().unwrap(),
)?;
).await?;
}
r.set_response(ConversationMessageAPI::for_list(&messages))
@@ -395,7 +394,7 @@ pub async fn send_message(r: &mut HttpRequestHandler) -> RequestResult {
message,
file,
server_message: None,
})?;
}).await?;
r.success("Conversation message was successfully sent!")
}
@@ -423,7 +422,7 @@ pub async fn delete_conversation(r: &mut HttpRequestHandler) -> RequestResult {
r.bad_request("This conversation is managed, it can not be deleted by this way!".to_string())?;
}
conversations_helper::remove_user_from_conversation(&r.user_id()?, &conv, r.user_id_ref()?)?;
conversations_helper::remove_user_from_conversation(&r.user_id()?, &conv, r.user_id_ref()?).await?;
r.success("The conversation has been deleted")
}
@@ -447,7 +446,7 @@ pub async fn update_message(r: &mut HttpRequestHandler) -> RequestResult {
r.bad_request("New message is too long!".to_string())?;
}
conversations_helper::update_message_content(msg_id, &new_content)?;
conversations_helper::update_message_content(msg_id, &new_content).await?;
r.success("Conversation message content successfully updated")
}
@@ -460,25 +459,25 @@ pub async fn delete_message(r: &mut HttpRequestHandler) -> RequestResult {
r.forbidden("You are not the owner of this message!".to_string())?;
}
conversations_helper::delete_message_by_id(msg_id)?;
conversations_helper::delete_message_by_id(msg_id).await?;
r.success("The message has been successfully deleted!")
}
/// A user is writing a message in a conversation
pub fn member_is_writing(r: &mut UserWsRequestHandler) -> RequestResult {
pub async fn member_is_writing(r: &mut UserWsRequestHandler) -> RequestResult {
let conv_id = r.post_registered_conv_id("convID")?;
// Propagate event
events_helper::propagate_event(
&Event::UserIsWritingMessageInConversation(r.user_id_ref()?, conv_id)
)?;
Event::UserIsWritingMessageInConversation(r.user_id()?, conv_id)
).await?;
r.ok()
}
/// Events handler
pub fn handle_event(e: &events_helper::Event) -> Res {
pub async fn handle_event(e: &events_helper::Event) -> Res {
match e {
Event::UpdatedNumberUnreadConversations(users) => {
for user in users.iter() {
@@ -498,23 +497,22 @@ pub fn handle_event(e: &events_helper::Event) -> Res {
user_ws_controller::send_message_to_specific_connections(
|s| s.conversations.contains(conv_id) && s.user_id() != user_id,
|_| UserWsMessage::no_id_message("writing_message_in_conv", UserIsWritingMessageInConversation::new(user_id, *conv_id)),
None::<fn(&_) -> _>,
)?;
}
Event::NewConversationMessage(msg) => {
user_ws_controller::send_message_to_specific_connections(
for conn in user_ws_controller::send_message_to_specific_connections(
|f| f.conversations.contains(&msg.conv_id),
|_| UserWsMessage::no_id_message("new_conv_message", ConversationMessageAPI::new(msg)),
Some(|conn: &UserWsConnection| conversations_helper::mark_user_seen(msg.conv_id, conn.user_id(), msg)),
)?;
)? {
conversations_helper::mark_user_seen(msg.conv_id, conn.user_id(), msg).await?;
}
}
Event::UpdatedConversationMessage(msg) => {
user_ws_controller::send_message_to_specific_connections(
|f| f.conversations.contains(&msg.conv_id),
|_| UserWsMessage::no_id_message("updated_conv_message", ConversationMessageAPI::new(msg)),
None::<fn(&_) -> _>,
)?;
}
@@ -522,7 +520,6 @@ pub fn handle_event(e: &events_helper::Event) -> Res {
user_ws_controller::send_message_to_specific_connections(
|f| f.conversations.contains(&msg.conv_id),
|_| UserWsMessage::no_id_message("deleted_conv_message", ConversationMessageAPI::new(msg)),
None::<fn(&_) -> _>,
)?;
}
@@ -531,7 +528,6 @@ pub fn handle_event(e: &events_helper::Event) -> Res {
user_ws_controller::send_message_to_specific_connections(
|f| f.conversations.contains(conv_id),
|_| UserWsMessage::no_id_message("removed_user_from_conv", RemovedUserFromConversationMessage::new(user_id, *conv_id)),
None::<fn(&_) -> _>,
)?;
// Disconnect user from conversation
@@ -551,7 +547,6 @@ pub fn handle_event(e: &events_helper::Event) -> Res {
user_ws_controller::send_message_to_specific_connections(
|f| f.conversations.contains(conv_id),
|_| UserWsMessage::no_id_message("deleted_conversation", conv_id.id()),
None::<fn(&_) -> _>,
)?;
// Disconnect user from conversation