2 chat_server::{ChatServer, SessionInfo},
6 use actix::{Actor, Context, Handler, ResponseFuture};
7 use lemmy_db_schema::naive_now;
8 use lemmy_utils::ConnectionId;
11 use tracing::{error, info};
13 /// Make actor from `ChatServer`
14 impl Actor for ChatServer {
15 /// We are going to use simple Context, we just need ability to communicate
16 /// with other actors.
17 type Context = Context<Self>;
20 /// Handler for Connect message.
22 /// Register new session and assign unique id to this session
23 impl Handler<Connect> for ChatServer {
24 type Result = ConnectionId;
26 fn handle(&mut self, msg: Connect, _ctx: &mut Context<Self>) -> Self::Result {
27 // register session with random id
28 let id = self.rng.gen::<usize>();
29 info!("{} joined", &msg.ip);
43 /// Handler for Disconnect message.
44 impl Handler<Disconnect> for ChatServer {
47 fn handle(&mut self, msg: Disconnect, _: &mut Context<Self>) {
48 // Remove connections from sessions and all 3 scopes
49 if self.sessions.remove(&msg.id).is_some() {
50 for sessions in self.user_rooms.values_mut() {
51 sessions.remove(&msg.id);
54 for sessions in self.post_rooms.values_mut() {
55 sessions.remove(&msg.id);
58 for sessions in self.community_rooms.values_mut() {
59 sessions.remove(&msg.id);
65 /// Handler for Message message.
66 impl Handler<StandardMessage> for ChatServer {
67 type Result = ResponseFuture<Result<String, std::convert::Infallible>>;
69 fn handle(&mut self, msg: StandardMessage, ctx: &mut Context<Self>) -> Self::Result {
70 let fut = self.parse_json_message(msg, ctx);
74 // info!("Message Sent: {}", m);
78 error!("Error during message handling {}", e);
81 .unwrap_or_else(|_| String::from(r#"{"error":"failed to serialize json"}"#)),
89 impl<OP, Response> Handler<SendAllMessage<OP, Response>> for ChatServer
91 OP: OperationType + ToString,
96 fn handle(&mut self, msg: SendAllMessage<OP, Response>, _: &mut Context<Self>) {
98 .send_all_message(&msg.op, &msg.response, msg.websocket_id)
103 impl<OP, Response> Handler<SendUserRoomMessage<OP, Response>> for ChatServer
105 OP: OperationType + ToString,
110 fn handle(&mut self, msg: SendUserRoomMessage<OP, Response>, _: &mut Context<Self>) {
112 .send_user_room_message(
115 msg.local_recipient_id,
122 impl<OP, Response> Handler<SendCommunityRoomMessage<OP, Response>> for ChatServer
124 OP: OperationType + ToString,
129 fn handle(&mut self, msg: SendCommunityRoomMessage<OP, Response>, _: &mut Context<Self>) {
131 .send_community_room_message(&msg.op, &msg.response, msg.community_id, msg.websocket_id)
136 impl<Response> Handler<SendModRoomMessage<Response>> for ChatServer
142 fn handle(&mut self, msg: SendModRoomMessage<Response>, _: &mut Context<Self>) {
144 .send_mod_room_message(&msg.op, &msg.response, msg.community_id, msg.websocket_id)
149 impl<OP> Handler<SendPost<OP>> for ChatServer
151 OP: OperationType + ToString,
155 fn handle(&mut self, msg: SendPost<OP>, _: &mut Context<Self>) {
156 self.send_post(&msg.op, &msg.post, msg.websocket_id).ok();
160 impl<OP> Handler<SendComment<OP>> for ChatServer
162 OP: OperationType + ToString,
166 fn handle(&mut self, msg: SendComment<OP>, _: &mut Context<Self>) {
168 .send_comment(&msg.op, &msg.comment, msg.websocket_id)
173 impl Handler<JoinUserRoom> for ChatServer {
176 fn handle(&mut self, msg: JoinUserRoom, _: &mut Context<Self>) {
177 self.join_user_room(msg.local_user_id, msg.id).ok();
181 impl Handler<JoinCommunityRoom> for ChatServer {
184 fn handle(&mut self, msg: JoinCommunityRoom, _: &mut Context<Self>) {
185 self.join_community_room(msg.community_id, msg.id).ok();
189 impl Handler<JoinModRoom> for ChatServer {
192 fn handle(&mut self, msg: JoinModRoom, _: &mut Context<Self>) {
193 self.join_mod_room(msg.community_id, msg.id).ok();
197 impl Handler<JoinPostRoom> for ChatServer {
200 fn handle(&mut self, msg: JoinPostRoom, _: &mut Context<Self>) {
201 self.join_post_room(msg.post_id, msg.id).ok();
205 impl Handler<GetUsersOnline> for ChatServer {
208 fn handle(&mut self, _msg: GetUsersOnline, _: &mut Context<Self>) -> Self::Result {
213 impl Handler<GetPostUsersOnline> for ChatServer {
216 fn handle(&mut self, msg: GetPostUsersOnline, _: &mut Context<Self>) -> Self::Result {
217 if let Some(users) = self.post_rooms.get(&msg.post_id) {
225 impl Handler<GetCommunityUsersOnline> for ChatServer {
228 fn handle(&mut self, msg: GetCommunityUsersOnline, _: &mut Context<Self>) -> Self::Result {
229 if let Some(users) = self.community_rooms.get(&msg.community_id) {
237 impl Handler<CaptchaItem> for ChatServer {
240 fn handle(&mut self, msg: CaptchaItem, _: &mut Context<Self>) {
241 self.captchas.push(msg);
245 impl Handler<CheckCaptcha> for ChatServer {
248 fn handle(&mut self, msg: CheckCaptcha, _: &mut Context<Self>) -> Self::Result {
249 // Remove all the ones that are past the expire time
250 self.captchas.retain(|x| x.expires.gt(&naive_now()));
255 .any(|r| r.uuid == msg.uuid && r.answer.to_lowercase() == msg.answer.to_lowercase());
257 // Remove this uuid so it can't be re-checked (Checks only work once)
258 self.captchas.retain(|x| x.uuid != msg.uuid);