mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-09-25 22:29:45 +00:00
Send message through WebSocket fro new conversation messages
This commit is contained in:
@@ -483,6 +483,31 @@ pub fn send_message_to_users(msg: &UserWsMessage, users: &Vec<UserID>) -> Res {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 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
|
||||
where F: Fn(&WsConnection) -> bool,
|
||||
M: Fn(&WsConnection) -> Res<UserWsMessage>,
|
||||
A: Fn(&WsConnection) -> Res
|
||||
{
|
||||
let connections = get_ws_connections_list()
|
||||
.lock()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter(|f| filter(f))
|
||||
.map(|f| f.clone())
|
||||
.collect::<Vec<WsConnection>>();
|
||||
|
||||
for con in connections {
|
||||
send_message(con.session.clone(), &msg_generator(&con)?)?;
|
||||
|
||||
if let Some(cb) = &after_send {
|
||||
cb(&con)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check out whether user is connected or not
|
||||
pub fn is_user_connected(user_id: &UserID) -> bool {
|
||||
get_ws_connections_list().lock().unwrap().iter().any(|c| &c.user_id == user_id)
|
||||
|
Reference in New Issue
Block a user