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