1 use actix_web::{guard, web, Error, HttpResponse, Result};
2 use lemmy_api::Perform;
3 use lemmy_api_common::{
30 context::LemmyContext,
31 custom_emoji::{CreateCustomEmoji, DeleteCustomEmoji, EditCustomEmoji},
46 MarkCommentReplyAsRead,
47 MarkPersonMentionAsRead,
48 PasswordChangeAfterReset,
72 CreatePrivateMessageReport,
76 ListPrivateMessageReports,
77 MarkPrivateMessageAsRead,
78 ResolvePrivateMessageReport,
81 ApproveRegistrationApplication,
84 GetFederatedInstances,
87 GetUnreadRegistrationApplicationCount,
89 ListRegistrationApplications,
96 use lemmy_api_crud::PerformCrud;
99 list_comments::list_comments,
100 list_posts::list_posts,
101 read_community::read_community,
102 read_person::read_person,
103 resolve_object::resolve_object,
108 use lemmy_utils::rate_limit::RateLimitCell;
109 use serde::Deserialize;
111 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
113 web::scope("/api/v3")
117 .wrap(rate_limit.message())
118 .route("", web::get().to(route_get_crud::<GetSite>))
120 .route("", web::post().to(route_post_crud::<CreateSite>))
121 .route("", web::put().to(route_post_crud::<EditSite>)),
124 web::resource("/modlog")
125 .wrap(rate_limit.message())
126 .route(web::get().to(route_get::<GetModlog>)),
129 web::resource("/search")
130 .wrap(rate_limit.search())
131 .route(web::get().to(search)),
134 web::resource("/resolve_object")
135 .wrap(rate_limit.message())
136 .route(web::get().to(resolve_object)),
140 web::resource("/community")
141 .guard(guard::Post())
142 .wrap(rate_limit.register())
143 .route(web::post().to(route_post_crud::<CreateCommunity>)),
146 web::scope("/community")
147 .wrap(rate_limit.message())
148 .route("", web::get().to(read_community))
149 .route("", web::put().to(route_post_crud::<EditCommunity>))
150 .route("/hide", web::put().to(route_post::<HideCommunity>))
151 .route("/list", web::get().to(route_get_crud::<ListCommunities>))
152 .route("/follow", web::post().to(route_post::<FollowCommunity>))
153 .route("/block", web::post().to(route_post::<BlockCommunity>))
156 web::post().to(route_post_crud::<DeleteCommunity>),
161 web::post().to(route_post_crud::<RemoveCommunity>),
163 .route("/transfer", web::post().to(route_post::<TransferCommunity>))
164 .route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
165 .route("/mod", web::post().to(route_post::<AddModToCommunity>)),
168 web::scope("/federated_instances")
169 .wrap(rate_limit.message())
170 .route("", web::get().to(route_get::<GetFederatedInstances>)),
174 // Handle POST to /post separately to add the post() rate limitter
175 web::resource("/post")
176 .guard(guard::Post())
177 .wrap(rate_limit.post())
178 .route(web::post().to(route_post_crud::<CreatePost>)),
182 .wrap(rate_limit.message())
183 .route("", web::get().to(route_get_crud::<GetPost>))
184 .route("", web::put().to(route_post_crud::<EditPost>))
185 .route("/delete", web::post().to(route_post_crud::<DeletePost>))
186 .route("/remove", web::post().to(route_post_crud::<RemovePost>))
189 web::post().to(route_post::<MarkPostAsRead>),
191 .route("/lock", web::post().to(route_post::<LockPost>))
192 .route("/feature", web::post().to(route_post::<FeaturePost>))
193 .route("/list", web::get().to(list_posts))
194 .route("/like", web::post().to(route_post::<CreatePostLike>))
195 .route("/save", web::put().to(route_post::<SavePost>))
196 .route("/report", web::post().to(route_post::<CreatePostReport>))
199 web::put().to(route_post::<ResolvePostReport>),
201 .route("/report/list", web::get().to(route_get::<ListPostReports>))
204 web::get().to(route_get::<GetSiteMetadata>),
209 // Handle POST to /comment separately to add the comment() rate limitter
210 web::resource("/comment")
211 .guard(guard::Post())
212 .wrap(rate_limit.comment())
213 .route(web::post().to(route_post_crud::<CreateComment>)),
216 web::scope("/comment")
217 .wrap(rate_limit.message())
218 .route("", web::get().to(route_get_crud::<GetComment>))
219 .route("", web::put().to(route_post_crud::<EditComment>))
220 .route("/delete", web::post().to(route_post_crud::<DeleteComment>))
221 .route("/remove", web::post().to(route_post_crud::<RemoveComment>))
224 web::post().to(route_post::<MarkCommentReplyAsRead>),
228 web::post().to(route_post::<DistinguishComment>),
230 .route("/like", web::post().to(route_post::<CreateCommentLike>))
231 .route("/save", web::put().to(route_post::<SaveComment>))
232 .route("/list", web::get().to(list_comments))
233 .route("/report", web::post().to(route_post::<CreateCommentReport>))
236 web::put().to(route_post::<ResolveCommentReport>),
240 web::get().to(route_get::<ListCommentReports>),
245 web::scope("/private_message")
246 .wrap(rate_limit.message())
247 .route("/list", web::get().to(route_get_crud::<GetPrivateMessages>))
248 .route("", web::post().to(route_post_crud::<CreatePrivateMessage>))
249 .route("", web::put().to(route_post_crud::<EditPrivateMessage>))
252 web::post().to(route_post_crud::<DeletePrivateMessage>),
256 web::post().to(route_post::<MarkPrivateMessageAsRead>),
260 web::post().to(route_post::<CreatePrivateMessageReport>),
264 web::put().to(route_post::<ResolvePrivateMessageReport>),
268 web::get().to(route_get::<ListPrivateMessageReports>),
273 // Account action, I don't like that it's in /user maybe /accounts
274 // Handle /user/register separately to add the register() rate limitter
275 web::resource("/user/register")
276 .guard(guard::Post())
277 .wrap(rate_limit.register())
278 .route(web::post().to(route_post_crud::<Register>)),
281 // Handle captcha separately
282 web::resource("/user/get_captcha")
283 .wrap(rate_limit.post())
284 .route(web::get().to(route_get::<GetCaptcha>)),
289 .wrap(rate_limit.message())
290 .route("", web::get().to(read_person))
291 .route("/mention", web::get().to(route_get::<GetPersonMentions>))
293 "/mention/mark_as_read",
294 web::post().to(route_post::<MarkPersonMentionAsRead>),
296 .route("/replies", web::get().to(route_get::<GetReplies>))
297 // Admin action. I don't like that it's in /user
298 .route("/ban", web::post().to(route_post::<BanPerson>))
299 .route("/banned", web::get().to(route_get::<GetBannedPersons>))
300 .route("/block", web::post().to(route_post::<BlockPerson>))
301 // Account actions. I don't like that they're in /user maybe /accounts
302 .route("/login", web::post().to(route_post::<Login>))
305 web::post().to(route_post_crud::<DeleteAccount>),
309 web::post().to(route_post::<PasswordReset>),
313 web::post().to(route_post::<PasswordChangeAfterReset>),
315 // mark_all_as_read feels off being in this section as well
318 web::post().to(route_post::<MarkAllAsRead>),
321 "/save_user_settings",
322 web::put().to(route_post::<SaveUserSettings>),
326 web::put().to(route_post::<ChangePassword>),
328 .route("/report_count", web::get().to(route_get::<GetReportCount>))
329 .route("/unread_count", web::get().to(route_get::<GetUnreadCount>))
330 .route("/verify_email", web::post().to(route_post::<VerifyEmail>))
331 .route("/leave_admin", web::post().to(route_post::<LeaveAdmin>)),
336 .wrap(rate_limit.message())
337 .route("/add", web::post().to(route_post::<AddAdmin>))
339 "/registration_application/count",
340 web::get().to(route_get::<GetUnreadRegistrationApplicationCount>),
343 "/registration_application/list",
344 web::get().to(route_get::<ListRegistrationApplications>),
347 "/registration_application/approve",
348 web::put().to(route_post::<ApproveRegistrationApplication>),
352 .route("/person", web::post().to(route_post::<PurgePerson>))
353 .route("/community", web::post().to(route_post::<PurgeCommunity>))
354 .route("/post", web::post().to(route_post::<PurgePost>))
355 .route("/comment", web::post().to(route_post::<PurgeComment>)),
359 web::scope("/custom_emoji")
360 .wrap(rate_limit.message())
361 .route("", web::post().to(route_post_crud::<CreateCustomEmoji>))
362 .route("", web::put().to(route_post_crud::<EditCustomEmoji>))
365 web::post().to(route_post_crud::<DeleteCustomEmoji>),
371 async fn perform<'a, Data>(
373 context: web::Data<LemmyContext>,
374 apub_data: activitypub_federation::config::Data<LemmyContext>,
375 ) -> Result<HttpResponse, Error>
378 + SendActivity<Response = <Data as Perform>::Response>
384 let res = data.perform(&context).await?;
385 SendActivity::send_activity(&data, &res, &apub_data).await?;
386 Ok(HttpResponse::Ok().json(res))
389 async fn route_get<'a, Data>(
390 data: web::Query<Data>,
391 context: web::Data<LemmyContext>,
392 apub_data: activitypub_federation::config::Data<LemmyContext>,
393 ) -> Result<HttpResponse, Error>
396 + SendActivity<Response = <Data as Perform>::Response>
402 perform::<Data>(data.0, context, apub_data).await
405 async fn route_post<'a, Data>(
406 data: web::Json<Data>,
407 context: web::Data<LemmyContext>,
408 apub_data: activitypub_federation::config::Data<LemmyContext>,
409 ) -> Result<HttpResponse, Error>
412 + SendActivity<Response = <Data as Perform>::Response>
418 perform::<Data>(data.0, context, apub_data).await
421 async fn perform_crud<'a, Data>(
423 context: web::Data<LemmyContext>,
424 apub_data: activitypub_federation::config::Data<LemmyContext>,
425 ) -> Result<HttpResponse, Error>
428 + SendActivity<Response = <Data as PerformCrud>::Response>
434 let res = data.perform(&context).await?;
435 SendActivity::send_activity(&data, &res, &apub_data).await?;
436 Ok(HttpResponse::Ok().json(res))
439 async fn route_get_crud<'a, Data>(
440 data: web::Query<Data>,
441 context: web::Data<LemmyContext>,
442 apub_data: activitypub_federation::config::Data<LemmyContext>,
443 ) -> Result<HttpResponse, Error>
446 + SendActivity<Response = <Data as PerformCrud>::Response>
452 perform_crud::<Data>(data.0, context, apub_data).await
455 async fn route_post_crud<'a, Data>(
456 data: web::Json<Data>,
457 context: web::Data<LemmyContext>,
458 apub_data: activitypub_federation::config::Data<LemmyContext>,
459 ) -> Result<HttpResponse, Error>
462 + SendActivity<Response = <Data as PerformCrud>::Response>
468 perform_crud::<Data>(data.0, context, apub_data).await