1 use actix_web::{guard, web, Error, HttpResponse, Result};
2 use lemmy_api::Perform;
3 use lemmy_api_common::{
46 MarkCommentReplyAsRead,
47 MarkPersonMentionAsRead,
48 PasswordChangeAfterReset,
73 CreatePrivateMessageReport,
77 ListPrivateMessageReports,
78 MarkPrivateMessageAsRead,
79 ResolvePrivateMessageReport,
82 ApproveRegistrationApplication,
87 GetUnreadRegistrationApplicationCount,
89 ListRegistrationApplications,
97 websocket::{CommunityJoin, ModJoin, PostJoin, UserJoin},
99 use lemmy_api_crud::PerformCrud;
100 use lemmy_utils::rate_limit::RateLimitCell;
101 use lemmy_websocket::{routes::chat_route, LemmyContext};
102 use serde::Deserialize;
104 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
106 web::scope("/api/v3")
108 .service(web::resource("/ws").to(chat_route))
112 .wrap(rate_limit.message())
113 .route("", web::get().to(route_get_crud::<GetSite>))
115 .route("", web::post().to(route_post_crud::<CreateSite>))
116 .route("", web::put().to(route_post_crud::<EditSite>)),
119 web::resource("/modlog")
120 .wrap(rate_limit.message())
121 .route(web::get().to(route_get::<GetModlog>)),
124 web::resource("/search")
125 .wrap(rate_limit.search())
126 .route(web::get().to(route_get::<Search>)),
129 web::resource("/resolve_object")
130 .wrap(rate_limit.message())
131 .route(web::get().to(route_get::<ResolveObject>)),
135 web::resource("/community")
136 .guard(guard::Post())
137 .wrap(rate_limit.register())
138 .route(web::post().to(route_post_crud::<CreateCommunity>)),
141 web::scope("/community")
142 .wrap(rate_limit.message())
143 .route("", web::get().to(route_get_crud::<GetCommunity>))
144 .route("", web::put().to(route_post_crud::<EditCommunity>))
145 .route("/hide", web::put().to(route_post::<HideCommunity>))
146 .route("/list", web::get().to(route_get_crud::<ListCommunities>))
147 .route("/follow", web::post().to(route_post::<FollowCommunity>))
148 .route("/block", web::post().to(route_post::<BlockCommunity>))
151 web::post().to(route_post_crud::<DeleteCommunity>),
156 web::post().to(route_post_crud::<RemoveCommunity>),
158 .route("/transfer", web::post().to(route_post::<TransferCommunity>))
159 .route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
160 .route("/mod", web::post().to(route_post::<AddModToCommunity>))
161 .route("/join", web::post().to(route_post::<CommunityJoin>))
162 .route("/mod/join", web::post().to(route_post::<ModJoin>)),
166 // Handle POST to /post separately to add the post() rate limitter
167 web::resource("/post")
168 .guard(guard::Post())
169 .wrap(rate_limit.post())
170 .route(web::post().to(route_post_crud::<CreatePost>)),
174 .wrap(rate_limit.message())
175 .route("", web::get().to(route_get_crud::<GetPost>))
176 .route("", web::put().to(route_post_crud::<EditPost>))
177 .route("/delete", web::post().to(route_post_crud::<DeletePost>))
178 .route("/remove", web::post().to(route_post_crud::<RemovePost>))
181 web::post().to(route_post::<MarkPostAsRead>),
183 .route("/lock", web::post().to(route_post::<LockPost>))
184 .route("/sticky", web::post().to(route_post::<StickyPost>))
185 .route("/list", web::get().to(route_get_crud::<GetPosts>))
186 .route("/like", web::post().to(route_post::<CreatePostLike>))
187 .route("/save", web::put().to(route_post::<SavePost>))
188 .route("/join", web::post().to(route_post::<PostJoin>))
189 .route("/report", web::post().to(route_post::<CreatePostReport>))
192 web::put().to(route_post::<ResolvePostReport>),
194 .route("/report/list", web::get().to(route_get::<ListPostReports>))
197 web::get().to(route_get::<GetSiteMetadata>),
202 // Handle POST to /comment separately to add the comment() rate limitter
203 web::resource("/comment")
204 .guard(guard::Post())
205 .wrap(rate_limit.comment())
206 .route(web::post().to(route_post_crud::<CreateComment>)),
209 web::scope("/comment")
210 .wrap(rate_limit.message())
211 .route("", web::get().to(route_get_crud::<GetComment>))
212 .route("", web::put().to(route_post_crud::<EditComment>))
213 .route("/delete", web::post().to(route_post_crud::<DeleteComment>))
214 .route("/remove", web::post().to(route_post_crud::<RemoveComment>))
217 web::post().to(route_post::<MarkCommentReplyAsRead>),
219 .route("/like", web::post().to(route_post::<CreateCommentLike>))
220 .route("/save", web::put().to(route_post::<SaveComment>))
221 .route("/list", web::get().to(route_get_crud::<GetComments>))
222 .route("/report", web::post().to(route_post::<CreateCommentReport>))
225 web::put().to(route_post::<ResolveCommentReport>),
229 web::get().to(route_get::<ListCommentReports>),
234 web::scope("/private_message")
235 .wrap(rate_limit.message())
236 .route("/list", web::get().to(route_get_crud::<GetPrivateMessages>))
237 .route("", web::post().to(route_post_crud::<CreatePrivateMessage>))
238 .route("", web::put().to(route_post_crud::<EditPrivateMessage>))
241 web::post().to(route_post_crud::<DeletePrivateMessage>),
245 web::post().to(route_post::<MarkPrivateMessageAsRead>),
249 web::post().to(route_post::<CreatePrivateMessageReport>),
253 web::put().to(route_post::<ResolvePrivateMessageReport>),
257 web::get().to(route_get::<ListPrivateMessageReports>),
262 // Account action, I don't like that it's in /user maybe /accounts
263 // Handle /user/register separately to add the register() rate limitter
264 web::resource("/user/register")
265 .guard(guard::Post())
266 .wrap(rate_limit.register())
267 .route(web::post().to(route_post_crud::<Register>)),
270 // Handle captcha separately
271 web::resource("/user/get_captcha")
272 .wrap(rate_limit.post())
273 .route(web::get().to(route_get::<GetCaptcha>)),
278 .wrap(rate_limit.message())
279 .route("", web::get().to(route_get_crud::<GetPersonDetails>))
280 .route("/mention", web::get().to(route_get::<GetPersonMentions>))
282 "/mention/mark_as_read",
283 web::post().to(route_post::<MarkPersonMentionAsRead>),
285 .route("/replies", web::get().to(route_get::<GetReplies>))
286 .route("/join", web::post().to(route_post::<UserJoin>))
287 // Admin action. I don't like that it's in /user
288 .route("/ban", web::post().to(route_post::<BanPerson>))
289 .route("/banned", web::get().to(route_get::<GetBannedPersons>))
290 .route("/block", web::post().to(route_post::<BlockPerson>))
291 // Account actions. I don't like that they're in /user maybe /accounts
292 .route("/login", web::post().to(route_post::<Login>))
295 web::post().to(route_post_crud::<DeleteAccount>),
299 web::post().to(route_post::<PasswordReset>),
303 web::post().to(route_post::<PasswordChangeAfterReset>),
305 // mark_all_as_read feels off being in this section as well
308 web::post().to(route_post::<MarkAllAsRead>),
311 "/save_user_settings",
312 web::put().to(route_post::<SaveUserSettings>),
316 web::put().to(route_post::<ChangePassword>),
318 .route("/report_count", web::get().to(route_get::<GetReportCount>))
319 .route("/unread_count", web::get().to(route_get::<GetUnreadCount>))
320 .route("/verify_email", web::post().to(route_post::<VerifyEmail>))
321 .route("/leave_admin", web::post().to(route_post::<LeaveAdmin>)),
326 .wrap(rate_limit.message())
327 .route("/add", web::post().to(route_post::<AddAdmin>))
329 "/registration_application/count",
330 web::get().to(route_get::<GetUnreadRegistrationApplicationCount>),
333 "/registration_application/list",
334 web::get().to(route_get::<ListRegistrationApplications>),
337 "/registration_application/approve",
338 web::put().to(route_post::<ApproveRegistrationApplication>),
342 web::scope("/admin/purge")
343 .wrap(rate_limit.message())
344 .route("/person", web::post().to(route_post::<PurgePerson>))
345 .route("/community", web::post().to(route_post::<PurgeCommunity>))
346 .route("/post", web::post().to(route_post::<PurgePost>))
347 .route("/comment", web::post().to(route_post::<PurgeComment>)),
352 async fn perform<Request>(
354 context: web::Data<LemmyContext>,
355 ) -> Result<HttpResponse, Error>
358 Request: Send + 'static,
361 .perform(&context, None)
363 .map(|json| HttpResponse::Ok().json(json))?;
367 async fn route_get<'a, Data>(
368 data: web::Query<Data>,
369 context: web::Data<LemmyContext>,
370 ) -> Result<HttpResponse, Error>
372 Data: Deserialize<'a> + Send + 'static + Perform,
374 perform::<Data>(data.0, context).await
377 async fn route_post<'a, Data>(
378 data: web::Json<Data>,
379 context: web::Data<LemmyContext>,
380 ) -> Result<HttpResponse, Error>
382 Data: Deserialize<'a> + Send + 'static + Perform,
384 perform::<Data>(data.0, context).await
387 async fn perform_crud<Request>(
389 context: web::Data<LemmyContext>,
390 ) -> Result<HttpResponse, Error>
392 Request: PerformCrud,
393 Request: Send + 'static,
396 .perform(&context, None)
398 .map(|json| HttpResponse::Ok().json(json))?;
402 async fn route_get_crud<'a, Data>(
403 data: web::Query<Data>,
404 context: web::Data<LemmyContext>,
405 ) -> Result<HttpResponse, Error>
407 Data: Deserialize<'a> + Send + 'static + PerformCrud,
409 perform_crud::<Data>(data.0, context).await
412 async fn route_post_crud<'a, Data>(
413 data: web::Json<Data>,
414 context: web::Data<LemmyContext>,
415 ) -> Result<HttpResponse, Error>
417 Data: Deserialize<'a> + Send + 'static + PerformCrud,
419 perform_crud::<Data>(data.0, context).await