2023-04-13 10:53:55 +00:00
|
|
|
use crate::websocket::{
|
|
|
|
handlers::{SessionInfo, WsMessage},
|
|
|
|
structs::CaptchaItem,
|
2021-03-25 19:30:15 +00:00
|
|
|
};
|
2023-04-13 10:53:55 +00:00
|
|
|
use actix::{Actor, Context};
|
2022-12-09 15:31:47 +00:00
|
|
|
use lemmy_db_schema::newtypes::{CommunityId, LocalUserId, PostId};
|
2023-04-13 10:53:55 +00:00
|
|
|
use lemmy_utils::ConnectionId;
|
2022-12-09 15:31:47 +00:00
|
|
|
use rand::{rngs::StdRng, SeedableRng};
|
2023-04-13 10:53:55 +00:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2022-11-28 14:29:33 +00:00
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
pub struct ChatServer {
|
2020-02-01 01:02:20 +00:00
|
|
|
/// A map from generated random ID to session addr
|
2023-04-13 10:53:55 +00:00
|
|
|
pub sessions: HashMap<ConnectionId, SessionInfo>,
|
2020-02-01 01:02:20 +00:00
|
|
|
|
|
|
|
/// A map from post_id to set of connectionIDs
|
2020-04-19 22:08:25 +00:00
|
|
|
pub post_rooms: HashMap<PostId, HashSet<ConnectionId>>,
|
2020-02-01 01:02:20 +00:00
|
|
|
|
|
|
|
/// A map from community to set of connectionIDs
|
2020-04-19 22:08:25 +00:00
|
|
|
pub community_rooms: HashMap<CommunityId, HashSet<ConnectionId>>,
|
2020-02-01 01:02:20 +00:00
|
|
|
|
2020-10-27 00:25:18 +00:00
|
|
|
pub mod_rooms: HashMap<CommunityId, HashSet<ConnectionId>>,
|
|
|
|
|
2020-02-01 01:02:20 +00:00
|
|
|
/// A map from user id to its connection ID for joined users. Remember a user can have multiple
|
|
|
|
/// sessions (IE clients)
|
2021-03-12 15:54:47 +00:00
|
|
|
pub(super) user_rooms: HashMap<LocalUserId, HashSet<ConnectionId>>,
|
2020-02-01 01:02:20 +00:00
|
|
|
|
2022-12-09 15:31:47 +00:00
|
|
|
pub(super) rng: StdRng,
|
2021-09-22 15:57:09 +00:00
|
|
|
|
2020-07-29 13:02:46 +00:00
|
|
|
/// A list of the current captchas
|
2020-08-31 15:20:13 +00:00
|
|
|
pub(super) captchas: Vec<CaptchaItem>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `ChatServer` is an actor. It maintains list of connection client session.
|
|
|
|
/// And manages available rooms. Peers send messages to other peers in same
|
|
|
|
/// room through `ChatServer`.
|
2020-01-12 15:31:51 +00:00
|
|
|
impl ChatServer {
|
2023-04-13 10:53:55 +00:00
|
|
|
pub fn new() -> ChatServer {
|
2019-03-21 01:22:31 +00:00
|
|
|
ChatServer {
|
2023-04-13 10:53:55 +00:00
|
|
|
sessions: Default::default(),
|
|
|
|
post_rooms: Default::default(),
|
|
|
|
community_rooms: Default::default(),
|
|
|
|
mod_rooms: Default::default(),
|
|
|
|
user_rooms: Default::default(),
|
|
|
|
rng: StdRng::from_entropy(),
|
|
|
|
captchas: vec![],
|
2020-02-02 17:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:53:55 +00:00
|
|
|
pub fn send_message(
|
2022-12-09 15:31:47 +00:00
|
|
|
&self,
|
2023-04-13 10:53:55 +00:00
|
|
|
connections: &HashSet<ConnectionId>,
|
2022-12-09 15:31:47 +00:00
|
|
|
message: &str,
|
|
|
|
exclude_connection: Option<ConnectionId>,
|
2023-04-13 10:53:55 +00:00
|
|
|
) {
|
|
|
|
for id in connections
|
|
|
|
.iter()
|
|
|
|
.filter(|c| Some(*c) != exclude_connection.as_ref())
|
|
|
|
{
|
|
|
|
if let Some(session) = self.sessions.get(id) {
|
|
|
|
session.addr.do_send(WsMessage(message.to_owned()));
|
|
|
|
}
|
2020-04-19 22:08:25 +00:00
|
|
|
}
|
2019-09-03 20:18:07 +00:00
|
|
|
}
|
2023-04-13 10:53:55 +00:00
|
|
|
}
|
2019-09-03 20:18:07 +00:00
|
|
|
|
2023-04-13 10:53:55 +00:00
|
|
|
impl Default for ChatServer {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
2019-05-01 22:44:39 +00:00
|
|
|
}
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
2023-04-13 10:53:55 +00:00
|
|
|
|
|
|
|
/// Make actor from `ChatServer`
|
|
|
|
impl Actor for ChatServer {
|
|
|
|
/// We are going to use simple Context, we just need ability to communicate
|
|
|
|
/// with other actors.
|
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|