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>)),
38 web::resource("/community")
40 .wrap(rate_limit.register())
41 .route(web::post().to(route_post_crud::<CreateCommunity>)),
44 web::scope("/community")
45 .wrap(rate_limit.message())
46 .route("", web::get().to(route_get_crud::<GetCommunity>))
47 .route("", web::put().to(route_post_crud::<EditCommunity>))
48 .route("/list", web::get().to(route_get_crud::<ListCommunities>))
49 .route("/follow", web::post().to(route_post::<FollowCommunity>))
50 .route("/block", web::post().to(route_post::<BlockCommunity>))
53 web::post().to(route_post_crud::<DeleteCommunity>),
58 web::post().to(route_post_crud::<RemoveCommunity>),
60 .route("/transfer", web::post().to(route_post::<TransferCommunity>))
61 .route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
62 .route("/mod", web::post().to(route_post::<AddModToCommunity>))
63 .route("/join", web::post().to(route_post::<CommunityJoin>))
64 .route("/mod/join", web::post().to(route_post::<ModJoin>)),
68 // Handle POST to /post separately to add the post() rate limitter
69 web::resource("/post")
71 .wrap(rate_limit.post())
72 .route(web::post().to(route_post_crud::<CreatePost>)),
76 .wrap(rate_limit.message())
77 .route("", web::get().to(route_get_crud::<GetPost>))
78 .route("", web::put().to(route_post_crud::<EditPost>))
79 .route("/delete", web::post().to(route_post_crud::<DeletePost>))
80 .route("/remove", web::post().to(route_post_crud::<RemovePost>))
81 .route("/lock", web::post().to(route_post::<LockPost>))
82 .route("/sticky", web::post().to(route_post::<StickyPost>))
83 .route("/list", web::get().to(route_get_crud::<GetPosts>))
84 .route("/like", web::post().to(route_post::<CreatePostLike>))
85 .route("/save", web::put().to(route_post::<SavePost>))
86 .route("/join", web::post().to(route_post::<PostJoin>))
87 .route("/report", web::post().to(route_post::<CreatePostReport>))
90 web::put().to(route_post::<ResolvePostReport>),
92 .route("/report/list", web::get().to(route_get::<ListPostReports>))
95 web::get().to(route_get::<GetSiteMetadata>),
100 web::scope("/comment")
101 .wrap(rate_limit.message())
102 .route("", web::post().to(route_post_crud::<CreateComment>))
103 .route("", web::put().to(route_post_crud::<EditComment>))
104 .route("/delete", web::post().to(route_post_crud::<DeleteComment>))
105 .route("/remove", web::post().to(route_post_crud::<RemoveComment>))
108 web::post().to(route_post::<MarkCommentAsRead>),
110 .route("/like", web::post().to(route_post::<CreateCommentLike>))
111 .route("/save", web::put().to(route_post::<SaveComment>))
112 .route("/list", web::get().to(route_get_crud::<GetComments>))
113 .route("/report", web::post().to(route_post::<CreateCommentReport>))
116 web::put().to(route_post::<ResolveCommentReport>),
120 web::get().to(route_get::<ListCommentReports>),
125 web::scope("/private_message")
126 .wrap(rate_limit.message())
127 .route("/list", web::get().to(route_get_crud::<GetPrivateMessages>))
128 .route("", web::post().to(route_post_crud::<CreatePrivateMessage>))
129 .route("", web::put().to(route_post_crud::<EditPrivateMessage>))
132 web::post().to(route_post_crud::<DeletePrivateMessage>),
136 web::post().to(route_post::<MarkPrivateMessageAsRead>),
141 // Account action, I don't like that it's in /user maybe /accounts
142 // Handle /user/register separately to add the register() rate limitter
143 web::resource("/user/register")
144 .guard(guard::Post())
145 .wrap(rate_limit.register())
146 .route(web::post().to(route_post_crud::<Register>)),
151 .wrap(rate_limit.message())
152 .route("", web::get().to(route_get_crud::<GetPersonDetails>))
153 .route("/mention", web::get().to(route_get::<GetPersonMentions>))
155 "/mention/mark_as_read",
156 web::post().to(route_post::<MarkPersonMentionAsRead>),
158 .route("/replies", web::get().to(route_get::<GetReplies>))
159 .route("/join", web::post().to(route_post::<UserJoin>))
160 // Admin action. I don't like that it's in /user
161 .route("/ban", web::post().to(route_post::<BanPerson>))
162 .route("/block", web::post().to(route_post::<BlockPerson>))
163 // Account actions. I don't like that they're in /user maybe /accounts
164 .route("/login", web::post().to(route_post::<Login>))
165 .route("/get_captcha", web::get().to(route_get::<GetCaptcha>))
168 web::post().to(route_post_crud::<DeleteAccount>),
172 web::post().to(route_post::<PasswordReset>),
176 web::post().to(route_post::<PasswordChange>),
178 // mark_all_as_read feels off being in this section as well
181 web::post().to(route_post::<MarkAllAsRead>),
184 "/save_user_settings",
185 web::put().to(route_post::<SaveUserSettings>),
189 web::put().to(route_post::<ChangePassword>),
191 .route("/report_count", web::get().to(route_get::<GetReportCount>)),
195 web::resource("/admin/add")
196 .wrap(rate_limit.message())
197 .route(web::post().to(route_post::<AddAdmin>)),
202 async fn perform<Request>(
204 context: web::Data<LemmyContext>,
205 ) -> Result<HttpResponse, Error>
208 Request: Send + 'static,
211 .perform(&context, None)
213 .map(|json| HttpResponse::Ok().json(json))
214 .map_err(ErrorBadRequest)?;
218 async fn route_get<'a, Data>(
219 data: web::Query<Data>,
220 context: web::Data<LemmyContext>,
221 ) -> Result<HttpResponse, Error>
223 Data: Deserialize<'a> + Send + 'static + Perform,
225 perform::<Data>(data.0, context).await
228 async fn route_post<'a, Data>(
229 data: web::Json<Data>,
230 context: web::Data<LemmyContext>,
231 ) -> Result<HttpResponse, Error>
233 Data: Deserialize<'a> + Send + 'static + Perform,
235 perform::<Data>(data.0, context).await
238 async fn perform_crud<Request>(
240 context: web::Data<LemmyContext>,
241 ) -> Result<HttpResponse, Error>
243 Request: PerformCrud,
244 Request: Send + 'static,
247 .perform(&context, None)
249 .map(|json| HttpResponse::Ok().json(json))
250 .map_err(ErrorBadRequest)?;
254 async fn route_get_crud<'a, Data>(
255 data: web::Query<Data>,
256 context: web::Data<LemmyContext>,
257 ) -> Result<HttpResponse, Error>
259 Data: Deserialize<'a> + Send + 'static + PerformCrud,
261 perform_crud::<Data>(data.0, context).await
264 async fn route_post_crud<'a, Data>(
265 data: web::Json<Data>,
266 context: web::Data<LemmyContext>,
267 ) -> Result<HttpResponse, Error>
269 Data: Deserialize<'a> + Send + 'static + PerformCrud,
271 perform_crud::<Data>(data.0, context).await