1 use actix_web::{error::ErrorBadRequest, *};
2 use lemmy_api::Perform;
3 use lemmy_api_common::{comment::*, community::*, person::*, post::*, site::*, websocket::*};
4 use lemmy_api_crud::PerformCrud;
5 use lemmy_utils::rate_limit::RateLimit;
6 use lemmy_websocket::{routes::chat_route, LemmyContext};
7 use serde::Deserialize;
9 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
13 .service(web::resource("/ws").to(chat_route))
17 .wrap(rate_limit.message())
18 .route("", web::get().to(route_get_crud::<GetSite>))
20 .route("", web::post().to(route_post_crud::<CreateSite>))
21 .route("", web::put().to(route_post_crud::<EditSite>))
22 .route("/transfer", web::post().to(route_post::<TransferSite>))
23 .route("/config", web::get().to(route_get::<GetSiteConfig>))
24 .route("/config", web::put().to(route_post::<SaveSiteConfig>)),
27 web::resource("/modlog")
28 .wrap(rate_limit.message())
29 .route(web::get().to(route_get::<GetModlog>)),
32 web::resource("/search")
33 .wrap(rate_limit.message())
34 .route(web::get().to(route_get::<Search>)),
37 web::resource("/resolve_object")
38 .wrap(rate_limit.message())
39 .route(web::get().to(route_get::<ResolveObject>)),
43 web::resource("/community")
45 .wrap(rate_limit.register())
46 .route(web::post().to(route_post_crud::<CreateCommunity>)),
49 web::scope("/community")
50 .wrap(rate_limit.message())
51 .route("", web::get().to(route_get_crud::<GetCommunity>))
52 .route("", web::put().to(route_post_crud::<EditCommunity>))
53 .route("/list", web::get().to(route_get_crud::<ListCommunities>))
54 .route("/follow", web::post().to(route_post::<FollowCommunity>))
55 .route("/block", web::post().to(route_post::<BlockCommunity>))
58 web::post().to(route_post_crud::<DeleteCommunity>),
63 web::post().to(route_post_crud::<RemoveCommunity>),
65 .route("/transfer", web::post().to(route_post::<TransferCommunity>))
66 .route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
67 .route("/mod", web::post().to(route_post::<AddModToCommunity>))
68 .route("/join", web::post().to(route_post::<CommunityJoin>))
69 .route("/mod/join", web::post().to(route_post::<ModJoin>)),
73 // Handle POST to /post separately to add the post() rate limitter
74 web::resource("/post")
76 .wrap(rate_limit.post())
77 .route(web::post().to(route_post_crud::<CreatePost>)),
81 .wrap(rate_limit.message())
82 .route("", web::get().to(route_get_crud::<GetPost>))
83 .route("", web::put().to(route_post_crud::<EditPost>))
84 .route("/delete", web::post().to(route_post_crud::<DeletePost>))
85 .route("/remove", web::post().to(route_post_crud::<RemovePost>))
88 web::post().to(route_post::<MarkPostAsRead>),
90 .route("/lock", web::post().to(route_post::<LockPost>))
91 .route("/sticky", web::post().to(route_post::<StickyPost>))
92 .route("/list", web::get().to(route_get_crud::<GetPosts>))
93 .route("/like", web::post().to(route_post::<CreatePostLike>))
94 .route("/save", web::put().to(route_post::<SavePost>))
95 .route("/join", web::post().to(route_post::<PostJoin>))
96 .route("/report", web::post().to(route_post::<CreatePostReport>))
99 web::put().to(route_post::<ResolvePostReport>),
101 .route("/report/list", web::get().to(route_get::<ListPostReports>))
104 web::get().to(route_get::<GetSiteMetadata>),
109 // Handle POST to /comment separately to add the comment() rate limitter
110 web::resource("/comment")
111 .guard(guard::Post())
112 .wrap(rate_limit.comment())
113 .route(web::post().to(route_post_crud::<CreateComment>)),
116 web::scope("/comment")
117 .wrap(rate_limit.message())
118 .route("", web::put().to(route_post_crud::<EditComment>))
119 .route("/delete", web::post().to(route_post_crud::<DeleteComment>))
120 .route("/remove", web::post().to(route_post_crud::<RemoveComment>))
123 web::post().to(route_post::<MarkCommentAsRead>),
125 .route("/like", web::post().to(route_post::<CreateCommentLike>))
126 .route("/save", web::put().to(route_post::<SaveComment>))
127 .route("/list", web::get().to(route_get_crud::<GetComments>))
128 .route("/report", web::post().to(route_post::<CreateCommentReport>))
131 web::put().to(route_post::<ResolveCommentReport>),
135 web::get().to(route_get::<ListCommentReports>),
140 web::scope("/private_message")
141 .wrap(rate_limit.message())
142 .route("/list", web::get().to(route_get_crud::<GetPrivateMessages>))
143 .route("", web::post().to(route_post_crud::<CreatePrivateMessage>))
144 .route("", web::put().to(route_post_crud::<EditPrivateMessage>))
147 web::post().to(route_post_crud::<DeletePrivateMessage>),
151 web::post().to(route_post::<MarkPrivateMessageAsRead>),
156 // Account action, I don't like that it's in /user maybe /accounts
157 // Handle /user/register separately to add the register() rate limitter
158 web::resource("/user/register")
159 .guard(guard::Post())
160 .wrap(rate_limit.register())
161 .route(web::post().to(route_post_crud::<Register>)),
166 .wrap(rate_limit.message())
167 .route("", web::get().to(route_get_crud::<GetPersonDetails>))
168 .route("/mention", web::get().to(route_get::<GetPersonMentions>))
170 "/mention/mark_as_read",
171 web::post().to(route_post::<MarkPersonMentionAsRead>),
173 .route("/replies", web::get().to(route_get::<GetReplies>))
174 .route("/join", web::post().to(route_post::<UserJoin>))
175 // Admin action. I don't like that it's in /user
176 .route("/ban", web::post().to(route_post::<BanPerson>))
177 .route("/block", web::post().to(route_post::<BlockPerson>))
178 // Account actions. I don't like that they're in /user maybe /accounts
179 .route("/login", web::post().to(route_post::<Login>))
180 .route("/get_captcha", web::get().to(route_get::<GetCaptcha>))
183 web::post().to(route_post_crud::<DeleteAccount>),
187 web::post().to(route_post::<PasswordReset>),
191 web::post().to(route_post::<PasswordChange>),
193 // mark_all_as_read feels off being in this section as well
196 web::post().to(route_post::<MarkAllAsRead>),
199 "/save_user_settings",
200 web::put().to(route_post::<SaveUserSettings>),
204 web::put().to(route_post::<ChangePassword>),
206 .route("/report_count", web::get().to(route_get::<GetReportCount>))
207 .route("/unread_count", web::get().to(route_get::<GetUnreadCount>)),
211 web::resource("/admin/add")
212 .wrap(rate_limit.message())
213 .route(web::post().to(route_post::<AddAdmin>)),
218 async fn perform<Request>(
220 context: web::Data<LemmyContext>,
221 ) -> Result<HttpResponse, Error>
224 Request: Send + 'static,
227 .perform(&context, None)
229 .map(|json| HttpResponse::Ok().json(json))
230 .map_err(ErrorBadRequest)?;
234 async fn route_get<'a, Data>(
235 data: web::Query<Data>,
236 context: web::Data<LemmyContext>,
237 ) -> Result<HttpResponse, Error>
239 Data: Deserialize<'a> + Send + 'static + Perform,
241 perform::<Data>(data.0, context).await
244 async fn route_post<'a, Data>(
245 data: web::Json<Data>,
246 context: web::Data<LemmyContext>,
247 ) -> Result<HttpResponse, Error>
249 Data: Deserialize<'a> + Send + 'static + Perform,
251 perform::<Data>(data.0, context).await
254 async fn perform_crud<Request>(
256 context: web::Data<LemmyContext>,
257 ) -> Result<HttpResponse, Error>
259 Request: PerformCrud,
260 Request: Send + 'static,
263 .perform(&context, None)
265 .map(|json| HttpResponse::Ok().json(json))
266 .map_err(ErrorBadRequest)?;
270 async fn route_get_crud<'a, Data>(
271 data: web::Query<Data>,
272 context: web::Data<LemmyContext>,
273 ) -> Result<HttpResponse, Error>
275 Data: Deserialize<'a> + Send + 'static + PerformCrud,
277 perform_crud::<Data>(data.0, context).await
280 async fn route_post_crud<'a, Data>(
281 data: web::Json<Data>,
282 context: web::Data<LemmyContext>,
283 ) -> Result<HttpResponse, Error>
285 Data: Deserialize<'a> + Send + 'static + PerformCrud,
287 perform_crud::<Data>(data.0, context).await