2020-08-18 13:43:50 +00:00
|
|
|
use actix_web::{error::ErrorBadRequest, *};
|
2020-09-24 13:53:21 +00:00
|
|
|
use lemmy_api::Perform;
|
2020-10-25 02:59:13 +00:00
|
|
|
use lemmy_structs::{comment::*, community::*, post::*, site::*, user::*};
|
2020-12-21 23:27:42 +00:00
|
|
|
use lemmy_utils::rate_limit::RateLimit;
|
2020-09-24 13:53:21 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
2020-09-03 19:45:12 +00:00
|
|
|
use serde::Deserialize;
|
2020-01-15 15:37:25 +00:00
|
|
|
|
2020-04-20 03:59:07 +00:00
|
|
|
pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
|
|
|
|
cfg.service(
|
2020-12-20 21:16:57 +00:00
|
|
|
web::scope("/api/v2")
|
2020-04-20 15:25:47 +00:00
|
|
|
// Websockets
|
|
|
|
.service(web::resource("/ws").to(super::websocket::chat_route))
|
2020-04-20 03:59:07 +00:00
|
|
|
// Site
|
|
|
|
.service(
|
|
|
|
web::scope("/site")
|
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route("", web::get().to(route_get::<GetSite>))
|
|
|
|
// Admin Actions
|
|
|
|
.route("", web::post().to(route_post::<CreateSite>))
|
|
|
|
.route("", web::put().to(route_post::<EditSite>))
|
|
|
|
.route("/transfer", web::post().to(route_post::<TransferSite>))
|
|
|
|
.route("/config", web::get().to(route_get::<GetSiteConfig>))
|
|
|
|
.route("/config", web::put().to(route_post::<SaveSiteConfig>)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/categories")
|
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route(web::get().to(route_get::<ListCategories>)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/modlog")
|
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route(web::get().to(route_get::<GetModlog>)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/search")
|
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route(web::get().to(route_get::<Search>)),
|
|
|
|
)
|
|
|
|
// Community
|
2020-04-20 17:31:22 +00:00
|
|
|
.service(
|
|
|
|
web::resource("/community")
|
|
|
|
.guard(guard::Post())
|
2020-04-20 18:05:07 +00:00
|
|
|
.wrap(rate_limit.register())
|
2020-04-20 17:31:22 +00:00
|
|
|
.route(web::post().to(route_post::<CreateCommunity>)),
|
|
|
|
)
|
2020-04-20 03:59:07 +00:00
|
|
|
.service(
|
|
|
|
web::scope("/community")
|
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route("", web::get().to(route_get::<GetCommunity>))
|
|
|
|
.route("", web::put().to(route_post::<EditCommunity>))
|
|
|
|
.route("/list", web::get().to(route_get::<ListCommunities>))
|
|
|
|
.route("/follow", web::post().to(route_post::<FollowCommunity>))
|
2020-07-20 17:37:39 +00:00
|
|
|
.route("/delete", web::post().to(route_post::<DeleteCommunity>))
|
2020-04-20 03:59:07 +00:00
|
|
|
// Mod Actions
|
2020-07-20 17:37:39 +00:00
|
|
|
.route("/remove", web::post().to(route_post::<RemoveCommunity>))
|
2020-04-20 03:59:07 +00:00
|
|
|
.route("/transfer", web::post().to(route_post::<TransferCommunity>))
|
|
|
|
.route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
|
2020-09-15 19:26:47 +00:00
|
|
|
.route("/mod", web::post().to(route_post::<AddModToCommunity>))
|
2020-10-27 00:25:18 +00:00
|
|
|
.route("/join", web::post().to(route_post::<CommunityJoin>))
|
|
|
|
.route("/mod/join", web::post().to(route_post::<ModJoin>)),
|
2020-04-20 03:59:07 +00:00
|
|
|
)
|
|
|
|
// Post
|
|
|
|
.service(
|
|
|
|
// Handle POST to /post separately to add the post() rate limitter
|
|
|
|
web::resource("/post")
|
|
|
|
.guard(guard::Post())
|
|
|
|
.wrap(rate_limit.post())
|
|
|
|
.route(web::post().to(route_post::<CreatePost>)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::scope("/post")
|
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route("", web::get().to(route_get::<GetPost>))
|
|
|
|
.route("", web::put().to(route_post::<EditPost>))
|
2020-07-21 03:46:36 +00:00
|
|
|
.route("/delete", web::post().to(route_post::<DeletePost>))
|
|
|
|
.route("/remove", web::post().to(route_post::<RemovePost>))
|
|
|
|
.route("/lock", web::post().to(route_post::<LockPost>))
|
|
|
|
.route("/sticky", web::post().to(route_post::<StickyPost>))
|
2020-04-20 03:59:07 +00:00
|
|
|
.route("/list", web::get().to(route_get::<GetPosts>))
|
|
|
|
.route("/like", web::post().to(route_post::<CreatePostLike>))
|
2020-09-15 19:26:47 +00:00
|
|
|
.route("/save", web::put().to(route_post::<SavePost>))
|
2020-10-13 23:32:35 +00:00
|
|
|
.route("/join", web::post().to(route_post::<PostJoin>))
|
2020-10-25 02:59:13 +00:00
|
|
|
.route("/report", web::post().to(route_post::<CreatePostReport>))
|
2020-11-04 02:15:11 +00:00
|
|
|
.route(
|
|
|
|
"/report/resolve",
|
|
|
|
web::put().to(route_post::<ResolvePostReport>),
|
|
|
|
)
|
|
|
|
.route("/report/list", web::get().to(route_get::<ListPostReports>)),
|
2020-04-20 03:59:07 +00:00
|
|
|
)
|
|
|
|
// Comment
|
|
|
|
.service(
|
2020-10-25 02:59:13 +00:00
|
|
|
web::scope("/comment")
|
2020-04-20 03:59:07 +00:00
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route("", web::post().to(route_post::<CreateComment>))
|
|
|
|
.route("", web::put().to(route_post::<EditComment>))
|
2020-07-21 01:37:44 +00:00
|
|
|
.route("/delete", web::post().to(route_post::<DeleteComment>))
|
|
|
|
.route("/remove", web::post().to(route_post::<RemoveComment>))
|
|
|
|
.route(
|
|
|
|
"/mark_as_read",
|
|
|
|
web::post().to(route_post::<MarkCommentAsRead>),
|
|
|
|
)
|
2020-04-20 03:59:07 +00:00
|
|
|
.route("/like", web::post().to(route_post::<CreateCommentLike>))
|
2020-08-20 14:50:26 +00:00
|
|
|
.route("/save", web::put().to(route_post::<SaveComment>))
|
2020-10-13 23:32:35 +00:00
|
|
|
.route("/list", web::get().to(route_get::<GetComments>))
|
2020-10-25 02:59:13 +00:00
|
|
|
.route("/report", web::post().to(route_post::<CreateCommentReport>))
|
2020-11-04 02:15:11 +00:00
|
|
|
.route(
|
|
|
|
"/report/resolve",
|
|
|
|
web::put().to(route_post::<ResolveCommentReport>),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/report/list",
|
|
|
|
web::get().to(route_get::<ListCommentReports>),
|
|
|
|
),
|
2020-04-20 03:59:07 +00:00
|
|
|
)
|
2020-05-06 02:06:24 +00:00
|
|
|
// Private Message
|
|
|
|
.service(
|
|
|
|
web::scope("/private_message")
|
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route("/list", web::get().to(route_get::<GetPrivateMessages>))
|
|
|
|
.route("", web::post().to(route_post::<CreatePrivateMessage>))
|
2020-07-20 04:29:44 +00:00
|
|
|
.route("", web::put().to(route_post::<EditPrivateMessage>))
|
|
|
|
.route(
|
|
|
|
"/delete",
|
|
|
|
web::post().to(route_post::<DeletePrivateMessage>),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/mark_as_read",
|
|
|
|
web::post().to(route_post::<MarkPrivateMessageAsRead>),
|
|
|
|
),
|
2020-05-06 02:06:24 +00:00
|
|
|
)
|
2020-04-20 03:59:07 +00:00
|
|
|
// User
|
|
|
|
.service(
|
|
|
|
// Account action, I don't like that it's in /user maybe /accounts
|
|
|
|
// Handle /user/register separately to add the register() rate limitter
|
|
|
|
web::resource("/user/register")
|
|
|
|
.guard(guard::Post())
|
|
|
|
.wrap(rate_limit.register())
|
|
|
|
.route(web::post().to(route_post::<Register>)),
|
|
|
|
)
|
|
|
|
// User actions
|
|
|
|
.service(
|
|
|
|
web::scope("/user")
|
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route("", web::get().to(route_get::<GetUserDetails>))
|
|
|
|
.route("/mention", web::get().to(route_get::<GetUserMentions>))
|
2020-07-20 14:56:40 +00:00
|
|
|
.route(
|
|
|
|
"/mention/mark_as_read",
|
|
|
|
web::post().to(route_post::<MarkUserMentionAsRead>),
|
|
|
|
)
|
2020-04-20 03:59:07 +00:00
|
|
|
.route("/replies", web::get().to(route_get::<GetReplies>))
|
2020-04-20 04:47:20 +00:00
|
|
|
.route(
|
|
|
|
"/followed_communities",
|
|
|
|
web::get().to(route_get::<GetFollowedCommunities>),
|
|
|
|
)
|
2020-08-20 14:50:26 +00:00
|
|
|
.route("/join", web::post().to(route_post::<UserJoin>))
|
2020-04-20 03:59:07 +00:00
|
|
|
// Admin action. I don't like that it's in /user
|
|
|
|
.route("/ban", web::post().to(route_post::<BanUser>))
|
|
|
|
// Account actions. I don't like that they're in /user maybe /accounts
|
|
|
|
.route("/login", web::post().to(route_post::<Login>))
|
2020-09-15 01:22:53 +00:00
|
|
|
.route("/get_captcha", web::get().to(route_get::<GetCaptcha>))
|
2020-04-20 04:47:20 +00:00
|
|
|
.route(
|
|
|
|
"/delete_account",
|
|
|
|
web::post().to(route_post::<DeleteAccount>),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/password_reset",
|
|
|
|
web::post().to(route_post::<PasswordReset>),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/password_change",
|
|
|
|
web::post().to(route_post::<PasswordChange>),
|
|
|
|
)
|
2020-04-20 03:59:07 +00:00
|
|
|
// mark_all_as_read feels off being in this section as well
|
2020-04-20 04:47:20 +00:00
|
|
|
.route(
|
|
|
|
"/mark_all_as_read",
|
|
|
|
web::post().to(route_post::<MarkAllAsRead>),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/save_user_settings",
|
|
|
|
web::put().to(route_post::<SaveUserSettings>),
|
2020-10-25 02:59:13 +00:00
|
|
|
)
|
2020-11-04 02:15:11 +00:00
|
|
|
.route("/report_count", web::get().to(route_get::<GetReportCount>)),
|
2020-04-20 03:59:07 +00:00
|
|
|
)
|
|
|
|
// Admin Actions
|
|
|
|
.service(
|
|
|
|
web::resource("/admin/add")
|
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route(web::post().to(route_post::<AddAdmin>)),
|
|
|
|
),
|
|
|
|
);
|
2020-01-15 15:37:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 20:40:03 +00:00
|
|
|
async fn perform<Request>(
|
2020-04-19 22:08:25 +00:00
|
|
|
data: Request,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: web::Data<LemmyContext>,
|
2020-04-19 22:08:25 +00:00
|
|
|
) -> Result<HttpResponse, Error>
|
2020-01-15 15:37:25 +00:00
|
|
|
where
|
2020-08-12 11:31:45 +00:00
|
|
|
Request: Perform,
|
2020-04-21 20:40:03 +00:00
|
|
|
Request: Send + 'static,
|
2020-01-15 15:37:25 +00:00
|
|
|
{
|
2020-08-12 11:31:45 +00:00
|
|
|
let res = data
|
2020-08-24 11:58:24 +00:00
|
|
|
.perform(&context, None)
|
2020-04-21 20:40:03 +00:00
|
|
|
.await
|
|
|
|
.map(|json| HttpResponse::Ok().json(json))
|
2020-04-21 20:48:54 +00:00
|
|
|
.map_err(ErrorBadRequest)?;
|
2020-04-21 20:40:03 +00:00
|
|
|
Ok(res)
|
2020-01-15 15:37:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 19:45:12 +00:00
|
|
|
async fn route_get<'a, Data>(
|
2020-01-23 14:22:17 +00:00
|
|
|
data: web::Query<Data>,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: web::Data<LemmyContext>,
|
2020-01-23 14:22:17 +00:00
|
|
|
) -> Result<HttpResponse, Error>
|
|
|
|
where
|
2020-09-03 19:45:12 +00:00
|
|
|
Data: Deserialize<'a> + Send + 'static + Perform,
|
2020-01-23 14:22:17 +00:00
|
|
|
{
|
2020-08-18 13:43:50 +00:00
|
|
|
perform::<Data>(data.0, context).await
|
2020-01-23 14:22:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 19:45:12 +00:00
|
|
|
async fn route_post<'a, Data>(
|
2020-01-23 14:22:17 +00:00
|
|
|
data: web::Json<Data>,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: web::Data<LemmyContext>,
|
2020-01-23 14:22:17 +00:00
|
|
|
) -> Result<HttpResponse, Error>
|
2020-01-15 15:37:25 +00:00
|
|
|
where
|
2020-09-03 19:45:12 +00:00
|
|
|
Data: Deserialize<'a> + Send + 'static + Perform,
|
2020-01-15 15:37:25 +00:00
|
|
|
{
|
2020-08-18 13:43:50 +00:00
|
|
|
perform::<Data>(data.0, context).await
|
2020-01-15 15:37:25 +00:00
|
|
|
}
|