]> Untitled Git - lemmy.git/blob - crates/api_crud/src/lib.rs
fde81eddbe01e8e25b024ebd8489e662316721dd
[lemmy.git] / crates / api_crud / src / lib.rs
1 use actix_web::{web, web::Data};
2 use lemmy_api_common::{
3   comment::{CreateComment, DeleteComment, EditComment, GetComment, GetComments, RemoveComment},
4   community::{
5     CreateCommunity,
6     DeleteCommunity,
7     EditCommunity,
8     GetCommunity,
9     ListCommunities,
10     RemoveCommunity,
11   },
12   person::{DeleteAccount, GetPersonDetails, Register},
13   post::{CreatePost, DeletePost, EditPost, GetPost, GetPosts, RemovePost},
14   private_message::{
15     CreatePrivateMessage,
16     DeletePrivateMessage,
17     EditPrivateMessage,
18     GetPrivateMessages,
19   },
20   site::{CreateSite, EditSite, GetSite},
21   websocket::{serialize_websocket_message, UserOperationCrud},
22   LemmyContext,
23 };
24 use lemmy_utils::{error::LemmyError, ConnectionId};
25 use serde::Deserialize;
26
27 mod comment;
28 mod community;
29 mod post;
30 mod private_message;
31 mod site;
32 mod user;
33
34 #[async_trait::async_trait(?Send)]
35 pub trait PerformCrud {
36   type Response: serde::ser::Serialize + Send;
37
38   async fn perform(
39     &self,
40     context: &Data<LemmyContext>,
41     websocket_id: Option<ConnectionId>,
42   ) -> Result<Self::Response, LemmyError>;
43 }
44
45 pub async fn match_websocket_operation_crud(
46   context: LemmyContext,
47   id: ConnectionId,
48   op: UserOperationCrud,
49   data: &str,
50 ) -> Result<String, LemmyError> {
51   //TODO: handle commented out actions in crud crate
52
53   match op {
54     // User ops
55     UserOperationCrud::Register => do_websocket_operation::<Register>(context, id, op, data).await,
56     UserOperationCrud::GetPersonDetails => {
57       do_websocket_operation::<GetPersonDetails>(context, id, op, data).await
58     }
59     UserOperationCrud::DeleteAccount => {
60       do_websocket_operation::<DeleteAccount>(context, id, op, data).await
61     }
62
63     // Private Message ops
64     UserOperationCrud::CreatePrivateMessage => {
65       do_websocket_operation::<CreatePrivateMessage>(context, id, op, data).await
66     }
67     UserOperationCrud::EditPrivateMessage => {
68       do_websocket_operation::<EditPrivateMessage>(context, id, op, data).await
69     }
70     UserOperationCrud::DeletePrivateMessage => {
71       do_websocket_operation::<DeletePrivateMessage>(context, id, op, data).await
72     }
73     UserOperationCrud::GetPrivateMessages => {
74       do_websocket_operation::<GetPrivateMessages>(context, id, op, data).await
75     }
76
77     // Site ops
78     UserOperationCrud::CreateSite => {
79       do_websocket_operation::<CreateSite>(context, id, op, data).await
80     }
81     UserOperationCrud::EditSite => do_websocket_operation::<EditSite>(context, id, op, data).await,
82     UserOperationCrud::GetSite => do_websocket_operation::<GetSite>(context, id, op, data).await,
83
84     // Community ops
85     UserOperationCrud::GetCommunity => {
86       do_websocket_operation::<GetCommunity>(context, id, op, data).await
87     }
88     UserOperationCrud::ListCommunities => {
89       do_websocket_operation::<ListCommunities>(context, id, op, data).await
90     }
91     UserOperationCrud::CreateCommunity => {
92       do_websocket_operation::<CreateCommunity>(context, id, op, data).await
93     }
94     UserOperationCrud::EditCommunity => {
95       do_websocket_operation::<EditCommunity>(context, id, op, data).await
96     }
97     UserOperationCrud::DeleteCommunity => {
98       do_websocket_operation::<DeleteCommunity>(context, id, op, data).await
99     }
100     UserOperationCrud::RemoveCommunity => {
101       do_websocket_operation::<RemoveCommunity>(context, id, op, data).await
102     }
103
104     // Post ops
105     UserOperationCrud::CreatePost => {
106       do_websocket_operation::<CreatePost>(context, id, op, data).await
107     }
108     UserOperationCrud::GetPost => do_websocket_operation::<GetPost>(context, id, op, data).await,
109     UserOperationCrud::GetPosts => do_websocket_operation::<GetPosts>(context, id, op, data).await,
110     UserOperationCrud::EditPost => do_websocket_operation::<EditPost>(context, id, op, data).await,
111     UserOperationCrud::DeletePost => {
112       do_websocket_operation::<DeletePost>(context, id, op, data).await
113     }
114     UserOperationCrud::RemovePost => {
115       do_websocket_operation::<RemovePost>(context, id, op, data).await
116     }
117
118     // Comment ops
119     UserOperationCrud::CreateComment => {
120       do_websocket_operation::<CreateComment>(context, id, op, data).await
121     }
122     UserOperationCrud::EditComment => {
123       do_websocket_operation::<EditComment>(context, id, op, data).await
124     }
125     UserOperationCrud::DeleteComment => {
126       do_websocket_operation::<DeleteComment>(context, id, op, data).await
127     }
128     UserOperationCrud::RemoveComment => {
129       do_websocket_operation::<RemoveComment>(context, id, op, data).await
130     }
131     UserOperationCrud::GetComment => {
132       do_websocket_operation::<GetComment>(context, id, op, data).await
133     }
134     UserOperationCrud::GetComments => {
135       do_websocket_operation::<GetComments>(context, id, op, data).await
136     }
137   }
138 }
139
140 async fn do_websocket_operation<'a, 'b, Data>(
141   context: LemmyContext,
142   id: ConnectionId,
143   op: UserOperationCrud,
144   data: &str,
145 ) -> Result<String, LemmyError>
146 where
147   for<'de> Data: Deserialize<'de> + 'a,
148   Data: PerformCrud,
149 {
150   let parsed_data: Data = serde_json::from_str(data)?;
151   let res = parsed_data
152     .perform(&web::Data::new(context), Some(id))
153     .await?;
154   serialize_websocket_message(&op, &res)
155 }