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