1 use crate::PerformCrud;
2 use actix_web::{error::ErrorBadRequest, *};
3 use lemmy_api_common::{comment::*, community::*, person::*, post::*, site::*};
4 use lemmy_utils::rate_limit::RateLimit;
5 use lemmy_websocket::LemmyContext;
6 use serde::Deserialize;
8 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
15 .wrap(rate_limit.message())
16 .route("", web::get().to(route_get::<GetSite>))
18 .route("", web::post().to(route_post::<CreateSite>))
19 .route("", web::put().to(route_post::<EditSite>)),
23 web::resource("/community")
25 .wrap(rate_limit.register())
26 .route(web::post().to(route_post::<CreateCommunity>)),
29 web::scope("/community")
30 .wrap(rate_limit.message())
31 .route("", web::get().to(route_get::<GetCommunity>))
32 .route("", web::put().to(route_post::<EditCommunity>))
33 .route("/list", web::get().to(route_get::<ListCommunities>))
34 .route("/delete", web::post().to(route_post::<DeleteCommunity>))
36 .route("/remove", web::post().to(route_post::<RemoveCommunity>)),
40 // Handle POST to /post separately to add the post() rate limitter
41 web::resource("/post")
43 .wrap(rate_limit.post())
44 .route(web::post().to(route_post::<CreatePost>)),
48 .wrap(rate_limit.message())
49 .route("", web::get().to(route_get::<GetPost>))
50 .route("", web::put().to(route_post::<EditPost>))
51 .route("/delete", web::post().to(route_post::<DeletePost>))
52 .route("/remove", web::post().to(route_post::<RemovePost>))
53 .route("/list", web::get().to(route_get::<GetPosts>)),
57 web::scope("/comment")
58 .wrap(rate_limit.message())
59 .route("", web::post().to(route_post::<CreateComment>))
60 .route("", web::put().to(route_post::<EditComment>))
61 .route("/delete", web::post().to(route_post::<DeleteComment>))
62 .route("/remove", web::post().to(route_post::<RemoveComment>))
63 .route("/list", web::get().to(route_get::<GetComments>)),
68 web::scope("/private_message")
69 .wrap(rate_limit.message())
70 .route("/list", web::get().to(route_get::<GetPrivateMessages>))
71 .route("", web::post().to(route_post::<CreatePrivateMessage>))
72 .route("", web::put().to(route_post::<EditPrivateMessage>))
75 web::post().to(route_post::<DeletePrivateMessage>),
80 // Account action, I don't like that it's in /user maybe /accounts
81 // Handle /user/register separately to add the register() rate limitter
82 web::resource("/user/register")
84 .wrap(rate_limit.register())
85 .route(web::post().to(route_post::<Register>)),
90 .wrap(rate_limit.message())
91 .route("", web::get().to(route_get::<GetPersonDetails>))
94 web::post().to(route_post::<DeleteAccount>),
99 async fn perform<Request>(
101 context: web::Data<LemmyContext>,
102 ) -> Result<HttpResponse, Error>
104 Request: PerformCrud,
105 Request: Send + 'static,
108 .perform(&context, None)
110 .map(|json| HttpResponse::Ok().json(json))
111 .map_err(ErrorBadRequest)?;
115 async fn route_get<'a, Data>(
116 data: web::Query<Data>,
117 context: web::Data<LemmyContext>,
118 ) -> Result<HttpResponse, Error>
120 Data: Deserialize<'a> + Send + 'static + PerformCrud,
122 perform::<Data>(data.0, context).await
125 async fn route_post<'a, Data>(
126 data: web::Json<Data>,
127 context: web::Data<LemmyContext>,
128 ) -> Result<HttpResponse, Error>
130 Data: Deserialize<'a> + Send + 'static + PerformCrud,
132 perform::<Data>(data.0, context).await