2 chat_server::{ChatServer, SessionInfo},
6 use actix::{Actor, Context, Handler, ResponseFuture};
7 use lemmy_db_schema::naive_now;
8 use lemmy_utils::ConnectionId;
9 use log::{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);
86 impl<OP, Response> Handler<SendAllMessage<OP, Response>> for ChatServer
88 OP: OperationType + ToString,
93 fn handle(&mut self, msg: SendAllMessage<OP, Response>, _: &mut Context<Self>) {
95 .send_all_message(&msg.op, &msg.response, msg.websocket_id)
100 impl<OP, Response> Handler<SendUserRoomMessage<OP, Response>> for ChatServer
102 OP: OperationType + ToString,
107 fn handle(&mut self, msg: SendUserRoomMessage<OP, Response>, _: &mut Context<Self>) {
109 .send_user_room_message(
112 msg.local_recipient_id,
119 impl<OP, Response> Handler<SendCommunityRoomMessage<OP, Response>> for ChatServer
121 OP: OperationType + ToString,
126 fn handle(&mut self, msg: SendCommunityRoomMessage<OP, Response>, _: &mut Context<Self>) {
128 .send_community_room_message(&msg.op, &msg.response, msg.community_id, msg.websocket_id)
133 impl<Response> Handler<SendModRoomMessage<Response>> for ChatServer
139 fn handle(&mut self, msg: SendModRoomMessage<Response>, _: &mut Context<Self>) {
141 .send_mod_room_message(&msg.op, &msg.response, msg.community_id, msg.websocket_id)
146 impl<OP> Handler<SendPost<OP>> for ChatServer
148 OP: OperationType + ToString,
152 fn handle(&mut self, msg: SendPost<OP>, _: &mut Context<Self>) {
153 self.send_post(&msg.op, &msg.post, msg.websocket_id).ok();
157 impl<OP> Handler<SendComment<OP>> for ChatServer
159 OP: OperationType + ToString,
163 fn handle(&mut self, msg: SendComment<OP>, _: &mut Context<Self>) {
165 .send_comment(&msg.op, &msg.comment, msg.websocket_id)
170 impl Handler<JoinUserRoom> for ChatServer {
173 fn handle(&mut self, msg: JoinUserRoom, _: &mut Context<Self>) {
174 self.join_user_room(msg.local_user_id, msg.id).ok();
178 impl Handler<JoinCommunityRoom> for ChatServer {
181 fn handle(&mut self, msg: JoinCommunityRoom, _: &mut Context<Self>) {
182 self.join_community_room(msg.community_id, msg.id).ok();
186 impl Handler<JoinModRoom> for ChatServer {
189 fn handle(&mut self, msg: JoinModRoom, _: &mut Context<Self>) {
190 self.join_mod_room(msg.community_id, msg.id).ok();
194 impl Handler<JoinPostRoom> for ChatServer {
197 fn handle(&mut self, msg: JoinPostRoom, _: &mut Context<Self>) {
198 self.join_post_room(msg.post_id, msg.id).ok();
202 impl Handler<GetUsersOnline> for ChatServer {
205 fn handle(&mut self, _msg: GetUsersOnline, _: &mut Context<Self>) -> Self::Result {
210 impl Handler<GetPostUsersOnline> for ChatServer {
213 fn handle(&mut self, msg: GetPostUsersOnline, _: &mut Context<Self>) -> Self::Result {
214 if let Some(users) = self.post_rooms.get(&msg.post_id) {
222 impl Handler<GetCommunityUsersOnline> for ChatServer {
225 fn handle(&mut self, msg: GetCommunityUsersOnline, _: &mut Context<Self>) -> Self::Result {
226 if let Some(users) = self.community_rooms.get(&msg.community_id) {
234 impl Handler<CaptchaItem> for ChatServer {
237 fn handle(&mut self, msg: CaptchaItem, _: &mut Context<Self>) {
238 self.captchas.push(msg);
242 impl Handler<CheckCaptcha> for ChatServer {
245 fn handle(&mut self, msg: CheckCaptcha, _: &mut Context<Self>) -> Self::Result {
246 // Remove all the ones that are past the expire time
247 self.captchas.retain(|x| x.expires.gt(&naive_now()));
252 .any(|r| r.uuid == msg.uuid && r.answer == msg.answer);
254 // Remove this uuid so it can't be re-checked (Checks only work once)
255 self.captchas.retain(|x| x.uuid != msg.uuid);