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>))
86 .route("/lock", web::post().to(route_post::<LockPost>))
87 .route("/sticky", web::post().to(route_post::<StickyPost>))
88 .route("/list", web::get().to(route_get_crud::<GetPosts>))
89 .route("/like", web::post().to(route_post::<CreatePostLike>))
90 .route("/save", web::put().to(route_post::<SavePost>))
91 .route("/join", web::post().to(route_post::<PostJoin>))
92 .route("/report", web::post().to(route_post::<CreatePostReport>))
95 web::put().to(route_post::<ResolvePostReport>),
97 .route("/report/list", web::get().to(route_get::<ListPostReports>))
100 web::get().to(route_get::<GetSiteMetadata>),
105 web::scope("/comment")
106 .wrap(rate_limit.message())
107 .route("", web::post().to(route_post_crud::<CreateComment>))
108 .route("", web::put().to(route_post_crud::<EditComment>))
109 .route("/delete", web::post().to(route_post_crud::<DeleteComment>))
110 .route("/remove", web::post().to(route_post_crud::<RemoveComment>))
113 web::post().to(route_post::<MarkCommentAsRead>),
115 .route("/like", web::post().to(route_post::<CreateCommentLike>))
116 .route("/save", web::put().to(route_post::<SaveComment>))
117 .route("/list", web::get().to(route_get_crud::<GetComments>))
118 .route("/report", web::post().to(route_post::<CreateCommentReport>))
121 web::put().to(route_post::<ResolveCommentReport>),
125 web::get().to(route_get::<ListCommentReports>),
130 web::scope("/private_message")
131 .wrap(rate_limit.message())
132 .route("/list", web::get().to(route_get_crud::<GetPrivateMessages>))
133 .route("", web::post().to(route_post_crud::<CreatePrivateMessage>))
134 .route("", web::put().to(route_post_crud::<EditPrivateMessage>))
137 web::post().to(route_post_crud::<DeletePrivateMessage>),
141 web::post().to(route_post::<MarkPrivateMessageAsRead>),
146 // Account action, I don't like that it's in /user maybe /accounts
147 // Handle /user/register separately to add the register() rate limitter
148 web::resource("/user/register")
149 .guard(guard::Post())
150 .wrap(rate_limit.register())
151 .route(web::post().to(route_post_crud::<Register>)),
156 .wrap(rate_limit.message())
157 .route("", web::get().to(route_get_crud::<GetPersonDetails>))
158 .route("/mention", web::get().to(route_get::<GetPersonMentions>))
160 "/mention/mark_as_read",
161 web::post().to(route_post::<MarkPersonMentionAsRead>),
163 .route("/replies", web::get().to(route_get::<GetReplies>))
164 .route("/join", web::post().to(route_post::<UserJoin>))
165 // Admin action. I don't like that it's in /user
166 .route("/ban", web::post().to(route_post::<BanPerson>))
167 .route("/block", web::post().to(route_post::<BlockPerson>))
168 // Account actions. I don't like that they're in /user maybe /accounts
169 .route("/login", web::post().to(route_post::<Login>))
170 .route("/get_captcha", web::get().to(route_get::<GetCaptcha>))
173 web::post().to(route_post_crud::<DeleteAccount>),
177 web::post().to(route_post::<PasswordReset>),
181 web::post().to(route_post::<PasswordChange>),
183 // mark_all_as_read feels off being in this section as well
186 web::post().to(route_post::<MarkAllAsRead>),
189 "/save_user_settings",
190 web::put().to(route_post::<SaveUserSettings>),
194 web::put().to(route_post::<ChangePassword>),
196 .route("/report_count", web::get().to(route_get::<GetReportCount>)),
200 web::resource("/admin/add")
201 .wrap(rate_limit.message())
202 .route(web::post().to(route_post::<AddAdmin>)),
207 async fn perform<Request>(
209 context: web::Data<LemmyContext>,
210 ) -> Result<HttpResponse, Error>
213 Request: Send + 'static,
216 .perform(&context, None)
218 .map(|json| HttpResponse::Ok().json(json))
219 .map_err(ErrorBadRequest)?;
223 async fn route_get<'a, Data>(
224 data: web::Query<Data>,
225 context: web::Data<LemmyContext>,
226 ) -> Result<HttpResponse, Error>
228 Data: Deserialize<'a> + Send + 'static + Perform,
230 perform::<Data>(data.0, context).await
233 async fn route_post<'a, Data>(
234 data: web::Json<Data>,
235 context: web::Data<LemmyContext>,
236 ) -> Result<HttpResponse, Error>
238 Data: Deserialize<'a> + Send + 'static + Perform,
240 perform::<Data>(data.0, context).await
243 async fn perform_crud<Request>(
245 context: web::Data<LemmyContext>,
246 ) -> Result<HttpResponse, Error>
248 Request: PerformCrud,
249 Request: Send + 'static,
252 .perform(&context, None)
254 .map(|json| HttpResponse::Ok().json(json))
255 .map_err(ErrorBadRequest)?;
259 async fn route_get_crud<'a, Data>(
260 data: web::Query<Data>,
261 context: web::Data<LemmyContext>,
262 ) -> Result<HttpResponse, Error>
264 Data: Deserialize<'a> + Send + 'static + PerformCrud,
266 perform_crud::<Data>(data.0, context).await
269 async fn route_post_crud<'a, Data>(
270 data: web::Json<Data>,
271 context: web::Data<LemmyContext>,
272 ) -> Result<HttpResponse, Error>
274 Data: Deserialize<'a> + Send + 'static + PerformCrud,
276 perform_crud::<Data>(data.0, context).await