]> Untitled Git - lemmy.git/blob - crates/api/src/websocket.rs
Check user accepted before sending jwt in password reset (fixes #2591) (#2597)
[lemmy.git] / crates / api / src / websocket.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   utils::get_local_user_view_from_jwt,
5   websocket::{
6     CommunityJoin,
7     CommunityJoinResponse,
8     ModJoin,
9     ModJoinResponse,
10     PostJoin,
11     PostJoinResponse,
12     UserJoin,
13     UserJoinResponse,
14   },
15 };
16 use lemmy_utils::{error::LemmyError, ConnectionId};
17 use lemmy_websocket::{
18   messages::{JoinCommunityRoom, JoinModRoom, JoinPostRoom, JoinUserRoom},
19   LemmyContext,
20 };
21
22 #[async_trait::async_trait(?Send)]
23 impl Perform for UserJoin {
24   type Response = UserJoinResponse;
25
26   #[tracing::instrument(skip(context, websocket_id))]
27   async fn perform(
28     &self,
29     context: &Data<LemmyContext>,
30     websocket_id: Option<ConnectionId>,
31   ) -> Result<UserJoinResponse, LemmyError> {
32     let data: &UserJoin = self;
33     let local_user_view =
34       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
35
36     if let Some(ws_id) = websocket_id {
37       context.chat_server().do_send(JoinUserRoom {
38         local_user_id: local_user_view.local_user.id,
39         id: ws_id,
40       });
41     }
42
43     Ok(UserJoinResponse { joined: true })
44   }
45 }
46
47 #[async_trait::async_trait(?Send)]
48 impl Perform for CommunityJoin {
49   type Response = CommunityJoinResponse;
50
51   #[tracing::instrument(skip(context, websocket_id))]
52   async fn perform(
53     &self,
54     context: &Data<LemmyContext>,
55     websocket_id: Option<ConnectionId>,
56   ) -> Result<CommunityJoinResponse, LemmyError> {
57     let data: &CommunityJoin = self;
58
59     if let Some(ws_id) = websocket_id {
60       context.chat_server().do_send(JoinCommunityRoom {
61         community_id: data.community_id,
62         id: ws_id,
63       });
64     }
65
66     Ok(CommunityJoinResponse { joined: true })
67   }
68 }
69
70 #[async_trait::async_trait(?Send)]
71 impl Perform for ModJoin {
72   type Response = ModJoinResponse;
73
74   #[tracing::instrument(skip(context, websocket_id))]
75   async fn perform(
76     &self,
77     context: &Data<LemmyContext>,
78     websocket_id: Option<ConnectionId>,
79   ) -> Result<ModJoinResponse, LemmyError> {
80     let data: &ModJoin = self;
81
82     if let Some(ws_id) = websocket_id {
83       context.chat_server().do_send(JoinModRoom {
84         community_id: data.community_id,
85         id: ws_id,
86       });
87     }
88
89     Ok(ModJoinResponse { joined: true })
90   }
91 }
92
93 #[async_trait::async_trait(?Send)]
94 impl Perform for PostJoin {
95   type Response = PostJoinResponse;
96
97   #[tracing::instrument(skip(context, websocket_id))]
98   async fn perform(
99     &self,
100     context: &Data<LemmyContext>,
101     websocket_id: Option<ConnectionId>,
102   ) -> Result<PostJoinResponse, LemmyError> {
103     let data: &PostJoin = self;
104
105     if let Some(ws_id) = websocket_id {
106       context.chat_server().do_send(JoinPostRoom {
107         post_id: data.post_id,
108         id: ws_id,
109       });
110     }
111
112     Ok(PostJoinResponse { joined: true })
113   }
114 }