1 //! `ChatServer` is an actor. It maintains list of connection client session.
2 //! And manages available rooms. Peers send messages to other peers in same
3 //! room through `ChatServer`.
7 use rand::{rngs::ThreadRng, Rng};
8 use serde::{Deserialize, Serialize};
10 use std::collections::{HashMap, HashSet};
11 use std::str::FromStr;
12 use std::time::SystemTime;
14 use crate::api::comment::*;
15 use crate::api::community::*;
16 use crate::api::post::*;
17 use crate::api::site::*;
18 use crate::api::user::*;
22 /// Chat server sends this messages to session
24 pub struct WSMessage(pub String);
26 /// Message for chat server communications
28 /// New chat session is created
32 pub addr: Recipient<WSMessage>,
36 /// Session is disconnected
38 pub struct Disconnect {
43 /// Send message to specific room
45 pub struct ClientMessage {
46 /// Id of the client session
54 #[derive(Serialize, Deserialize)]
55 pub struct StandardMessage {
56 /// Id of the client session
62 impl actix::Message for StandardMessage {
67 pub struct RateLimitBucket {
68 last_checked: SystemTime,
72 pub struct SessionInfo {
73 pub addr: Recipient<WSMessage>,
77 /// `ChatServer` manages chat rooms and responsible for coordinating chat
78 /// session. implementation is super primitive
79 pub struct ChatServer {
80 sessions: HashMap<usize, SessionInfo>, // A map from generated random ID to session addr
81 rate_limits: HashMap<String, RateLimitBucket>,
82 rooms: HashMap<i32, HashSet<usize>>, // A map from room / post name to set of connectionIDs
86 impl Default for ChatServer {
87 fn default() -> ChatServer {
89 let rooms = HashMap::new();
92 sessions: HashMap::new(),
93 rate_limits: HashMap::new(),
95 rng: rand::thread_rng(),
101 /// Send message to all users in the room
102 fn send_room_message(&self, room: &i32, message: &str, skip_id: usize) {
103 if let Some(sessions) = self.rooms.get(room) {
106 if let Some(info) = self.sessions.get(id) {
107 let _ = info.addr.do_send(WSMessage(message.to_owned()));
114 fn join_room(&mut self, room_id: i32, id: usize) {
115 // remove session from all rooms
116 for (_n, sessions) in &mut self.rooms {
117 sessions.remove(&id);
120 // If the room doesn't exist yet
121 if self.rooms.get_mut(&room_id).is_none() {
122 self.rooms.insert(room_id, HashSet::new());
125 &self.rooms.get_mut(&room_id).unwrap().insert(id);
128 fn send_community_message(
133 ) -> Result<(), Error> {
134 use crate::db::post_view::*;
136 let conn = establish_connection();
138 let posts = PostQueryBuilder::create(&conn)
139 .listing_type(ListingType::Community)
140 .sort(&SortType::New)
141 .for_community_id(*community_id)
146 self.send_room_message(&post.id, message, skip_id);
152 fn check_rate_limit_register(&mut self, id: usize) -> Result<(), Error> {
153 self.check_rate_limit_full(
155 Settings::get().rate_limit.register,
156 Settings::get().rate_limit.register_per_second,
160 fn check_rate_limit_post(&mut self, id: usize) -> Result<(), Error> {
161 self.check_rate_limit_full(
163 Settings::get().rate_limit.post,
164 Settings::get().rate_limit.post_per_second,
168 fn check_rate_limit_message(&mut self, id: usize) -> Result<(), Error> {
169 self.check_rate_limit_full(
171 Settings::get().rate_limit.message,
172 Settings::get().rate_limit.message_per_second,
176 fn check_rate_limit_full(&mut self, id: usize, rate: i32, per: i32) -> Result<(), Error> {
177 if let Some(info) = self.sessions.get(&id) {
178 if let Some(rate_limit) = self.rate_limits.get_mut(&info.ip) {
180 if rate_limit.allowance == -2f64 {
181 rate_limit.allowance = rate as f64;
184 let current = SystemTime::now();
185 let time_passed = current.duration_since(rate_limit.last_checked)?.as_secs() as f64;
186 rate_limit.last_checked = current;
187 rate_limit.allowance += time_passed * (rate as f64 / per as f64);
188 if rate_limit.allowance > rate as f64 {
189 rate_limit.allowance = rate as f64;
192 if rate_limit.allowance < 1.0 {
194 "Rate limited IP: {}, time_passed: {}, allowance: {}",
195 &info.ip, time_passed, rate_limit.allowance
198 op: "Rate Limit".to_string(),
199 message: format!("Too many requests. {} per {} seconds", rate, per),
202 rate_limit.allowance -= 1.0;
214 /// Make actor from `ChatServer`
215 impl Actor for ChatServer {
216 /// We are going to use simple Context, we just need ability to communicate
217 /// with other actors.
218 type Context = Context<Self>;
221 /// Handler for Connect message.
223 /// Register new session and assign unique id to this session
224 impl Handler<Connect> for ChatServer {
227 fn handle(&mut self, msg: Connect, _ctx: &mut Context<Self>) -> Self::Result {
228 // notify all users in same room
229 // self.send_room_message(&"Main".to_owned(), "Someone joined", 0);
231 // register session with random id
232 let id = self.rng.gen::<usize>();
233 println!("{} joined", &msg.ip);
235 self.sessions.insert(
239 ip: msg.ip.to_owned(),
243 if self.rate_limits.get(&msg.ip).is_none() {
244 self.rate_limits.insert(
247 last_checked: SystemTime::now(),
257 /// Handler for Disconnect message.
258 impl Handler<Disconnect> for ChatServer {
261 fn handle(&mut self, msg: Disconnect, _: &mut Context<Self>) {
262 // let mut rooms: Vec<i32> = Vec::new();
265 if self.sessions.remove(&msg.id).is_some() {
266 // remove session from all rooms
267 for (_id, sessions) in &mut self.rooms {
268 if sessions.remove(&msg.id) {
276 /// Handler for Message message.
277 impl Handler<StandardMessage> for ChatServer {
278 type Result = MessageResult<StandardMessage>;
280 fn handle(&mut self, msg: StandardMessage, _: &mut Context<Self>) -> Self::Result {
281 let msg_out = match parse_json_message(self, msg) {
283 Err(e) => e.to_string(),
286 MessageResult(msg_out)
290 fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<String, Error> {
291 let json: Value = serde_json::from_str(&msg.msg)?;
292 let data = &json["data"].to_string();
293 let op = &json["op"].as_str().ok_or(APIError {
294 op: "Unknown op type".to_string(),
295 message: format!("Unknown op type"),
298 let user_operation: UserOperation = UserOperation::from_str(&op)?;
300 match user_operation {
301 UserOperation::Login => {
302 let login: Login = serde_json::from_str(data)?;
303 let res = Oper::new(user_operation, login).perform()?;
304 Ok(serde_json::to_string(&res)?)
306 UserOperation::Register => {
307 let register: Register = serde_json::from_str(data)?;
308 let res = Oper::new(user_operation, register).perform();
310 chat.check_rate_limit_register(msg.id)?;
312 Ok(serde_json::to_string(&res?)?)
314 UserOperation::GetUserDetails => {
315 let get_user_details: GetUserDetails = serde_json::from_str(data)?;
316 let res = Oper::new(user_operation, get_user_details).perform()?;
317 Ok(serde_json::to_string(&res)?)
319 UserOperation::SaveUserSettings => {
320 let save_user_settings: SaveUserSettings = serde_json::from_str(data)?;
321 let res = Oper::new(user_operation, save_user_settings).perform()?;
322 Ok(serde_json::to_string(&res)?)
324 UserOperation::AddAdmin => {
325 let add_admin: AddAdmin = serde_json::from_str(data)?;
326 let res = Oper::new(user_operation, add_admin).perform()?;
327 Ok(serde_json::to_string(&res)?)
329 UserOperation::BanUser => {
330 let ban_user: BanUser = serde_json::from_str(data)?;
331 let res = Oper::new(user_operation, ban_user).perform()?;
332 Ok(serde_json::to_string(&res)?)
334 UserOperation::GetReplies => {
335 let get_replies: GetReplies = serde_json::from_str(data)?;
336 let res = Oper::new(user_operation, get_replies).perform()?;
337 Ok(serde_json::to_string(&res)?)
339 UserOperation::GetUserMentions => {
340 let get_user_mentions: GetUserMentions = serde_json::from_str(data)?;
341 let res = Oper::new(user_operation, get_user_mentions).perform()?;
342 Ok(serde_json::to_string(&res)?)
344 UserOperation::EditUserMention => {
345 let edit_user_mention: EditUserMention = serde_json::from_str(data)?;
346 let res = Oper::new(user_operation, edit_user_mention).perform()?;
347 Ok(serde_json::to_string(&res)?)
349 UserOperation::MarkAllAsRead => {
350 let mark_all_as_read: MarkAllAsRead = serde_json::from_str(data)?;
351 let res = Oper::new(user_operation, mark_all_as_read).perform()?;
352 Ok(serde_json::to_string(&res)?)
354 UserOperation::GetCommunity => {
355 let get_community: GetCommunity = serde_json::from_str(data)?;
356 let res = Oper::new(user_operation, get_community).perform()?;
357 Ok(serde_json::to_string(&res)?)
359 UserOperation::ListCommunities => {
360 let list_communities: ListCommunities = serde_json::from_str(data)?;
361 let res = Oper::new(user_operation, list_communities).perform()?;
362 Ok(serde_json::to_string(&res)?)
364 UserOperation::CreateCommunity => {
365 chat.check_rate_limit_register(msg.id)?;
366 let create_community: CreateCommunity = serde_json::from_str(data)?;
367 let res = Oper::new(user_operation, create_community).perform()?;
368 Ok(serde_json::to_string(&res)?)
370 UserOperation::EditCommunity => {
371 let edit_community: EditCommunity = serde_json::from_str(data)?;
372 let res = Oper::new(user_operation, edit_community).perform()?;
373 let mut community_sent: CommunityResponse = res.clone();
374 community_sent.community.user_id = None;
375 community_sent.community.subscribed = None;
376 let community_sent_str = serde_json::to_string(&community_sent)?;
377 chat.send_community_message(&community_sent.community.id, &community_sent_str, msg.id)?;
378 Ok(serde_json::to_string(&res)?)
380 UserOperation::FollowCommunity => {
381 let follow_community: FollowCommunity = serde_json::from_str(data)?;
382 let res = Oper::new(user_operation, follow_community).perform()?;
383 Ok(serde_json::to_string(&res)?)
385 UserOperation::GetFollowedCommunities => {
386 let followed_communities: GetFollowedCommunities = serde_json::from_str(data)?;
387 let res = Oper::new(user_operation, followed_communities).perform()?;
388 Ok(serde_json::to_string(&res)?)
390 UserOperation::BanFromCommunity => {
391 let ban_from_community: BanFromCommunity = serde_json::from_str(data)?;
392 let community_id = ban_from_community.community_id;
393 let res = Oper::new(user_operation, ban_from_community).perform()?;
394 let res_str = serde_json::to_string(&res)?;
395 chat.send_community_message(&community_id, &res_str, msg.id)?;
398 UserOperation::AddModToCommunity => {
399 let mod_add_to_community: AddModToCommunity = serde_json::from_str(data)?;
400 let community_id = mod_add_to_community.community_id;
401 let res = Oper::new(user_operation, mod_add_to_community).perform()?;
402 let res_str = serde_json::to_string(&res)?;
403 chat.send_community_message(&community_id, &res_str, msg.id)?;
406 UserOperation::ListCategories => {
407 let list_categories: ListCategories = ListCategories;
408 let res = Oper::new(user_operation, list_categories).perform()?;
409 Ok(serde_json::to_string(&res)?)
411 UserOperation::CreatePost => {
412 chat.check_rate_limit_post(msg.id)?;
413 let create_post: CreatePost = serde_json::from_str(data)?;
414 let res = Oper::new(user_operation, create_post).perform()?;
415 Ok(serde_json::to_string(&res)?)
417 UserOperation::GetPost => {
418 let get_post: GetPost = serde_json::from_str(data)?;
419 chat.join_room(get_post.id, msg.id);
420 let res = Oper::new(user_operation, get_post).perform()?;
421 Ok(serde_json::to_string(&res)?)
423 UserOperation::GetPosts => {
424 let get_posts: GetPosts = serde_json::from_str(data)?;
425 let res = Oper::new(user_operation, get_posts).perform()?;
426 Ok(serde_json::to_string(&res)?)
428 UserOperation::CreatePostLike => {
429 chat.check_rate_limit_message(msg.id)?;
430 let create_post_like: CreatePostLike = serde_json::from_str(data)?;
431 let res = Oper::new(user_operation, create_post_like).perform()?;
432 Ok(serde_json::to_string(&res)?)
434 UserOperation::EditPost => {
435 let edit_post: EditPost = serde_json::from_str(data)?;
436 let res = Oper::new(user_operation, edit_post).perform()?;
437 let mut post_sent = res.clone();
438 post_sent.post.my_vote = None;
439 let post_sent_str = serde_json::to_string(&post_sent)?;
440 chat.send_room_message(&post_sent.post.id, &post_sent_str, msg.id);
441 Ok(serde_json::to_string(&res)?)
443 UserOperation::SavePost => {
444 let save_post: SavePost = serde_json::from_str(data)?;
445 let res = Oper::new(user_operation, save_post).perform()?;
446 Ok(serde_json::to_string(&res)?)
448 UserOperation::CreateComment => {
449 chat.check_rate_limit_message(msg.id)?;
450 let create_comment: CreateComment = serde_json::from_str(data)?;
451 let post_id = create_comment.post_id;
452 let res = Oper::new(user_operation, create_comment).perform()?;
453 let mut comment_sent = res.clone();
454 comment_sent.comment.my_vote = None;
455 comment_sent.comment.user_id = None;
456 let comment_sent_str = serde_json::to_string(&comment_sent)?;
457 chat.send_room_message(&post_id, &comment_sent_str, msg.id);
458 Ok(serde_json::to_string(&res)?)
460 UserOperation::EditComment => {
461 let edit_comment: EditComment = serde_json::from_str(data)?;
462 let post_id = edit_comment.post_id;
463 let res = Oper::new(user_operation, edit_comment).perform()?;
464 let mut comment_sent = res.clone();
465 comment_sent.comment.my_vote = None;
466 comment_sent.comment.user_id = None;
467 let comment_sent_str = serde_json::to_string(&comment_sent)?;
468 chat.send_room_message(&post_id, &comment_sent_str, msg.id);
469 Ok(serde_json::to_string(&res)?)
471 UserOperation::SaveComment => {
472 let save_comment: SaveComment = serde_json::from_str(data)?;
473 let res = Oper::new(user_operation, save_comment).perform()?;
474 Ok(serde_json::to_string(&res)?)
476 UserOperation::CreateCommentLike => {
477 chat.check_rate_limit_message(msg.id)?;
478 let create_comment_like: CreateCommentLike = serde_json::from_str(data)?;
479 let post_id = create_comment_like.post_id;
480 let res = Oper::new(user_operation, create_comment_like).perform()?;
481 let mut comment_sent = res.clone();
482 comment_sent.comment.my_vote = None;
483 comment_sent.comment.user_id = None;
484 let comment_sent_str = serde_json::to_string(&comment_sent)?;
485 chat.send_room_message(&post_id, &comment_sent_str, msg.id);
486 Ok(serde_json::to_string(&res)?)
488 UserOperation::GetModlog => {
489 let get_modlog: GetModlog = serde_json::from_str(data)?;
490 let res = Oper::new(user_operation, get_modlog).perform()?;
491 Ok(serde_json::to_string(&res)?)
493 UserOperation::CreateSite => {
494 let create_site: CreateSite = serde_json::from_str(data)?;
495 let res = Oper::new(user_operation, create_site).perform()?;
496 Ok(serde_json::to_string(&res)?)
498 UserOperation::EditSite => {
499 let edit_site: EditSite = serde_json::from_str(data)?;
500 let res = Oper::new(user_operation, edit_site).perform()?;
501 Ok(serde_json::to_string(&res)?)
503 UserOperation::GetSite => {
504 let online: usize = chat.sessions.len();
505 let get_site: GetSite = serde_json::from_str(data)?;
506 let mut res = Oper::new(user_operation, get_site).perform()?;
508 Ok(serde_json::to_string(&res)?)
510 UserOperation::Search => {
511 let search: Search = serde_json::from_str(data)?;
512 let res = Oper::new(user_operation, search).perform()?;
513 Ok(serde_json::to_string(&res)?)
515 UserOperation::TransferCommunity => {
516 let transfer_community: TransferCommunity = serde_json::from_str(data)?;
517 let res = Oper::new(user_operation, transfer_community).perform()?;
518 Ok(serde_json::to_string(&res)?)
520 UserOperation::TransferSite => {
521 let transfer_site: TransferSite = serde_json::from_str(data)?;
522 let res = Oper::new(user_operation, transfer_site).perform()?;
523 Ok(serde_json::to_string(&res)?)
525 UserOperation::DeleteAccount => {
526 let delete_account: DeleteAccount = serde_json::from_str(data)?;
527 let res = Oper::new(user_operation, delete_account).perform()?;
528 Ok(serde_json::to_string(&res)?)
530 UserOperation::PasswordReset => {
531 let password_reset: PasswordReset = serde_json::from_str(data)?;
532 let res = Oper::new(user_operation, password_reset).perform()?;
533 Ok(serde_json::to_string(&res)?)
535 UserOperation::PasswordChange => {
536 let password_change: PasswordChange = serde_json::from_str(data)?;
537 let res = Oper::new(user_operation, password_change).perform()?;
538 Ok(serde_json::to_string(&res)?)