1 use actix_web::{web, web::Data};
2 use lemmy_api_common::{
3 comment::{CreateComment, DeleteComment, EditComment, GetComment, GetComments, RemoveComment},
12 person::{DeleteAccount, GetPersonDetails, Register},
13 post::{CreatePost, DeletePost, EditPost, GetPost, GetPosts, RemovePost},
20 site::{CreateSite, EditSite, GetSite},
22 use lemmy_utils::{error::LemmyError, ConnectionId};
23 use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperationCrud};
24 use serde::Deserialize;
33 #[async_trait::async_trait(?Send)]
34 pub trait PerformCrud {
35 type Response: serde::ser::Serialize + Send;
39 context: &Data<LemmyContext>,
40 websocket_id: Option<ConnectionId>,
41 ) -> Result<Self::Response, LemmyError>;
44 pub async fn match_websocket_operation_crud(
45 context: LemmyContext,
47 op: UserOperationCrud,
49 ) -> Result<String, LemmyError> {
50 //TODO: handle commented out actions in crud crate
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
58 UserOperationCrud::DeleteAccount => {
59 do_websocket_operation::<DeleteAccount>(context, id, op, data).await
62 // Private Message ops
63 UserOperationCrud::CreatePrivateMessage => {
64 do_websocket_operation::<CreatePrivateMessage>(context, id, op, data).await
66 UserOperationCrud::EditPrivateMessage => {
67 do_websocket_operation::<EditPrivateMessage>(context, id, op, data).await
69 UserOperationCrud::DeletePrivateMessage => {
70 do_websocket_operation::<DeletePrivateMessage>(context, id, op, data).await
72 UserOperationCrud::GetPrivateMessages => {
73 do_websocket_operation::<GetPrivateMessages>(context, id, op, data).await
77 UserOperationCrud::CreateSite => {
78 do_websocket_operation::<CreateSite>(context, id, op, data).await
80 UserOperationCrud::EditSite => do_websocket_operation::<EditSite>(context, id, op, data).await,
81 UserOperationCrud::GetSite => do_websocket_operation::<GetSite>(context, id, op, data).await,
84 UserOperationCrud::GetCommunity => {
85 do_websocket_operation::<GetCommunity>(context, id, op, data).await
87 UserOperationCrud::ListCommunities => {
88 do_websocket_operation::<ListCommunities>(context, id, op, data).await
90 UserOperationCrud::CreateCommunity => {
91 do_websocket_operation::<CreateCommunity>(context, id, op, data).await
93 UserOperationCrud::EditCommunity => {
94 do_websocket_operation::<EditCommunity>(context, id, op, data).await
96 UserOperationCrud::DeleteCommunity => {
97 do_websocket_operation::<DeleteCommunity>(context, id, op, data).await
99 UserOperationCrud::RemoveCommunity => {
100 do_websocket_operation::<RemoveCommunity>(context, id, op, data).await
104 UserOperationCrud::CreatePost => {
105 do_websocket_operation::<CreatePost>(context, id, op, data).await
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
113 UserOperationCrud::RemovePost => {
114 do_websocket_operation::<RemovePost>(context, id, op, data).await
118 UserOperationCrud::CreateComment => {
119 do_websocket_operation::<CreateComment>(context, id, op, data).await
121 UserOperationCrud::EditComment => {
122 do_websocket_operation::<EditComment>(context, id, op, data).await
124 UserOperationCrud::DeleteComment => {
125 do_websocket_operation::<DeleteComment>(context, id, op, data).await
127 UserOperationCrud::RemoveComment => {
128 do_websocket_operation::<RemoveComment>(context, id, op, data).await
130 UserOperationCrud::GetComment => {
131 do_websocket_operation::<GetComment>(context, id, op, data).await
133 UserOperationCrud::GetComments => {
134 do_websocket_operation::<GetComments>(context, id, op, data).await
139 async fn do_websocket_operation<'a, 'b, Data>(
140 context: LemmyContext,
142 op: UserOperationCrud,
144 ) -> Result<String, LemmyError>
146 for<'de> Data: Deserialize<'de> + 'a,
149 let parsed_data: Data = serde_json::from_str(data)?;
150 let res = parsed_data
151 .perform(&web::Data::new(context), Some(id))
153 serialize_websocket_message(&op, &res)