2 chat_server::{ChatServer, SessionInfo},
5 use actix::{Actor, Context, Handler, ResponseFuture};
6 use lemmy_db_schema::naive_now;
7 use lemmy_utils::ConnectionId;
8 use log::{error, info};
12 /// Make actor from `ChatServer`
13 impl Actor for ChatServer {
14 /// We are going to use simple Context, we just need ability to communicate
15 /// with other actors.
16 type Context = Context<Self>;
19 /// Handler for Connect message.
21 /// Register new session and assign unique id to this session
22 impl Handler<Connect> for ChatServer {
23 type Result = ConnectionId;
25 fn handle(&mut self, msg: Connect, _ctx: &mut Context<Self>) -> Self::Result {
26 // register session with random id
27 let id = self.rng.gen::<usize>();
28 info!("{} joined", &msg.ip);
42 /// Handler for Disconnect message.
43 impl Handler<Disconnect> for ChatServer {
46 fn handle(&mut self, msg: Disconnect, _: &mut Context<Self>) {
47 // Remove connections from sessions and all 3 scopes
48 if self.sessions.remove(&msg.id).is_some() {
49 for sessions in self.user_rooms.values_mut() {
50 sessions.remove(&msg.id);
53 for sessions in self.post_rooms.values_mut() {
54 sessions.remove(&msg.id);
57 for sessions in self.community_rooms.values_mut() {
58 sessions.remove(&msg.id);
64 /// Handler for Message message.
65 impl Handler<StandardMessage> for ChatServer {
66 type Result = ResponseFuture<Result<String, std::convert::Infallible>>;
68 fn handle(&mut self, msg: StandardMessage, ctx: &mut Context<Self>) -> Self::Result {
69 let fut = self.parse_json_message(msg, ctx);
73 // info!("Message Sent: {}", m);
77 error!("Error during message handling {}", e);
85 impl<Response> Handler<SendAllMessage<Response>> for ChatServer
91 fn handle(&mut self, msg: SendAllMessage<Response>, _: &mut Context<Self>) {
93 .send_all_message(&msg.op, &msg.response, msg.websocket_id)
98 impl<Response> Handler<SendUserRoomMessage<Response>> for ChatServer
104 fn handle(&mut self, msg: SendUserRoomMessage<Response>, _: &mut Context<Self>) {
106 .send_user_room_message(
109 msg.local_recipient_id,
116 impl<Response> Handler<SendCommunityRoomMessage<Response>> for ChatServer
122 fn handle(&mut self, msg: SendCommunityRoomMessage<Response>, _: &mut Context<Self>) {
124 .send_community_room_message(&msg.op, &msg.response, msg.community_id, msg.websocket_id)
129 impl<Response> Handler<SendModRoomMessage<Response>> for ChatServer
135 fn handle(&mut self, msg: SendModRoomMessage<Response>, _: &mut Context<Self>) {
137 .send_mod_room_message(&msg.op, &msg.response, msg.community_id, msg.websocket_id)
142 impl Handler<SendPost> for ChatServer {
145 fn handle(&mut self, msg: SendPost, _: &mut Context<Self>) {
146 self.send_post(&msg.op, &msg.post, msg.websocket_id).ok();
150 impl Handler<SendComment> for ChatServer {
153 fn handle(&mut self, msg: SendComment, _: &mut Context<Self>) {
155 .send_comment(&msg.op, &msg.comment, msg.websocket_id)
160 impl Handler<JoinUserRoom> for ChatServer {
163 fn handle(&mut self, msg: JoinUserRoom, _: &mut Context<Self>) {
164 self.join_user_room(msg.local_user_id, msg.id).ok();
168 impl Handler<JoinCommunityRoom> for ChatServer {
171 fn handle(&mut self, msg: JoinCommunityRoom, _: &mut Context<Self>) {
172 self.join_community_room(msg.community_id, msg.id).ok();
176 impl Handler<JoinModRoom> for ChatServer {
179 fn handle(&mut self, msg: JoinModRoom, _: &mut Context<Self>) {
180 self.join_mod_room(msg.community_id, msg.id).ok();
184 impl Handler<JoinPostRoom> for ChatServer {
187 fn handle(&mut self, msg: JoinPostRoom, _: &mut Context<Self>) {
188 self.join_post_room(msg.post_id, msg.id).ok();
192 impl Handler<GetUsersOnline> for ChatServer {
195 fn handle(&mut self, _msg: GetUsersOnline, _: &mut Context<Self>) -> Self::Result {
200 impl Handler<GetPostUsersOnline> for ChatServer {
203 fn handle(&mut self, msg: GetPostUsersOnline, _: &mut Context<Self>) -> Self::Result {
204 if let Some(users) = self.post_rooms.get(&msg.post_id) {
212 impl Handler<GetCommunityUsersOnline> for ChatServer {
215 fn handle(&mut self, msg: GetCommunityUsersOnline, _: &mut Context<Self>) -> Self::Result {
216 if let Some(users) = self.community_rooms.get(&msg.community_id) {
224 impl Handler<CaptchaItem> for ChatServer {
227 fn handle(&mut self, msg: CaptchaItem, _: &mut Context<Self>) {
228 self.captchas.push(msg);
232 impl Handler<CheckCaptcha> for ChatServer {
235 fn handle(&mut self, msg: CheckCaptcha, _: &mut Context<Self>) -> Self::Result {
236 // Remove all the ones that are past the expire time
237 self.captchas.retain(|x| x.expires.gt(&naive_now()));
242 .any(|r| r.uuid == msg.uuid && r.answer == msg.answer);
244 // Remove this uuid so it can't be re-checked (Checks only work once)
245 self.captchas.retain(|x| x.uuid != msg.uuid);