2 use lemmy_api::Perform;
3 use lemmy_api_common::{
12 use lemmy_api_crud::PerformCrud;
13 use lemmy_utils::rate_limit::RateLimit;
14 use lemmy_websocket::{routes::chat_route, LemmyContext};
15 use serde::Deserialize;
17 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
21 .service(web::resource("/ws").to(chat_route))
25 .wrap(rate_limit.message())
26 .route("", web::get().to(route_get_crud::<GetSite>))
28 .route("", web::post().to(route_post_crud::<CreateSite>))
29 .route("", web::put().to(route_post_crud::<EditSite>)),
32 web::resource("/modlog")
33 .wrap(rate_limit.message())
34 .route(web::get().to(route_get::<GetModlog>)),
37 web::resource("/search")
38 .wrap(rate_limit.search())
39 .route(web::get().to(route_get::<Search>)),
42 web::resource("/resolve_object")
43 .wrap(rate_limit.message())
44 .route(web::get().to(route_get::<ResolveObject>)),
48 web::resource("/community")
50 .wrap(rate_limit.register())
51 .route(web::post().to(route_post_crud::<CreateCommunity>)),
54 web::scope("/community")
55 .wrap(rate_limit.message())
56 .route("", web::get().to(route_get_crud::<GetCommunity>))
57 .route("", web::put().to(route_post_crud::<EditCommunity>))
58 .route("/hide", web::put().to(route_post::<HideCommunity>))
59 .route("/list", web::get().to(route_get_crud::<ListCommunities>))
60 .route("/follow", web::post().to(route_post::<FollowCommunity>))
61 .route("/block", web::post().to(route_post::<BlockCommunity>))
64 web::post().to(route_post_crud::<DeleteCommunity>),
69 web::post().to(route_post_crud::<RemoveCommunity>),
71 .route("/transfer", web::post().to(route_post::<TransferCommunity>))
72 .route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
73 .route("/mod", web::post().to(route_post::<AddModToCommunity>))
74 .route("/join", web::post().to(route_post::<CommunityJoin>))
75 .route("/mod/join", web::post().to(route_post::<ModJoin>)),
79 // Handle POST to /post separately to add the post() rate limitter
80 web::resource("/post")
82 .wrap(rate_limit.post())
83 .route(web::post().to(route_post_crud::<CreatePost>)),
87 .wrap(rate_limit.message())
88 .route("", web::get().to(route_get_crud::<GetPost>))
89 .route("", web::put().to(route_post_crud::<EditPost>))
90 .route("/delete", web::post().to(route_post_crud::<DeletePost>))
91 .route("/remove", web::post().to(route_post_crud::<RemovePost>))
94 web::post().to(route_post::<MarkPostAsRead>),
96 .route("/lock", web::post().to(route_post::<LockPost>))
97 .route("/sticky", web::post().to(route_post::<StickyPost>))
98 .route("/list", web::get().to(route_get_crud::<GetPosts>))
99 .route("/like", web::post().to(route_post::<CreatePostLike>))
100 .route("/save", web::put().to(route_post::<SavePost>))
101 .route("/join", web::post().to(route_post::<PostJoin>))
102 .route("/report", web::post().to(route_post::<CreatePostReport>))
105 web::put().to(route_post::<ResolvePostReport>),
107 .route("/report/list", web::get().to(route_get::<ListPostReports>))
110 web::get().to(route_get::<GetSiteMetadata>),
115 // Handle POST to /comment separately to add the comment() rate limitter
116 web::resource("/comment")
117 .guard(guard::Post())
118 .wrap(rate_limit.comment())
119 .route(web::post().to(route_post_crud::<CreateComment>)),
122 web::scope("/comment")
123 .wrap(rate_limit.message())
124 .route("", web::get().to(route_get_crud::<GetComment>))
125 .route("", web::put().to(route_post_crud::<EditComment>))
126 .route("/delete", web::post().to(route_post_crud::<DeleteComment>))
127 .route("/remove", web::post().to(route_post_crud::<RemoveComment>))
130 web::post().to(route_post::<MarkCommentReplyAsRead>),
132 .route("/like", web::post().to(route_post::<CreateCommentLike>))
133 .route("/save", web::put().to(route_post::<SaveComment>))
134 .route("/list", web::get().to(route_get_crud::<GetComments>))
135 .route("/report", web::post().to(route_post::<CreateCommentReport>))
138 web::put().to(route_post::<ResolveCommentReport>),
142 web::get().to(route_get::<ListCommentReports>),
147 web::scope("/private_message")
148 .wrap(rate_limit.message())
149 .route("/list", web::get().to(route_get_crud::<GetPrivateMessages>))
150 .route("", web::post().to(route_post_crud::<CreatePrivateMessage>))
151 .route("", web::put().to(route_post_crud::<EditPrivateMessage>))
154 web::post().to(route_post_crud::<DeletePrivateMessage>),
158 web::post().to(route_post::<MarkPrivateMessageAsRead>),
162 web::post().to(route_post::<CreatePrivateMessageReport>),
166 web::put().to(route_post::<ResolvePrivateMessageReport>),
170 web::get().to(route_get::<ListPrivateMessageReports>),
175 // Account action, I don't like that it's in /user maybe /accounts
176 // Handle /user/register separately to add the register() rate limitter
177 web::resource("/user/register")
178 .guard(guard::Post())
179 .wrap(rate_limit.register())
180 .route(web::post().to(route_post_crud::<Register>)),
183 // Handle captcha separately
184 web::resource("/user/get_captcha")
185 .wrap(rate_limit.post())
186 .route(web::get().to(route_get::<GetCaptcha>)),
191 .wrap(rate_limit.message())
192 .route("", web::get().to(route_get_crud::<GetPersonDetails>))
193 .route("/mention", web::get().to(route_get::<GetPersonMentions>))
195 "/mention/mark_as_read",
196 web::post().to(route_post::<MarkPersonMentionAsRead>),
198 .route("/replies", web::get().to(route_get::<GetReplies>))
199 .route("/join", web::post().to(route_post::<UserJoin>))
200 // Admin action. I don't like that it's in /user
201 .route("/ban", web::post().to(route_post::<BanPerson>))
202 .route("/banned", web::get().to(route_get::<GetBannedPersons>))
203 .route("/block", web::post().to(route_post::<BlockPerson>))
204 // Account actions. I don't like that they're in /user maybe /accounts
205 .route("/login", web::post().to(route_post::<Login>))
208 web::post().to(route_post_crud::<DeleteAccount>),
212 web::post().to(route_post::<PasswordReset>),
216 web::post().to(route_post::<PasswordChangeAfterReset>),
218 // mark_all_as_read feels off being in this section as well
221 web::post().to(route_post::<MarkAllAsRead>),
224 "/save_user_settings",
225 web::put().to(route_post::<SaveUserSettings>),
229 web::put().to(route_post::<ChangePassword>),
231 .route("/report_count", web::get().to(route_get::<GetReportCount>))
232 .route("/unread_count", web::get().to(route_get::<GetUnreadCount>))
233 .route("/verify_email", web::post().to(route_post::<VerifyEmail>))
234 .route("/leave_admin", web::post().to(route_post::<LeaveAdmin>)),
239 .wrap(rate_limit.message())
240 .route("/add", web::post().to(route_post::<AddAdmin>))
242 "/registration_application/count",
243 web::get().to(route_get::<GetUnreadRegistrationApplicationCount>),
246 "/registration_application/list",
247 web::get().to(route_get::<ListRegistrationApplications>),
250 "/registration_application/approve",
251 web::put().to(route_post::<ApproveRegistrationApplication>),
255 web::scope("/admin/purge")
256 .wrap(rate_limit.message())
257 .route("/person", web::post().to(route_post::<PurgePerson>))
258 .route("/community", web::post().to(route_post::<PurgeCommunity>))
259 .route("/post", web::post().to(route_post::<PurgePost>))
260 .route("/comment", web::post().to(route_post::<PurgeComment>)),
265 async fn perform<Request>(
267 context: web::Data<LemmyContext>,
268 ) -> Result<HttpResponse, Error>
271 Request: Send + 'static,
274 .perform(&context, None)
276 .map(|json| HttpResponse::Ok().json(json))?;
280 async fn route_get<'a, Data>(
281 data: web::Query<Data>,
282 context: web::Data<LemmyContext>,
283 ) -> Result<HttpResponse, Error>
285 Data: Deserialize<'a> + Send + 'static + Perform,
287 perform::<Data>(data.0, context).await
290 async fn route_post<'a, Data>(
291 data: web::Json<Data>,
292 context: web::Data<LemmyContext>,
293 ) -> Result<HttpResponse, Error>
295 Data: Deserialize<'a> + Send + 'static + Perform,
297 perform::<Data>(data.0, context).await
300 async fn perform_crud<Request>(
302 context: web::Data<LemmyContext>,
303 ) -> Result<HttpResponse, Error>
305 Request: PerformCrud,
306 Request: Send + 'static,
309 .perform(&context, None)
311 .map(|json| HttpResponse::Ok().json(json))?;
315 async fn route_get_crud<'a, Data>(
316 data: web::Query<Data>,
317 context: web::Data<LemmyContext>,
318 ) -> Result<HttpResponse, Error>
320 Data: Deserialize<'a> + Send + 'static + PerformCrud,
322 perform_crud::<Data>(data.0, context).await
325 async fn route_post_crud<'a, Data>(
326 data: web::Json<Data>,
327 context: web::Data<LemmyContext>,
328 ) -> Result<HttpResponse, Error>
330 Data: Deserialize<'a> + Send + 'static + PerformCrud,
332 perform_crud::<Data>(data.0, context).await