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