1 use crate::{api::Perform, LemmyContext};
2 use actix_web::{error::ErrorBadRequest, *};
3 use lemmy_structs::{comment::*, community::*, post::*, site::*, user::*};
4 use lemmy_rate_limit::RateLimit;
5 use serde::Deserialize;
7 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
11 .service(web::resource("/ws").to(super::websocket::chat_route))
15 .wrap(rate_limit.message())
16 .route("", web::get().to(route_get::<GetSite>))
18 .route("", web::post().to(route_post::<CreateSite>))
19 .route("", web::put().to(route_post::<EditSite>))
20 .route("/transfer", web::post().to(route_post::<TransferSite>))
21 .route("/config", web::get().to(route_get::<GetSiteConfig>))
22 .route("/config", web::put().to(route_post::<SaveSiteConfig>)),
25 web::resource("/categories")
26 .wrap(rate_limit.message())
27 .route(web::get().to(route_get::<ListCategories>)),
30 web::resource("/modlog")
31 .wrap(rate_limit.message())
32 .route(web::get().to(route_get::<GetModlog>)),
35 web::resource("/search")
36 .wrap(rate_limit.message())
37 .route(web::get().to(route_get::<Search>)),
41 web::resource("/community")
43 .wrap(rate_limit.register())
44 .route(web::post().to(route_post::<CreateCommunity>)),
47 web::scope("/community")
48 .wrap(rate_limit.message())
49 .route("", web::get().to(route_get::<GetCommunity>))
50 .route("", web::put().to(route_post::<EditCommunity>))
51 .route("/list", web::get().to(route_get::<ListCommunities>))
52 .route("/follow", web::post().to(route_post::<FollowCommunity>))
53 .route("/delete", web::post().to(route_post::<DeleteCommunity>))
55 .route("/remove", web::post().to(route_post::<RemoveCommunity>))
56 .route("/transfer", web::post().to(route_post::<TransferCommunity>))
57 .route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
58 .route("/mod", web::post().to(route_post::<AddModToCommunity>))
59 .route("/join", web::post().to(route_post::<CommunityJoin>)),
63 // Handle POST to /post separately to add the post() rate limitter
64 web::resource("/post")
66 .wrap(rate_limit.post())
67 .route(web::post().to(route_post::<CreatePost>)),
71 .wrap(rate_limit.message())
72 .route("", web::get().to(route_get::<GetPost>))
73 .route("", web::put().to(route_post::<EditPost>))
74 .route("/delete", web::post().to(route_post::<DeletePost>))
75 .route("/remove", web::post().to(route_post::<RemovePost>))
76 .route("/lock", web::post().to(route_post::<LockPost>))
77 .route("/sticky", web::post().to(route_post::<StickyPost>))
78 .route("/list", web::get().to(route_get::<GetPosts>))
79 .route("/like", web::post().to(route_post::<CreatePostLike>))
80 .route("/save", web::put().to(route_post::<SavePost>))
81 .route("/join", web::post().to(route_post::<PostJoin>)),
85 web::scope("/comment")
86 .wrap(rate_limit.message())
87 .route("", web::post().to(route_post::<CreateComment>))
88 .route("", web::put().to(route_post::<EditComment>))
89 .route("/delete", web::post().to(route_post::<DeleteComment>))
90 .route("/remove", web::post().to(route_post::<RemoveComment>))
93 web::post().to(route_post::<MarkCommentAsRead>),
95 .route("/like", web::post().to(route_post::<CreateCommentLike>))
96 .route("/save", web::put().to(route_post::<SaveComment>))
97 .route("/list", web::get().to(route_get::<GetComments>)),
101 web::scope("/private_message")
102 .wrap(rate_limit.message())
103 .route("/list", web::get().to(route_get::<GetPrivateMessages>))
104 .route("", web::post().to(route_post::<CreatePrivateMessage>))
105 .route("", web::put().to(route_post::<EditPrivateMessage>))
108 web::post().to(route_post::<DeletePrivateMessage>),
112 web::post().to(route_post::<MarkPrivateMessageAsRead>),
117 // Account action, I don't like that it's in /user maybe /accounts
118 // Handle /user/register separately to add the register() rate limitter
119 web::resource("/user/register")
120 .guard(guard::Post())
121 .wrap(rate_limit.register())
122 .route(web::post().to(route_post::<Register>)),
127 .wrap(rate_limit.message())
128 .route("", web::get().to(route_get::<GetUserDetails>))
129 .route("/mention", web::get().to(route_get::<GetUserMentions>))
131 "/mention/mark_as_read",
132 web::post().to(route_post::<MarkUserMentionAsRead>),
134 .route("/replies", web::get().to(route_get::<GetReplies>))
136 "/followed_communities",
137 web::get().to(route_get::<GetFollowedCommunities>),
139 .route("/join", web::post().to(route_post::<UserJoin>))
140 // Admin action. I don't like that it's in /user
141 .route("/ban", web::post().to(route_post::<BanUser>))
142 // Account actions. I don't like that they're in /user maybe /accounts
143 .route("/login", web::post().to(route_post::<Login>))
144 .route("/get_captcha", web::get().to(route_get::<GetCaptcha>))
147 web::post().to(route_post::<DeleteAccount>),
151 web::post().to(route_post::<PasswordReset>),
155 web::post().to(route_post::<PasswordChange>),
157 // mark_all_as_read feels off being in this section as well
160 web::post().to(route_post::<MarkAllAsRead>),
163 "/save_user_settings",
164 web::put().to(route_post::<SaveUserSettings>),
169 web::resource("/admin/add")
170 .wrap(rate_limit.message())
171 .route(web::post().to(route_post::<AddAdmin>)),
176 async fn perform<Request>(
178 context: web::Data<LemmyContext>,
179 ) -> Result<HttpResponse, Error>
182 Request: Send + 'static,
185 .perform(&context, None)
187 .map(|json| HttpResponse::Ok().json(json))
188 .map_err(ErrorBadRequest)?;
192 async fn route_get<'a, Data>(
193 data: web::Query<Data>,
194 context: web::Data<LemmyContext>,
195 ) -> Result<HttpResponse, Error>
197 Data: Deserialize<'a> + Send + 'static + Perform,
199 perform::<Data>(data.0, context).await
202 async fn route_post<'a, Data>(
203 data: web::Json<Data>,
204 context: web::Data<LemmyContext>,
205 ) -> Result<HttpResponse, Error>
207 Data: Deserialize<'a> + Send + 'static + Perform,
209 perform::<Data>(data.0, context).await