2021-03-25 19:30:15 +00:00
|
|
|
use actix_web::{web, web::Data};
|
2022-11-19 04:33:54 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
comment::{CreateComment, DeleteComment, EditComment, GetComment, GetComments, RemoveComment},
|
|
|
|
community::{
|
|
|
|
CreateCommunity,
|
|
|
|
DeleteCommunity,
|
|
|
|
EditCommunity,
|
|
|
|
GetCommunity,
|
|
|
|
ListCommunities,
|
|
|
|
RemoveCommunity,
|
|
|
|
},
|
|
|
|
person::{DeleteAccount, GetPersonDetails, Register},
|
|
|
|
post::{CreatePost, DeletePost, EditPost, GetPost, GetPosts, RemovePost},
|
|
|
|
private_message::{
|
|
|
|
CreatePrivateMessage,
|
|
|
|
DeletePrivateMessage,
|
|
|
|
EditPrivateMessage,
|
|
|
|
GetPrivateMessages,
|
|
|
|
},
|
|
|
|
site::{CreateSite, EditSite, GetSite},
|
2022-11-26 02:04:46 +00:00
|
|
|
websocket::{serialize_websocket_message, UserOperationCrud},
|
|
|
|
LemmyContext,
|
2022-11-19 04:33:54 +00:00
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::{error::LemmyError, ConnectionId};
|
2021-03-25 19:30:15 +00:00
|
|
|
use serde::Deserialize;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
mod comment;
|
|
|
|
mod community;
|
|
|
|
mod post;
|
|
|
|
mod private_message;
|
|
|
|
mod site;
|
|
|
|
mod user;
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub trait PerformCrud {
|
|
|
|
type Response: serde::ser::Serialize + Send;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<Self::Response, LemmyError>;
|
|
|
|
}
|
2021-03-25 19:30:15 +00:00
|
|
|
|
|
|
|
pub async fn match_websocket_operation_crud(
|
|
|
|
context: LemmyContext,
|
|
|
|
id: ConnectionId,
|
|
|
|
op: UserOperationCrud,
|
|
|
|
data: &str,
|
|
|
|
) -> Result<String, LemmyError> {
|
|
|
|
//TODO: handle commented out actions in crud crate
|
|
|
|
|
|
|
|
match op {
|
|
|
|
// User ops
|
|
|
|
UserOperationCrud::Register => do_websocket_operation::<Register>(context, id, op, data).await,
|
|
|
|
UserOperationCrud::GetPersonDetails => {
|
|
|
|
do_websocket_operation::<GetPersonDetails>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::DeleteAccount => {
|
|
|
|
do_websocket_operation::<DeleteAccount>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private Message ops
|
|
|
|
UserOperationCrud::CreatePrivateMessage => {
|
|
|
|
do_websocket_operation::<CreatePrivateMessage>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::EditPrivateMessage => {
|
|
|
|
do_websocket_operation::<EditPrivateMessage>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::DeletePrivateMessage => {
|
|
|
|
do_websocket_operation::<DeletePrivateMessage>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::GetPrivateMessages => {
|
|
|
|
do_websocket_operation::<GetPrivateMessages>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Site ops
|
|
|
|
UserOperationCrud::CreateSite => {
|
|
|
|
do_websocket_operation::<CreateSite>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::EditSite => do_websocket_operation::<EditSite>(context, id, op, data).await,
|
|
|
|
UserOperationCrud::GetSite => do_websocket_operation::<GetSite>(context, id, op, data).await,
|
|
|
|
|
|
|
|
// Community ops
|
|
|
|
UserOperationCrud::GetCommunity => {
|
|
|
|
do_websocket_operation::<GetCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::ListCommunities => {
|
|
|
|
do_websocket_operation::<ListCommunities>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::CreateCommunity => {
|
|
|
|
do_websocket_operation::<CreateCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::EditCommunity => {
|
|
|
|
do_websocket_operation::<EditCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::DeleteCommunity => {
|
|
|
|
do_websocket_operation::<DeleteCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::RemoveCommunity => {
|
|
|
|
do_websocket_operation::<RemoveCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post ops
|
|
|
|
UserOperationCrud::CreatePost => {
|
|
|
|
do_websocket_operation::<CreatePost>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::GetPost => do_websocket_operation::<GetPost>(context, id, op, data).await,
|
|
|
|
UserOperationCrud::GetPosts => do_websocket_operation::<GetPosts>(context, id, op, data).await,
|
|
|
|
UserOperationCrud::EditPost => do_websocket_operation::<EditPost>(context, id, op, data).await,
|
|
|
|
UserOperationCrud::DeletePost => {
|
|
|
|
do_websocket_operation::<DeletePost>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::RemovePost => {
|
|
|
|
do_websocket_operation::<RemovePost>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comment ops
|
|
|
|
UserOperationCrud::CreateComment => {
|
|
|
|
do_websocket_operation::<CreateComment>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::EditComment => {
|
|
|
|
do_websocket_operation::<EditComment>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::DeleteComment => {
|
|
|
|
do_websocket_operation::<DeleteComment>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::RemoveComment => {
|
|
|
|
do_websocket_operation::<RemoveComment>(context, id, op, data).await
|
|
|
|
}
|
2021-11-23 15:53:48 +00:00
|
|
|
UserOperationCrud::GetComment => {
|
|
|
|
do_websocket_operation::<GetComment>(context, id, op, data).await
|
|
|
|
}
|
2021-03-25 19:30:15 +00:00
|
|
|
UserOperationCrud::GetComments => {
|
|
|
|
do_websocket_operation::<GetComments>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn do_websocket_operation<'a, 'b, Data>(
|
|
|
|
context: LemmyContext,
|
|
|
|
id: ConnectionId,
|
|
|
|
op: UserOperationCrud,
|
|
|
|
data: &str,
|
|
|
|
) -> Result<String, LemmyError>
|
|
|
|
where
|
|
|
|
for<'de> Data: Deserialize<'de> + 'a,
|
|
|
|
Data: PerformCrud,
|
|
|
|
{
|
2021-07-05 16:07:26 +00:00
|
|
|
let parsed_data: Data = serde_json::from_str(data)?;
|
2021-03-25 19:30:15 +00:00
|
|
|
let res = parsed_data
|
|
|
|
.perform(&web::Data::new(context), Some(id))
|
|
|
|
.await?;
|
|
|
|
serialize_websocket_message(&op, &res)
|
|
|
|
}
|