2 use actix_web::{error::ErrorBadRequest, *};
3 use lemmy_structs::{comment::*, community::*, post::*, site::*, user::*, websocket::*};
4 use lemmy_utils::rate_limit::RateLimit;
5 use lemmy_websocket::{routes::chat_route, LemmyContext};
6 use serde::Deserialize;
8 pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
12 .service(web::resource("/ws").to(chat_route))
16 .wrap(rate_limit.message())
17 .route("", web::get().to(route_get::<GetSite>))
19 .route("", web::post().to(route_post::<CreateSite>))
20 .route("", web::put().to(route_post::<EditSite>))
21 .route("/transfer", web::post().to(route_post::<TransferSite>))
22 .route("/config", web::get().to(route_get::<GetSiteConfig>))
23 .route("/config", web::put().to(route_post::<SaveSiteConfig>)),
26 web::resource("/categories")
27 .wrap(rate_limit.message())
28 .route(web::get().to(route_get::<ListCategories>)),
31 web::resource("/modlog")
32 .wrap(rate_limit.message())
33 .route(web::get().to(route_get::<GetModlog>)),
36 web::resource("/search")
37 .wrap(rate_limit.message())
38 .route(web::get().to(route_get::<Search>)),
42 web::resource("/community")
44 .wrap(rate_limit.register())
45 .route(web::post().to(route_post::<CreateCommunity>)),
48 web::scope("/community")
49 .wrap(rate_limit.message())
50 .route("", web::get().to(route_get::<GetCommunity>))
51 .route("", web::put().to(route_post::<EditCommunity>))
52 .route("/list", web::get().to(route_get::<ListCommunities>))
53 .route("/follow", web::post().to(route_post::<FollowCommunity>))
54 .route("/delete", web::post().to(route_post::<DeleteCommunity>))
56 .route("/remove", web::post().to(route_post::<RemoveCommunity>))
57 .route("/transfer", web::post().to(route_post::<TransferCommunity>))
58 .route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
59 .route("/mod", web::post().to(route_post::<AddModToCommunity>))
60 .route("/join", web::post().to(route_post::<CommunityJoin>))
61 .route("/mod/join", web::post().to(route_post::<ModJoin>)),
65 // Handle POST to /post separately to add the post() rate limitter
66 web::resource("/post")
68 .wrap(rate_limit.post())
69 .route(web::post().to(route_post::<CreatePost>)),
73 .wrap(rate_limit.message())
74 .route("", web::get().to(route_get::<GetPost>))
75 .route("", web::put().to(route_post::<EditPost>))
76 .route("/delete", web::post().to(route_post::<DeletePost>))
77 .route("/remove", web::post().to(route_post::<RemovePost>))
78 .route("/lock", web::post().to(route_post::<LockPost>))
79 .route("/sticky", web::post().to(route_post::<StickyPost>))
80 .route("/list", web::get().to(route_get::<GetPosts>))
81 .route("/like", web::post().to(route_post::<CreatePostLike>))
82 .route("/save", web::put().to(route_post::<SavePost>))
83 .route("/join", web::post().to(route_post::<PostJoin>))
84 .route("/report", web::post().to(route_post::<CreatePostReport>))
87 web::put().to(route_post::<ResolvePostReport>),
89 .route("/report/list", web::get().to(route_get::<ListPostReports>)),
93 web::scope("/comment")
94 .wrap(rate_limit.message())
95 .route("", web::post().to(route_post::<CreateComment>))
96 .route("", web::put().to(route_post::<EditComment>))
97 .route("/delete", web::post().to(route_post::<DeleteComment>))
98 .route("/remove", web::post().to(route_post::<RemoveComment>))
101 web::post().to(route_post::<MarkCommentAsRead>),
103 .route("/like", web::post().to(route_post::<CreateCommentLike>))
104 .route("/save", web::put().to(route_post::<SaveComment>))
105 .route("/list", web::get().to(route_get::<GetComments>))
106 .route("/report", web::post().to(route_post::<CreateCommentReport>))
109 web::put().to(route_post::<ResolveCommentReport>),
113 web::get().to(route_get::<ListCommentReports>),
118 web::scope("/private_message")
119 .wrap(rate_limit.message())
120 .route("/list", web::get().to(route_get::<GetPrivateMessages>))
121 .route("", web::post().to(route_post::<CreatePrivateMessage>))
122 .route("", web::put().to(route_post::<EditPrivateMessage>))
125 web::post().to(route_post::<DeletePrivateMessage>),
129 web::post().to(route_post::<MarkPrivateMessageAsRead>),
134 // Account action, I don't like that it's in /user maybe /accounts
135 // Handle /user/register separately to add the register() rate limitter
136 web::resource("/user/register")
137 .guard(guard::Post())
138 .wrap(rate_limit.register())
139 .route(web::post().to(route_post::<Register>)),
144 .wrap(rate_limit.message())
145 .route("", web::get().to(route_get::<GetUserDetails>))
146 .route("/mention", web::get().to(route_get::<GetUserMentions>))
148 "/mention/mark_as_read",
149 web::post().to(route_post::<MarkUserMentionAsRead>),
151 .route("/replies", web::get().to(route_get::<GetReplies>))
153 "/followed_communities",
154 web::get().to(route_get::<GetFollowedCommunities>),
156 .route("/join", web::post().to(route_post::<UserJoin>))
157 // Admin action. I don't like that it's in /user
158 .route("/ban", web::post().to(route_post::<BanUser>))
159 // Account actions. I don't like that they're in /user maybe /accounts
160 .route("/login", web::post().to(route_post::<Login>))
161 .route("/get_captcha", web::get().to(route_get::<GetCaptcha>))
164 web::post().to(route_post::<DeleteAccount>),
168 web::post().to(route_post::<PasswordReset>),
172 web::post().to(route_post::<PasswordChange>),
174 // mark_all_as_read feels off being in this section as well
177 web::post().to(route_post::<MarkAllAsRead>),
180 "/save_user_settings",
181 web::put().to(route_post::<SaveUserSettings>),
183 .route("/report_count", web::get().to(route_get::<GetReportCount>)),
187 web::resource("/admin/add")
188 .wrap(rate_limit.message())
189 .route(web::post().to(route_post::<AddAdmin>)),
194 async fn perform<Request>(
196 context: web::Data<LemmyContext>,
197 ) -> Result<HttpResponse, Error>
200 Request: Send + 'static,
203 .perform(&context, None)
205 .map(|json| HttpResponse::Ok().json(json))
206 .map_err(ErrorBadRequest)?;
210 async fn route_get<'a, Data>(
211 data: web::Query<Data>,
212 context: web::Data<LemmyContext>,
213 ) -> Result<HttpResponse, Error>
215 Data: Deserialize<'a> + Send + 'static + Perform,
217 perform::<Data>(data.0, context).await
220 async fn route_post<'a, Data>(
221 data: web::Json<Data>,
222 context: web::Data<LemmyContext>,
223 ) -> Result<HttpResponse, Error>
225 Data: Deserialize<'a> + Send + 'static + Perform,
227 perform::<Data>(data.0, context).await