use actix_web::*; use lemmy_api::Perform; use lemmy_api_common::{ comment::*, community::*, person::*, post::*, private_message::*, site::*, websocket::*, }; use lemmy_api_crud::PerformCrud; use lemmy_utils::rate_limit::RateLimit; use lemmy_websocket::{routes::chat_route, LemmyContext}; use serde::Deserialize; pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) { cfg.service( web::scope("/api/v3") // Websocket .service(web::resource("/ws").to(chat_route)) // Site .service( web::scope("/site") .wrap(rate_limit.message()) .route("", web::get().to(route_get_crud::)) // Admin Actions .route("", web::post().to(route_post_crud::)) .route("", web::put().to(route_post_crud::)), ) .service( web::resource("/modlog") .wrap(rate_limit.message()) .route(web::get().to(route_get::)), ) .service( web::resource("/search") .wrap(rate_limit.search()) .route(web::get().to(route_get::)), ) .service( web::resource("/resolve_object") .wrap(rate_limit.message()) .route(web::get().to(route_get::)), ) // Community .service( web::resource("/community") .guard(guard::Post()) .wrap(rate_limit.register()) .route(web::post().to(route_post_crud::)), ) .service( web::scope("/community") .wrap(rate_limit.message()) .route("", web::get().to(route_get_crud::)) .route("", web::put().to(route_post_crud::)) .route("/hide", web::put().to(route_post::)) .route("/list", web::get().to(route_get_crud::)) .route("/follow", web::post().to(route_post::)) .route("/block", web::post().to(route_post::)) .route( "/delete", web::post().to(route_post_crud::), ) // Mod Actions .route( "/remove", web::post().to(route_post_crud::), ) .route("/transfer", web::post().to(route_post::)) .route("/ban_user", web::post().to(route_post::)) .route("/mod", web::post().to(route_post::)) .route("/join", web::post().to(route_post::)) .route("/mod/join", web::post().to(route_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::)), ) .service( web::scope("/post") .wrap(rate_limit.message()) .route("", web::get().to(route_get_crud::)) .route("", web::put().to(route_post_crud::)) .route("/delete", web::post().to(route_post_crud::)) .route("/remove", web::post().to(route_post_crud::)) .route( "/mark_as_read", web::post().to(route_post::), ) .route("/lock", web::post().to(route_post::)) .route("/sticky", web::post().to(route_post::)) .route("/list", web::get().to(route_get_crud::)) .route("/like", web::post().to(route_post::)) .route("/save", web::put().to(route_post::)) .route("/join", web::post().to(route_post::)) .route("/report", web::post().to(route_post::)) .route( "/report/resolve", web::put().to(route_post::), ) .route("/report/list", web::get().to(route_get::)) .route( "/site_metadata", web::get().to(route_get::), ), ) // Comment .service( // Handle POST to /comment separately to add the comment() rate limitter web::resource("/comment") .guard(guard::Post()) .wrap(rate_limit.comment()) .route(web::post().to(route_post_crud::)), ) .service( web::scope("/comment") .wrap(rate_limit.message()) .route("", web::get().to(route_get_crud::)) .route("", web::put().to(route_post_crud::)) .route("/delete", web::post().to(route_post_crud::)) .route("/remove", web::post().to(route_post_crud::)) .route( "/mark_as_read", web::post().to(route_post::), ) .route("/like", web::post().to(route_post::)) .route("/save", web::put().to(route_post::)) .route("/list", web::get().to(route_get_crud::)) .route("/report", web::post().to(route_post::)) .route( "/report/resolve", web::put().to(route_post::), ) .route( "/report/list", web::get().to(route_get::), ), ) // Private Message .service( web::scope("/private_message") .wrap(rate_limit.message()) .route("/list", web::get().to(route_get_crud::)) .route("", web::post().to(route_post_crud::)) .route("", web::put().to(route_post_crud::)) .route( "/delete", web::post().to(route_post_crud::), ) .route( "/mark_as_read", web::post().to(route_post::), ) .route( "/report", web::post().to(route_post::), ) .route( "/report/resolve", web::put().to(route_post::), ) .route( "/report/list", web::get().to(route_get::), ), ) // 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::)), ) .service( // Handle captcha separately web::resource("/user/get_captcha") .wrap(rate_limit.post()) .route(web::get().to(route_get::)), ) // User actions .service( web::scope("/user") .wrap(rate_limit.message()) .route("", web::get().to(route_get_crud::)) .route("/mention", web::get().to(route_get::)) .route( "/mention/mark_as_read", web::post().to(route_post::), ) .route("/replies", web::get().to(route_get::)) .route("/join", web::post().to(route_post::)) // Admin action. I don't like that it's in /user .route("/ban", web::post().to(route_post::)) .route("/banned", web::get().to(route_get::)) .route("/block", web::post().to(route_post::)) // Account actions. I don't like that they're in /user maybe /accounts .route("/login", web::post().to(route_post::)) .route( "/delete_account", web::post().to(route_post_crud::), ) .route( "/password_reset", web::post().to(route_post::), ) .route( "/password_change", web::post().to(route_post::), ) // mark_all_as_read feels off being in this section as well .route( "/mark_all_as_read", web::post().to(route_post::), ) .route( "/save_user_settings", web::put().to(route_post::), ) .route( "/change_password", web::put().to(route_post::), ) .route("/report_count", web::get().to(route_get::)) .route("/unread_count", web::get().to(route_get::)) .route("/verify_email", web::post().to(route_post::)) .route("/leave_admin", web::post().to(route_post::)), ) // Admin Actions .service( web::scope("/admin") .wrap(rate_limit.message()) .route("/add", web::post().to(route_post::)) .route( "/registration_application/count", web::get().to(route_get::), ) .route( "/registration_application/list", web::get().to(route_get::), ) .route( "/registration_application/approve", web::put().to(route_post::), ), ) .service( web::scope("/admin/purge") .wrap(rate_limit.message()) .route("/person", web::post().to(route_post::)) .route("/community", web::post().to(route_post::)) .route("/post", web::post().to(route_post::)) .route("/comment", web::post().to(route_post::)), ), ); } async fn perform( data: Request, context: web::Data, ) -> Result where Request: Perform, Request: Send + 'static, { let res = data .perform(&context, None) .await .map(|json| HttpResponse::Ok().json(json))?; Ok(res) } async fn route_get<'a, Data>( data: web::Query, context: web::Data, ) -> Result where Data: Deserialize<'a> + Send + 'static + Perform, { perform::(data.0, context).await } async fn route_post<'a, Data>( data: web::Json, context: web::Data, ) -> Result where Data: Deserialize<'a> + Send + 'static + Perform, { perform::(data.0, context).await } async fn perform_crud( data: Request, context: web::Data, ) -> Result where Request: PerformCrud, Request: Send + 'static, { let res = data .perform(&context, None) .await .map(|json| HttpResponse::Ok().json(json))?; Ok(res) } async fn route_get_crud<'a, Data>( data: web::Query, context: web::Data, ) -> Result where Data: Deserialize<'a> + Send + 'static + PerformCrud, { perform_crud::(data.0, context).await } async fn route_post_crud<'a, Data>( data: web::Json, context: web::Data, ) -> Result where Data: Deserialize<'a> + Send + 'static + PerformCrud, { perform_crud::(data.0, context).await }