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>))
52 web::post().to(route_post_crud::<DeleteCommunity>),
57 web::post().to(route_post_crud::<RemoveCommunity>),
59 .route("/transfer", web::post().to(route_post::<TransferCommunity>))
60 .route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
61 .route("/mod", web::post().to(route_post::<AddModToCommunity>))
62 .route("/join", web::post().to(route_post::<CommunityJoin>))
63 .route("/mod/join", web::post().to(route_post::<ModJoin>)),
67 // Handle POST to /post separately to add the post() rate limitter
68 web::resource("/post")
70 .wrap(rate_limit.post())
71 .route(web::post().to(route_post_crud::<CreatePost>)),
75 .wrap(rate_limit.message())
76 .route("", web::get().to(route_get_crud::<GetPost>))
77 .route("", web::put().to(route_post_crud::<EditPost>))
78 .route("/delete", web::post().to(route_post_crud::<DeletePost>))
79 .route("/remove", web::post().to(route_post_crud::<RemovePost>))
80 .route("/lock", web::post().to(route_post::<LockPost>))
81 .route("/sticky", web::post().to(route_post::<StickyPost>))
82 .route("/list", web::get().to(route_get_crud::<GetPosts>))
83 .route("/like", web::post().to(route_post::<CreatePostLike>))
84 .route("/save", web::put().to(route_post::<SavePost>))
85 .route("/join", web::post().to(route_post::<PostJoin>))
86 .route("/report", web::post().to(route_post::<CreatePostReport>))
89 web::put().to(route_post::<ResolvePostReport>),
91 .route("/report/list", web::get().to(route_get::<ListPostReports>))
94 web::get().to(route_get::<GetSiteMetadata>),
99 web::scope("/comment")
100 .wrap(rate_limit.message())
101 .route("", web::post().to(route_post_crud::<CreateComment>))
102 .route("", web::put().to(route_post_crud::<EditComment>))
103 .route("/delete", web::post().to(route_post_crud::<DeleteComment>))
104 .route("/remove", web::post().to(route_post_crud::<RemoveComment>))
107 web::post().to(route_post::<MarkCommentAsRead>),
109 .route("/like", web::post().to(route_post::<CreateCommentLike>))
110 .route("/save", web::put().to(route_post::<SaveComment>))
111 .route("/list", web::get().to(route_get_crud::<GetComments>))
112 .route("/report", web::post().to(route_post::<CreateCommentReport>))
115 web::put().to(route_post::<ResolveCommentReport>),
119 web::get().to(route_get::<ListCommentReports>),
124 web::scope("/private_message")
125 .wrap(rate_limit.message())
126 .route("/list", web::get().to(route_get_crud::<GetPrivateMessages>))
127 .route("", web::post().to(route_post_crud::<CreatePrivateMessage>))
128 .route("", web::put().to(route_post_crud::<EditPrivateMessage>))
131 web::post().to(route_post_crud::<DeletePrivateMessage>),
135 web::post().to(route_post::<MarkPrivateMessageAsRead>),
140 // Account action, I don't like that it's in /user maybe /accounts
141 // Handle /user/register separately to add the register() rate limitter
142 web::resource("/user/register")
143 .guard(guard::Post())
144 .wrap(rate_limit.register())
145 .route(web::post().to(route_post_crud::<Register>)),
150 .wrap(rate_limit.message())
151 .route("", web::get().to(route_get_crud::<GetPersonDetails>))
152 .route("/mention", web::get().to(route_get::<GetPersonMentions>))
154 "/mention/mark_as_read",
155 web::post().to(route_post::<MarkPersonMentionAsRead>),
157 .route("/replies", web::get().to(route_get::<GetReplies>))
159 "/followed_communities",
160 web::get().to(route_get::<GetFollowedCommunities>),
162 .route("/join", web::post().to(route_post::<UserJoin>))
163 // Admin action. I don't like that it's in /user
164 .route("/ban", web::post().to(route_post::<BanPerson>))
165 // Account actions. I don't like that they're in /user maybe /accounts
166 .route("/login", web::post().to(route_post::<Login>))
167 .route("/get_captcha", web::get().to(route_get::<GetCaptcha>))
170 web::post().to(route_post_crud::<DeleteAccount>),
174 web::post().to(route_post::<PasswordReset>),
178 web::post().to(route_post::<PasswordChange>),
180 // mark_all_as_read feels off being in this section as well
183 web::post().to(route_post::<MarkAllAsRead>),
186 "/save_user_settings",
187 web::put().to(route_post::<SaveUserSettings>),
191 web::put().to(route_post::<ChangePassword>),
193 .route("/report_count", web::get().to(route_get::<GetReportCount>)),
197 web::resource("/admin/add")
198 .wrap(rate_limit.message())
199 .route(web::post().to(route_post::<AddAdmin>)),
204 async fn perform<Request>(
206 context: web::Data<LemmyContext>,
207 ) -> Result<HttpResponse, Error>
210 Request: Send + 'static,
213 .perform(&context, None)
215 .map(|json| HttpResponse::Ok().json(json))
216 .map_err(ErrorBadRequest)?;
220 async fn route_get<'a, Data>(
221 data: web::Query<Data>,
222 context: web::Data<LemmyContext>,
223 ) -> Result<HttpResponse, Error>
225 Data: Deserialize<'a> + Send + 'static + Perform,
227 perform::<Data>(data.0, context).await
230 async fn route_post<'a, Data>(
231 data: web::Json<Data>,
232 context: web::Data<LemmyContext>,
233 ) -> Result<HttpResponse, Error>
235 Data: Deserialize<'a> + Send + 'static + Perform,
237 perform::<Data>(data.0, context).await
240 async fn perform_crud<Request>(
242 context: web::Data<LemmyContext>,
243 ) -> Result<HttpResponse, Error>
245 Request: PerformCrud,
246 Request: Send + 'static,
249 .perform(&context, None)
251 .map(|json| HttpResponse::Ok().json(json))
252 .map_err(ErrorBadRequest)?;
256 async fn route_get_crud<'a, Data>(
257 data: web::Query<Data>,
258 context: web::Data<LemmyContext>,
259 ) -> Result<HttpResponse, Error>
261 Data: Deserialize<'a> + Send + 'static + PerformCrud,
263 perform_crud::<Data>(data.0, context).await
266 async fn route_post_crud<'a, Data>(
267 data: web::Json<Data>,
268 context: web::Data<LemmyContext>,
269 ) -> Result<HttpResponse, Error>
271 Data: Deserialize<'a> + Send + 'static + PerformCrud,
273 perform_crud::<Data>(data.0, context).await