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::get().to(route_get_crud::<GetComment>))
119 .route("", web::put().to(route_post_crud::<EditComment>))
120 .route("/delete", web::post().to(route_post_crud::<DeleteComment>))
121 .route("/remove", web::post().to(route_post_crud::<RemoveComment>))
124 web::post().to(route_post::<MarkCommentAsRead>),
126 .route("/like", web::post().to(route_post::<CreateCommentLike>))
127 .route("/save", web::put().to(route_post::<SaveComment>))
128 .route("/list", web::get().to(route_get_crud::<GetComments>))
129 .route("/report", web::post().to(route_post::<CreateCommentReport>))
132 web::put().to(route_post::<ResolveCommentReport>),
136 web::get().to(route_get::<ListCommentReports>),
141 web::scope("/private_message")
142 .wrap(rate_limit.message())
143 .route("/list", web::get().to(route_get_crud::<GetPrivateMessages>))
144 .route("", web::post().to(route_post_crud::<CreatePrivateMessage>))
145 .route("", web::put().to(route_post_crud::<EditPrivateMessage>))
148 web::post().to(route_post_crud::<DeletePrivateMessage>),
152 web::post().to(route_post::<MarkPrivateMessageAsRead>),
157 // Account action, I don't like that it's in /user maybe /accounts
158 // Handle /user/register separately to add the register() rate limitter
159 web::resource("/user/register")
160 .guard(guard::Post())
161 .wrap(rate_limit.register())
162 .route(web::post().to(route_post_crud::<Register>)),
167 .wrap(rate_limit.message())
168 .route("", web::get().to(route_get_crud::<GetPersonDetails>))
169 .route("/mention", web::get().to(route_get::<GetPersonMentions>))
171 "/mention/mark_as_read",
172 web::post().to(route_post::<MarkPersonMentionAsRead>),
174 .route("/replies", web::get().to(route_get::<GetReplies>))
175 .route("/join", web::post().to(route_post::<UserJoin>))
176 // Admin action. I don't like that it's in /user
177 .route("/ban", web::post().to(route_post::<BanPerson>))
178 .route("/block", web::post().to(route_post::<BlockPerson>))
179 // Account actions. I don't like that they're in /user maybe /accounts
180 .route("/login", web::post().to(route_post::<Login>))
181 .route("/get_captcha", web::get().to(route_get::<GetCaptcha>))
184 web::post().to(route_post_crud::<DeleteAccount>),
188 web::post().to(route_post::<PasswordReset>),
192 web::post().to(route_post::<PasswordChange>),
194 // mark_all_as_read feels off being in this section as well
197 web::post().to(route_post::<MarkAllAsRead>),
200 "/save_user_settings",
201 web::put().to(route_post::<SaveUserSettings>),
205 web::put().to(route_post::<ChangePassword>),
207 .route("/report_count", web::get().to(route_get::<GetReportCount>))
208 .route("/unread_count", web::get().to(route_get::<GetUnreadCount>)),
212 web::resource("/admin/add")
213 .wrap(rate_limit.message())
214 .route(web::post().to(route_post::<AddAdmin>)),
219 async fn perform<Request>(
221 context: web::Data<LemmyContext>,
222 ) -> Result<HttpResponse, Error>
225 Request: Send + 'static,
228 .perform(&context, None)
230 .map(|json| HttpResponse::Ok().json(json))
231 .map_err(ErrorBadRequest)?;
235 async fn route_get<'a, Data>(
236 data: web::Query<Data>,
237 context: web::Data<LemmyContext>,
238 ) -> Result<HttpResponse, Error>
240 Data: Deserialize<'a> + Send + 'static + Perform,
242 perform::<Data>(data.0, context).await
245 async fn route_post<'a, Data>(
246 data: web::Json<Data>,
247 context: web::Data<LemmyContext>,
248 ) -> Result<HttpResponse, Error>
250 Data: Deserialize<'a> + Send + 'static + Perform,
252 perform::<Data>(data.0, context).await
255 async fn perform_crud<Request>(
257 context: web::Data<LemmyContext>,
258 ) -> Result<HttpResponse, Error>
260 Request: PerformCrud,
261 Request: Send + 'static,
264 .perform(&context, None)
266 .map(|json| HttpResponse::Ok().json(json))
267 .map_err(ErrorBadRequest)?;
271 async fn route_get_crud<'a, Data>(
272 data: web::Query<Data>,
273 context: web::Data<LemmyContext>,
274 ) -> Result<HttpResponse, Error>
276 Data: Deserialize<'a> + Send + 'static + PerformCrud,
278 perform_crud::<Data>(data.0, context).await
281 async fn route_post_crud<'a, Data>(
282 data: web::Json<Data>,
283 context: web::Data<LemmyContext>,
284 ) -> Result<HttpResponse, Error>
286 Data: Deserialize<'a> + Send + 'static + PerformCrud,
288 perform_crud::<Data>(data.0, context).await