Fix API and clippy warnings

remotes/yerba/split-api-crate
Felix Ableitner 2021-03-25 20:30:15 +01:00
parent 249fcc5066
commit 01fc1228d5
36 changed files with 440 additions and 416 deletions

View File

@ -188,10 +188,7 @@ impl Perform for BanFromCommunity {
// Mod tables // Mod tables
// TODO eventually do correct expires // TODO eventually do correct expires
let expires = match data.expires { let expires = data.expires.map(naive_from_unix);
Some(time) => Some(naive_from_unix(time)),
None => None,
};
let form = ModBanFromCommunityForm { let form = ModBanFromCommunityForm {
mod_person_id: local_user_view.person.id, mod_person_id: local_user_view.person.id,

View File

@ -12,7 +12,6 @@ mod local_user;
mod post; mod post;
mod post_report; mod post_report;
mod private_message; mod private_message;
pub mod routes;
mod site; mod site;
mod websocket; mod websocket;
@ -33,29 +32,15 @@ pub async fn match_websocket_operation(
op: UserOperation, op: UserOperation,
data: &str, data: &str,
) -> Result<String, LemmyError> { ) -> Result<String, LemmyError> {
//TODO: handle commented out actions in crud crate
match op { match op {
// User ops // User ops
UserOperation::Login => { UserOperation::Login => do_websocket_operation::<Login>(context, id, op, data).await,
//do_websocket_operation::<Login>(context, id, op, data).await
todo!()
}
UserOperation::Register => {
//do_websocket_operation::<Register>(context, id, op, data).await
todo!()
}
UserOperation::GetCaptcha => do_websocket_operation::<GetCaptcha>(context, id, op, data).await, UserOperation::GetCaptcha => do_websocket_operation::<GetCaptcha>(context, id, op, data).await,
UserOperation::GetPersonDetails => {
//do_websocket_operation::<GetPersonDetails>(context, id, op, data).await
todo!()
}
UserOperation::GetReplies => do_websocket_operation::<GetReplies>(context, id, op, data).await, UserOperation::GetReplies => do_websocket_operation::<GetReplies>(context, id, op, data).await,
UserOperation::AddAdmin => do_websocket_operation::<AddAdmin>(context, id, op, data).await, UserOperation::AddAdmin => do_websocket_operation::<AddAdmin>(context, id, op, data).await,
UserOperation::BanPerson => do_websocket_operation::<BanPerson>(context, id, op, data).await, UserOperation::BanPerson => do_websocket_operation::<BanPerson>(context, id, op, data).await,
UserOperation::GetPersonMentions => { UserOperation::GetPersonMentions => {
//do_websocket_operation::<GetPersonMentions>(context, id, op, data).await do_websocket_operation::<GetPersonMentions>(context, id, op, data).await
todo!()
} }
UserOperation::MarkPersonMentionAsRead => { UserOperation::MarkPersonMentionAsRead => {
do_websocket_operation::<MarkPersonMentionAsRead>(context, id, op, data).await do_websocket_operation::<MarkPersonMentionAsRead>(context, id, op, data).await
@ -63,10 +48,6 @@ pub async fn match_websocket_operation(
UserOperation::MarkAllAsRead => { UserOperation::MarkAllAsRead => {
do_websocket_operation::<MarkAllAsRead>(context, id, op, data).await do_websocket_operation::<MarkAllAsRead>(context, id, op, data).await
} }
UserOperation::DeleteAccount => {
//do_websocket_operation::<DeleteAccount>(context, id, op, data).await
todo!()
}
UserOperation::PasswordReset => { UserOperation::PasswordReset => {
do_websocket_operation::<PasswordReset>(context, id, op, data).await do_websocket_operation::<PasswordReset>(context, id, op, data).await
} }
@ -87,40 +68,12 @@ pub async fn match_websocket_operation(
} }
// Private Message ops // Private Message ops
UserOperation::CreatePrivateMessage => {
//do_websocket_operation::<CreatePrivateMessage>(context, id, op, data).await
todo!()
}
UserOperation::EditPrivateMessage => {
//do_websocket_operation::<EditPrivateMessage>(context, id, op, data).await
todo!()
}
UserOperation::DeletePrivateMessage => {
//do_websocket_operation::<DeletePrivateMessage>(context, id, op, data).await
todo!()
}
UserOperation::MarkPrivateMessageAsRead => { UserOperation::MarkPrivateMessageAsRead => {
do_websocket_operation::<MarkPrivateMessageAsRead>(context, id, op, data).await do_websocket_operation::<MarkPrivateMessageAsRead>(context, id, op, data).await
} }
UserOperation::GetPrivateMessages => {
//do_websocket_operation::<GetPrivateMessages>(context, id, op, data).await
todo!()
}
// Site ops // Site ops
UserOperation::GetModlog => do_websocket_operation::<GetModlog>(context, id, op, data).await, UserOperation::GetModlog => do_websocket_operation::<GetModlog>(context, id, op, data).await,
UserOperation::CreateSite => {
//do_websocket_operation::<CreateSite>(context, id, op, data).await
todo!()
}
UserOperation::EditSite => {
//do_websocket_operation::<EditSite>(context, id, op, data).await
todo!()
}
UserOperation::GetSite => {
//do_websocket_operation::<GetSite>(context, id, op, data).await
todo!()
}
UserOperation::GetSiteConfig => { UserOperation::GetSiteConfig => {
do_websocket_operation::<GetSiteConfig>(context, id, op, data).await do_websocket_operation::<GetSiteConfig>(context, id, op, data).await
} }
@ -136,30 +89,6 @@ pub async fn match_websocket_operation(
} }
// Community ops // Community ops
UserOperation::GetCommunity => {
//do_websocket_operation::<GetCommunity>(context, id, op, data).await
todo!()
}
UserOperation::ListCommunities => {
//do_websocket_operation::<ListCommunities>(context, id, op, data).await
todo!()
}
UserOperation::CreateCommunity => {
//do_websocket_operation::<CreateCommunity>(context, id, op, data).await
todo!()
}
UserOperation::EditCommunity => {
//do_websocket_operation::<EditCommunity>(context, id, op, data).await
todo!()
}
UserOperation::DeleteCommunity => {
//do_websocket_operation::<DeleteCommunity>(context, id, op, data).await
todo!()
}
UserOperation::RemoveCommunity => {
//do_websocket_operation::<RemoveCommunity>(context, id, op, data).await
todo!()
}
UserOperation::FollowCommunity => { UserOperation::FollowCommunity => {
do_websocket_operation::<FollowCommunity>(context, id, op, data).await do_websocket_operation::<FollowCommunity>(context, id, op, data).await
} }
@ -174,30 +103,6 @@ pub async fn match_websocket_operation(
} }
// Post ops // Post ops
UserOperation::CreatePost => {
//do_websocket_operation::<CreatePost>(context, id, op, data).await
todo!()
}
UserOperation::GetPost => {
//do_websocket_operation::<GetPost>(context, id, op, data).await
todo!()
}
UserOperation::GetPosts => {
//do_websocket_operation::<GetPosts>(context, id, op, data).await
todo!()
}
UserOperation::EditPost => {
//do_websocket_operation::<EditPost>(context, id, op, data).await
todo!()
}
UserOperation::DeletePost => {
//do_websocket_operation::<DeletePost>(context, id, op, data).await
todo!()
}
UserOperation::RemovePost => {
//do_websocket_operation::<RemovePost>(context, id, op, data).await
todo!()
}
UserOperation::LockPost => do_websocket_operation::<LockPost>(context, id, op, data).await, UserOperation::LockPost => do_websocket_operation::<LockPost>(context, id, op, data).await,
UserOperation::StickyPost => do_websocket_operation::<StickyPost>(context, id, op, data).await, UserOperation::StickyPost => do_websocket_operation::<StickyPost>(context, id, op, data).await,
UserOperation::CreatePostLike => { UserOperation::CreatePostLike => {
@ -215,32 +120,12 @@ pub async fn match_websocket_operation(
} }
// Comment ops // Comment ops
UserOperation::CreateComment => {
//do_websocket_operation::<CreateComment>(context, id, op, data).await
todo!()
}
UserOperation::EditComment => {
//do_websocket_operation::<EditComment>(context, id, op, data).await
todo!()
}
UserOperation::DeleteComment => {
//do_websocket_operation::<DeleteComment>(context, id, op, data).await
todo!()
}
UserOperation::RemoveComment => {
//do_websocket_operation::<RemoveComment>(context, id, op, data).await
todo!()
}
UserOperation::MarkCommentAsRead => { UserOperation::MarkCommentAsRead => {
do_websocket_operation::<MarkCommentAsRead>(context, id, op, data).await do_websocket_operation::<MarkCommentAsRead>(context, id, op, data).await
} }
UserOperation::SaveComment => { UserOperation::SaveComment => {
do_websocket_operation::<SaveComment>(context, id, op, data).await do_websocket_operation::<SaveComment>(context, id, op, data).await
} }
UserOperation::GetComments => {
//do_websocket_operation::<GetComments>(context, id, op, data).await
todo!()
}
UserOperation::CreateCommentLike => { UserOperation::CreateCommentLike => {
do_websocket_operation::<CreateCommentLike>(context, id, op, data).await do_websocket_operation::<CreateCommentLike>(context, id, op, data).await
} }
@ -326,7 +211,7 @@ pub(crate) fn espeak_wav_base64(text: &str) -> Result<String, LemmyError> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{captcha_espeak_wav_base64, check_validator_time}; use crate::captcha_espeak_wav_base64;
use lemmy_api_common::check_validator_time; use lemmy_api_common::check_validator_time;
use lemmy_db_queries::{establish_unpooled_connection, source::local_user::LocalUser_, Crud}; use lemmy_db_queries::{establish_unpooled_connection, source::local_user::LocalUser_, Crud};
use lemmy_db_schema::source::{ use lemmy_db_schema::source::{

View File

@ -153,7 +153,7 @@ impl Perform for GetCaptcha {
context.chat_server().do_send(captcha_item); context.chat_server().do_send(captcha_item);
Ok(GetCaptchaResponse { Ok(GetCaptchaResponse {
ok: Some(CaptchaResponse { png, uuid, wav }), ok: Some(CaptchaResponse { png, wav, uuid }),
}) })
} }
} }
@ -407,10 +407,7 @@ impl Perform for BanPerson {
} }
// Mod tables // Mod tables
let expires = match data.expires { let expires = data.expires.map(naive_from_unix);
Some(time) => Some(naive_from_unix(time)),
None => None,
};
let form = ModBanForm { let form = ModBanForm {
mod_person_id: local_user_view.person.id, mod_person_id: local_user_view.person.id,

View File

@ -18,7 +18,7 @@ use lemmy_utils::{
ConnectionId, ConnectionId,
LemmyError, LemmyError,
}; };
use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperationCrud};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl PerformCrud for CreateComment { impl PerformCrud for CreateComment {
@ -158,7 +158,7 @@ impl PerformCrud for CreateComment {
}; };
context.chat_server().do_send(SendComment { context.chat_server().do_send(SendComment {
op: UserOperation::CreateComment, op: UserOperationCrud::CreateComment,
comment: res.clone(), comment: res.clone(),
websocket_id, websocket_id,
}); });

View File

@ -13,7 +13,7 @@ use lemmy_db_queries::{source::comment::Comment_, Crud};
use lemmy_db_schema::source::{comment::*, moderator::*}; use lemmy_db_schema::source::{comment::*, moderator::*};
use lemmy_db_views::comment_view::CommentView; use lemmy_db_views::comment_view::CommentView;
use lemmy_utils::{ApiError, ConnectionId, LemmyError}; use lemmy_utils::{ApiError, ConnectionId, LemmyError};
use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperationCrud};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl PerformCrud for DeleteComment { impl PerformCrud for DeleteComment {
@ -95,7 +95,7 @@ impl PerformCrud for DeleteComment {
}; };
context.chat_server().do_send(SendComment { context.chat_server().do_send(SendComment {
op: UserOperation::DeleteComment, op: UserOperationCrud::DeleteComment,
comment: res.clone(), comment: res.clone(),
websocket_id, websocket_id,
}); });
@ -200,7 +200,7 @@ impl PerformCrud for RemoveComment {
}; };
context.chat_server().do_send(SendComment { context.chat_server().do_send(SendComment {
op: UserOperation::RemoveComment, op: UserOperationCrud::RemoveComment,
comment: res.clone(), comment: res.clone(),
websocket_id, websocket_id,
}); });

View File

@ -17,7 +17,7 @@ use lemmy_utils::{
ConnectionId, ConnectionId,
LemmyError, LemmyError,
}; };
use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperationCrud};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl PerformCrud for EditComment { impl PerformCrud for EditComment {
@ -93,7 +93,7 @@ impl PerformCrud for EditComment {
}; };
context.chat_server().do_send(SendComment { context.chat_server().do_send(SendComment {
op: UserOperation::EditComment, op: UserOperationCrud::EditComment,
comment: res.clone(), comment: res.clone(),
websocket_id, websocket_id,
}); });

View File

@ -9,7 +9,7 @@ use lemmy_db_schema::source::{
}; };
use lemmy_db_views_actor::community_view::CommunityView; use lemmy_db_views_actor::community_view::CommunityView;
use lemmy_utils::{utils::naive_from_unix, ApiError, ConnectionId, LemmyError}; use lemmy_utils::{utils::naive_from_unix, ApiError, ConnectionId, LemmyError};
use lemmy_websocket::{LemmyContext, UserOperation}; use lemmy_websocket::{LemmyContext, UserOperationCrud};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl PerformCrud for DeleteCommunity { impl PerformCrud for DeleteCommunity {
@ -61,7 +61,12 @@ impl PerformCrud for DeleteCommunity {
let res = CommunityResponse { community_view }; let res = CommunityResponse { community_view };
send_community_websocket(&res, context, websocket_id, UserOperation::DeleteCommunity); send_community_websocket(
&res,
context,
websocket_id,
UserOperationCrud::DeleteCommunity,
);
Ok(res) Ok(res)
} }
@ -95,10 +100,7 @@ impl PerformCrud for RemoveCommunity {
}; };
// Mod tables // Mod tables
let expires = match data.expires { let expires = data.expires.map(naive_from_unix);
Some(time) => Some(naive_from_unix(time)),
None => None,
};
let form = ModRemoveCommunityForm { let form = ModRemoveCommunityForm {
mod_person_id: local_user_view.person.id, mod_person_id: local_user_view.person.id,
community_id: data.community_id, community_id: data.community_id,
@ -127,7 +129,12 @@ impl PerformCrud for RemoveCommunity {
let res = CommunityResponse { community_view }; let res = CommunityResponse { community_view };
send_community_websocket(&res, context, websocket_id, UserOperation::RemoveCommunity); send_community_websocket(
&res,
context,
websocket_id,
UserOperationCrud::RemoveCommunity,
);
Ok(res) Ok(res)
} }

View File

@ -1,7 +1,7 @@
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::community::CommunityResponse; use lemmy_api_common::community::CommunityResponse;
use lemmy_utils::ConnectionId; use lemmy_utils::ConnectionId;
use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperationCrud};
mod create; mod create;
mod delete; mod delete;
@ -12,7 +12,7 @@ pub(in crate::community) fn send_community_websocket(
res: &CommunityResponse, res: &CommunityResponse,
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
websocket_id: Option<ConnectionId>, websocket_id: Option<ConnectionId>,
op: UserOperation, op: UserOperationCrud,
) { ) {
// Strip out the person id and subscribed when sending to others // Strip out the person id and subscribed when sending to others
let mut res_sent = res.clone(); let mut res_sent = res.clone();

View File

@ -87,10 +87,7 @@ impl PerformCrud for ListCommunities {
let data: &ListCommunities = &self; let data: &ListCommunities = &self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?; let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let person_id = match &local_user_view { let person_id = local_user_view.to_owned().map(|l| l.person.id);
Some(uv) => Some(uv.person.id),
None => None,
};
// Don't show NSFW by default // Don't show NSFW by default
let show_nsfw = match &local_user_view { let show_nsfw = match &local_user_view {

View File

@ -21,7 +21,7 @@ use lemmy_utils::{
ConnectionId, ConnectionId,
LemmyError, LemmyError,
}; };
use lemmy_websocket::{LemmyContext, UserOperation}; use lemmy_websocket::{LemmyContext, UserOperationCrud};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl PerformCrud for EditCommunity { impl PerformCrud for EditCommunity {
@ -102,7 +102,12 @@ impl PerformCrud for EditCommunity {
let res = CommunityResponse { community_view }; let res = CommunityResponse { community_view };
send_community_websocket(&res, context, websocket_id, UserOperation::EditCommunity); send_community_websocket(
&res,
context,
websocket_id,
UserOperationCrud::EditCommunity,
);
Ok(res) Ok(res)
} }

View File

@ -1,12 +1,13 @@
use actix_web::web::Data; use actix_web::{web, web::Data};
use lemmy_api_common::{comment::*, community::*, person::*, post::*, site::*};
use lemmy_utils::{ConnectionId, LemmyError}; use lemmy_utils::{ConnectionId, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperationCrud};
use serde::Deserialize;
mod comment; mod comment;
mod community; mod community;
mod post; mod post;
mod private_message; mod private_message;
pub mod routes;
mod site; mod site;
mod user; mod user;
@ -20,3 +21,112 @@ pub trait PerformCrud {
websocket_id: Option<ConnectionId>, websocket_id: Option<ConnectionId>,
) -> Result<Self::Response, LemmyError>; ) -> Result<Self::Response, LemmyError>;
} }
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
}
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,
{
let parsed_data: Data = serde_json::from_str(&data)?;
let res = parsed_data
.perform(&web::Data::new(context), Some(id))
.await?;
serialize_websocket_message(&op, &res)
}

View File

@ -12,7 +12,7 @@ use lemmy_utils::{
ConnectionId, ConnectionId,
LemmyError, LemmyError,
}; };
use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperationCrud};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl PerformCrud for CreatePost { impl PerformCrud for CreatePost {
@ -120,7 +120,7 @@ impl PerformCrud for CreatePost {
let res = PostResponse { post_view }; let res = PostResponse { post_view };
context.chat_server().do_send(SendPost { context.chat_server().do_send(SendPost {
op: UserOperation::CreatePost, op: UserOperationCrud::CreatePost,
post: res.clone(), post: res.clone(),
websocket_id, websocket_id,
}); });

View File

@ -12,7 +12,7 @@ use lemmy_db_queries::{source::post::Post_, Crud};
use lemmy_db_schema::source::{moderator::*, post::*}; use lemmy_db_schema::source::{moderator::*, post::*};
use lemmy_db_views::post_view::PostView; use lemmy_db_views::post_view::PostView;
use lemmy_utils::{ApiError, ConnectionId, LemmyError}; use lemmy_utils::{ApiError, ConnectionId, LemmyError};
use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperationCrud};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl PerformCrud for DeletePost { impl PerformCrud for DeletePost {
@ -70,7 +70,7 @@ impl PerformCrud for DeletePost {
let res = PostResponse { post_view }; let res = PostResponse { post_view };
context.chat_server().do_send(SendPost { context.chat_server().do_send(SendPost {
op: UserOperation::DeletePost, op: UserOperationCrud::DeletePost,
post: res.clone(), post: res.clone(),
websocket_id, websocket_id,
}); });
@ -151,7 +151,7 @@ impl PerformCrud for RemovePost {
let res = PostResponse { post_view }; let res = PostResponse { post_view };
context.chat_server().do_send(SendPost { context.chat_server().do_send(SendPost {
op: UserOperation::RemovePost, op: UserOperationCrud::RemovePost,
post: res.clone(), post: res.clone(),
websocket_id, websocket_id,
}); });

View File

@ -92,10 +92,7 @@ impl PerformCrud for GetPosts {
let data: &GetPosts = &self; let data: &GetPosts = &self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?; let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let person_id = match &local_user_view { let person_id = local_user_view.to_owned().map(|l| l.person.id);
Some(uv) => Some(uv.person.id),
None => None,
};
let show_nsfw = match &local_user_view { let show_nsfw = match &local_user_view {
Some(uv) => uv.local_user.show_nsfw, Some(uv) => uv.local_user.show_nsfw,

View File

@ -12,7 +12,7 @@ use lemmy_utils::{
ConnectionId, ConnectionId,
LemmyError, LemmyError,
}; };
use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperationCrud};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl PerformCrud for EditPost { impl PerformCrud for EditPost {
@ -106,7 +106,7 @@ impl PerformCrud for EditPost {
let res = PostResponse { post_view }; let res = PostResponse { post_view };
context.chat_server().do_send(SendPost { context.chat_server().do_send(SendPost {
op: UserOperation::EditPost, op: UserOperationCrud::EditPost,
post: res.clone(), post: res.clone(),
websocket_id, websocket_id,
}); });

View File

@ -11,7 +11,7 @@ use lemmy_db_queries::{source::private_message::PrivateMessage_, Crud};
use lemmy_db_schema::source::private_message::{PrivateMessage, PrivateMessageForm}; use lemmy_db_schema::source::private_message::{PrivateMessage, PrivateMessageForm};
use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView}; use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView};
use lemmy_utils::{utils::remove_slurs, ApiError, ConnectionId, LemmyError}; use lemmy_utils::{utils::remove_slurs, ApiError, ConnectionId, LemmyError};
use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperationCrud};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl PerformCrud for CreatePrivateMessage { impl PerformCrud for CreatePrivateMessage {
@ -100,7 +100,7 @@ impl PerformCrud for CreatePrivateMessage {
let local_recipient_id = local_recipient.local_user.id; let local_recipient_id = local_recipient.local_user.id;
context.chat_server().do_send(SendUserRoomMessage { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::CreatePrivateMessage, op: UserOperationCrud::CreatePrivateMessage,
response: res.clone(), response: res.clone(),
local_recipient_id, local_recipient_id,
websocket_id, websocket_id,

View File

@ -10,7 +10,7 @@ use lemmy_db_queries::{source::private_message::PrivateMessage_, Crud};
use lemmy_db_schema::source::private_message::PrivateMessage; use lemmy_db_schema::source::private_message::PrivateMessage;
use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView}; use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView};
use lemmy_utils::{ApiError, ConnectionId, LemmyError}; use lemmy_utils::{ApiError, ConnectionId, LemmyError};
use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperationCrud};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl PerformCrud for DeletePrivateMessage { impl PerformCrud for DeletePrivateMessage {
@ -76,7 +76,7 @@ impl PerformCrud for DeletePrivateMessage {
{ {
let local_recipient_id = local_recipient.local_user.id; let local_recipient_id = local_recipient.local_user.id;
context.chat_server().do_send(SendUserRoomMessage { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::DeletePrivateMessage, op: UserOperationCrud::DeletePrivateMessage,
response: res.clone(), response: res.clone(),
local_recipient_id, local_recipient_id,
websocket_id, websocket_id,

View File

@ -10,7 +10,7 @@ use lemmy_db_queries::{source::private_message::PrivateMessage_, Crud};
use lemmy_db_schema::source::private_message::PrivateMessage; use lemmy_db_schema::source::private_message::PrivateMessage;
use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView}; use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView};
use lemmy_utils::{utils::remove_slurs, ApiError, ConnectionId, LemmyError}; use lemmy_utils::{utils::remove_slurs, ApiError, ConnectionId, LemmyError};
use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperationCrud};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl PerformCrud for EditPrivateMessage { impl PerformCrud for EditPrivateMessage {
@ -70,7 +70,7 @@ impl PerformCrud for EditPrivateMessage {
{ {
let local_recipient_id = local_recipient.local_user.id; let local_recipient_id = local_recipient.local_user.id;
context.chat_server().do_send(SendUserRoomMessage { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::EditPrivateMessage, op: UserOperationCrud::EditPrivateMessage,
response: res.clone(), response: res.clone(),
local_recipient_id, local_recipient_id,
websocket_id, websocket_id,

View File

@ -1,133 +0,0 @@
use crate::PerformCrud;
use actix_web::{error::ErrorBadRequest, *};
use lemmy_api_common::{comment::*, community::*, person::*, post::*, site::*};
use lemmy_utils::rate_limit::RateLimit;
use lemmy_websocket::LemmyContext;
use serde::Deserialize;
pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
cfg
.service(
web::scope("/api/v2")
// Site
.service(
web::scope("/site")
.wrap(rate_limit.message())
.route("", web::get().to(route_get::<GetSite>))
// Admin Actions
.route("", web::post().to(route_post::<CreateSite>))
.route("", web::put().to(route_post::<EditSite>)),
)
// Community
.service(
web::resource("/community")
.guard(guard::Post())
.wrap(rate_limit.register())
.route(web::post().to(route_post::<CreateCommunity>)),
)
.service(
web::scope("/community")
.wrap(rate_limit.message())
.route("", web::get().to(route_get::<GetCommunity>))
.route("", web::put().to(route_post::<EditCommunity>))
.route("/list", web::get().to(route_get::<ListCommunities>))
.route("/delete", web::post().to(route_post::<DeleteCommunity>))
// Mod Actions
.route("/remove", web::post().to(route_post::<RemoveCommunity>)),
)
// Post
.service(
// Handle POST to /post separately to add the post() rate limitter
web::resource("/post")
.guard(guard::Post())
.wrap(rate_limit.post())
.route(web::post().to(route_post::<CreatePost>)),
)
.service(
web::scope("/post")
.wrap(rate_limit.message())
.route("", web::get().to(route_get::<GetPost>))
.route("", web::put().to(route_post::<EditPost>))
.route("/delete", web::post().to(route_post::<DeletePost>))
.route("/remove", web::post().to(route_post::<RemovePost>))
.route("/list", web::get().to(route_get::<GetPosts>)),
)
// Comment
.service(
web::scope("/comment")
.wrap(rate_limit.message())
.route("", web::post().to(route_post::<CreateComment>))
.route("", web::put().to(route_post::<EditComment>))
.route("/delete", web::post().to(route_post::<DeleteComment>))
.route("/remove", web::post().to(route_post::<RemoveComment>))
.route("/list", web::get().to(route_get::<GetComments>)),
),
)
// Private Message
.service(
web::scope("/private_message")
.wrap(rate_limit.message())
.route("/list", web::get().to(route_get::<GetPrivateMessages>))
.route("", web::post().to(route_post::<CreatePrivateMessage>))
.route("", web::put().to(route_post::<EditPrivateMessage>))
.route(
"/delete",
web::post().to(route_post::<DeletePrivateMessage>),
),
)
// User
.service(
// Account action, I don't like that it's in /user maybe /accounts
// Handle /user/register separately to add the register() rate limitter
web::resource("/user/register")
.guard(guard::Post())
.wrap(rate_limit.register())
.route(web::post().to(route_post::<Register>)),
)
// User actions
.service(
web::scope("/user")
.wrap(rate_limit.message())
.route("", web::get().to(route_get::<GetPersonDetails>))
.route(
"/delete_account",
web::post().to(route_post::<DeleteAccount>),
),
);
}
async fn perform<Request>(
data: Request,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error>
where
Request: PerformCrud,
Request: Send + 'static,
{
let res = data
.perform(&context, None)
.await
.map(|json| HttpResponse::Ok().json(json))
.map_err(ErrorBadRequest)?;
Ok(res)
}
async fn route_get<'a, Data>(
data: web::Query<Data>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error>
where
Data: Deserialize<'a> + Send + 'static + PerformCrud,
{
perform::<Data>(data.0, context).await
}
async fn route_post<'a, Data>(
data: web::Json<Data>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error>
where
Data: Deserialize<'a> + Send + 'static + PerformCrud,
{
perform::<Data>(data.0, context).await
}

View File

@ -18,7 +18,7 @@ use lemmy_utils::{
ConnectionId, ConnectionId,
LemmyError, LemmyError,
}; };
use lemmy_websocket::{messages::SendAllMessage, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendAllMessage, LemmyContext, UserOperationCrud};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl PerformCrud for EditSite { impl PerformCrud for EditSite {
@ -64,7 +64,7 @@ impl PerformCrud for EditSite {
let res = SiteResponse { site_view }; let res = SiteResponse { site_view };
context.chat_server().do_send(SendAllMessage { context.chat_server().do_send(SendAllMessage {
op: UserOperation::EditSite, op: UserOperationCrud::EditSite,
response: res.clone(), response: res.clone(),
websocket_id, websocket_id,
}); });

View File

@ -12,7 +12,7 @@ use lemmy_db_schema::source::{
}; };
use lemmy_db_views::comment_view::CommentView; use lemmy_db_views::comment_view::CommentView;
use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError}; use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError};
use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation, UserOperationCrud};
pub(crate) async fn receive_create_comment( pub(crate) async fn receive_create_comment(
create: Create, create: Create,
@ -57,7 +57,7 @@ pub(crate) async fn receive_create_comment(
}; };
context.chat_server().do_send(SendComment { context.chat_server().do_send(SendComment {
op: UserOperation::CreateComment, op: UserOperationCrud::CreateComment,
comment: res, comment: res,
websocket_id: None, websocket_id: None,
}); });
@ -98,7 +98,7 @@ pub(crate) async fn receive_update_comment(
}; };
context.chat_server().do_send(SendComment { context.chat_server().do_send(SendComment {
op: UserOperation::EditComment, op: UserOperationCrud::EditComment,
comment: res, comment: res,
websocket_id: None, websocket_id: None,
}); });
@ -220,7 +220,7 @@ pub(crate) async fn receive_delete_comment(
form_id: None, form_id: None,
}; };
context.chat_server().do_send(SendComment { context.chat_server().do_send(SendComment {
op: UserOperation::EditComment, op: UserOperationCrud::EditComment,
comment: res, comment: res,
websocket_id: None, websocket_id: None,
}); });
@ -252,7 +252,7 @@ pub(crate) async fn receive_remove_comment(
form_id: None, form_id: None,
}; };
context.chat_server().do_send(SendComment { context.chat_server().do_send(SendComment {
op: UserOperation::EditComment, op: UserOperationCrud::EditComment,
comment: res, comment: res,
websocket_id: None, websocket_id: None,
}); });

View File

@ -5,7 +5,7 @@ use lemmy_db_queries::{source::comment::Comment_, Likeable};
use lemmy_db_schema::source::comment::{Comment, CommentLike}; use lemmy_db_schema::source::comment::{Comment, CommentLike};
use lemmy_db_views::comment_view::CommentView; use lemmy_db_views::comment_view::CommentView;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation, UserOperationCrud};
pub(crate) async fn receive_undo_like_comment( pub(crate) async fn receive_undo_like_comment(
like: &Like, like: &Like,
@ -108,7 +108,7 @@ pub(crate) async fn receive_undo_delete_comment(
}; };
context.chat_server().do_send(SendComment { context.chat_server().do_send(SendComment {
op: UserOperation::EditComment, op: UserOperationCrud::EditComment,
comment: res, comment: res,
websocket_id: None, websocket_id: None,
}); });
@ -141,7 +141,7 @@ pub(crate) async fn receive_undo_remove_comment(
}; };
context.chat_server().do_send(SendComment { context.chat_server().do_send(SendComment {
op: UserOperation::EditComment, op: UserOperationCrud::EditComment,
comment: res, comment: res,
websocket_id: None, websocket_id: None,
}); });

View File

@ -3,7 +3,7 @@ use lemmy_db_queries::source::community::Community_;
use lemmy_db_schema::source::community::Community; use lemmy_db_schema::source::community::Community;
use lemmy_db_views_actor::community_view::CommunityView; use lemmy_db_views_actor::community_view::CommunityView;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperationCrud};
pub(crate) async fn receive_delete_community( pub(crate) async fn receive_delete_community(
context: &LemmyContext, context: &LemmyContext,
@ -24,7 +24,7 @@ pub(crate) async fn receive_delete_community(
let community_id = res.community_view.community.id; let community_id = res.community_view.community.id;
context.chat_server().do_send(SendCommunityRoomMessage { context.chat_server().do_send(SendCommunityRoomMessage {
op: UserOperation::EditCommunity, op: UserOperationCrud::EditCommunity,
response: res, response: res,
community_id, community_id,
websocket_id: None, websocket_id: None,
@ -52,7 +52,7 @@ pub(crate) async fn receive_remove_community(
let community_id = res.community_view.community.id; let community_id = res.community_view.community.id;
context.chat_server().do_send(SendCommunityRoomMessage { context.chat_server().do_send(SendCommunityRoomMessage {
op: UserOperation::EditCommunity, op: UserOperationCrud::EditCommunity,
response: res, response: res,
community_id, community_id,
websocket_id: None, websocket_id: None,
@ -80,7 +80,7 @@ pub(crate) async fn receive_undo_delete_community(
let community_id = res.community_view.community.id; let community_id = res.community_view.community.id;
context.chat_server().do_send(SendCommunityRoomMessage { context.chat_server().do_send(SendCommunityRoomMessage {
op: UserOperation::EditCommunity, op: UserOperationCrud::EditCommunity,
response: res, response: res,
community_id, community_id,
websocket_id: None, websocket_id: None,
@ -109,7 +109,7 @@ pub(crate) async fn receive_undo_remove_community(
let community_id = res.community_view.community.id; let community_id = res.community_view.community.id;
context.chat_server().do_send(SendCommunityRoomMessage { context.chat_server().do_send(SendCommunityRoomMessage {
op: UserOperation::EditCommunity, op: UserOperationCrud::EditCommunity,
response: res, response: res,
community_id, community_id,
websocket_id: None, websocket_id: None,

View File

@ -21,7 +21,7 @@ use lemmy_db_schema::{
}; };
use lemmy_db_views::post_view::PostView; use lemmy_db_views::post_view::PostView;
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation, UserOperationCrud};
pub(crate) async fn receive_create_post( pub(crate) async fn receive_create_post(
create: Create, create: Create,
@ -44,7 +44,7 @@ pub(crate) async fn receive_create_post(
let res = PostResponse { post_view }; let res = PostResponse { post_view };
context.chat_server().do_send(SendPost { context.chat_server().do_send(SendPost {
op: UserOperation::CreatePost, op: UserOperationCrud::CreatePost,
post: res, post: res,
websocket_id: None, websocket_id: None,
}); });
@ -107,7 +107,7 @@ pub(crate) async fn receive_update_post(
let res = PostResponse { post_view }; let res = PostResponse { post_view };
context.chat_server().do_send(SendPost { context.chat_server().do_send(SendPost {
op: UserOperation::EditPost, op: UserOperationCrud::EditPost,
post: res, post: res,
websocket_id: None, websocket_id: None,
}); });
@ -209,7 +209,7 @@ pub(crate) async fn receive_delete_post(
let res = PostResponse { post_view }; let res = PostResponse { post_view };
context.chat_server().do_send(SendPost { context.chat_server().do_send(SendPost {
op: UserOperation::EditPost, op: UserOperationCrud::EditPost,
post: res, post: res,
websocket_id: None, websocket_id: None,
}); });
@ -235,7 +235,7 @@ pub(crate) async fn receive_remove_post(
let res = PostResponse { post_view }; let res = PostResponse { post_view };
context.chat_server().do_send(SendPost { context.chat_server().do_send(SendPost {
op: UserOperation::EditPost, op: UserOperationCrud::EditPost,
post: res, post: res,
websocket_id: None, websocket_id: None,
}); });

View File

@ -5,7 +5,7 @@ use lemmy_db_queries::{source::post::Post_, Likeable};
use lemmy_db_schema::source::post::{Post, PostLike}; use lemmy_db_schema::source::post::{Post, PostLike};
use lemmy_db_views::post_view::PostView; use lemmy_db_views::post_view::PostView;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation, UserOperationCrud};
pub(crate) async fn receive_undo_like_post( pub(crate) async fn receive_undo_like_post(
like: &Like, like: &Like,
@ -89,7 +89,7 @@ pub(crate) async fn receive_undo_delete_post(
let res = PostResponse { post_view }; let res = PostResponse { post_view };
context.chat_server().do_send(SendPost { context.chat_server().do_send(SendPost {
op: UserOperation::EditPost, op: UserOperationCrud::EditPost,
post: res, post: res,
websocket_id: None, websocket_id: None,
}); });
@ -116,7 +116,7 @@ pub(crate) async fn receive_undo_remove_post(
let res = PostResponse { post_view }; let res = PostResponse { post_view };
context.chat_server().do_send(SendPost { context.chat_server().do_send(SendPost {
op: UserOperation::EditPost, op: UserOperationCrud::EditPost,
post: res, post: res,
websocket_id: None, websocket_id: None,
}); });

View File

@ -18,7 +18,7 @@ use lemmy_db_queries::source::private_message::PrivateMessage_;
use lemmy_db_schema::source::private_message::PrivateMessage; use lemmy_db_schema::source::private_message::PrivateMessage;
use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView}; use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView};
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperationCrud};
use url::Url; use url::Url;
pub(crate) async fn receive_create_private_message( pub(crate) async fn receive_create_private_message(
@ -60,7 +60,7 @@ pub(crate) async fn receive_create_private_message(
.id; .id;
context.chat_server().do_send(SendUserRoomMessage { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::CreatePrivateMessage, op: UserOperationCrud::CreatePrivateMessage,
response: res, response: res,
local_recipient_id, local_recipient_id,
websocket_id: None, websocket_id: None,
@ -106,7 +106,7 @@ pub(crate) async fn receive_update_private_message(
.id; .id;
context.chat_server().do_send(SendUserRoomMessage { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::EditPrivateMessage, op: UserOperationCrud::EditPrivateMessage,
response: res, response: res,
local_recipient_id, local_recipient_id,
websocket_id: None, websocket_id: None,
@ -146,7 +146,7 @@ pub(crate) async fn receive_delete_private_message(
.id; .id;
context.chat_server().do_send(SendUserRoomMessage { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::EditPrivateMessage, op: UserOperationCrud::EditPrivateMessage,
response: res, response: res,
local_recipient_id, local_recipient_id,
websocket_id: None, websocket_id: None,
@ -191,7 +191,7 @@ pub(crate) async fn receive_undo_delete_private_message(
.id; .id;
context.chat_server().do_send(SendUserRoomMessage { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::EditPrivateMessage, op: UserOperationCrud::EditPrivateMessage,
response: res, response: res,
local_recipient_id, local_recipient_id,
websocket_id: None, websocket_id: None,

View File

@ -70,9 +70,9 @@ impl LocalUserView {
)) ))
.first::<LocalUserViewTuple>(conn)?; .first::<LocalUserViewTuple>(conn)?;
Ok(Self { Ok(Self {
local_user,
person, person,
counts, counts,
local_user,
}) })
} }
@ -92,9 +92,9 @@ impl LocalUserView {
)) ))
.first::<LocalUserViewTuple>(conn)?; .first::<LocalUserViewTuple>(conn)?;
Ok(Self { Ok(Self {
local_user,
person, person,
counts, counts,
local_user,
}) })
} }
@ -110,9 +110,9 @@ impl LocalUserView {
)) ))
.first::<LocalUserViewTuple>(conn)?; .first::<LocalUserViewTuple>(conn)?;
Ok(Self { Ok(Self {
local_user,
person, person,
counts, counts,
local_user,
}) })
} }
} }
@ -139,9 +139,9 @@ impl LocalUserSettingsView {
)) ))
.first::<LocalUserSettingsViewTuple>(conn)?; .first::<LocalUserSettingsViewTuple>(conn)?;
Ok(Self { Ok(Self {
local_user,
person, person,
counts, counts,
local_user,
}) })
} }
} }

View File

@ -1,4 +1,11 @@
use crate::{messages::*, serialize_websocket_message, LemmyContext, UserOperation}; use crate::{
messages::*,
serialize_websocket_message,
LemmyContext,
OperationType,
UserOperation,
UserOperationCrud,
};
use actix::prelude::*; use actix::prelude::*;
use anyhow::Context as acontext; use anyhow::Context as acontext;
use background_jobs::QueueHandle; use background_jobs::QueueHandle;
@ -33,6 +40,13 @@ type MessageHandlerType = fn(
data: &str, data: &str,
) -> Pin<Box<dyn Future<Output = Result<String, LemmyError>> + '_>>; ) -> Pin<Box<dyn Future<Output = Result<String, LemmyError>> + '_>>;
type MessageHandlerCrudType = fn(
context: LemmyContext,
id: ConnectionId,
op: UserOperationCrud,
data: &str,
) -> Pin<Box<dyn Future<Output = Result<String, LemmyError>> + '_>>;
/// `ChatServer` manages chat rooms and responsible for coordinating chat /// `ChatServer` manages chat rooms and responsible for coordinating chat
/// session. /// session.
pub struct ChatServer { pub struct ChatServer {
@ -63,6 +77,7 @@ pub struct ChatServer {
pub(super) captchas: Vec<CaptchaItem>, pub(super) captchas: Vec<CaptchaItem>,
message_handler: MessageHandlerType, message_handler: MessageHandlerType,
message_handler_crud: MessageHandlerCrudType,
/// An HTTP Client /// An HTTP Client
client: Client, client: Client,
@ -83,6 +98,7 @@ impl ChatServer {
pool: Pool<ConnectionManager<PgConnection>>, pool: Pool<ConnectionManager<PgConnection>>,
rate_limiter: RateLimit, rate_limiter: RateLimit,
message_handler: MessageHandlerType, message_handler: MessageHandlerType,
message_handler_crud: MessageHandlerCrudType,
client: Client, client: Client,
activity_queue: QueueHandle, activity_queue: QueueHandle,
) -> ChatServer { ) -> ChatServer {
@ -97,6 +113,7 @@ impl ChatServer {
rate_limiter, rate_limiter,
captchas: Vec::new(), captchas: Vec::new(),
message_handler, message_handler,
message_handler_crud,
client, client,
activity_queue, activity_queue,
} }
@ -207,14 +224,15 @@ impl ChatServer {
Ok(()) Ok(())
} }
fn send_post_room_message<Response>( fn send_post_room_message<OP, Response>(
&self, &self,
op: &UserOperation, op: &OP,
response: &Response, response: &Response,
post_id: PostId, post_id: PostId,
websocket_id: Option<ConnectionId>, websocket_id: Option<ConnectionId>,
) -> Result<(), LemmyError> ) -> Result<(), LemmyError>
where where
OP: OperationType + ToString,
Response: Serialize, Response: Serialize,
{ {
let res_str = &serialize_websocket_message(op, response)?; let res_str = &serialize_websocket_message(op, response)?;
@ -231,14 +249,15 @@ impl ChatServer {
Ok(()) Ok(())
} }
pub fn send_community_room_message<Response>( pub fn send_community_room_message<OP, Response>(
&self, &self,
op: &UserOperation, op: &OP,
response: &Response, response: &Response,
community_id: CommunityId, community_id: CommunityId,
websocket_id: Option<ConnectionId>, websocket_id: Option<ConnectionId>,
) -> Result<(), LemmyError> ) -> Result<(), LemmyError>
where where
OP: OperationType + ToString,
Response: Serialize, Response: Serialize,
{ {
let res_str = &serialize_websocket_message(op, response)?; let res_str = &serialize_websocket_message(op, response)?;
@ -255,14 +274,15 @@ impl ChatServer {
Ok(()) Ok(())
} }
pub fn send_mod_room_message<Response>( pub fn send_mod_room_message<OP, Response>(
&self, &self,
op: &UserOperation, op: &OP,
response: &Response, response: &Response,
community_id: CommunityId, community_id: CommunityId,
websocket_id: Option<ConnectionId>, websocket_id: Option<ConnectionId>,
) -> Result<(), LemmyError> ) -> Result<(), LemmyError>
where where
OP: OperationType + ToString,
Response: Serialize, Response: Serialize,
{ {
let res_str = &serialize_websocket_message(op, response)?; let res_str = &serialize_websocket_message(op, response)?;
@ -279,13 +299,14 @@ impl ChatServer {
Ok(()) Ok(())
} }
pub fn send_all_message<Response>( pub fn send_all_message<OP, Response>(
&self, &self,
op: &UserOperation, op: &OP,
response: &Response, response: &Response,
websocket_id: Option<ConnectionId>, websocket_id: Option<ConnectionId>,
) -> Result<(), LemmyError> ) -> Result<(), LemmyError>
where where
OP: OperationType + ToString,
Response: Serialize, Response: Serialize,
{ {
let res_str = &serialize_websocket_message(op, response)?; let res_str = &serialize_websocket_message(op, response)?;
@ -300,14 +321,15 @@ impl ChatServer {
Ok(()) Ok(())
} }
pub fn send_user_room_message<Response>( pub fn send_user_room_message<OP, Response>(
&self, &self,
op: &UserOperation, op: &OP,
response: &Response, response: &Response,
recipient_id: LocalUserId, recipient_id: LocalUserId,
websocket_id: Option<ConnectionId>, websocket_id: Option<ConnectionId>,
) -> Result<(), LemmyError> ) -> Result<(), LemmyError>
where where
OP: OperationType + ToString,
Response: Serialize, Response: Serialize,
{ {
let res_str = &serialize_websocket_message(op, response)?; let res_str = &serialize_websocket_message(op, response)?;
@ -324,12 +346,15 @@ impl ChatServer {
Ok(()) Ok(())
} }
pub fn send_comment( pub fn send_comment<OP>(
&self, &self,
user_operation: &UserOperation, user_operation: &OP,
comment: &CommentResponse, comment: &CommentResponse,
websocket_id: Option<ConnectionId>, websocket_id: Option<ConnectionId>,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError>
where
OP: OperationType + ToString,
{
let mut comment_reply_sent = comment.clone(); let mut comment_reply_sent = comment.clone();
// Strip out my specific user info // Strip out my specific user info
@ -373,12 +398,15 @@ impl ChatServer {
Ok(()) Ok(())
} }
pub fn send_post( pub fn send_post<OP>(
&self, &self,
user_operation: &UserOperation, user_operation: &OP,
post_res: &PostResponse, post_res: &PostResponse,
websocket_id: Option<ConnectionId>, websocket_id: Option<ConnectionId>,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError>
where
OP: OperationType + ToString,
{
let community_id = post_res.post_view.community.id; let community_id = post_res.post_view.community.id;
// Don't send my data with it // Don't send my data with it
@ -424,6 +452,7 @@ impl ChatServer {
client: self.client.to_owned(), client: self.client.to_owned(),
activity_queue: self.activity_queue.to_owned(), activity_queue: self.activity_queue.to_owned(),
}; };
let message_handler_crud = self.message_handler_crud;
let message_handler = self.message_handler; let message_handler = self.message_handler;
async move { async move {
let json: Value = serde_json::from_str(&msg.msg)?; let json: Value = serde_json::from_str(&msg.msg)?;
@ -432,13 +461,18 @@ impl ChatServer {
message: "Unknown op type".to_string(), message: "Unknown op type".to_string(),
})?; })?;
if let Ok(user_operation_crud) = UserOperationCrud::from_str(&op) {
let fut = (message_handler_crud)(context, msg.id, user_operation_crud.clone(), data);
match user_operation_crud {
UserOperationCrud::Register => rate_limiter.register().wrap(ip, fut).await,
UserOperationCrud::CreatePost => rate_limiter.post().wrap(ip, fut).await,
UserOperationCrud::CreateCommunity => rate_limiter.register().wrap(ip, fut).await,
_ => rate_limiter.message().wrap(ip, fut).await,
}
} else {
let user_operation = UserOperation::from_str(&op)?; let user_operation = UserOperation::from_str(&op)?;
let fut = (message_handler)(context, msg.id, user_operation.clone(), data); let fut = (message_handler)(context, msg.id, user_operation.clone(), data);
match user_operation { rate_limiter.message().wrap(ip, fut).await
UserOperation::Register => rate_limiter.register().wrap(ip, fut).await,
UserOperation::CreatePost => rate_limiter.post().wrap(ip, fut).await,
UserOperation::CreateCommunity => rate_limiter.register().wrap(ip, fut).await,
_ => rate_limiter.message().wrap(ip, fut).await,
} }
} }
} }

View File

@ -1,6 +1,7 @@
use crate::{ use crate::{
chat_server::{ChatServer, SessionInfo}, chat_server::{ChatServer, SessionInfo},
messages::*, messages::*,
OperationType,
}; };
use actix::{Actor, Context, Handler, ResponseFuture}; use actix::{Actor, Context, Handler, ResponseFuture};
use lemmy_db_schema::naive_now; use lemmy_db_schema::naive_now;
@ -82,26 +83,28 @@ impl Handler<StandardMessage> for ChatServer {
} }
} }
impl<Response> Handler<SendAllMessage<Response>> for ChatServer impl<OP, Response> Handler<SendAllMessage<OP, Response>> for ChatServer
where where
OP: OperationType + ToString,
Response: Serialize, Response: Serialize,
{ {
type Result = (); type Result = ();
fn handle(&mut self, msg: SendAllMessage<Response>, _: &mut Context<Self>) { fn handle(&mut self, msg: SendAllMessage<OP, Response>, _: &mut Context<Self>) {
self self
.send_all_message(&msg.op, &msg.response, msg.websocket_id) .send_all_message(&msg.op, &msg.response, msg.websocket_id)
.ok(); .ok();
} }
} }
impl<Response> Handler<SendUserRoomMessage<Response>> for ChatServer impl<OP, Response> Handler<SendUserRoomMessage<OP, Response>> for ChatServer
where where
OP: OperationType + ToString,
Response: Serialize, Response: Serialize,
{ {
type Result = (); type Result = ();
fn handle(&mut self, msg: SendUserRoomMessage<Response>, _: &mut Context<Self>) { fn handle(&mut self, msg: SendUserRoomMessage<OP, Response>, _: &mut Context<Self>) {
self self
.send_user_room_message( .send_user_room_message(
&msg.op, &msg.op,
@ -113,13 +116,14 @@ where
} }
} }
impl<Response> Handler<SendCommunityRoomMessage<Response>> for ChatServer impl<OP, Response> Handler<SendCommunityRoomMessage<OP, Response>> for ChatServer
where where
OP: OperationType + ToString,
Response: Serialize, Response: Serialize,
{ {
type Result = (); type Result = ();
fn handle(&mut self, msg: SendCommunityRoomMessage<Response>, _: &mut Context<Self>) { fn handle(&mut self, msg: SendCommunityRoomMessage<OP, Response>, _: &mut Context<Self>) {
self self
.send_community_room_message(&msg.op, &msg.response, msg.community_id, msg.websocket_id) .send_community_room_message(&msg.op, &msg.response, msg.community_id, msg.websocket_id)
.ok(); .ok();
@ -139,18 +143,24 @@ where
} }
} }
impl Handler<SendPost> for ChatServer { impl<OP> Handler<SendPost<OP>> for ChatServer
where
OP: OperationType + ToString,
{
type Result = (); type Result = ();
fn handle(&mut self, msg: SendPost, _: &mut Context<Self>) { fn handle(&mut self, msg: SendPost<OP>, _: &mut Context<Self>) {
self.send_post(&msg.op, &msg.post, msg.websocket_id).ok(); self.send_post(&msg.op, &msg.post, msg.websocket_id).ok();
} }
} }
impl Handler<SendComment> for ChatServer { impl<OP> Handler<SendComment<OP>> for ChatServer
where
OP: OperationType + ToString,
{
type Result = (); type Result = ();
fn handle(&mut self, msg: SendComment, _: &mut Context<Self>) { fn handle(&mut self, msg: SendComment<OP>, _: &mut Context<Self>) {
self self
.send_comment(&msg.op, &msg.comment, msg.websocket_id) .send_comment(&msg.op, &msg.comment, msg.websocket_id)
.ok(); .ok();

View File

@ -66,12 +66,13 @@ struct WebsocketResponse<T> {
data: T, data: T,
} }
pub fn serialize_websocket_message<Response>( pub fn serialize_websocket_message<OP, Response>(
op: &UserOperation, op: &OP,
data: &Response, data: &Response,
) -> Result<String, LemmyError> ) -> Result<String, LemmyError>
where where
Response: Serialize, Response: Serialize,
OP: ToString,
{ {
let response = WebsocketResponse { let response = WebsocketResponse {
op: op.to_string(), op: op.to_string(),
@ -83,28 +84,14 @@ where
#[derive(EnumString, ToString, Debug, Clone)] #[derive(EnumString, ToString, Debug, Clone)]
pub enum UserOperation { pub enum UserOperation {
Login, Login,
Register,
GetCaptcha, GetCaptcha,
CreateCommunity,
CreatePost,
ListCommunities,
GetPost,
GetCommunity,
CreateComment,
EditComment,
DeleteComment,
RemoveComment,
MarkCommentAsRead, MarkCommentAsRead,
SaveComment, SaveComment,
CreateCommentLike, CreateCommentLike,
CreateCommentReport, CreateCommentReport,
ResolveCommentReport, ResolveCommentReport,
ListCommentReports, ListCommentReports,
GetPosts,
CreatePostLike, CreatePostLike,
EditPost,
DeletePost,
RemovePost,
LockPost, LockPost,
StickyPost, StickyPost,
SavePost, SavePost,
@ -112,21 +99,14 @@ pub enum UserOperation {
ResolvePostReport, ResolvePostReport,
ListPostReports, ListPostReports,
GetReportCount, GetReportCount,
EditCommunity,
DeleteCommunity,
RemoveCommunity,
FollowCommunity, FollowCommunity,
GetFollowedCommunities, GetFollowedCommunities,
GetPersonDetails,
GetReplies, GetReplies,
GetPersonMentions, GetPersonMentions,
MarkPersonMentionAsRead, MarkPersonMentionAsRead,
GetModlog, GetModlog,
BanFromCommunity, BanFromCommunity,
AddModToCommunity, AddModToCommunity,
CreateSite,
EditSite,
GetSite,
AddAdmin, AddAdmin,
BanPerson, BanPerson,
Search, Search,
@ -134,19 +114,56 @@ pub enum UserOperation {
SaveUserSettings, SaveUserSettings,
TransferCommunity, TransferCommunity,
TransferSite, TransferSite,
DeleteAccount,
PasswordReset, PasswordReset,
PasswordChange, PasswordChange,
CreatePrivateMessage,
EditPrivateMessage,
DeletePrivateMessage,
MarkPrivateMessageAsRead, MarkPrivateMessageAsRead,
GetPrivateMessages,
UserJoin, UserJoin,
GetComments,
GetSiteConfig, GetSiteConfig,
SaveSiteConfig, SaveSiteConfig,
PostJoin, PostJoin,
CommunityJoin, CommunityJoin,
ModJoin, ModJoin,
} }
#[derive(EnumString, ToString, Debug, Clone)]
pub enum UserOperationCrud {
// Site
CreateSite,
GetSite,
EditSite,
// Community
CreateCommunity,
ListCommunities,
GetCommunity,
EditCommunity,
DeleteCommunity,
RemoveCommunity,
// Post
CreatePost,
GetPost,
GetPosts,
EditPost,
DeletePost,
RemovePost,
// Comment
CreateComment,
GetComments,
EditComment,
DeleteComment,
RemoveComment,
// User
Register,
GetPersonDetails,
DeleteAccount,
// Private Message
CreatePrivateMessage,
GetPrivateMessages,
EditPrivateMessage,
DeletePrivateMessage,
}
pub trait OperationType {}
impl OperationType for UserOperationCrud {}
impl OperationType for UserOperation {}

View File

@ -40,16 +40,16 @@ pub struct StandardMessage {
#[derive(Message)] #[derive(Message)]
#[rtype(result = "()")] #[rtype(result = "()")]
pub struct SendAllMessage<Response> { pub struct SendAllMessage<OP: ToString, Response> {
pub op: UserOperation, pub op: OP,
pub response: Response, pub response: Response,
pub websocket_id: Option<ConnectionId>, pub websocket_id: Option<ConnectionId>,
} }
#[derive(Message)] #[derive(Message)]
#[rtype(result = "()")] #[rtype(result = "()")]
pub struct SendUserRoomMessage<Response> { pub struct SendUserRoomMessage<OP: ToString, Response> {
pub op: UserOperation, pub op: OP,
pub response: Response, pub response: Response,
pub local_recipient_id: LocalUserId, pub local_recipient_id: LocalUserId,
pub websocket_id: Option<ConnectionId>, pub websocket_id: Option<ConnectionId>,
@ -57,8 +57,8 @@ pub struct SendUserRoomMessage<Response> {
#[derive(Message)] #[derive(Message)]
#[rtype(result = "()")] #[rtype(result = "()")]
pub struct SendCommunityRoomMessage<Response> { pub struct SendCommunityRoomMessage<OP: ToString, Response> {
pub op: UserOperation, pub op: OP,
pub response: Response, pub response: Response,
pub community_id: CommunityId, pub community_id: CommunityId,
pub websocket_id: Option<ConnectionId>, pub websocket_id: Option<ConnectionId>,
@ -75,16 +75,16 @@ pub struct SendModRoomMessage<Response> {
#[derive(Message)] #[derive(Message)]
#[rtype(result = "()")] #[rtype(result = "()")]
pub struct SendPost { pub struct SendPost<OP: ToString> {
pub op: UserOperation, pub op: OP,
pub post: PostResponse, pub post: PostResponse,
pub websocket_id: Option<ConnectionId>, pub websocket_id: Option<ConnectionId>,
} }
#[derive(Message)] #[derive(Message)]
#[rtype(result = "()")] #[rtype(result = "()")]
pub struct SendComment { pub struct SendComment<OP: ToString> {
pub op: UserOperation, pub op: OP,
pub comment: CommentResponse, pub comment: CommentResponse,
pub websocket_id: Option<ConnectionId>, pub websocket_id: Option<ConnectionId>,
} }

View File

@ -15,8 +15,12 @@ const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
/// How long before lack of client response causes a timeout /// How long before lack of client response causes a timeout
const CLIENT_TIMEOUT: Duration = Duration::from_secs(10); const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);
pub fn config(cfg: &mut web::ServiceConfig) {
cfg.service(web::resource("/ws").to(chat_route));
}
/// Entry point for our route /// Entry point for our route
pub async fn chat_route( async fn chat_route(
req: HttpRequest, req: HttpRequest,
stream: web::Payload, stream: web::Payload,
context: web::Data<LemmyContext>, context: web::Data<LemmyContext>,

View File

@ -29,7 +29,7 @@ services:
- ./volumes/pictrs_alpha:/mnt - ./volumes/pictrs_alpha:/mnt
lemmy-alpha-ui: lemmy-alpha-ui:
image: dessalines/lemmy-ui:0.9.9 image: dessalines/lemmy-ui:0.10.0-rc.12
environment: environment:
- LEMMY_INTERNAL_HOST=lemmy-alpha:8541 - LEMMY_INTERNAL_HOST=lemmy-alpha:8541
- LEMMY_EXTERNAL_HOST=localhost:8541 - LEMMY_EXTERNAL_HOST=localhost:8541

View File

@ -1,20 +1,22 @@
use crate::Perform;
use actix_web::{error::ErrorBadRequest, *}; use actix_web::{error::ErrorBadRequest, *};
use lemmy_api::Perform;
use lemmy_api_common::{comment::*, community::*, person::*, post::*, site::*, websocket::*}; use lemmy_api_common::{comment::*, community::*, person::*, post::*, site::*, websocket::*};
use lemmy_api_crud::PerformCrud;
use lemmy_utils::rate_limit::RateLimit; use lemmy_utils::rate_limit::RateLimit;
use lemmy_websocket::{routes::chat_route, LemmyContext}; use lemmy_websocket::LemmyContext;
use serde::Deserialize; use serde::Deserialize;
pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) { pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
cfg.service( cfg.service(
web::scope("/api/v2") web::scope("/api/v2")
// Websockets
.service(web::resource("/ws").to(chat_route))
// Site // Site
.service( .service(
web::scope("/site") web::scope("/site")
.wrap(rate_limit.message()) .wrap(rate_limit.message())
.route("", web::get().to(route_get_crud::<GetSite>))
// Admin Actions // Admin Actions
.route("", web::post().to(route_post_crud::<CreateSite>))
.route("", web::put().to(route_post_crud::<EditSite>))
.route("/transfer", web::post().to(route_post::<TransferSite>)) .route("/transfer", web::post().to(route_post::<TransferSite>))
.route("/config", web::get().to(route_get::<GetSiteConfig>)) .route("/config", web::get().to(route_get::<GetSiteConfig>))
.route("/config", web::put().to(route_post::<SaveSiteConfig>)), .route("/config", web::put().to(route_post::<SaveSiteConfig>)),
@ -30,10 +32,28 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
.route(web::get().to(route_get::<Search>)), .route(web::get().to(route_get::<Search>)),
) )
// Community // Community
.service(
web::resource("/community")
.guard(guard::Post())
.wrap(rate_limit.register())
.route(web::post().to(route_post_crud::<CreateCommunity>)),
)
.service( .service(
web::scope("/community") web::scope("/community")
.wrap(rate_limit.message()) .wrap(rate_limit.message())
.route("", web::get().to(route_get_crud::<GetCommunity>))
.route("", web::put().to(route_post_crud::<EditCommunity>))
.route("/list", web::get().to(route_get_crud::<ListCommunities>))
.route("/follow", web::post().to(route_post::<FollowCommunity>)) .route("/follow", web::post().to(route_post::<FollowCommunity>))
.route(
"/delete",
web::post().to(route_post_crud::<DeleteCommunity>),
)
// Mod Actions
.route(
"/remove",
web::post().to(route_post_crud::<RemoveCommunity>),
)
.route("/transfer", web::post().to(route_post::<TransferCommunity>)) .route("/transfer", web::post().to(route_post::<TransferCommunity>))
.route("/ban_user", web::post().to(route_post::<BanFromCommunity>)) .route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
.route("/mod", web::post().to(route_post::<AddModToCommunity>)) .route("/mod", web::post().to(route_post::<AddModToCommunity>))
@ -41,11 +61,23 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
.route("/mod/join", web::post().to(route_post::<ModJoin>)), .route("/mod/join", web::post().to(route_post::<ModJoin>)),
) )
// Post // Post
.service(
// Handle POST to /post separately to add the post() rate limitter
web::resource("/post")
.guard(guard::Post())
.wrap(rate_limit.post())
.route(web::post().to(route_post_crud::<CreatePost>)),
)
.service( .service(
web::scope("/post") web::scope("/post")
.wrap(rate_limit.message()) .wrap(rate_limit.message())
.route("", web::get().to(route_get_crud::<GetPost>))
.route("", web::put().to(route_post_crud::<EditPost>))
.route("/delete", web::post().to(route_post_crud::<DeletePost>))
.route("/remove", web::post().to(route_post_crud::<RemovePost>))
.route("/lock", web::post().to(route_post::<LockPost>)) .route("/lock", web::post().to(route_post::<LockPost>))
.route("/sticky", web::post().to(route_post::<StickyPost>)) .route("/sticky", web::post().to(route_post::<StickyPost>))
.route("/list", web::get().to(route_get_crud::<GetPosts>))
.route("/like", web::post().to(route_post::<CreatePostLike>)) .route("/like", web::post().to(route_post::<CreatePostLike>))
.route("/save", web::put().to(route_post::<SavePost>)) .route("/save", web::put().to(route_post::<SavePost>))
.route("/join", web::post().to(route_post::<PostJoin>)) .route("/join", web::post().to(route_post::<PostJoin>))
@ -60,12 +92,17 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
.service( .service(
web::scope("/comment") web::scope("/comment")
.wrap(rate_limit.message()) .wrap(rate_limit.message())
.route("", web::post().to(route_post_crud::<CreateComment>))
.route("", web::put().to(route_post_crud::<EditComment>))
.route("/delete", web::post().to(route_post_crud::<DeleteComment>))
.route("/remove", web::post().to(route_post_crud::<RemoveComment>))
.route( .route(
"/mark_as_read", "/mark_as_read",
web::post().to(route_post::<MarkCommentAsRead>), web::post().to(route_post::<MarkCommentAsRead>),
) )
.route("/like", web::post().to(route_post::<CreateCommentLike>)) .route("/like", web::post().to(route_post::<CreateCommentLike>))
.route("/save", web::put().to(route_post::<SaveComment>)) .route("/save", web::put().to(route_post::<SaveComment>))
.route("/list", web::get().to(route_get_crud::<GetComments>))
.route("/report", web::post().to(route_post::<CreateCommentReport>)) .route("/report", web::post().to(route_post::<CreateCommentReport>))
.route( .route(
"/report/resolve", "/report/resolve",
@ -80,15 +117,32 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
.service( .service(
web::scope("/private_message") web::scope("/private_message")
.wrap(rate_limit.message()) .wrap(rate_limit.message())
.route("/list", web::get().to(route_get_crud::<GetPrivateMessages>))
.route("", web::post().to(route_post_crud::<CreatePrivateMessage>))
.route("", web::put().to(route_post_crud::<EditPrivateMessage>))
.route(
"/delete",
web::post().to(route_post_crud::<DeletePrivateMessage>),
)
.route( .route(
"/mark_as_read", "/mark_as_read",
web::post().to(route_post::<MarkPrivateMessageAsRead>), web::post().to(route_post::<MarkPrivateMessageAsRead>),
), ),
) )
// User
.service(
// Account action, I don't like that it's in /user maybe /accounts
// Handle /user/register separately to add the register() rate limitter
web::resource("/user/register")
.guard(guard::Post())
.wrap(rate_limit.register())
.route(web::post().to(route_post_crud::<Register>)),
)
// User actions // User actions
.service( .service(
web::scope("/user") web::scope("/user")
.wrap(rate_limit.message()) .wrap(rate_limit.message())
.route("", web::get().to(route_get_crud::<GetPersonDetails>))
.route("/mention", web::get().to(route_get::<GetPersonMentions>)) .route("/mention", web::get().to(route_get::<GetPersonMentions>))
.route( .route(
"/mention/mark_as_read", "/mention/mark_as_read",
@ -105,6 +159,10 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
// Account actions. I don't like that they're in /user maybe /accounts // Account actions. I don't like that they're in /user maybe /accounts
.route("/login", web::post().to(route_post::<Login>)) .route("/login", web::post().to(route_post::<Login>))
.route("/get_captcha", web::get().to(route_get::<GetCaptcha>)) .route("/get_captcha", web::get().to(route_get::<GetCaptcha>))
.route(
"/delete_account",
web::post().to(route_post_crud::<DeleteAccount>),
)
.route( .route(
"/password_reset", "/password_reset",
web::post().to(route_post::<PasswordReset>), web::post().to(route_post::<PasswordReset>),
@ -168,3 +226,39 @@ where
{ {
perform::<Data>(data.0, context).await perform::<Data>(data.0, context).await
} }
async fn perform_crud<Request>(
data: Request,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error>
where
Request: PerformCrud,
Request: Send + 'static,
{
let res = data
.perform(&context, None)
.await
.map(|json| HttpResponse::Ok().json(json))
.map_err(ErrorBadRequest)?;
Ok(res)
}
async fn route_get_crud<'a, Data>(
data: web::Query<Data>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error>
where
Data: Deserialize<'a> + Send + 'static + PerformCrud,
{
perform_crud::<Data>(data.0, context).await
}
async fn route_post_crud<'a, Data>(
data: web::Json<Data>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error>
where
Data: Deserialize<'a> + Send + 'static + PerformCrud,
{
perform_crud::<Data>(data.0, context).await
}

View File

@ -1,3 +1,4 @@
#![recursion_limit = "512"] #![recursion_limit = "512"]
pub mod api_routes;
pub mod code_migrations; pub mod code_migrations;
pub mod scheduled_tasks; pub mod scheduled_tasks;

View File

@ -9,10 +9,11 @@ use diesel::{
}; };
use lemmy_api::match_websocket_operation; use lemmy_api::match_websocket_operation;
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_api_crud::match_websocket_operation_crud;
use lemmy_apub::activity_queue::create_activity_queue; use lemmy_apub::activity_queue::create_activity_queue;
use lemmy_db_queries::get_database_url_from_env; use lemmy_db_queries::get_database_url_from_env;
use lemmy_routes::{feeds, images, nodeinfo, webfinger}; use lemmy_routes::{feeds, images, nodeinfo, webfinger};
use lemmy_server::{code_migrations::run_advanced_migrations, scheduled_tasks}; use lemmy_server::{api_routes, code_migrations::run_advanced_migrations, scheduled_tasks};
use lemmy_utils::{ use lemmy_utils::{
rate_limit::{rate_limiter::RateLimiter, RateLimit}, rate_limit::{rate_limiter::RateLimiter, RateLimit},
settings::structs::Settings, settings::structs::Settings,
@ -70,6 +71,7 @@ async fn main() -> Result<(), LemmyError> {
pool.clone(), pool.clone(),
rate_limiter.clone(), rate_limiter.clone(),
|c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)), |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
|c, i, o, d| Box::pin(match_websocket_operation_crud(c, i, o, d)),
Client::default(), Client::default(),
activity_queue.clone(), activity_queue.clone(),
) )
@ -88,8 +90,8 @@ async fn main() -> Result<(), LemmyError> {
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.data(context) .data(context)
// The routes // The routes
.configure(|cfg| lemmy_api_crud::routes::config(cfg, &rate_limiter)) .configure(|cfg| api_routes::config(cfg, &rate_limiter))
.configure(|cfg| lemmy_api::routes::config(cfg, &rate_limiter)) .configure(lemmy_websocket::routes::config)
.configure(lemmy_apub::routes::config) .configure(lemmy_apub::routes::config)
.configure(feeds::config) .configure(feeds::config)
.configure(|cfg| images::config(cfg, &rate_limiter)) .configure(|cfg| images::config(cfg, &rate_limiter))