2 use actix_web::{error::ErrorBadRequest, *};
3 use lemmy_api_common::{comment::*, community::*, person::*, post::*, site::*, websocket::*};
4 use lemmy_utils::rate_limit::RateLimit;
5 use lemmy_websocket::{routes::chat_route, LemmyContext};
6 use serde::Deserialize;
8 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
12 .service(web::resource("/ws").to(chat_route))
16 .wrap(rate_limit.message())
18 .route("/transfer", web::post().to(route_post::<TransferSite>))
19 .route("/config", web::get().to(route_get::<GetSiteConfig>))
20 .route("/config", web::put().to(route_post::<SaveSiteConfig>)),
23 web::resource("/modlog")
24 .wrap(rate_limit.message())
25 .route(web::get().to(route_get::<GetModlog>)),
28 web::resource("/search")
29 .wrap(rate_limit.message())
30 .route(web::get().to(route_get::<Search>)),
34 web::scope("/community")
35 .wrap(rate_limit.message())
36 .route("/follow", web::post().to(route_post::<FollowCommunity>))
37 .route("/transfer", web::post().to(route_post::<TransferCommunity>))
38 .route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
39 .route("/mod", web::post().to(route_post::<AddModToCommunity>))
40 .route("/join", web::post().to(route_post::<CommunityJoin>))
41 .route("/mod/join", web::post().to(route_post::<ModJoin>)),
46 .wrap(rate_limit.message())
47 .route("/lock", web::post().to(route_post::<LockPost>))
48 .route("/sticky", web::post().to(route_post::<StickyPost>))
49 .route("/like", web::post().to(route_post::<CreatePostLike>))
50 .route("/save", web::put().to(route_post::<SavePost>))
51 .route("/join", web::post().to(route_post::<PostJoin>))
52 .route("/report", web::post().to(route_post::<CreatePostReport>))
55 web::put().to(route_post::<ResolvePostReport>),
57 .route("/report/list", web::get().to(route_get::<ListPostReports>)),
61 web::scope("/comment")
62 .wrap(rate_limit.message())
65 web::post().to(route_post::<MarkCommentAsRead>),
67 .route("/like", web::post().to(route_post::<CreateCommentLike>))
68 .route("/save", web::put().to(route_post::<SaveComment>))
69 .route("/report", web::post().to(route_post::<CreateCommentReport>))
72 web::put().to(route_post::<ResolveCommentReport>),
76 web::get().to(route_get::<ListCommentReports>),
81 web::scope("/private_message")
82 .wrap(rate_limit.message())
85 web::post().to(route_post::<MarkPrivateMessageAsRead>),
91 .wrap(rate_limit.message())
92 .route("/mention", web::get().to(route_get::<GetPersonMentions>))
94 "/mention/mark_as_read",
95 web::post().to(route_post::<MarkPersonMentionAsRead>),
97 .route("/replies", web::get().to(route_get::<GetReplies>))
99 "/followed_communities",
100 web::get().to(route_get::<GetFollowedCommunities>),
102 .route("/join", web::post().to(route_post::<UserJoin>))
103 // Admin action. I don't like that it's in /user
104 .route("/ban", web::post().to(route_post::<BanPerson>))
105 // Account actions. I don't like that they're in /user maybe /accounts
106 .route("/login", web::post().to(route_post::<Login>))
107 .route("/get_captcha", web::get().to(route_get::<GetCaptcha>))
110 web::post().to(route_post::<PasswordReset>),
114 web::post().to(route_post::<PasswordChange>),
116 // mark_all_as_read feels off being in this section as well
119 web::post().to(route_post::<MarkAllAsRead>),
122 "/save_user_settings",
123 web::put().to(route_post::<SaveUserSettings>),
125 .route("/report_count", web::get().to(route_get::<GetReportCount>)),
129 web::resource("/admin/add")
130 .wrap(rate_limit.message())
131 .route(web::post().to(route_post::<AddAdmin>)),
136 async fn perform<Request>(
138 context: web::Data<LemmyContext>,
139 ) -> Result<HttpResponse, Error>
142 Request: Send + 'static,
145 .perform(&context, None)
147 .map(|json| HttpResponse::Ok().json(json))
148 .map_err(ErrorBadRequest)?;
152 async fn route_get<'a, Data>(
153 data: web::Query<Data>,
154 context: web::Data<LemmyContext>,
155 ) -> Result<HttpResponse, Error>
157 Data: Deserialize<'a> + Send + 'static + Perform,
159 perform::<Data>(data.0, context).await
162 async fn route_post<'a, Data>(
163 data: web::Json<Data>,
164 context: web::Data<LemmyContext>,
165 ) -> Result<HttpResponse, Error>
167 Data: Deserialize<'a> + Send + 'static + Perform,
169 perform::<Data>(data.0, context).await