2021-12-06 14:54:47 +00:00
|
|
|
use actix_web::*;
|
2021-03-25 19:30:15 +00:00
|
|
|
use lemmy_api::Perform;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::{comment::*, community::*, person::*, post::*, site::*, websocket::*};
|
2021-03-25 19:30:15 +00:00
|
|
|
use lemmy_api_crud::PerformCrud;
|
2020-12-21 23:27:42 +00:00
|
|
|
use lemmy_utils::rate_limit::RateLimit;
|
2021-03-29 21:48:37 +00:00
|
|
|
use lemmy_websocket::{routes::chat_route, 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(
|
2021-03-29 15:26:15 +00:00
|
|
|
web::scope("/api/v3")
|
2021-03-29 21:48:37 +00:00
|
|
|
// Websocket
|
|
|
|
.service(web::resource("/ws").to(chat_route))
|
2020-04-20 03:59:07 +00:00
|
|
|
// Site
|
|
|
|
.service(
|
|
|
|
web::scope("/site")
|
|
|
|
.wrap(rate_limit.message())
|
2021-03-25 19:30:15 +00:00
|
|
|
.route("", web::get().to(route_get_crud::<GetSite>))
|
2020-04-20 03:59:07 +00:00
|
|
|
// Admin Actions
|
2021-03-25 19:30:15 +00:00
|
|
|
.route("", web::post().to(route_post_crud::<CreateSite>))
|
|
|
|
.route("", web::put().to(route_post_crud::<EditSite>))
|
2020-04-20 03:59:07 +00:00
|
|
|
.route("/config", web::get().to(route_get::<GetSiteConfig>))
|
|
|
|
.route("/config", web::put().to(route_post::<SaveSiteConfig>)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/modlog")
|
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route(web::get().to(route_get::<GetModlog>)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/search")
|
2022-03-29 15:46:03 +00:00
|
|
|
.wrap(rate_limit.search())
|
2020-04-20 03:59:07 +00:00
|
|
|
.route(web::get().to(route_get::<Search>)),
|
|
|
|
)
|
2021-07-20 07:00:20 +00:00
|
|
|
.service(
|
|
|
|
web::resource("/resolve_object")
|
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route(web::get().to(route_get::<ResolveObject>)),
|
|
|
|
)
|
2020-04-20 03:59:07 +00:00
|
|
|
// 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())
|
2021-03-25 19:30:15 +00:00
|
|
|
.route(web::post().to(route_post_crud::<CreateCommunity>)),
|
2020-04-20 17:31:22 +00:00
|
|
|
)
|
2020-04-20 03:59:07 +00:00
|
|
|
.service(
|
|
|
|
web::scope("/community")
|
|
|
|
.wrap(rate_limit.message())
|
2021-03-25 19:30:15 +00:00
|
|
|
.route("", web::get().to(route_get_crud::<GetCommunity>))
|
|
|
|
.route("", web::put().to(route_post_crud::<EditCommunity>))
|
2022-02-18 02:30:47 +00:00
|
|
|
.route("/hide", web::put().to(route_post_crud::<HideCommunity>))
|
2021-03-25 19:30:15 +00:00
|
|
|
.route("/list", web::get().to(route_get_crud::<ListCommunities>))
|
2020-04-20 03:59:07 +00:00
|
|
|
.route("/follow", web::post().to(route_post::<FollowCommunity>))
|
2021-08-19 20:54:15 +00:00
|
|
|
.route("/block", web::post().to(route_post::<BlockCommunity>))
|
2021-03-25 19:30:15 +00:00
|
|
|
.route(
|
|
|
|
"/delete",
|
|
|
|
web::post().to(route_post_crud::<DeleteCommunity>),
|
|
|
|
)
|
2020-04-20 03:59:07 +00:00
|
|
|
// Mod Actions
|
2021-03-25 19:30:15 +00:00
|
|
|
.route(
|
|
|
|
"/remove",
|
|
|
|
web::post().to(route_post_crud::<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())
|
2021-03-25 19:30:15 +00:00
|
|
|
.route(web::post().to(route_post_crud::<CreatePost>)),
|
2020-04-20 03:59:07 +00:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::scope("/post")
|
|
|
|
.wrap(rate_limit.message())
|
2021-03-25 19:30:15 +00:00
|
|
|
.route("", web::get().to(route_get_crud::<GetPost>))
|
|
|
|
.route("", web::put().to(route_post_crud::<EditPost>))
|
|
|
|
.route("/delete", web::post().to(route_post_crud::<DeletePost>))
|
|
|
|
.route("/remove", web::post().to(route_post_crud::<RemovePost>))
|
2021-11-23 14:15:43 +00:00
|
|
|
.route(
|
|
|
|
"/mark_as_read",
|
|
|
|
web::post().to(route_post::<MarkPostAsRead>),
|
|
|
|
)
|
2020-07-21 03:46:36 +00:00
|
|
|
.route("/lock", web::post().to(route_post::<LockPost>))
|
|
|
|
.route("/sticky", web::post().to(route_post::<StickyPost>))
|
2021-03-25 19:30:15 +00:00
|
|
|
.route("/list", web::get().to(route_get_crud::<GetPosts>))
|
2020-04-20 03:59:07 +00:00
|
|
|
.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>),
|
|
|
|
)
|
2021-08-19 14:12:49 +00:00
|
|
|
.route("/report/list", web::get().to(route_get::<ListPostReports>))
|
|
|
|
.route(
|
|
|
|
"/site_metadata",
|
|
|
|
web::get().to(route_get::<GetSiteMetadata>),
|
|
|
|
),
|
2020-04-20 03:59:07 +00:00
|
|
|
)
|
|
|
|
// Comment
|
2021-11-11 20:40:25 +00:00
|
|
|
.service(
|
|
|
|
// Handle POST to /comment separately to add the comment() rate limitter
|
|
|
|
web::resource("/comment")
|
|
|
|
.guard(guard::Post())
|
|
|
|
.wrap(rate_limit.comment())
|
|
|
|
.route(web::post().to(route_post_crud::<CreateComment>)),
|
|
|
|
)
|
2020-04-20 03:59:07 +00:00
|
|
|
.service(
|
2020-10-25 02:59:13 +00:00
|
|
|
web::scope("/comment")
|
2020-04-20 03:59:07 +00:00
|
|
|
.wrap(rate_limit.message())
|
2021-11-23 15:53:48 +00:00
|
|
|
.route("", web::get().to(route_get_crud::<GetComment>))
|
2021-03-25 19:30:15 +00:00
|
|
|
.route("", web::put().to(route_post_crud::<EditComment>))
|
|
|
|
.route("/delete", web::post().to(route_post_crud::<DeleteComment>))
|
|
|
|
.route("/remove", web::post().to(route_post_crud::<RemoveComment>))
|
2020-07-21 01:37:44 +00:00
|
|
|
.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>))
|
2021-03-25 19:30:15 +00:00
|
|
|
.route("/list", web::get().to(route_get_crud::<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())
|
2021-03-25 19:30:15 +00:00
|
|
|
.route("/list", web::get().to(route_get_crud::<GetPrivateMessages>))
|
|
|
|
.route("", web::post().to(route_post_crud::<CreatePrivateMessage>))
|
|
|
|
.route("", web::put().to(route_post_crud::<EditPrivateMessage>))
|
2020-07-20 04:29:44 +00:00
|
|
|
.route(
|
|
|
|
"/delete",
|
2021-03-25 19:30:15 +00:00
|
|
|
web::post().to(route_post_crud::<DeletePrivateMessage>),
|
2020-07-20 04:29:44 +00:00
|
|
|
)
|
|
|
|
.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())
|
2021-03-25 19:30:15 +00:00
|
|
|
.route(web::post().to(route_post_crud::<Register>)),
|
2020-04-20 03:59:07 +00:00
|
|
|
)
|
2021-11-25 13:04:19 +00:00
|
|
|
.service(
|
|
|
|
// Handle captcha separately
|
|
|
|
web::resource("/user/get_captcha")
|
|
|
|
.wrap(rate_limit.post())
|
|
|
|
.route(web::get().to(route_get::<GetCaptcha>)),
|
|
|
|
)
|
2020-04-20 03:59:07 +00:00
|
|
|
// User actions
|
|
|
|
.service(
|
|
|
|
web::scope("/user")
|
|
|
|
.wrap(rate_limit.message())
|
2021-03-25 19:30:15 +00:00
|
|
|
.route("", web::get().to(route_get_crud::<GetPersonDetails>))
|
2021-03-10 22:33:55 +00:00
|
|
|
.route("/mention", web::get().to(route_get::<GetPersonMentions>))
|
2020-07-20 14:56:40 +00:00
|
|
|
.route(
|
|
|
|
"/mention/mark_as_read",
|
2021-03-10 22:33:55 +00:00
|
|
|
web::post().to(route_post::<MarkPersonMentionAsRead>),
|
2020-07-20 14:56:40 +00:00
|
|
|
)
|
2020-04-20 03:59:07 +00:00
|
|
|
.route("/replies", web::get().to(route_get::<GetReplies>))
|
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
|
2021-03-10 22:33:55 +00:00
|
|
|
.route("/ban", web::post().to(route_post::<BanPerson>))
|
2021-11-19 20:56:41 +00:00
|
|
|
.route("/banned", web::get().to(route_get::<GetBannedPersons>))
|
2021-08-19 20:54:15 +00:00
|
|
|
.route("/block", web::post().to(route_post::<BlockPerson>))
|
2020-04-20 03:59:07 +00:00
|
|
|
// Account actions. I don't like that they're in /user maybe /accounts
|
|
|
|
.route("/login", web::post().to(route_post::<Login>))
|
2020-04-20 04:47:20 +00:00
|
|
|
.route(
|
|
|
|
"/delete_account",
|
2021-03-25 19:30:15 +00:00
|
|
|
web::post().to(route_post_crud::<DeleteAccount>),
|
2020-04-20 04:47:20 +00:00
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/password_reset",
|
|
|
|
web::post().to(route_post::<PasswordReset>),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/password_change",
|
2022-04-13 18:12:25 +00:00
|
|
|
web::post().to(route_post::<PasswordChangeAfterReset>),
|
2020-04-20 04:47:20 +00:00
|
|
|
)
|
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
|
|
|
)
|
2021-04-01 21:39:01 +00:00
|
|
|
.route(
|
|
|
|
"/change_password",
|
|
|
|
web::put().to(route_post::<ChangePassword>),
|
|
|
|
)
|
2021-10-16 10:43:41 +00:00
|
|
|
.route("/report_count", web::get().to(route_get::<GetReportCount>))
|
2021-12-15 19:49:59 +00:00
|
|
|
.route("/unread_count", web::get().to(route_get::<GetUnreadCount>))
|
2022-01-26 17:57:16 +00:00
|
|
|
.route("/verify_email", web::post().to(route_post::<VerifyEmail>))
|
|
|
|
.route("/leave_admin", web::post().to(route_post::<LeaveAdmin>)),
|
2020-04-20 03:59:07 +00:00
|
|
|
)
|
|
|
|
// Admin Actions
|
|
|
|
.service(
|
2021-12-15 19:49:59 +00:00
|
|
|
web::scope("/admin")
|
2020-04-20 03:59:07 +00:00
|
|
|
.wrap(rate_limit.message())
|
2021-12-15 19:49:59 +00:00
|
|
|
.route("/add", web::post().to(route_post::<AddAdmin>))
|
|
|
|
.route(
|
|
|
|
"/registration_application/count",
|
|
|
|
web::get().to(route_get::<GetUnreadRegistrationApplicationCount>),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/registration_application/list",
|
|
|
|
web::get().to(route_get::<ListRegistrationApplications>),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/registration_application/approve",
|
|
|
|
web::put().to(route_post::<ApproveRegistrationApplication>),
|
|
|
|
),
|
2020-04-20 03:59:07 +00:00
|
|
|
),
|
|
|
|
);
|
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
|
2021-12-06 14:54:47 +00:00
|
|
|
.map(|json| HttpResponse::Ok().json(json))?;
|
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
|
|
|
}
|
2021-03-25 19:30:15 +00:00
|
|
|
|
|
|
|
async fn perform_crud<Request>(
|
|
|
|
data: Request,
|
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse, Error>
|
|
|
|
where
|
|
|
|
Request: PerformCrud,
|
|
|
|
Request: Send + 'static,
|
|
|
|
{
|
|
|
|
let res = data
|
|
|
|
.perform(&context, None)
|
|
|
|
.await
|
2021-12-06 14:54:47 +00:00
|
|
|
.map(|json| HttpResponse::Ok().json(json))?;
|
2021-03-25 19:30:15 +00:00
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn route_get_crud<'a, Data>(
|
|
|
|
data: web::Query<Data>,
|
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse, Error>
|
|
|
|
where
|
|
|
|
Data: Deserialize<'a> + Send + 'static + PerformCrud,
|
|
|
|
{
|
|
|
|
perform_crud::<Data>(data.0, context).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn route_post_crud<'a, Data>(
|
|
|
|
data: web::Json<Data>,
|
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse, Error>
|
|
|
|
where
|
|
|
|
Data: Deserialize<'a> + Send + 'static + PerformCrud,
|
|
|
|
{
|
|
|
|
perform_crud::<Data>(data.0, context).await
|
|
|
|
}
|