2021-03-25 19:30:15 +00:00
|
|
|
use crate::{
|
|
|
|
messages::*,
|
|
|
|
serialize_websocket_message,
|
|
|
|
LemmyContext,
|
|
|
|
OperationType,
|
|
|
|
UserOperation,
|
|
|
|
UserOperationCrud,
|
|
|
|
};
|
2020-09-01 13:20:22 +00:00
|
|
|
use actix::prelude::*;
|
2020-08-13 15:46:31 +00:00
|
|
|
use anyhow::Context as acontext;
|
2020-08-31 13:48:02 +00:00
|
|
|
use background_jobs::QueueHandle;
|
2020-09-01 13:20:22 +00:00
|
|
|
use diesel::{
|
|
|
|
r2d2::{ConnectionManager, Pool},
|
|
|
|
PgConnection,
|
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::{comment::*, post::*};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
newtypes::{CommunityId, LocalUserId, PostId},
|
|
|
|
source::secret::Secret,
|
|
|
|
};
|
2020-09-03 19:45:12 +00:00
|
|
|
use lemmy_utils::{
|
|
|
|
location_info,
|
2020-12-21 23:27:42 +00:00
|
|
|
rate_limit::RateLimit,
|
2021-09-22 15:57:09 +00:00
|
|
|
settings::structs::Settings,
|
2020-09-03 19:45:12 +00:00
|
|
|
ConnectionId,
|
2021-02-22 18:04:32 +00:00
|
|
|
IpAddr,
|
2020-09-03 19:45:12 +00:00
|
|
|
LemmyError,
|
|
|
|
};
|
2020-09-01 13:20:22 +00:00
|
|
|
use rand::rngs::ThreadRng;
|
2021-12-06 22:54:34 +00:00
|
|
|
use reqwest_middleware::ClientWithMiddleware;
|
2020-09-01 13:20:22 +00:00
|
|
|
use serde::Serialize;
|
|
|
|
use serde_json::Value;
|
|
|
|
use std::{
|
|
|
|
collections::{HashMap, HashSet},
|
2021-07-06 13:26:46 +00:00
|
|
|
future::Future,
|
2020-09-01 13:20:22 +00:00
|
|
|
str::FromStr,
|
|
|
|
};
|
2020-09-24 13:53:21 +00:00
|
|
|
use tokio::macros::support::Pin;
|
|
|
|
|
|
|
|
type MessageHandlerType = fn(
|
|
|
|
context: LemmyContext,
|
|
|
|
id: ConnectionId,
|
|
|
|
op: UserOperation,
|
|
|
|
data: &str,
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<String, LemmyError>> + '_>>;
|
2020-02-01 01:02:20 +00:00
|
|
|
|
2021-03-25 19:30:15 +00:00
|
|
|
type MessageHandlerCrudType = fn(
|
|
|
|
context: LemmyContext,
|
|
|
|
id: ConnectionId,
|
|
|
|
op: UserOperationCrud,
|
|
|
|
data: &str,
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<String, LemmyError>> + '_>>;
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2020-08-31 15:20:13 +00:00
|
|
|
pub(super) rng: ThreadRng,
|
2020-04-19 22:08:25 +00:00
|
|
|
|
|
|
|
/// The DB Pool
|
2020-08-31 15:20:13 +00:00
|
|
|
pub(super) pool: Pool<ConnectionManager<PgConnection>>,
|
2020-04-19 22:08:25 +00:00
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
/// The Settings
|
|
|
|
pub(super) settings: Settings,
|
|
|
|
|
|
|
|
/// The Secrets
|
|
|
|
pub(super) secret: Secret,
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
/// Rate limiting based on rate type and IP addr
|
2020-08-31 15:20:13 +00:00
|
|
|
pub(super) rate_limiter: RateLimit,
|
2020-07-01 12:54:29 +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>,
|
2020-07-29 13:02:46 +00:00
|
|
|
|
2020-09-24 13:53:21 +00:00
|
|
|
message_handler: MessageHandlerType,
|
2021-03-25 19:30:15 +00:00
|
|
|
message_handler_crud: MessageHandlerCrudType,
|
2020-09-24 13:53:21 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
/// An HTTP Client
|
2021-12-06 22:54:34 +00:00
|
|
|
client: ClientWithMiddleware,
|
2020-08-31 13:48:02 +00:00
|
|
|
|
|
|
|
activity_queue: QueueHandle,
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 15:20:13 +00:00
|
|
|
pub struct SessionInfo {
|
2021-02-22 18:04:32 +00:00
|
|
|
pub addr: Recipient<WsMessage>,
|
|
|
|
pub ip: IpAddr,
|
2020-08-31 15:20:13 +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-01-12 15:31:51 +00:00
|
|
|
impl ChatServer {
|
2021-09-22 15:57:09 +00:00
|
|
|
#![allow(clippy::too_many_arguments)]
|
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-09-24 13:53:21 +00:00
|
|
|
message_handler: MessageHandlerType,
|
2021-03-25 19:30:15 +00:00
|
|
|
message_handler_crud: MessageHandlerCrudType,
|
2021-12-06 22:54:34 +00:00
|
|
|
client: ClientWithMiddleware,
|
2020-08-31 13:48:02 +00:00
|
|
|
activity_queue: QueueHandle,
|
2021-09-22 15:57:09 +00:00
|
|
|
settings: Settings,
|
|
|
|
secret: Secret,
|
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(),
|
2020-10-27 00:25:18 +00:00
|
|
|
mod_rooms: HashMap::new(),
|
2020-02-01 01:02:20 +00:00
|
|
|
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,
|
2020-07-29 13:02:46 +00:00
|
|
|
captchas: Vec::new(),
|
2020-09-24 13:53:21 +00:00
|
|
|
message_handler,
|
2021-03-25 19:30:15 +00:00
|
|
|
message_handler_crud,
|
2020-07-01 12:54:29 +00:00
|
|
|
client,
|
2020-08-31 13:48:02 +00:00
|
|
|
activity_queue,
|
2021-09-22 15:57:09 +00:00
|
|
|
settings,
|
|
|
|
secret,
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 15:46:31 +00:00
|
|
|
pub fn join_community_room(
|
|
|
|
&mut self,
|
|
|
|
community_id: CommunityId,
|
|
|
|
id: ConnectionId,
|
|
|
|
) -> Result<(), LemmyError> {
|
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)
|
2020-08-13 15:46:31 +00:00
|
|
|
.context(location_info!())?
|
2020-02-01 01:02:20 +00:00
|
|
|
.insert(id);
|
2020-08-13 15:46:31 +00:00
|
|
|
Ok(())
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
|
2020-10-27 00:25:18 +00:00
|
|
|
pub fn join_mod_room(
|
|
|
|
&mut self,
|
|
|
|
community_id: CommunityId,
|
|
|
|
id: ConnectionId,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
// remove session from all rooms
|
|
|
|
for sessions in self.mod_rooms.values_mut() {
|
|
|
|
sessions.remove(&id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the room doesn't exist yet
|
|
|
|
if self.mod_rooms.get_mut(&community_id).is_none() {
|
|
|
|
self.mod_rooms.insert(community_id, HashSet::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
.mod_rooms
|
|
|
|
.get_mut(&community_id)
|
|
|
|
.context(location_info!())?
|
|
|
|
.insert(id);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-13 15:46:31 +00:00
|
|
|
pub fn join_post_room(&mut self, post_id: PostId, id: ConnectionId) -> Result<(), LemmyError> {
|
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
|
2020-07-20 17:37:39 +00:00
|
|
|
// TODO found a bug, whereby community messages like
|
|
|
|
// delete and remove aren't sent, because
|
|
|
|
// you left the community room
|
2020-02-08 04:05:15 +00:00
|
|
|
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-08-13 15:46:31 +00:00
|
|
|
self
|
|
|
|
.post_rooms
|
|
|
|
.get_mut(&post_id)
|
|
|
|
.context(location_info!())?
|
|
|
|
.insert(id);
|
|
|
|
|
|
|
|
Ok(())
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 15:54:47 +00:00
|
|
|
pub fn join_user_room(
|
|
|
|
&mut self,
|
|
|
|
user_id: LocalUserId,
|
|
|
|
id: ConnectionId,
|
|
|
|
) -> Result<(), LemmyError> {
|
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-08-13 15:46:31 +00:00
|
|
|
self
|
|
|
|
.user_rooms
|
|
|
|
.get_mut(&user_id)
|
|
|
|
.context(location_info!())?
|
|
|
|
.insert(id);
|
|
|
|
|
|
|
|
Ok(())
|
2020-02-01 01:02:20 +00:00
|
|
|
}
|
2019-12-07 12:03:03 +00:00
|
|
|
|
2021-03-25 19:30:15 +00:00
|
|
|
fn send_post_room_message<OP, Response>(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2021-03-25 19:30:15 +00:00
|
|
|
op: &OP,
|
2020-04-19 22:08:25 +00:00
|
|
|
response: &Response,
|
|
|
|
post_id: PostId,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<(), LemmyError>
|
2020-04-19 22:08:25 +00:00
|
|
|
where
|
2021-03-25 19:30:15 +00:00
|
|
|
OP: OperationType + ToString,
|
2020-04-19 22:08:25 +00:00
|
|
|
Response: Serialize,
|
|
|
|
{
|
2020-09-24 13:53:21 +00:00
|
|
|
let res_str = &serialize_websocket_message(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-08-24 11:58:24 +00:00
|
|
|
if let Some(my_id) = websocket_id {
|
2020-04-19 22:08:25 +00:00
|
|
|
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
|
|
|
|
2021-03-25 19:30:15 +00:00
|
|
|
pub fn send_community_room_message<OP, Response>(
|
2020-02-01 01:02:20 +00:00
|
|
|
&self,
|
2021-03-25 19:30:15 +00:00
|
|
|
op: &OP,
|
2020-04-19 22:08:25 +00:00
|
|
|
response: &Response,
|
2020-02-01 01:02:20 +00:00
|
|
|
community_id: CommunityId,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<(), LemmyError>
|
2020-04-19 22:08:25 +00:00
|
|
|
where
|
2021-03-25 19:30:15 +00:00
|
|
|
OP: OperationType + ToString,
|
2020-04-19 22:08:25 +00:00
|
|
|
Response: Serialize,
|
|
|
|
{
|
2020-09-24 13:53:21 +00:00
|
|
|
let res_str = &serialize_websocket_message(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-08-24 11:58:24 +00:00
|
|
|
if let Some(my_id) = websocket_id {
|
2020-04-19 22:08:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-25 19:30:15 +00:00
|
|
|
pub fn send_mod_room_message<OP, Response>(
|
2020-10-27 00:25:18 +00:00
|
|
|
&self,
|
2021-03-25 19:30:15 +00:00
|
|
|
op: &OP,
|
2020-10-27 00:25:18 +00:00
|
|
|
response: &Response,
|
|
|
|
community_id: CommunityId,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<(), LemmyError>
|
2020-11-04 02:15:11 +00:00
|
|
|
where
|
2021-03-25 19:30:15 +00:00
|
|
|
OP: OperationType + ToString,
|
2020-11-04 02:15:11 +00:00
|
|
|
Response: Serialize,
|
2020-10-27 00:25:18 +00:00
|
|
|
{
|
|
|
|
let res_str = &serialize_websocket_message(op, response)?;
|
|
|
|
if let Some(sessions) = self.mod_rooms.get(&community_id) {
|
|
|
|
for id in sessions {
|
|
|
|
if let Some(my_id) = websocket_id {
|
|
|
|
if *id == my_id {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.sendit(res_str, *id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-03-25 19:30:15 +00:00
|
|
|
pub fn send_all_message<OP, Response>(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2021-03-25 19:30:15 +00:00
|
|
|
op: &OP,
|
2020-04-19 22:08:25 +00:00
|
|
|
response: &Response,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<(), LemmyError>
|
2020-04-19 22:08:25 +00:00
|
|
|
where
|
2021-03-25 19:30:15 +00:00
|
|
|
OP: OperationType + ToString,
|
2020-04-19 22:08:25 +00:00
|
|
|
Response: Serialize,
|
|
|
|
{
|
2020-09-24 13:53:21 +00:00
|
|
|
let res_str = &serialize_websocket_message(op, response)?;
|
2020-04-19 22:08:25 +00:00
|
|
|
for id in self.sessions.keys() {
|
2020-08-24 11:58:24 +00:00
|
|
|
if let Some(my_id) = websocket_id {
|
2020-04-19 22:08:25 +00:00
|
|
|
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
|
|
|
|
2021-03-25 19:30:15 +00:00
|
|
|
pub fn send_user_room_message<OP, Response>(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2021-03-25 19:30:15 +00:00
|
|
|
op: &OP,
|
2020-04-19 22:08:25 +00:00
|
|
|
response: &Response,
|
2021-03-12 15:54:47 +00:00
|
|
|
recipient_id: LocalUserId,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<(), LemmyError>
|
2020-04-19 22:08:25 +00:00
|
|
|
where
|
2021-03-25 19:30:15 +00:00
|
|
|
OP: OperationType + ToString,
|
2020-04-19 22:08:25 +00:00
|
|
|
Response: Serialize,
|
|
|
|
{
|
2020-09-24 13:53:21 +00:00
|
|
|
let res_str = &serialize_websocket_message(op, response)?;
|
2020-04-19 22:08:25 +00:00
|
|
|
if let Some(sessions) = self.user_rooms.get(&recipient_id) {
|
|
|
|
for id in sessions {
|
2020-08-24 11:58:24 +00:00
|
|
|
if let Some(my_id) = websocket_id {
|
2020-04-19 22:08:25 +00:00
|
|
|
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
|
|
|
|
2021-03-25 19:30:15 +00:00
|
|
|
pub fn send_comment<OP>(
|
2020-02-02 17:45:41 +00:00
|
|
|
&self,
|
2021-03-25 19:30:15 +00:00
|
|
|
user_operation: &OP,
|
2020-04-19 22:08:25 +00:00
|
|
|
comment: &CommentResponse,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2021-03-25 19:30:15 +00:00
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
|
|
|
OP: OperationType + ToString,
|
|
|
|
{
|
2021-01-07 06:17:42 +00:00
|
|
|
let mut comment_reply_sent = comment.clone();
|
2020-02-02 17:45:41 +00:00
|
|
|
|
2021-01-07 06:17:42 +00:00
|
|
|
// Strip out my specific user info
|
|
|
|
comment_reply_sent.comment_view.my_vote = None;
|
2020-02-02 17:45:41 +00:00
|
|
|
|
|
|
|
// Send it to the post room
|
2021-01-07 06:17:42 +00:00
|
|
|
let mut comment_post_sent = comment_reply_sent.clone();
|
2021-01-13 17:01:42 +00:00
|
|
|
// Remove the recipients here to separate mentions / user messages from post or community comments
|
2021-01-07 06:17:42 +00:00
|
|
|
comment_post_sent.recipient_ids = Vec::new();
|
2020-04-19 22:08:25 +00:00
|
|
|
self.send_post_room_message(
|
|
|
|
user_operation,
|
|
|
|
&comment_post_sent,
|
2020-12-15 19:39:18 +00:00
|
|
|
comment_post_sent.comment_view.post.id,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id,
|
2020-04-19 22:08:25 +00:00
|
|
|
)?;
|
2020-02-02 17:45:41 +00:00
|
|
|
|
2021-01-07 06:17:42 +00:00
|
|
|
// Send it to the community too
|
2021-03-18 20:25:21 +00:00
|
|
|
self.send_community_room_message(
|
|
|
|
user_operation,
|
|
|
|
&comment_post_sent,
|
|
|
|
CommunityId(0),
|
|
|
|
websocket_id,
|
|
|
|
)?;
|
2021-01-07 06:17:42 +00:00
|
|
|
self.send_community_room_message(
|
|
|
|
user_operation,
|
|
|
|
&comment_post_sent,
|
|
|
|
comment.comment_view.community.id,
|
|
|
|
websocket_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 {
|
2020-08-24 11:58:24 +00:00
|
|
|
self.send_user_room_message(
|
|
|
|
user_operation,
|
|
|
|
&comment_reply_sent,
|
|
|
|
*recipient_id,
|
|
|
|
websocket_id,
|
|
|
|
)?;
|
2020-02-02 17:45:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
Ok(())
|
2020-02-02 17:45:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 19:30:15 +00:00
|
|
|
pub fn send_post<OP>(
|
2020-02-02 17:45:41 +00:00
|
|
|
&self,
|
2021-03-25 19:30:15 +00:00
|
|
|
user_operation: &OP,
|
2020-12-11 15:27:33 +00:00
|
|
|
post_res: &PostResponse,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2021-03-25 19:30:15 +00:00
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
|
|
|
OP: OperationType + ToString,
|
|
|
|
{
|
2020-12-11 15:27:33 +00:00
|
|
|
let community_id = post_res.post_view.community.id;
|
2020-02-02 17:45:41 +00:00
|
|
|
|
|
|
|
// Don't send my data with it
|
2021-01-07 06:17:42 +00:00
|
|
|
let mut post_sent = post_res.clone();
|
|
|
|
post_sent.post_view.my_vote = None;
|
2020-02-02 17:45:41 +00:00
|
|
|
|
|
|
|
// Send it to /c/all and that community
|
2021-03-18 20:25:21 +00:00
|
|
|
self.send_community_room_message(user_operation, &post_sent, CommunityId(0), websocket_id)?;
|
2021-01-07 06:17:42 +00:00
|
|
|
self.send_community_room_message(user_operation, &post_sent, community_id, websocket_id)?;
|
2020-02-02 17:45:41 +00:00
|
|
|
|
2020-02-09 20:04:41 +00:00
|
|
|
// Send it to the post room
|
2020-12-11 15:27:33 +00:00
|
|
|
self.send_post_room_message(
|
|
|
|
user_operation,
|
2021-01-07 06:17:42 +00:00
|
|
|
&post_sent,
|
2020-12-11 15:27:33 +00:00
|
|
|
post_res.post_view.post.id,
|
|
|
|
websocket_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) {
|
2021-02-22 18:04:32 +00:00
|
|
|
let _ = info.addr.do_send(WsMessage(message.to_owned()));
|
2020-04-19 22:08:25 +00:00
|
|
|
}
|
2019-09-03 20:18:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 15:20:13 +00:00
|
|
|
pub(super) 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-07-01 12:54:29 +00:00
|
|
|
) -> impl Future<Output = Result<String, LemmyError>> {
|
2020-04-20 03:59:07 +00:00
|
|
|
let rate_limiter = self.rate_limiter.clone();
|
2020-04-19 22:08:25 +00:00
|
|
|
|
2021-02-22 18:04:32 +00:00
|
|
|
let ip: IpAddr = match self.sessions.get(&msg.id) {
|
2020-04-19 22:08:25 +00:00
|
|
|
Some(info) => info.ip.to_owned(),
|
2021-03-18 20:25:21 +00:00
|
|
|
None => IpAddr("blank_ip".to_string()),
|
2020-04-19 22:08:25 +00:00
|
|
|
};
|
|
|
|
|
2020-09-24 13:53:21 +00:00
|
|
|
let context = LemmyContext {
|
|
|
|
pool: self.pool.clone(),
|
|
|
|
chat_server: ctx.address(),
|
|
|
|
client: self.client.to_owned(),
|
|
|
|
activity_queue: self.activity_queue.to_owned(),
|
2021-09-22 15:57:09 +00:00
|
|
|
settings: self.settings.to_owned(),
|
|
|
|
secret: self.secret.to_owned(),
|
2020-09-24 13:53:21 +00:00
|
|
|
};
|
2021-03-25 19:30:15 +00:00
|
|
|
let message_handler_crud = self.message_handler_crud;
|
2020-09-24 13:53:21 +00:00
|
|
|
let message_handler = self.message_handler;
|
2020-04-20 03:59:07 +00:00
|
|
|
async move {
|
|
|
|
let json: Value = serde_json::from_str(&msg.msg)?;
|
|
|
|
let data = &json["data"].to_string();
|
2021-10-13 19:50:21 +00:00
|
|
|
let op = &json["op"]
|
|
|
|
.as_str()
|
2021-12-06 14:54:47 +00:00
|
|
|
.ok_or_else(|| LemmyError::from_message("missing op"))?;
|
2020-04-20 03:59:07 +00:00
|
|
|
|
2022-03-25 15:41:38 +00:00
|
|
|
// check if api call passes the rate limit, and generate future for later execution
|
|
|
|
let (passed, fut) = if let Ok(user_operation_crud) = UserOperationCrud::from_str(op) {
|
|
|
|
let passed = match user_operation_crud {
|
2022-03-27 00:29:05 +00:00
|
|
|
UserOperationCrud::Register => rate_limiter.register().check(ip),
|
|
|
|
UserOperationCrud::CreatePost => rate_limiter.post().check(ip),
|
|
|
|
UserOperationCrud::CreateCommunity => rate_limiter.register().check(ip),
|
|
|
|
UserOperationCrud::CreateComment => rate_limiter.comment().check(ip),
|
2022-03-28 20:31:35 +00:00
|
|
|
_ => true,
|
2022-03-25 15:41:38 +00:00
|
|
|
};
|
|
|
|
let fut = (message_handler_crud)(context, msg.id, user_operation_crud, data);
|
|
|
|
(passed, fut)
|
2021-03-25 19:30:15 +00:00
|
|
|
} else {
|
2021-07-05 16:07:26 +00:00
|
|
|
let user_operation = UserOperation::from_str(op)?;
|
2022-03-25 15:41:38 +00:00
|
|
|
let passed = match user_operation {
|
2022-03-27 00:29:05 +00:00
|
|
|
UserOperation::GetCaptcha => rate_limiter.post().check(ip),
|
2022-03-29 15:46:03 +00:00
|
|
|
UserOperation::Search => rate_limiter.search().check(ip),
|
2022-03-28 20:31:35 +00:00
|
|
|
_ => true,
|
2022-03-25 15:41:38 +00:00
|
|
|
};
|
|
|
|
let fut = (message_handler)(context, msg.id, user_operation, data);
|
|
|
|
(passed, fut)
|
|
|
|
};
|
|
|
|
|
|
|
|
// if rate limit passed, execute api call future
|
|
|
|
if passed {
|
|
|
|
fut.await
|
|
|
|
} else {
|
|
|
|
// if rate limit was hit, respond with empty message
|
|
|
|
Ok("".to_string())
|
2020-04-20 03:59:07 +00:00
|
|
|
}
|
2019-05-01 22:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|