2 use actix_web::{error::ErrorBadRequest, *};
3 use lemmy_api_structs::{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())
17 .route("", web::get().to(route_get::<GetSite>))
19 .route("", web::post().to(route_post::<CreateSite>))
20 .route("", web::put().to(route_post::<EditSite>))
21 .route("/transfer", web::post().to(route_post::<TransferSite>))
22 .route("/config", web::get().to(route_get::<GetSiteConfig>))
23 .route("/config", web::put().to(route_post::<SaveSiteConfig>)),
26 web::resource("/modlog")
27 .wrap(rate_limit.message())
28 .route(web::get().to(route_get::<GetModlog>)),
31 web::resource("/search")
32 .wrap(rate_limit.message())
33 .route(web::get().to(route_get::<Search>)),
37 web::resource("/community")
39 .wrap(rate_limit.register())
40 .route(web::post().to(route_post::<CreateCommunity>)),
43 web::scope("/community")
44 .wrap(rate_limit.message())
45 .route("", web::get().to(route_get::<GetCommunity>))
46 .route("", web::put().to(route_post::<EditCommunity>))
47 .route("/list", web::get().to(route_get::<ListCommunities>))
48 .route("/follow", web::post().to(route_post::<FollowCommunity>))
49 .route("/delete", web::post().to(route_post::<DeleteCommunity>))
51 .route("/remove", web::post().to(route_post::<RemoveCommunity>))
52 .route("/transfer", web::post().to(route_post::<TransferCommunity>))
53 .route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
54 .route("/mod", web::post().to(route_post::<AddModToCommunity>))
55 .route("/join", web::post().to(route_post::<CommunityJoin>))
56 .route("/mod/join", web::post().to(route_post::<ModJoin>)),
60 // Handle POST to /post separately to add the post() rate limitter
61 web::resource("/post")
63 .wrap(rate_limit.post())
64 .route(web::post().to(route_post::<CreatePost>)),
68 .wrap(rate_limit.message())
69 .route("", web::get().to(route_get::<GetPost>))
70 .route("", web::put().to(route_post::<EditPost>))
71 .route("/delete", web::post().to(route_post::<DeletePost>))
72 .route("/remove", web::post().to(route_post::<RemovePost>))
73 .route("/lock", web::post().to(route_post::<LockPost>))
74 .route("/sticky", web::post().to(route_post::<StickyPost>))
75 .route("/list", web::get().to(route_get::<GetPosts>))
76 .route("/like", web::post().to(route_post::<CreatePostLike>))
77 .route("/save", web::put().to(route_post::<SavePost>))
78 .route("/join", web::post().to(route_post::<PostJoin>))
79 .route("/report", web::post().to(route_post::<CreatePostReport>))
82 web::put().to(route_post::<ResolvePostReport>),
84 .route("/report/list", web::get().to(route_get::<ListPostReports>)),
88 web::scope("/comment")
89 .wrap(rate_limit.message())
90 .route("", web::post().to(route_post::<CreateComment>))
91 .route("", web::put().to(route_post::<EditComment>))
92 .route("/delete", web::post().to(route_post::<DeleteComment>))
93 .route("/remove", web::post().to(route_post::<RemoveComment>))
96 web::post().to(route_post::<MarkCommentAsRead>),
98 .route("/like", web::post().to(route_post::<CreateCommentLike>))
99 .route("/save", web::put().to(route_post::<SaveComment>))
100 .route("/list", web::get().to(route_get::<GetComments>))
101 .route("/report", web::post().to(route_post::<CreateCommentReport>))
104 web::put().to(route_post::<ResolveCommentReport>),
108 web::get().to(route_get::<ListCommentReports>),
113 web::scope("/private_message")
114 .wrap(rate_limit.message())
115 .route("/list", web::get().to(route_get::<GetPrivateMessages>))
116 .route("", web::post().to(route_post::<CreatePrivateMessage>))
117 .route("", web::put().to(route_post::<EditPrivateMessage>))
120 web::post().to(route_post::<DeletePrivateMessage>),
124 web::post().to(route_post::<MarkPrivateMessageAsRead>),
129 // Account action, I don't like that it's in /user maybe /accounts
130 // Handle /user/register separately to add the register() rate limitter
131 web::resource("/user/register")
132 .guard(guard::Post())
133 .wrap(rate_limit.register())
134 .route(web::post().to(route_post::<Register>)),
139 .wrap(rate_limit.message())
140 .route("", web::get().to(route_get::<GetPersonDetails>))
141 .route("/mention", web::get().to(route_get::<GetPersonMentions>))
143 "/mention/mark_as_read",
144 web::post().to(route_post::<MarkPersonMentionAsRead>),
146 .route("/replies", web::get().to(route_get::<GetReplies>))
148 "/followed_communities",
149 web::get().to(route_get::<GetFollowedCommunities>),
151 .route("/join", web::post().to(route_post::<UserJoin>))
152 // Admin action. I don't like that it's in /user
153 .route("/ban", web::post().to(route_post::<BanPerson>))
154 // Account actions. I don't like that they're in /user maybe /accounts
155 .route("/login", web::post().to(route_post::<Login>))
156 .route("/get_captcha", web::get().to(route_get::<GetCaptcha>))
159 web::post().to(route_post::<DeleteAccount>),
163 web::post().to(route_post::<PasswordReset>),
167 web::post().to(route_post::<PasswordChange>),
169 // mark_all_as_read feels off being in this section as well
172 web::post().to(route_post::<MarkAllAsRead>),
175 "/save_user_settings",
176 web::put().to(route_post::<SaveUserSettings>),
178 .route("/report_count", web::get().to(route_get::<GetReportCount>)),
182 web::resource("/admin/add")
183 .wrap(rate_limit.message())
184 .route(web::post().to(route_post::<AddAdmin>)),
189 async fn perform<Request>(
191 context: web::Data<LemmyContext>,
192 ) -> Result<HttpResponse, Error>
195 Request: Send + 'static,
198 .perform(&context, None)
200 .map(|json| HttpResponse::Ok().json(json))
201 .map_err(ErrorBadRequest)?;
205 async fn route_get<'a, Data>(
206 data: web::Query<Data>,
207 context: web::Data<LemmyContext>,
208 ) -> Result<HttpResponse, Error>
210 Data: Deserialize<'a> + Send + 'static + Perform,
212 perform::<Data>(data.0, context).await
215 async fn route_post<'a, Data>(
216 data: web::Json<Data>,
217 context: web::Data<LemmyContext>,
218 ) -> Result<HttpResponse, Error>
220 Data: Deserialize<'a> + Send + 'static + Perform,
222 perform::<Data>(data.0, context).await