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},
21 websocket::{serialize_websocket_message, UserOperationCrud},
24 use lemmy_utils::{error::LemmyError, ConnectionId};
25 use serde::Deserialize;
34 #[async_trait::async_trait(?Send)]
35 pub trait PerformCrud {
36 type Response: serde::ser::Serialize + Send;
40 context: &Data<LemmyContext>,
41 websocket_id: Option<ConnectionId>,
42 ) -> Result<Self::Response, LemmyError>;
45 pub async fn match_websocket_operation_crud(
46 context: LemmyContext,
48 op: UserOperationCrud,
50 ) -> Result<String, LemmyError> {
51 //TODO: handle commented out actions in crud crate
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
59 UserOperationCrud::DeleteAccount => {
60 do_websocket_operation::<DeleteAccount>(context, id, op, data).await
63 // Private Message ops
64 UserOperationCrud::CreatePrivateMessage => {
65 do_websocket_operation::<CreatePrivateMessage>(context, id, op, data).await
67 UserOperationCrud::EditPrivateMessage => {
68 do_websocket_operation::<EditPrivateMessage>(context, id, op, data).await
70 UserOperationCrud::DeletePrivateMessage => {
71 do_websocket_operation::<DeletePrivateMessage>(context, id, op, data).await
73 UserOperationCrud::GetPrivateMessages => {
74 do_websocket_operation::<GetPrivateMessages>(context, id, op, data).await
78 UserOperationCrud::CreateSite => {
79 do_websocket_operation::<CreateSite>(context, id, op, data).await
81 UserOperationCrud::EditSite => do_websocket_operation::<EditSite>(context, id, op, data).await,
82 UserOperationCrud::GetSite => do_websocket_operation::<GetSite>(context, id, op, data).await,
85 UserOperationCrud::GetCommunity => {
86 do_websocket_operation::<GetCommunity>(context, id, op, data).await
88 UserOperationCrud::ListCommunities => {
89 do_websocket_operation::<ListCommunities>(context, id, op, data).await
91 UserOperationCrud::CreateCommunity => {
92 do_websocket_operation::<CreateCommunity>(context, id, op, data).await
94 UserOperationCrud::EditCommunity => {
95 do_websocket_operation::<EditCommunity>(context, id, op, data).await
97 UserOperationCrud::DeleteCommunity => {
98 do_websocket_operation::<DeleteCommunity>(context, id, op, data).await
100 UserOperationCrud::RemoveCommunity => {
101 do_websocket_operation::<RemoveCommunity>(context, id, op, data).await
105 UserOperationCrud::CreatePost => {
106 do_websocket_operation::<CreatePost>(context, id, op, data).await
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
114 UserOperationCrud::RemovePost => {
115 do_websocket_operation::<RemovePost>(context, id, op, data).await
119 UserOperationCrud::CreateComment => {
120 do_websocket_operation::<CreateComment>(context, id, op, data).await
122 UserOperationCrud::EditComment => {
123 do_websocket_operation::<EditComment>(context, id, op, data).await
125 UserOperationCrud::DeleteComment => {
126 do_websocket_operation::<DeleteComment>(context, id, op, data).await
128 UserOperationCrud::RemoveComment => {
129 do_websocket_operation::<RemoveComment>(context, id, op, data).await
131 UserOperationCrud::GetComment => {
132 do_websocket_operation::<GetComment>(context, id, op, data).await
134 UserOperationCrud::GetComments => {
135 do_websocket_operation::<GetComments>(context, id, op, data).await
140 async fn do_websocket_operation<'a, 'b, Data>(
141 context: LemmyContext,
143 op: UserOperationCrud,
145 ) -> Result<String, LemmyError>
147 for<'de> Data: Deserialize<'de> + 'a,
150 let parsed_data: Data = serde_json::from_str(data)?;
151 let res = parsed_data
152 .perform(&web::Data::new(context), Some(id))
154 serialize_websocket_message(&op, &res)