2 chat_server::{ChatServer, SessionInfo},
5 use actix::{Actor, Context, Handler, ResponseFuture};
6 use lemmy_db_schema::naive_now;
7 use log::{error, info};
11 /// Make actor from `ChatServer`
12 impl Actor for ChatServer {
13 /// We are going to use simple Context, we just need ability to communicate
14 /// with other actors.
15 type Context = Context<Self>;
18 /// Handler for Connect message.
20 /// Register new session and assign unique id to this session
21 impl Handler<Connect> for ChatServer {
24 fn handle(&mut self, msg: Connect, _ctx: &mut Context<Self>) -> Self::Result {
25 // register session with random id
26 let id = self.rng.gen::<usize>();
27 info!("{} joined", &msg.ip);
41 /// Handler for Disconnect message.
42 impl Handler<Disconnect> for ChatServer {
45 fn handle(&mut self, msg: Disconnect, _: &mut Context<Self>) {
46 // Remove connections from sessions and all 3 scopes
47 if self.sessions.remove(&msg.id).is_some() {
48 for sessions in self.user_rooms.values_mut() {
49 sessions.remove(&msg.id);
52 for sessions in self.post_rooms.values_mut() {
53 sessions.remove(&msg.id);
56 for sessions in self.community_rooms.values_mut() {
57 sessions.remove(&msg.id);
63 /// Handler for Message message.
64 impl Handler<StandardMessage> for ChatServer {
65 type Result = ResponseFuture<Result<String, std::convert::Infallible>>;
67 fn handle(&mut self, msg: StandardMessage, ctx: &mut Context<Self>) -> Self::Result {
68 let fut = self.parse_json_message(msg, ctx);
72 // info!("Message Sent: {}", m);
76 error!("Error during message handling {}", e);
84 impl<Response> Handler<SendAllMessage<Response>> for ChatServer
90 fn handle(&mut self, msg: SendAllMessage<Response>, _: &mut Context<Self>) {
92 .send_all_message(&msg.op, &msg.response, msg.websocket_id)
97 impl<Response> Handler<SendUserRoomMessage<Response>> for ChatServer
103 fn handle(&mut self, msg: SendUserRoomMessage<Response>, _: &mut Context<Self>) {
105 .send_user_room_message(&msg.op, &msg.response, msg.recipient_id, msg.websocket_id)
110 impl<Response> Handler<SendCommunityRoomMessage<Response>> for ChatServer
116 fn handle(&mut self, msg: SendCommunityRoomMessage<Response>, _: &mut Context<Self>) {
118 .send_community_room_message(&msg.op, &msg.response, msg.community_id, msg.websocket_id)
123 impl<Response> Handler<SendModRoomMessage<Response>> for ChatServer
129 fn handle(&mut self, msg: SendModRoomMessage<Response>, _: &mut Context<Self>) {
131 .send_mod_room_message(&msg.op, &msg.response, msg.community_id, msg.websocket_id)
136 impl Handler<SendPost> for ChatServer {
139 fn handle(&mut self, msg: SendPost, _: &mut Context<Self>) {
140 self.send_post(&msg.op, &msg.post, msg.websocket_id).ok();
144 impl Handler<SendComment> for ChatServer {
147 fn handle(&mut self, msg: SendComment, _: &mut Context<Self>) {
149 .send_comment(&msg.op, &msg.comment, msg.websocket_id)
154 impl Handler<JoinUserRoom> for ChatServer {
157 fn handle(&mut self, msg: JoinUserRoom, _: &mut Context<Self>) {
158 self.join_user_room(msg.user_id, msg.id).ok();
162 impl Handler<JoinCommunityRoom> for ChatServer {
165 fn handle(&mut self, msg: JoinCommunityRoom, _: &mut Context<Self>) {
166 self.join_community_room(msg.community_id, msg.id).ok();
170 impl Handler<JoinModRoom> for ChatServer {
173 fn handle(&mut self, msg: JoinModRoom, _: &mut Context<Self>) {
174 self.join_mod_room(msg.community_id, msg.id).ok();
178 impl Handler<JoinPostRoom> for ChatServer {
181 fn handle(&mut self, msg: JoinPostRoom, _: &mut Context<Self>) {
182 self.join_post_room(msg.post_id, msg.id).ok();
186 impl Handler<GetUsersOnline> for ChatServer {
189 fn handle(&mut self, _msg: GetUsersOnline, _: &mut Context<Self>) -> Self::Result {
194 impl Handler<GetPostUsersOnline> for ChatServer {
197 fn handle(&mut self, msg: GetPostUsersOnline, _: &mut Context<Self>) -> Self::Result {
198 if let Some(users) = self.post_rooms.get(&msg.post_id) {
206 impl Handler<GetCommunityUsersOnline> for ChatServer {
209 fn handle(&mut self, msg: GetCommunityUsersOnline, _: &mut Context<Self>) -> Self::Result {
210 if let Some(users) = self.community_rooms.get(&msg.community_id) {
218 impl Handler<CaptchaItem> for ChatServer {
221 fn handle(&mut self, msg: CaptchaItem, _: &mut Context<Self>) {
222 self.captchas.push(msg);
226 impl Handler<CheckCaptcha> for ChatServer {
229 fn handle(&mut self, msg: CheckCaptcha, _: &mut Context<Self>) -> Self::Result {
230 // Remove all the ones that are past the expire time
231 self.captchas.retain(|x| x.expires.gt(&naive_now()));
236 .any(|r| r.uuid == msg.uuid && r.answer == msg.answer);
238 // Remove this uuid so it can't be re-checked (Checks only work once)
239 self.captchas.retain(|x| x.uuid != msg.uuid);