2019-03-21 01:22:31 +00:00
|
|
|
//! `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`.
|
|
|
|
|
|
|
|
use actix::prelude::*;
|
2020-01-19 13:25:50 +00:00
|
|
|
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
|
2020-01-12 15:31:51 +00:00
|
|
|
use diesel::PgConnection;
|
2019-09-07 15:35:05 +00:00
|
|
|
use failure::Error;
|
2019-03-21 01:22:31 +00:00
|
|
|
use rand::{rngs::ThreadRng, Rng};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2019-09-07 15:35:05 +00:00
|
|
|
use serde_json::Value;
|
|
|
|
use std::collections::{HashMap, HashSet};
|
2019-03-25 03:51:27 +00:00
|
|
|
use std::str::FromStr;
|
2019-09-07 15:35:05 +00:00
|
|
|
use std::time::SystemTime;
|
2020-02-06 21:07:59 +00:00
|
|
|
use strum::IntoEnumIterator;
|
2019-03-21 01:22:31 +00:00
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
use crate::api::comment::*;
|
2019-06-03 17:47:12 +00:00
|
|
|
use crate::api::community::*;
|
|
|
|
use crate::api::post::*;
|
|
|
|
use crate::api::site::*;
|
2019-09-07 15:35:05 +00:00
|
|
|
use crate::api::user::*;
|
|
|
|
use crate::api::*;
|
2020-01-16 14:39:08 +00:00
|
|
|
use crate::websocket::UserOperation;
|
2019-10-13 19:06:18 +00:00
|
|
|
use crate::Settings;
|
2019-03-21 01:22:31 +00:00
|
|
|
|
2020-02-01 01:02:20 +00:00
|
|
|
type ConnectionId = usize;
|
|
|
|
type PostId = i32;
|
|
|
|
type CommunityId = i32;
|
|
|
|
type UserId = i32;
|
|
|
|
type IPAddr = String;
|
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
/// Chat server sends this messages to session
|
|
|
|
#[derive(Message)]
|
2020-01-10 22:41:08 +00:00
|
|
|
#[rtype(result = "()")]
|
2019-03-21 01:22:31 +00:00
|
|
|
pub struct WSMessage(pub String);
|
|
|
|
|
|
|
|
/// Message for chat server communications
|
|
|
|
|
|
|
|
/// New chat session is created
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(usize)]
|
|
|
|
pub struct Connect {
|
|
|
|
pub addr: Recipient<WSMessage>,
|
2020-02-01 01:02:20 +00:00
|
|
|
pub ip: IPAddr,
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Session is disconnected
|
|
|
|
#[derive(Message)]
|
2020-01-10 22:41:08 +00:00
|
|
|
#[rtype(result = "()")]
|
2019-03-21 01:22:31 +00:00
|
|
|
pub struct Disconnect {
|
2020-02-01 01:02:20 +00:00
|
|
|
pub id: ConnectionId,
|
|
|
|
pub ip: IPAddr,
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 22:41:08 +00:00
|
|
|
#[derive(Serialize, Deserialize, Message)]
|
|
|
|
#[rtype(String)]
|
2019-03-25 03:51:27 +00:00
|
|
|
pub struct StandardMessage {
|
|
|
|
/// Id of the client session
|
2020-02-01 01:02:20 +00:00
|
|
|
pub id: ConnectionId,
|
2019-03-25 03:51:27 +00:00
|
|
|
/// Peer message
|
|
|
|
pub msg: String,
|
|
|
|
}
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RateLimitBucket {
|
|
|
|
last_checked: SystemTime,
|
2019-09-07 15:35:05 +00:00
|
|
|
allowance: f64,
|
2019-05-01 22:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SessionInfo {
|
|
|
|
pub addr: Recipient<WSMessage>,
|
2020-02-01 01:02:20 +00:00
|
|
|
pub ip: IPAddr,
|
2019-05-01 22:44:39 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 21:07:59 +00:00
|
|
|
#[derive(Eq, PartialEq, Hash, Debug, EnumIter, Copy, Clone)]
|
|
|
|
pub enum RateLimitType {
|
|
|
|
Message,
|
|
|
|
Register,
|
|
|
|
Post,
|
|
|
|
}
|
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
/// `ChatServer` manages chat rooms and responsible for coordinating chat
|
2020-02-01 01:02:20 +00:00
|
|
|
/// session.
|
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
|
|
|
|
sessions: HashMap<ConnectionId, SessionInfo>,
|
|
|
|
|
|
|
|
/// A map from post_id to set of connectionIDs
|
|
|
|
post_rooms: HashMap<PostId, HashSet<ConnectionId>>,
|
|
|
|
|
|
|
|
/// A map from community to set of connectionIDs
|
|
|
|
community_rooms: HashMap<CommunityId, HashSet<ConnectionId>>,
|
|
|
|
|
|
|
|
/// A map from user id to its connection ID for joined users. Remember a user can have multiple
|
|
|
|
/// sessions (IE clients)
|
|
|
|
user_rooms: HashMap<UserId, HashSet<ConnectionId>>,
|
|
|
|
|
2020-02-06 21:07:59 +00:00
|
|
|
/// Rate limiting based on rate type and IP addr
|
|
|
|
rate_limit_buckets: HashMap<RateLimitType, HashMap<IPAddr, RateLimitBucket>>,
|
2020-02-01 01:02:20 +00:00
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
rng: ThreadRng,
|
2020-01-12 15:31:51 +00:00
|
|
|
db: Pool<ConnectionManager<PgConnection>>,
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 15:31:51 +00:00
|
|
|
impl ChatServer {
|
|
|
|
pub fn startup(db: Pool<ConnectionManager<PgConnection>>) -> ChatServer {
|
2019-03-21 01:22:31 +00:00
|
|
|
ChatServer {
|
|
|
|
sessions: HashMap::new(),
|
2020-02-06 21:07:59 +00:00
|
|
|
rate_limit_buckets: HashMap::new(),
|
2020-02-01 01:02:20 +00:00
|
|
|
post_rooms: HashMap::new(),
|
|
|
|
community_rooms: HashMap::new(),
|
|
|
|
user_rooms: HashMap::new(),
|
2019-03-21 01:22:31 +00:00
|
|
|
rng: rand::thread_rng(),
|
2020-01-12 15:31:51 +00:00
|
|
|
db,
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 01:02:20 +00:00
|
|
|
fn join_community_room(&mut self, community_id: CommunityId, id: ConnectionId) {
|
|
|
|
// remove session from all rooms
|
|
|
|
for sessions in self.community_rooms.values_mut() {
|
|
|
|
sessions.remove(&id);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
2020-02-01 01:02:20 +00:00
|
|
|
|
2020-02-08 04:05:15 +00:00
|
|
|
// Also leave all post rooms
|
|
|
|
// This avoids double messages
|
|
|
|
for sessions in self.post_rooms.values_mut() {
|
|
|
|
sessions.remove(&id);
|
|
|
|
}
|
|
|
|
|
2020-02-01 01:02:20 +00:00
|
|
|
// If the room doesn't exist yet
|
|
|
|
if self.community_rooms.get_mut(&community_id).is_none() {
|
|
|
|
self.community_rooms.insert(community_id, HashSet::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
.community_rooms
|
|
|
|
.get_mut(&community_id)
|
|
|
|
.unwrap()
|
|
|
|
.insert(id);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
|
2020-02-01 01:02:20 +00:00
|
|
|
fn join_post_room(&mut self, post_id: PostId, id: ConnectionId) {
|
2019-05-05 05:20:38 +00:00
|
|
|
// remove session from all rooms
|
2020-02-01 01:02:20 +00:00
|
|
|
for sessions in self.post_rooms.values_mut() {
|
2019-05-05 05:20:38 +00:00
|
|
|
sessions.remove(&id);
|
|
|
|
}
|
|
|
|
|
2020-02-08 04:05:15 +00:00
|
|
|
// Also leave all communities
|
|
|
|
// This avoids double messages
|
|
|
|
for sessions in self.community_rooms.values_mut() {
|
|
|
|
sessions.remove(&id);
|
|
|
|
}
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
// If the room doesn't exist yet
|
2020-02-01 01:02:20 +00:00
|
|
|
if self.post_rooms.get_mut(&post_id).is_none() {
|
|
|
|
self.post_rooms.insert(post_id, HashSet::new());
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 01:02:20 +00:00
|
|
|
self.post_rooms.get_mut(&post_id).unwrap().insert(id);
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 01:02:20 +00:00
|
|
|
fn join_user_room(&mut self, user_id: UserId, id: ConnectionId) {
|
|
|
|
// remove session from all rooms
|
|
|
|
for sessions in self.user_rooms.values_mut() {
|
|
|
|
sessions.remove(&id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the room doesn't exist yet
|
|
|
|
if self.user_rooms.get_mut(&user_id).is_none() {
|
|
|
|
self.user_rooms.insert(user_id, HashSet::new());
|
|
|
|
}
|
2020-01-12 15:31:51 +00:00
|
|
|
|
2020-02-01 01:02:20 +00:00
|
|
|
self.user_rooms.get_mut(&user_id).unwrap().insert(id);
|
|
|
|
}
|
2019-12-07 12:03:03 +00:00
|
|
|
|
2020-02-01 01:02:20 +00:00
|
|
|
fn send_post_room_message(&self, post_id: PostId, message: &str, skip_id: ConnectionId) {
|
|
|
|
if let Some(sessions) = self.post_rooms.get(&post_id) {
|
|
|
|
for id in sessions {
|
|
|
|
if *id != skip_id {
|
|
|
|
if let Some(info) = self.sessions.get(id) {
|
|
|
|
let _ = info.addr.do_send(WSMessage(message.to_owned()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-07 12:03:03 +00:00
|
|
|
|
2020-02-01 01:02:20 +00:00
|
|
|
fn send_community_room_message(
|
|
|
|
&self,
|
|
|
|
community_id: CommunityId,
|
|
|
|
message: &str,
|
|
|
|
skip_id: ConnectionId,
|
|
|
|
) {
|
|
|
|
if let Some(sessions) = self.community_rooms.get(&community_id) {
|
|
|
|
for id in sessions {
|
|
|
|
if *id != skip_id {
|
|
|
|
if let Some(info) = self.sessions.get(id) {
|
|
|
|
let _ = info.addr.do_send(WSMessage(message.to_owned()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_user_room_message(&self, user_id: UserId, message: &str, skip_id: ConnectionId) {
|
|
|
|
if let Some(sessions) = self.user_rooms.get(&user_id) {
|
|
|
|
for id in sessions {
|
|
|
|
if *id != skip_id {
|
|
|
|
if let Some(info) = self.sessions.get(id) {
|
|
|
|
let _ = info.addr.do_send(WSMessage(message.to_owned()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
2020-02-01 01:02:20 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
|
2020-02-01 01:02:20 +00:00
|
|
|
fn send_all_message(&self, message: &str, skip_id: ConnectionId) {
|
|
|
|
for id in self.sessions.keys() {
|
|
|
|
if *id != skip_id {
|
|
|
|
if let Some(info) = self.sessions.get(id) {
|
|
|
|
let _ = info.addr.do_send(WSMessage(message.to_owned()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
2019-05-01 22:44:39 +00:00
|
|
|
|
2020-02-02 17:45:41 +00:00
|
|
|
fn comment_sends(
|
|
|
|
&self,
|
|
|
|
user_operation: UserOperation,
|
|
|
|
comment: CommentResponse,
|
|
|
|
id: ConnectionId,
|
|
|
|
) -> Result<String, Error> {
|
|
|
|
let mut comment_reply_sent = comment.clone();
|
|
|
|
comment_reply_sent.comment.my_vote = None;
|
|
|
|
comment_reply_sent.comment.user_id = None;
|
|
|
|
|
|
|
|
// For the post room ones, and the directs back to the user
|
|
|
|
// strip out the recipient_ids, so that
|
|
|
|
// users don't get double notifs
|
|
|
|
let mut comment_user_sent = comment.clone();
|
|
|
|
comment_user_sent.recipient_ids = Vec::new();
|
|
|
|
|
|
|
|
let mut comment_post_sent = comment_reply_sent.clone();
|
|
|
|
comment_post_sent.recipient_ids = Vec::new();
|
|
|
|
|
|
|
|
let comment_reply_sent_str = to_json_string(&user_operation, &comment_reply_sent)?;
|
|
|
|
let comment_post_sent_str = to_json_string(&user_operation, &comment_post_sent)?;
|
|
|
|
let comment_user_sent_str = to_json_string(&user_operation, &comment_user_sent)?;
|
|
|
|
|
|
|
|
// Send it to the post room
|
|
|
|
self.send_post_room_message(comment.comment.post_id, &comment_post_sent_str, id);
|
|
|
|
|
|
|
|
// Send it to the recipient(s) including the mentioned users
|
|
|
|
for recipient_id in comment_reply_sent.recipient_ids {
|
|
|
|
self.send_user_room_message(recipient_id, &comment_reply_sent_str, id);
|
|
|
|
}
|
|
|
|
|
2020-02-08 04:05:15 +00:00
|
|
|
// Send it to the community too
|
|
|
|
self.send_community_room_message(0, &comment_post_sent_str, id);
|
|
|
|
self.send_community_room_message(comment.comment.community_id, &comment_post_sent_str, id);
|
|
|
|
|
2020-02-02 17:45:41 +00:00
|
|
|
Ok(comment_user_sent_str)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn post_sends(
|
|
|
|
&self,
|
|
|
|
user_operation: UserOperation,
|
|
|
|
post: PostResponse,
|
|
|
|
id: ConnectionId,
|
|
|
|
) -> Result<String, Error> {
|
|
|
|
let community_id = post.post.community_id;
|
|
|
|
|
|
|
|
// Don't send my data with it
|
|
|
|
let mut post_sent = post.clone();
|
|
|
|
post_sent.post.my_vote = None;
|
|
|
|
post_sent.post.user_id = None;
|
|
|
|
let post_sent_str = to_json_string(&user_operation, &post_sent)?;
|
|
|
|
|
|
|
|
// Send it to /c/all and that community
|
|
|
|
self.send_community_room_message(0, &post_sent_str, id);
|
|
|
|
self.send_community_room_message(community_id, &post_sent_str, id);
|
|
|
|
|
2020-02-09 20:04:41 +00:00
|
|
|
// Send it to the post room
|
|
|
|
self.send_post_room_message(post_sent.post.id, &post_sent_str, id);
|
|
|
|
|
2020-02-02 17:45:41 +00:00
|
|
|
to_json_string(&user_operation, post)
|
|
|
|
}
|
|
|
|
|
2020-02-06 21:07:59 +00:00
|
|
|
fn check_rate_limit_register(&mut self, id: usize, check_only: bool) -> Result<(), Error> {
|
2019-10-13 19:06:18 +00:00
|
|
|
self.check_rate_limit_full(
|
2020-02-06 21:07:59 +00:00
|
|
|
RateLimitType::Register,
|
2019-10-13 19:06:18 +00:00
|
|
|
id,
|
2019-12-15 16:40:55 +00:00
|
|
|
Settings::get().rate_limit.register,
|
|
|
|
Settings::get().rate_limit.register_per_second,
|
2020-02-06 21:07:59 +00:00
|
|
|
check_only,
|
2019-10-13 19:06:18 +00:00
|
|
|
)
|
2019-05-01 22:44:39 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 21:07:59 +00:00
|
|
|
fn check_rate_limit_post(&mut self, id: usize, check_only: bool) -> Result<(), Error> {
|
2019-10-13 19:06:18 +00:00
|
|
|
self.check_rate_limit_full(
|
2020-02-06 21:07:59 +00:00
|
|
|
RateLimitType::Post,
|
2019-10-13 19:06:18 +00:00
|
|
|
id,
|
2019-12-15 16:40:55 +00:00
|
|
|
Settings::get().rate_limit.post,
|
|
|
|
Settings::get().rate_limit.post_per_second,
|
2020-02-06 21:07:59 +00:00
|
|
|
check_only,
|
2019-10-13 19:06:18 +00:00
|
|
|
)
|
2019-09-03 20:18:07 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 21:07:59 +00:00
|
|
|
fn check_rate_limit_message(&mut self, id: usize, check_only: bool) -> Result<(), Error> {
|
2019-10-13 19:06:18 +00:00
|
|
|
self.check_rate_limit_full(
|
2020-02-06 21:07:59 +00:00
|
|
|
RateLimitType::Message,
|
2019-10-13 19:06:18 +00:00
|
|
|
id,
|
2019-12-15 16:40:55 +00:00
|
|
|
Settings::get().rate_limit.message,
|
|
|
|
Settings::get().rate_limit.message_per_second,
|
2020-02-06 21:07:59 +00:00
|
|
|
check_only,
|
2019-10-13 19:06:18 +00:00
|
|
|
)
|
2019-05-01 22:44:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 11:30:00 +00:00
|
|
|
#[allow(clippy::float_cmp)]
|
2020-02-06 21:07:59 +00:00
|
|
|
fn check_rate_limit_full(
|
|
|
|
&mut self,
|
|
|
|
type_: RateLimitType,
|
|
|
|
id: usize,
|
|
|
|
rate: i32,
|
|
|
|
per: i32,
|
|
|
|
check_only: bool,
|
|
|
|
) -> Result<(), Error> {
|
2019-05-05 05:20:38 +00:00
|
|
|
if let Some(info) = self.sessions.get(&id) {
|
2020-02-06 21:07:59 +00:00
|
|
|
if let Some(bucket) = self.rate_limit_buckets.get_mut(&type_) {
|
|
|
|
if let Some(rate_limit) = bucket.get_mut(&info.ip) {
|
|
|
|
let current = SystemTime::now();
|
|
|
|
let time_passed = current.duration_since(rate_limit.last_checked)?.as_secs() as f64;
|
|
|
|
|
|
|
|
// The initial value
|
|
|
|
if rate_limit.allowance == -2f64 {
|
|
|
|
rate_limit.allowance = rate as f64;
|
|
|
|
};
|
|
|
|
|
|
|
|
rate_limit.last_checked = current;
|
2020-02-07 03:48:43 +00:00
|
|
|
rate_limit.allowance += time_passed * (rate as f64 / per as f64);
|
|
|
|
if !check_only && rate_limit.allowance > rate as f64 {
|
|
|
|
rate_limit.allowance = rate as f64;
|
2020-02-06 21:07:59 +00:00
|
|
|
}
|
2019-05-01 22:44:39 +00:00
|
|
|
|
2020-02-06 21:07:59 +00:00
|
|
|
if rate_limit.allowance < 1.0 {
|
|
|
|
println!(
|
|
|
|
"Rate limited IP: {}, time_passed: {}, allowance: {}",
|
|
|
|
&info.ip, time_passed, rate_limit.allowance
|
|
|
|
);
|
|
|
|
Err(
|
|
|
|
APIError {
|
|
|
|
message: format!("Too many requests. {} per {} seconds", rate, per),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
if !check_only {
|
|
|
|
rate_limit.allowance -= 1.0;
|
2020-01-02 11:30:00 +00:00
|
|
|
}
|
2020-02-06 21:07:59 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-05-01 22:44:39 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 01:22:31 +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>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handler for Connect message.
|
|
|
|
///
|
|
|
|
/// Register new session and assign unique id to this session
|
|
|
|
impl Handler<Connect> for ChatServer {
|
|
|
|
type Result = usize;
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
fn handle(&mut self, msg: Connect, _ctx: &mut Context<Self>) -> Self::Result {
|
2019-03-21 01:22:31 +00:00
|
|
|
// register session with random id
|
|
|
|
let id = self.rng.gen::<usize>();
|
2019-05-02 05:26:31 +00:00
|
|
|
println!("{} joined", &msg.ip);
|
2019-05-01 22:44:39 +00:00
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
self.sessions.insert(
|
|
|
|
id,
|
|
|
|
SessionInfo {
|
|
|
|
addr: msg.addr,
|
|
|
|
ip: msg.ip.to_owned(),
|
|
|
|
},
|
|
|
|
);
|
2019-05-01 22:44:39 +00:00
|
|
|
|
2020-02-06 21:07:59 +00:00
|
|
|
for rate_limit_type in RateLimitType::iter() {
|
|
|
|
if self.rate_limit_buckets.get(&rate_limit_type).is_none() {
|
|
|
|
self
|
|
|
|
.rate_limit_buckets
|
|
|
|
.insert(rate_limit_type, HashMap::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(bucket) = self.rate_limit_buckets.get_mut(&rate_limit_type) {
|
|
|
|
if bucket.get(&msg.ip).is_none() {
|
|
|
|
bucket.insert(
|
|
|
|
msg.ip.to_owned(),
|
|
|
|
RateLimitBucket {
|
|
|
|
last_checked: SystemTime::now(),
|
|
|
|
allowance: -2f64,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-05-01 22:44:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handler for Disconnect message.
|
|
|
|
impl Handler<Disconnect> for ChatServer {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: Disconnect, _: &mut Context<Self>) {
|
2020-02-01 01:02:20 +00:00
|
|
|
// Remove connections from sessions and all 3 scopes
|
2019-03-21 01:22:31 +00:00
|
|
|
if self.sessions.remove(&msg.id).is_some() {
|
2020-02-01 01:02:20 +00:00
|
|
|
for sessions in self.user_rooms.values_mut() {
|
|
|
|
sessions.remove(&msg.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
for sessions in self.post_rooms.values_mut() {
|
|
|
|
sessions.remove(&msg.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
for sessions in self.community_rooms.values_mut() {
|
|
|
|
sessions.remove(&msg.id);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:51:27 +00:00
|
|
|
/// Handler for Message message.
|
|
|
|
impl Handler<StandardMessage> for ChatServer {
|
|
|
|
type Result = MessageResult<StandardMessage>;
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: StandardMessage, _: &mut Context<Self>) -> Self::Result {
|
2019-04-21 07:26:26 +00:00
|
|
|
let msg_out = match parse_json_message(self, msg) {
|
|
|
|
Ok(m) => m,
|
2019-09-07 15:35:05 +00:00
|
|
|
Err(e) => e.to_string(),
|
2019-04-21 07:26:26 +00:00
|
|
|
};
|
|
|
|
|
2020-01-25 14:58:53 +00:00
|
|
|
println!("Message Sent: {}", msg_out);
|
2019-04-21 07:26:26 +00:00
|
|
|
MessageResult(msg_out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 16:25:45 +00:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct WebsocketResponse<T> {
|
|
|
|
op: String,
|
|
|
|
data: T,
|
|
|
|
}
|
|
|
|
|
2020-01-16 15:09:01 +00:00
|
|
|
fn to_json_string<T>(op: &UserOperation, data: T) -> Result<String, Error>
|
2020-01-16 14:39:08 +00:00
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
2020-01-18 16:25:45 +00:00
|
|
|
let response = WebsocketResponse {
|
|
|
|
op: op.to_string(),
|
|
|
|
data,
|
2020-01-16 14:39:08 +00:00
|
|
|
};
|
2020-01-18 16:25:45 +00:00
|
|
|
Ok(serde_json::to_string(&response)?)
|
2020-01-16 14:39:08 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 13:25:50 +00:00
|
|
|
fn do_user_operation<'a, Data, Response>(
|
|
|
|
op: UserOperation,
|
|
|
|
data: &str,
|
|
|
|
conn: &PooledConnection<ConnectionManager<PgConnection>>,
|
|
|
|
) -> Result<String, Error>
|
|
|
|
where
|
|
|
|
for<'de> Data: Deserialize<'de> + 'a,
|
|
|
|
Response: Serialize,
|
|
|
|
Oper<Data>: Perform<Response>,
|
|
|
|
{
|
|
|
|
let parsed_data: Data = serde_json::from_str(data)?;
|
|
|
|
let res = Oper::new(parsed_data).perform(&conn)?;
|
|
|
|
to_json_string(&op, &res)
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<String, Error> {
|
|
|
|
let json: Value = serde_json::from_str(&msg.msg)?;
|
|
|
|
let data = &json["data"].to_string();
|
2019-08-30 20:33:20 +00:00
|
|
|
let op = &json["op"].as_str().ok_or(APIError {
|
2020-01-02 11:30:00 +00:00
|
|
|
message: "Unknown op type".to_string(),
|
2019-08-30 20:33:20 +00:00
|
|
|
})?;
|
2019-04-21 07:26:26 +00:00
|
|
|
|
2020-01-12 15:31:51 +00:00
|
|
|
let conn = chat.db.get()?;
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let user_operation: UserOperation = UserOperation::from_str(&op)?;
|
|
|
|
|
2020-01-19 13:25:50 +00:00
|
|
|
// TODO: none of the chat messages are going to work if stuff is submitted via http api,
|
|
|
|
// need to move that handling elsewhere
|
2020-02-06 21:07:59 +00:00
|
|
|
|
|
|
|
// A DDOS check
|
|
|
|
chat.check_rate_limit_message(msg.id, false)?;
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
match user_operation {
|
2020-01-19 13:25:50 +00:00
|
|
|
UserOperation::Login => do_user_operation::<Login, LoginResponse>(user_operation, data, &conn),
|
2019-04-21 07:26:26 +00:00
|
|
|
UserOperation::Register => {
|
2020-02-06 21:07:59 +00:00
|
|
|
chat.check_rate_limit_register(msg.id, true)?;
|
|
|
|
let register: Register = serde_json::from_str(data)?;
|
|
|
|
let res = Oper::new(register).perform(&conn)?;
|
|
|
|
chat.check_rate_limit_register(msg.id, false)?;
|
|
|
|
to_json_string(&user_operation, &res)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::GetUserDetails => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<GetUserDetails, GetUserDetailsResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-08-14 02:52:43 +00:00
|
|
|
UserOperation::SaveUserSettings => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<SaveUserSettings, LoginResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::AddAdmin => {
|
2020-02-01 01:02:20 +00:00
|
|
|
let add_admin: AddAdmin = serde_json::from_str(data)?;
|
|
|
|
let res = Oper::new(add_admin).perform(&conn)?;
|
|
|
|
let res_str = to_json_string(&user_operation, &res)?;
|
|
|
|
chat.send_all_message(&res_str, msg.id);
|
|
|
|
Ok(res_str)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::BanUser => {
|
2020-02-01 01:02:20 +00:00
|
|
|
let ban_user: BanUser = serde_json::from_str(data)?;
|
|
|
|
let res = Oper::new(ban_user).perform(&conn)?;
|
|
|
|
let res_str = to_json_string(&user_operation, &res)?;
|
|
|
|
chat.send_all_message(&res_str, msg.id);
|
|
|
|
Ok(res_str)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::GetReplies => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<GetReplies, GetRepliesResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-10-20 00:46:29 +00:00
|
|
|
UserOperation::GetUserMentions => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<GetUserMentions, GetUserMentionsResponse>(user_operation, data, &conn)
|
2019-10-20 00:46:29 +00:00
|
|
|
}
|
|
|
|
UserOperation::EditUserMention => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<EditUserMention, UserMentionResponse>(user_operation, data, &conn)
|
2019-10-20 00:46:29 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::MarkAllAsRead => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<MarkAllAsRead, GetRepliesResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::GetCommunity => {
|
2020-02-01 01:02:20 +00:00
|
|
|
let get_community: GetCommunity = serde_json::from_str(data)?;
|
|
|
|
let mut res = Oper::new(get_community).perform(&conn)?;
|
|
|
|
let community_id = res.community.id;
|
|
|
|
|
|
|
|
chat.join_community_room(community_id, msg.id);
|
|
|
|
|
|
|
|
res.online = if let Some(community_users) = chat.community_rooms.get(&community_id) {
|
|
|
|
community_users.len()
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
to_json_string(&user_operation, &res)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
UserOperation::ListCommunities => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<ListCommunities, ListCommunitiesResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::CreateCommunity => {
|
2020-02-06 21:07:59 +00:00
|
|
|
chat.check_rate_limit_register(msg.id, true)?;
|
|
|
|
let create_community: CreateCommunity = serde_json::from_str(data)?;
|
|
|
|
let res = Oper::new(create_community).perform(&conn)?;
|
|
|
|
chat.check_rate_limit_register(msg.id, false)?;
|
|
|
|
to_json_string(&user_operation, &res)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::EditCommunity => {
|
|
|
|
let edit_community: EditCommunity = serde_json::from_str(data)?;
|
2020-01-16 14:39:08 +00:00
|
|
|
let res = Oper::new(edit_community).perform(&conn)?;
|
2019-05-05 05:20:38 +00:00
|
|
|
let mut community_sent: CommunityResponse = res.clone();
|
|
|
|
community_sent.community.user_id = None;
|
|
|
|
community_sent.community.subscribed = None;
|
2020-01-16 15:09:01 +00:00
|
|
|
let community_sent_str = to_json_string(&user_operation, &community_sent)?;
|
2020-02-01 01:02:20 +00:00
|
|
|
chat.send_community_room_message(community_sent.community.id, &community_sent_str, msg.id);
|
2020-01-16 15:09:01 +00:00
|
|
|
to_json_string(&user_operation, &res)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::FollowCommunity => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<FollowCommunity, CommunityResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2020-01-19 13:25:50 +00:00
|
|
|
UserOperation::GetFollowedCommunities => do_user_operation::<
|
|
|
|
GetFollowedCommunities,
|
|
|
|
GetFollowedCommunitiesResponse,
|
|
|
|
>(user_operation, data, &conn),
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::BanFromCommunity => {
|
|
|
|
let ban_from_community: BanFromCommunity = serde_json::from_str(data)?;
|
2019-05-05 16:20:30 +00:00
|
|
|
let community_id = ban_from_community.community_id;
|
2020-01-16 14:39:08 +00:00
|
|
|
let res = Oper::new(ban_from_community).perform(&conn)?;
|
2020-01-16 15:09:01 +00:00
|
|
|
let res_str = to_json_string(&user_operation, &res)?;
|
2020-02-01 01:02:20 +00:00
|
|
|
chat.send_community_room_message(community_id, &res_str, msg.id);
|
2019-05-05 05:20:38 +00:00
|
|
|
Ok(res_str)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::AddModToCommunity => {
|
|
|
|
let mod_add_to_community: AddModToCommunity = serde_json::from_str(data)?;
|
2019-05-05 16:20:30 +00:00
|
|
|
let community_id = mod_add_to_community.community_id;
|
2020-01-16 14:39:08 +00:00
|
|
|
let res = Oper::new(mod_add_to_community).perform(&conn)?;
|
2020-01-16 15:09:01 +00:00
|
|
|
let res_str = to_json_string(&user_operation, &res)?;
|
2020-02-01 01:02:20 +00:00
|
|
|
chat.send_community_room_message(community_id, &res_str, msg.id);
|
2019-05-05 05:20:38 +00:00
|
|
|
Ok(res_str)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
UserOperation::ListCategories => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<ListCategories, ListCategoriesResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
UserOperation::GetPost => {
|
|
|
|
let get_post: GetPost = serde_json::from_str(data)?;
|
2020-02-01 01:02:20 +00:00
|
|
|
let post_id = get_post.id;
|
|
|
|
chat.join_post_room(post_id, msg.id);
|
|
|
|
let mut res = Oper::new(get_post).perform(&conn)?;
|
|
|
|
|
|
|
|
res.online = if let Some(post_users) = chat.post_rooms.get(&post_id) {
|
|
|
|
post_users.len()
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2020-01-16 15:09:01 +00:00
|
|
|
to_json_string(&user_operation, &res)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
UserOperation::GetPosts => {
|
2020-02-01 01:02:20 +00:00
|
|
|
let get_posts: GetPosts = serde_json::from_str(data)?;
|
|
|
|
if get_posts.community_id.is_none() {
|
|
|
|
// 0 is the "all" community
|
|
|
|
chat.join_community_room(0, msg.id);
|
|
|
|
}
|
|
|
|
let res = Oper::new(get_posts).perform(&conn)?;
|
|
|
|
to_json_string(&user_operation, &res)
|
|
|
|
}
|
2020-02-08 04:05:15 +00:00
|
|
|
UserOperation::GetComments => {
|
|
|
|
let get_comments: GetComments = serde_json::from_str(data)?;
|
|
|
|
if get_comments.community_id.is_none() {
|
|
|
|
// 0 is the "all" community
|
|
|
|
chat.join_community_room(0, msg.id);
|
|
|
|
}
|
|
|
|
let res = Oper::new(get_comments).perform(&conn)?;
|
|
|
|
to_json_string(&user_operation, &res)
|
|
|
|
}
|
2020-02-02 17:45:41 +00:00
|
|
|
UserOperation::CreatePost => {
|
2020-02-06 21:07:59 +00:00
|
|
|
chat.check_rate_limit_post(msg.id, true)?;
|
2020-02-02 17:45:41 +00:00
|
|
|
let create_post: CreatePost = serde_json::from_str(data)?;
|
|
|
|
let res = Oper::new(create_post).perform(&conn)?;
|
2020-02-06 21:07:59 +00:00
|
|
|
chat.check_rate_limit_post(msg.id, false)?;
|
2020-02-02 17:45:41 +00:00
|
|
|
|
|
|
|
chat.post_sends(UserOperation::CreatePost, res, msg.id)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
UserOperation::CreatePostLike => {
|
2020-02-01 01:02:20 +00:00
|
|
|
let create_post_like: CreatePostLike = serde_json::from_str(data)?;
|
|
|
|
let res = Oper::new(create_post_like).perform(&conn)?;
|
|
|
|
|
2020-02-02 17:45:41 +00:00
|
|
|
chat.post_sends(UserOperation::CreatePostLike, res, msg.id)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
UserOperation::EditPost => {
|
|
|
|
let edit_post: EditPost = serde_json::from_str(data)?;
|
2020-01-16 14:39:08 +00:00
|
|
|
let res = Oper::new(edit_post).perform(&conn)?;
|
2020-02-02 17:45:41 +00:00
|
|
|
|
|
|
|
chat.post_sends(UserOperation::EditPost, res, msg.id)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
UserOperation::SavePost => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<SavePost, PostResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::CreateComment => {
|
|
|
|
let create_comment: CreateComment = serde_json::from_str(data)?;
|
2020-01-16 14:39:08 +00:00
|
|
|
let res = Oper::new(create_comment).perform(&conn)?;
|
2020-02-02 15:09:01 +00:00
|
|
|
|
2020-02-02 17:45:41 +00:00
|
|
|
chat.comment_sends(UserOperation::CreateComment, res, msg.id)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::EditComment => {
|
|
|
|
let edit_comment: EditComment = serde_json::from_str(data)?;
|
2020-01-16 14:39:08 +00:00
|
|
|
let res = Oper::new(edit_comment).perform(&conn)?;
|
2020-02-01 01:02:20 +00:00
|
|
|
|
2020-02-02 17:45:41 +00:00
|
|
|
chat.comment_sends(UserOperation::EditComment, res, msg.id)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::SaveComment => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<SaveComment, CommentResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
UserOperation::CreateCommentLike => {
|
|
|
|
let create_comment_like: CreateCommentLike = serde_json::from_str(data)?;
|
2020-01-16 14:39:08 +00:00
|
|
|
let res = Oper::new(create_comment_like).perform(&conn)?;
|
2020-02-01 01:02:20 +00:00
|
|
|
|
2020-02-02 17:45:41 +00:00
|
|
|
chat.comment_sends(UserOperation::CreateCommentLike, res, msg.id)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
UserOperation::GetModlog => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<GetModlog, GetModlogResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
UserOperation::CreateSite => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<CreateSite, SiteResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
UserOperation::EditSite => {
|
2020-02-01 01:02:20 +00:00
|
|
|
let edit_site: EditSite = serde_json::from_str(data)?;
|
|
|
|
let res = Oper::new(edit_site).perform(&conn)?;
|
|
|
|
let res_str = to_json_string(&user_operation, &res)?;
|
|
|
|
chat.send_all_message(&res_str, msg.id);
|
|
|
|
Ok(res_str)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
UserOperation::GetSite => {
|
|
|
|
let get_site: GetSite = serde_json::from_str(data)?;
|
2020-01-16 14:39:08 +00:00
|
|
|
let mut res = Oper::new(get_site).perform(&conn)?;
|
2020-02-01 01:02:20 +00:00
|
|
|
res.online = chat.sessions.len();
|
2020-01-16 15:09:01 +00:00
|
|
|
to_json_string(&user_operation, &res)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-04-23 22:05:50 +00:00
|
|
|
UserOperation::Search => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<Search, SearchResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-08-24 02:40:41 +00:00
|
|
|
UserOperation::TransferCommunity => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<TransferCommunity, GetCommunityResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-08-24 02:40:41 +00:00
|
|
|
UserOperation::TransferSite => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<TransferSite, GetSiteResponse>(user_operation, data, &conn)
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-10-15 22:09:01 +00:00
|
|
|
UserOperation::DeleteAccount => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<DeleteAccount, LoginResponse>(user_operation, data, &conn)
|
2019-10-15 22:09:01 +00:00
|
|
|
}
|
2019-10-30 03:35:39 +00:00
|
|
|
UserOperation::PasswordReset => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<PasswordReset, PasswordResetResponse>(user_operation, data, &conn)
|
2019-10-30 03:35:39 +00:00
|
|
|
}
|
|
|
|
UserOperation::PasswordChange => {
|
2020-01-19 13:25:50 +00:00
|
|
|
do_user_operation::<PasswordChange, LoginResponse>(user_operation, data, &conn)
|
2019-10-30 03:35:39 +00:00
|
|
|
}
|
2020-01-22 21:35:29 +00:00
|
|
|
UserOperation::CreatePrivateMessage => {
|
2020-02-01 01:02:20 +00:00
|
|
|
let create_private_message: CreatePrivateMessage = serde_json::from_str(data)?;
|
|
|
|
let recipient_id = create_private_message.recipient_id;
|
|
|
|
let res = Oper::new(create_private_message).perform(&conn)?;
|
|
|
|
let res_str = to_json_string(&user_operation, &res)?;
|
|
|
|
|
|
|
|
chat.send_user_room_message(recipient_id, &res_str, msg.id);
|
|
|
|
Ok(res_str)
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|
|
|
|
UserOperation::EditPrivateMessage => {
|
2020-01-24 00:17:42 +00:00
|
|
|
do_user_operation::<EditPrivateMessage, PrivateMessageResponse>(user_operation, data, &conn)
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|
|
|
|
UserOperation::GetPrivateMessages => {
|
2020-01-24 00:17:42 +00:00
|
|
|
do_user_operation::<GetPrivateMessages, PrivateMessagesResponse>(user_operation, data, &conn)
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|
2020-02-02 17:45:41 +00:00
|
|
|
UserOperation::UserJoin => {
|
|
|
|
let user_join: UserJoin = serde_json::from_str(data)?;
|
|
|
|
let res = Oper::new(user_join).perform(&conn)?;
|
|
|
|
chat.join_user_room(res.user_id, msg.id);
|
|
|
|
to_json_string(&user_operation, &res)
|
|
|
|
}
|
2019-04-29 16:51:13 +00:00
|
|
|
}
|
|
|
|
}
|