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