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`.
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
use super::*;
|
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-04-20 03:59:07 +00:00
|
|
|
use crate::rate_limit::RateLimit;
|
2020-01-16 14:39:08 +00:00
|
|
|
use crate::websocket::UserOperation;
|
2020-04-19 22:08:25 +00:00
|
|
|
use crate::{CommunityId, ConnectionId, IPAddr, PostId, UserId};
|
2020-02-01 01:02:20 +00:00
|
|
|
|
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-04-19 22:08:25 +00:00
|
|
|
/// The messages sent to websocket clients
|
2020-01-10 22:41:08 +00:00
|
|
|
#[derive(Serialize, Deserialize, Message)]
|
2020-04-20 17:31:22 +00:00
|
|
|
#[rtype(result = "Result<String, std::convert::Infallible>")]
|
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,
|
|
|
|
}
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct SendAllMessage<Response> {
|
|
|
|
pub op: UserOperation,
|
|
|
|
pub response: Response,
|
|
|
|
pub my_id: Option<ConnectionId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct SendUserRoomMessage<Response> {
|
|
|
|
pub op: UserOperation,
|
|
|
|
pub response: Response,
|
|
|
|
pub recipient_id: UserId,
|
|
|
|
pub my_id: Option<ConnectionId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct SendCommunityRoomMessage<Response> {
|
|
|
|
pub op: UserOperation,
|
|
|
|
pub response: Response,
|
|
|
|
pub community_id: CommunityId,
|
|
|
|
pub my_id: Option<ConnectionId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct SendPost {
|
|
|
|
pub op: UserOperation,
|
|
|
|
pub post: PostResponse,
|
|
|
|
pub my_id: Option<ConnectionId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct SendComment {
|
|
|
|
pub op: UserOperation,
|
|
|
|
pub comment: CommentResponse,
|
|
|
|
pub my_id: Option<ConnectionId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct JoinUserRoom {
|
|
|
|
pub user_id: UserId,
|
|
|
|
pub id: ConnectionId,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct JoinCommunityRoom {
|
|
|
|
pub community_id: CommunityId,
|
|
|
|
pub id: ConnectionId,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct JoinPostRoom {
|
|
|
|
pub post_id: PostId,
|
|
|
|
pub id: ConnectionId,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(usize)]
|
|
|
|
pub struct GetUsersOnline;
|
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(usize)]
|
|
|
|
pub struct GetPostUsersOnline {
|
|
|
|
pub post_id: PostId,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(usize)]
|
|
|
|
pub struct GetCommunityUsersOnline {
|
|
|
|
pub community_id: CommunityId,
|
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
|
|
|
}
|
|
|
|
|
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
|
2020-04-19 22:08:25 +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
|
|
|
|
|
|
|
/// 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>>,
|
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
rng: ThreadRng,
|
2020-04-19 22:08:25 +00:00
|
|
|
|
|
|
|
/// The DB Pool
|
|
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
|
|
|
|
|
|
/// Rate limiting based on rate type and IP addr
|
2020-04-20 03:59:07 +00:00
|
|
|
rate_limiter: RateLimit,
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 15:31:51 +00:00
|
|
|
impl ChatServer {
|
2020-04-19 22:08:25 +00:00
|
|
|
pub fn startup(
|
|
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
2020-04-20 03:59:07 +00:00
|
|
|
rate_limiter: RateLimit,
|
2020-04-19 22:08:25 +00:00
|
|
|
) -> ChatServer {
|
2019-03-21 01:22:31 +00:00
|
|
|
ChatServer {
|
|
|
|
sessions: 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-04-19 22:08:25 +00:00
|
|
|
pool,
|
|
|
|
rate_limiter,
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
pub fn join_community_room(&mut self, community_id: CommunityId, id: ConnectionId) {
|
2020-02-01 01:02:20 +00:00
|
|
|
// 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-04-19 22:08:25 +00:00
|
|
|
pub 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-04-19 22:08:25 +00:00
|
|
|
pub fn join_user_room(&mut self, user_id: UserId, id: ConnectionId) {
|
2020-02-01 01:02:20 +00:00
|
|
|
// 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-04-19 22:08:25 +00:00
|
|
|
fn send_post_room_message<Response>(
|
|
|
|
&self,
|
|
|
|
op: &UserOperation,
|
|
|
|
response: &Response,
|
|
|
|
post_id: PostId,
|
|
|
|
my_id: Option<ConnectionId>,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
Response: Serialize,
|
|
|
|
{
|
|
|
|
let res_str = &to_json_string(op, response)?;
|
2020-02-01 01:02:20 +00:00
|
|
|
if let Some(sessions) = self.post_rooms.get(&post_id) {
|
|
|
|
for id in sessions {
|
2020-04-19 22:08:25 +00:00
|
|
|
if let Some(my_id) = my_id {
|
|
|
|
if *id == my_id {
|
|
|
|
continue;
|
2020-02-01 01:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-19 22:08:25 +00:00
|
|
|
self.sendit(res_str, *id);
|
2020-02-01 01:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-19 22:08:25 +00:00
|
|
|
Ok(())
|
2020-02-01 01:02:20 +00:00
|
|
|
}
|
2019-12-07 12:03:03 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
pub fn send_community_room_message<Response>(
|
2020-02-01 01:02:20 +00:00
|
|
|
&self,
|
2020-04-19 22:08:25 +00:00
|
|
|
op: &UserOperation,
|
|
|
|
response: &Response,
|
2020-02-01 01:02:20 +00:00
|
|
|
community_id: CommunityId,
|
2020-04-19 22:08:25 +00:00
|
|
|
my_id: Option<ConnectionId>,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
Response: Serialize,
|
|
|
|
{
|
|
|
|
let res_str = &to_json_string(op, response)?;
|
2020-02-01 01:02:20 +00:00
|
|
|
if let Some(sessions) = self.community_rooms.get(&community_id) {
|
|
|
|
for id in sessions {
|
2020-04-19 22:08:25 +00:00
|
|
|
if let Some(my_id) = my_id {
|
|
|
|
if *id == my_id {
|
|
|
|
continue;
|
2020-02-01 01:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-19 22:08:25 +00:00
|
|
|
self.sendit(res_str, *id);
|
2020-02-01 01:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-19 22:08:25 +00:00
|
|
|
Ok(())
|
2020-02-01 01:02:20 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
pub fn send_all_message<Response>(
|
|
|
|
&self,
|
|
|
|
op: &UserOperation,
|
|
|
|
response: &Response,
|
|
|
|
my_id: Option<ConnectionId>,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
Response: Serialize,
|
|
|
|
{
|
|
|
|
let res_str = &to_json_string(op, response)?;
|
|
|
|
for id in self.sessions.keys() {
|
|
|
|
if let Some(my_id) = my_id {
|
|
|
|
if *id == my_id {
|
|
|
|
continue;
|
2020-02-01 01:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-19 22:08:25 +00:00
|
|
|
self.sendit(res_str, *id);
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
2020-04-19 22:08:25 +00:00
|
|
|
Ok(())
|
2020-02-01 01:02:20 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
pub fn send_user_room_message<Response>(
|
|
|
|
&self,
|
|
|
|
op: &UserOperation,
|
|
|
|
response: &Response,
|
|
|
|
recipient_id: UserId,
|
|
|
|
my_id: Option<ConnectionId>,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
Response: Serialize,
|
|
|
|
{
|
|
|
|
let res_str = &to_json_string(op, response)?;
|
|
|
|
if let Some(sessions) = self.user_rooms.get(&recipient_id) {
|
|
|
|
for id in sessions {
|
|
|
|
if let Some(my_id) = my_id {
|
|
|
|
if *id == my_id {
|
|
|
|
continue;
|
|
|
|
}
|
2020-02-01 01:02:20 +00:00
|
|
|
}
|
2020-04-19 22:08:25 +00:00
|
|
|
self.sendit(res_str, *id);
|
2020-02-01 01:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-19 22:08:25 +00:00
|
|
|
Ok(())
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
2019-05-01 22:44:39 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
pub fn send_comment(
|
2020-02-02 17:45:41 +00:00
|
|
|
&self,
|
2020-04-19 22:08:25 +00:00
|
|
|
user_operation: &UserOperation,
|
|
|
|
comment: &CommentResponse,
|
|
|
|
my_id: Option<ConnectionId>,
|
|
|
|
) -> Result<(), Error> {
|
2020-02-02 17:45:41 +00:00
|
|
|
let mut comment_reply_sent = comment.clone();
|
|
|
|
comment_reply_sent.comment.my_vote = None;
|
|
|
|
comment_reply_sent.comment.user_id = None;
|
|
|
|
|
|
|
|
let mut comment_post_sent = comment_reply_sent.clone();
|
|
|
|
comment_post_sent.recipient_ids = Vec::new();
|
|
|
|
|
|
|
|
// Send it to the post room
|
2020-04-19 22:08:25 +00:00
|
|
|
self.send_post_room_message(
|
|
|
|
user_operation,
|
|
|
|
&comment_post_sent,
|
|
|
|
comment_post_sent.comment.post_id,
|
|
|
|
my_id,
|
|
|
|
)?;
|
2020-02-02 17:45:41 +00:00
|
|
|
|
|
|
|
// Send it to the recipient(s) including the mentioned users
|
2020-04-19 22:08:25 +00:00
|
|
|
for recipient_id in &comment_reply_sent.recipient_ids {
|
|
|
|
self.send_user_room_message(user_operation, &comment_reply_sent, *recipient_id, my_id)?;
|
2020-02-02 17:45:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 04:05:15 +00:00
|
|
|
// Send it to the community too
|
2020-04-19 22:08:25 +00:00
|
|
|
self.send_community_room_message(user_operation, &comment_post_sent, 0, my_id)?;
|
|
|
|
self.send_community_room_message(
|
|
|
|
user_operation,
|
|
|
|
&comment_post_sent,
|
|
|
|
comment.comment.community_id,
|
|
|
|
my_id,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
2020-02-02 17:45:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
pub fn send_post(
|
2020-02-02 17:45:41 +00:00
|
|
|
&self,
|
2020-04-19 22:08:25 +00:00
|
|
|
user_operation: &UserOperation,
|
|
|
|
post: &PostResponse,
|
|
|
|
my_id: Option<ConnectionId>,
|
|
|
|
) -> Result<(), Error> {
|
2020-02-02 17:45:41 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
// Send it to /c/all and that community
|
2020-04-19 22:08:25 +00:00
|
|
|
self.send_community_room_message(user_operation, &post_sent, 0, my_id)?;
|
|
|
|
self.send_community_room_message(user_operation, &post_sent, community_id, my_id)?;
|
2020-02-02 17:45:41 +00:00
|
|
|
|
2020-02-09 20:04:41 +00:00
|
|
|
// Send it to the post room
|
2020-04-19 22:08:25 +00:00
|
|
|
self.send_post_room_message(user_operation, &post_sent, post.post.id, my_id)?;
|
2020-02-09 20:04:41 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
Ok(())
|
2020-02-02 17:45:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
fn sendit(&self, message: &str, id: ConnectionId) {
|
|
|
|
if let Some(info) = self.sessions.get(&id) {
|
|
|
|
let _ = info.addr.do_send(WSMessage(message.to_owned()));
|
|
|
|
}
|
2019-09-03 20:18:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
fn parse_json_message(
|
2020-02-06 21:07:59 +00:00
|
|
|
&mut self,
|
2020-04-19 22:08:25 +00:00
|
|
|
msg: StandardMessage,
|
|
|
|
ctx: &mut Context<Self>,
|
2020-04-20 03:59:07 +00:00
|
|
|
) -> impl Future<Output = Result<String, Error>> {
|
|
|
|
let addr = ctx.address();
|
|
|
|
let pool = self.pool.clone();
|
|
|
|
let rate_limiter = self.rate_limiter.clone();
|
2020-04-19 22:08:25 +00:00
|
|
|
|
|
|
|
let ip: IPAddr = match self.sessions.get(&msg.id) {
|
|
|
|
Some(info) => info.ip.to_owned(),
|
|
|
|
None => "blank_ip".to_string(),
|
|
|
|
};
|
|
|
|
|
2020-04-20 03:59:07 +00:00
|
|
|
async move {
|
|
|
|
let msg = msg;
|
|
|
|
let json: Value = serde_json::from_str(&msg.msg)?;
|
|
|
|
let data = &json["data"].to_string();
|
|
|
|
let op = &json["op"].as_str().ok_or(APIError {
|
|
|
|
message: "Unknown op type".to_string(),
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let user_operation: UserOperation = UserOperation::from_str(&op)?;
|
|
|
|
|
|
|
|
match user_operation {
|
|
|
|
// User ops
|
|
|
|
UserOperation::Login => {
|
|
|
|
do_user_operation::<Login>(pool, rate_limiter, addr, msg.id, ip, user_operation, data)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::Register => {
|
|
|
|
do_user_operation::<Register>(pool, rate_limiter, addr, msg.id, ip, user_operation, data)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::GetUserDetails => {
|
|
|
|
do_user_operation::<GetUserDetails>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::GetReplies => {
|
|
|
|
do_user_operation::<GetReplies>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::AddAdmin => {
|
|
|
|
do_user_operation::<AddAdmin>(pool, rate_limiter, addr, msg.id, ip, user_operation, data)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::BanUser => {
|
|
|
|
do_user_operation::<BanUser>(pool, rate_limiter, addr, msg.id, ip, user_operation, data)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::GetUserMentions => {
|
|
|
|
do_user_operation::<GetUserMentions>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::EditUserMention => {
|
|
|
|
do_user_operation::<EditUserMention>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::MarkAllAsRead => {
|
|
|
|
do_user_operation::<MarkAllAsRead>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::DeleteAccount => {
|
|
|
|
do_user_operation::<DeleteAccount>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::PasswordReset => {
|
|
|
|
do_user_operation::<PasswordReset>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::PasswordChange => {
|
|
|
|
do_user_operation::<PasswordChange>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::CreatePrivateMessage => {
|
|
|
|
do_user_operation::<CreatePrivateMessage>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::EditPrivateMessage => {
|
|
|
|
do_user_operation::<EditPrivateMessage>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::GetPrivateMessages => {
|
|
|
|
do_user_operation::<GetPrivateMessages>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::UserJoin => {
|
|
|
|
do_user_operation::<UserJoin>(pool, rate_limiter, addr, msg.id, ip, user_operation, data)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::SaveUserSettings => {
|
|
|
|
do_user_operation::<SaveUserSettings>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2020-04-19 22:08:25 +00:00
|
|
|
|
2020-04-20 03:59:07 +00:00
|
|
|
// Site ops
|
|
|
|
UserOperation::GetModlog => {
|
|
|
|
do_user_operation::<GetModlog>(pool, rate_limiter, addr, msg.id, ip, user_operation, data)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::CreateSite => {
|
|
|
|
do_user_operation::<CreateSite>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::EditSite => {
|
|
|
|
do_user_operation::<EditSite>(pool, rate_limiter, addr, msg.id, ip, user_operation, data)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::GetSite => {
|
|
|
|
do_user_operation::<GetSite>(pool, rate_limiter, addr, msg.id, ip, user_operation, data)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::GetSiteConfig => {
|
|
|
|
do_user_operation::<GetSiteConfig>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::SaveSiteConfig => {
|
|
|
|
do_user_operation::<SaveSiteConfig>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::Search => {
|
|
|
|
do_user_operation::<Search>(pool, rate_limiter, addr, msg.id, ip, user_operation, data)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::TransferCommunity => {
|
|
|
|
do_user_operation::<TransferCommunity>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::TransferSite => {
|
|
|
|
do_user_operation::<TransferSite>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::ListCategories => {
|
|
|
|
do_user_operation::<ListCategories>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Community ops
|
|
|
|
UserOperation::GetCommunity => {
|
|
|
|
do_user_operation::<GetCommunity>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::ListCommunities => {
|
|
|
|
do_user_operation::<ListCommunities>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::CreateCommunity => {
|
|
|
|
do_user_operation::<CreateCommunity>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::EditCommunity => {
|
|
|
|
do_user_operation::<EditCommunity>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::FollowCommunity => {
|
|
|
|
do_user_operation::<FollowCommunity>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::GetFollowedCommunities => {
|
|
|
|
do_user_operation::<GetFollowedCommunities>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::BanFromCommunity => {
|
|
|
|
do_user_operation::<BanFromCommunity>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::AddModToCommunity => {
|
|
|
|
do_user_operation::<AddModToCommunity>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post ops
|
|
|
|
UserOperation::CreatePost => {
|
|
|
|
do_user_operation::<CreatePost>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::GetPost => {
|
|
|
|
do_user_operation::<GetPost>(pool, rate_limiter, addr, msg.id, ip, user_operation, data)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::GetPosts => {
|
|
|
|
do_user_operation::<GetPosts>(pool, rate_limiter, addr, msg.id, ip, user_operation, data)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::EditPost => {
|
|
|
|
do_user_operation::<EditPost>(pool, rate_limiter, addr, msg.id, ip, user_operation, data)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::CreatePostLike => {
|
|
|
|
do_user_operation::<CreatePostLike>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::SavePost => {
|
|
|
|
do_user_operation::<SavePost>(pool, rate_limiter, addr, msg.id, ip, user_operation, data)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comment ops
|
|
|
|
UserOperation::CreateComment => {
|
|
|
|
do_user_operation::<CreateComment>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::EditComment => {
|
|
|
|
do_user_operation::<EditComment>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::SaveComment => {
|
|
|
|
do_user_operation::<SaveComment>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::GetComments => {
|
|
|
|
do_user_operation::<GetComments>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
UserOperation::CreateCommentLike => {
|
|
|
|
do_user_operation::<CreateCommentLike>(
|
|
|
|
pool,
|
|
|
|
rate_limiter,
|
|
|
|
addr,
|
|
|
|
msg.id,
|
|
|
|
ip,
|
|
|
|
user_operation,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|
2019-05-01 22:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 03:59:07 +00:00
|
|
|
async fn do_user_operation<'a, Data>(
|
|
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
|
|
rate_limiter: RateLimit,
|
|
|
|
chatserver: Addr<ChatServer>,
|
|
|
|
id: ConnectionId,
|
|
|
|
ip: IPAddr,
|
|
|
|
op: UserOperation,
|
|
|
|
data: &str,
|
|
|
|
) -> Result<String, Error>
|
|
|
|
where
|
|
|
|
for<'de> Data: Deserialize<'de> + 'a,
|
|
|
|
Oper<Data>: Perform,
|
|
|
|
{
|
|
|
|
let ws_info = WebsocketInfo {
|
|
|
|
chatserver,
|
|
|
|
id: Some(id),
|
|
|
|
};
|
|
|
|
|
|
|
|
let data = data.to_string();
|
|
|
|
let op2 = op.clone();
|
|
|
|
let fut = async move {
|
|
|
|
let parsed_data: Data = serde_json::from_str(&data)?;
|
|
|
|
let res = Oper::new(parsed_data).perform(pool, Some(ws_info))?;
|
|
|
|
to_json_string(&op, &res)
|
|
|
|
};
|
|
|
|
|
|
|
|
match op2 {
|
|
|
|
UserOperation::Register => rate_limiter.register().wrap(ip, fut).await,
|
|
|
|
UserOperation::CreatePost => rate_limiter.post().wrap(ip, fut).await,
|
2020-04-20 18:55:37 +00:00
|
|
|
UserOperation::CreateCommunity => rate_limiter.register().wrap(ip, fut).await,
|
2020-04-20 03:59:07 +00:00
|
|
|
_ => rate_limiter.message().wrap(ip, fut).await,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>();
|
2020-03-13 15:08:42 +00:00
|
|
|
info!("{} 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
|
|
|
|
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 {
|
2020-04-20 17:31:22 +00:00
|
|
|
type Result = ResponseFuture<Result<String, std::convert::Infallible>>;
|
2019-03-25 03:51:27 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
fn handle(&mut self, msg: StandardMessage, ctx: &mut Context<Self>) -> Self::Result {
|
2020-04-20 03:59:07 +00:00
|
|
|
let fut = self.parse_json_message(msg, ctx);
|
|
|
|
Box::pin(async move {
|
|
|
|
match fut.await {
|
|
|
|
Ok(m) => {
|
|
|
|
info!("Message Sent: {}", m);
|
|
|
|
Ok(m)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error during message handling {}", e);
|
|
|
|
Ok(e.to_string())
|
|
|
|
}
|
2020-03-12 11:03:04 +00:00
|
|
|
}
|
2020-04-20 03:59:07 +00:00
|
|
|
})
|
2019-04-21 07:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
impl<Response> Handler<SendAllMessage<Response>> for ChatServer
|
|
|
|
where
|
|
|
|
Response: Serialize,
|
|
|
|
{
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: SendAllMessage<Response>, _: &mut Context<Self>) {
|
|
|
|
self
|
|
|
|
.send_all_message(&msg.op, &msg.response, msg.my_id)
|
|
|
|
.unwrap();
|
|
|
|
}
|
2020-01-18 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
impl<Response> Handler<SendUserRoomMessage<Response>> for ChatServer
|
2020-01-16 14:39:08 +00:00
|
|
|
where
|
2020-04-19 22:08:25 +00:00
|
|
|
Response: Serialize,
|
2020-01-16 14:39:08 +00:00
|
|
|
{
|
2020-04-19 22:08:25 +00:00
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: SendUserRoomMessage<Response>, _: &mut Context<Self>) {
|
|
|
|
self
|
|
|
|
.send_user_room_message(&msg.op, &msg.response, msg.recipient_id, msg.my_id)
|
|
|
|
.unwrap();
|
|
|
|
}
|
2020-01-16 14:39:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
impl<Response> Handler<SendCommunityRoomMessage<Response>> for ChatServer
|
2020-01-19 13:25:50 +00:00
|
|
|
where
|
|
|
|
Response: Serialize,
|
|
|
|
{
|
2020-04-19 22:08:25 +00:00
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: SendCommunityRoomMessage<Response>, _: &mut Context<Self>) {
|
|
|
|
self
|
|
|
|
.send_community_room_message(&msg.op, &msg.response, msg.community_id, msg.my_id)
|
|
|
|
.unwrap();
|
|
|
|
}
|
2020-01-19 13:25:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
impl Handler<SendPost> for ChatServer {
|
|
|
|
type Result = ();
|
2019-04-21 07:26:26 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
fn handle(&mut self, msg: SendPost, _: &mut Context<Self>) {
|
|
|
|
self.send_post(&msg.op, &msg.post, msg.my_id).unwrap();
|
|
|
|
}
|
|
|
|
}
|
2020-01-12 15:31:51 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
impl Handler<SendComment> for ChatServer {
|
|
|
|
type Result = ();
|
2019-04-21 07:26:26 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
fn handle(&mut self, msg: SendComment, _: &mut Context<Self>) {
|
|
|
|
self.send_comment(&msg.op, &msg.comment, msg.my_id).unwrap();
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 21:07:59 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
impl Handler<JoinUserRoom> for ChatServer {
|
|
|
|
type Result = ();
|
2020-02-06 21:07:59 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
fn handle(&mut self, msg: JoinUserRoom, _: &mut Context<Self>) {
|
|
|
|
self.join_user_room(msg.user_id, msg.id);
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 01:02:20 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
impl Handler<JoinCommunityRoom> for ChatServer {
|
|
|
|
type Result = ();
|
2020-02-01 01:02:20 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
fn handle(&mut self, msg: JoinCommunityRoom, _: &mut Context<Self>) {
|
|
|
|
self.join_community_room(msg.community_id, msg.id);
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 01:02:20 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
impl Handler<JoinPostRoom> for ChatServer {
|
|
|
|
type Result = ();
|
2020-02-02 17:45:41 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
fn handle(&mut self, msg: JoinPostRoom, _: &mut Context<Self>) {
|
|
|
|
self.join_post_room(msg.post_id, msg.id);
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 01:02:20 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
impl Handler<GetUsersOnline> for ChatServer {
|
|
|
|
type Result = usize;
|
2020-02-02 17:45:41 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
fn handle(&mut self, _msg: GetUsersOnline, _: &mut Context<Self>) -> Self::Result {
|
|
|
|
self.sessions.len()
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 15:09:01 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
impl Handler<GetPostUsersOnline> for ChatServer {
|
|
|
|
type Result = usize;
|
2020-02-01 01:02:20 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
fn handle(&mut self, msg: GetPostUsersOnline, _: &mut Context<Self>) -> Self::Result {
|
|
|
|
if let Some(users) = self.post_rooms.get(&msg.post_id) {
|
|
|
|
users.len()
|
|
|
|
} else {
|
|
|
|
0
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2020-04-19 22:08:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-01 01:02:20 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
impl Handler<GetCommunityUsersOnline> for ChatServer {
|
|
|
|
type Result = usize;
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: GetCommunityUsersOnline, _: &mut Context<Self>) -> Self::Result {
|
|
|
|
if let Some(users) = self.community_rooms.get(&msg.community_id) {
|
|
|
|
users.len()
|
|
|
|
} else {
|
|
|
|
0
|
2020-02-02 17:45:41 +00:00
|
|
|
}
|
2019-04-29 16:51:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-19 22:08:25 +00:00
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct WebsocketResponse<T> {
|
|
|
|
op: String,
|
|
|
|
data: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_json_string<Response>(op: &UserOperation, data: &Response) -> Result<String, Error>
|
|
|
|
where
|
|
|
|
Response: Serialize,
|
|
|
|
{
|
|
|
|
let response = WebsocketResponse {
|
|
|
|
op: op.to_string(),
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
Ok(serde_json::to_string(&response)?)
|
|
|
|
}
|