1 use actix_web::{error::ErrorBadRequest, *};
2 use lemmy_api::Perform;
3 use lemmy_api_common::{comment::*, community::*, person::*, post::*, site::*, websocket::*};
4 use lemmy_api_crud::PerformCrud;
5 use lemmy_utils::rate_limit::RateLimit;
6 use lemmy_websocket::LemmyContext;
7 use serde::Deserialize;
9 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
15 .wrap(rate_limit.message())
16 .route("", web::get().to(route_get_crud::<GetSite>))
18 .route("", web::post().to(route_post_crud::<CreateSite>))
19 .route("", web::put().to(route_post_crud::<EditSite>))
20 .route("/transfer", web::post().to(route_post::<TransferSite>))
21 .route("/config", web::get().to(route_get::<GetSiteConfig>))
22 .route("/config", web::put().to(route_post::<SaveSiteConfig>)),
25 web::resource("/modlog")
26 .wrap(rate_limit.message())
27 .route(web::get().to(route_get::<GetModlog>)),
30 web::resource("/search")
31 .wrap(rate_limit.message())
32 .route(web::get().to(route_get::<Search>)),
36 web::resource("/community")
38 .wrap(rate_limit.register())
39 .route(web::post().to(route_post_crud::<CreateCommunity>)),
42 web::scope("/community")
43 .wrap(rate_limit.message())
44 .route("", web::get().to(route_get_crud::<GetCommunity>))
45 .route("", web::put().to(route_post_crud::<EditCommunity>))
46 .route("/list", web::get().to(route_get_crud::<ListCommunities>))
47 .route("/follow", web::post().to(route_post::<FollowCommunity>))
50 web::post().to(route_post_crud::<DeleteCommunity>),
55 web::post().to(route_post_crud::<RemoveCommunity>),
57 .route("/transfer", web::post().to(route_post::<TransferCommunity>))
58 .route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
59 .route("/mod", web::post().to(route_post::<AddModToCommunity>))
60 .route("/join", web::post().to(route_post::<CommunityJoin>))
61 .route("/mod/join", web::post().to(route_post::<ModJoin>)),
65 // Handle POST to /post separately to add the post() rate limitter
66 web::resource("/post")
68 .wrap(rate_limit.post())
69 .route(web::post().to(route_post_crud::<CreatePost>)),
73 .wrap(rate_limit.message())
74 .route("", web::get().to(route_get_crud::<GetPost>))
75 .route("", web::put().to(route_post_crud::<EditPost>))
76 .route("/delete", web::post().to(route_post_crud::<DeletePost>))
77 .route("/remove", web::post().to(route_post_crud::<RemovePost>))
78 .route("/lock", web::post().to(route_post::<LockPost>))
79 .route("/sticky", web::post().to(route_post::<StickyPost>))
80 .route("/list", web::get().to(route_get_crud::<GetPosts>))
81 .route("/like", web::post().to(route_post::<CreatePostLike>))
82 .route("/save", web::put().to(route_post::<SavePost>))
83 .route("/join", web::post().to(route_post::<PostJoin>))
84 .route("/report", web::post().to(route_post::<CreatePostReport>))
87 web::put().to(route_post::<ResolvePostReport>),
89 .route("/report/list", web::get().to(route_get::<ListPostReports>)),
93 web::scope("/comment")
94 .wrap(rate_limit.message())
95 .route("", web::post().to(route_post_crud::<CreateComment>))
96 .route("", web::put().to(route_post_crud::<EditComment>))
97 .route("/delete", web::post().to(route_post_crud::<DeleteComment>))
98 .route("/remove", web::post().to(route_post_crud::<RemoveComment>))
101 web::post().to(route_post::<MarkCommentAsRead>),
103 .route("/like", web::post().to(route_post::<CreateCommentLike>))
104 .route("/save", web::put().to(route_post::<SaveComment>))
105 .route("/list", web::get().to(route_get_crud::<GetComments>))
106 .route("/report", web::post().to(route_post::<CreateCommentReport>))
109 web::put().to(route_post::<ResolveCommentReport>),
113 web::get().to(route_get::<ListCommentReports>),
118 web::scope("/private_message")
119 .wrap(rate_limit.message())
120 .route("/list", web::get().to(route_get_crud::<GetPrivateMessages>))
121 .route("", web::post().to(route_post_crud::<CreatePrivateMessage>))
122 .route("", web::put().to(route_post_crud::<EditPrivateMessage>))
125 web::post().to(route_post_crud::<DeletePrivateMessage>),
129 web::post().to(route_post::<MarkPrivateMessageAsRead>),
134 // Account action, I don't like that it's in /user maybe /accounts
135 // Handle /user/register separately to add the register() rate limitter
136 web::resource("/user/register")
137 .guard(guard::Post())
138 .wrap(rate_limit.register())
139 .route(web::post().to(route_post_crud::<Register>)),
144 .wrap(rate_limit.message())
145 .route("", web::get().to(route_get_crud::<GetPersonDetails>))
146 .route("/mention", web::get().to(route_get::<GetPersonMentions>))
148 "/mention/mark_as_read",
149 web::post().to(route_post::<MarkPersonMentionAsRead>),
151 .route("/replies", web::get().to(route_get::<GetReplies>))
153 "/followed_communities",
154 web::get().to(route_get::<GetFollowedCommunities>),
156 .route("/join", web::post().to(route_post::<UserJoin>))
157 // Admin action. I don't like that it's in /user
158 .route("/ban", web::post().to(route_post::<BanPerson>))
159 // Account actions. I don't like that they're in /user maybe /accounts
160 .route("/login", web::post().to(route_post::<Login>))
161 .route("/get_captcha", web::get().to(route_get::<GetCaptcha>))
164 web::post().to(route_post_crud::<DeleteAccount>),
168 web::post().to(route_post::<PasswordReset>),
172 web::post().to(route_post::<PasswordChange>),
174 // mark_all_as_read feels off being in this section as well
177 web::post().to(route_post::<MarkAllAsRead>),
180 "/save_user_settings",
181 web::put().to(route_post::<SaveUserSettings>),
183 .route("/report_count", web::get().to(route_get::<GetReportCount>)),
187 web::resource("/admin/add")
188 .wrap(rate_limit.message())
189 .route(web::post().to(route_post::<AddAdmin>)),
194 async fn perform<Request>(
196 context: web::Data<LemmyContext>,
197 ) -> Result<HttpResponse, Error>
200 Request: Send + 'static,
203 .perform(&context, None)
205 .map(|json| HttpResponse::Ok().json(json))
206 .map_err(ErrorBadRequest)?;
210 async fn route_get<'a, Data>(
211 data: web::Query<Data>,
212 context: web::Data<LemmyContext>,
213 ) -> Result<HttpResponse, Error>
215 Data: Deserialize<'a> + Send + 'static + Perform,
217 perform::<Data>(data.0, context).await
220 async fn route_post<'a, Data>(
221 data: web::Json<Data>,
222 context: web::Data<LemmyContext>,
223 ) -> Result<HttpResponse, Error>
225 Data: Deserialize<'a> + Send + 'static + Perform,
227 perform::<Data>(data.0, context).await
230 async fn perform_crud<Request>(
232 context: web::Data<LemmyContext>,
233 ) -> Result<HttpResponse, Error>
235 Request: PerformCrud,
236 Request: Send + 'static,
239 .perform(&context, None)
241 .map(|json| HttpResponse::Ok().json(json))
242 .map_err(ErrorBadRequest)?;
246 async fn route_get_crud<'a, Data>(
247 data: web::Query<Data>,
248 context: web::Data<LemmyContext>,
249 ) -> Result<HttpResponse, Error>
251 Data: Deserialize<'a> + Send + 'static + PerformCrud,
253 perform_crud::<Data>(data.0, context).await
256 async fn route_post_crud<'a, Data>(
257 data: web::Json<Data>,
258 context: web::Data<LemmyContext>,
259 ) -> Result<HttpResponse, Error>
261 Data: Deserialize<'a> + Send + 'static + PerformCrud,
263 perform_crud::<Data>(data.0, context).await