2022-11-18 21:33:54 -07:00
|
|
|
use actix_web::{guard, web, Error, HttpResponse, Result};
|
2021-03-25 13:30:15 -06:00
|
|
|
use lemmy_api::Perform;
|
2022-09-19 16:58:42 -06:00
|
|
|
use lemmy_api_common::{
|
2022-11-18 21:33:54 -07:00
|
|
|
comment::{
|
|
|
|
CreateComment,
|
|
|
|
CreateCommentLike,
|
|
|
|
CreateCommentReport,
|
|
|
|
DeleteComment,
|
|
|
|
EditComment,
|
|
|
|
GetComment,
|
|
|
|
GetComments,
|
|
|
|
ListCommentReports,
|
|
|
|
RemoveComment,
|
|
|
|
ResolveCommentReport,
|
|
|
|
SaveComment,
|
|
|
|
},
|
|
|
|
community::{
|
|
|
|
AddModToCommunity,
|
|
|
|
BanFromCommunity,
|
|
|
|
BlockCommunity,
|
|
|
|
CreateCommunity,
|
|
|
|
DeleteCommunity,
|
|
|
|
EditCommunity,
|
|
|
|
FollowCommunity,
|
|
|
|
GetCommunity,
|
|
|
|
HideCommunity,
|
|
|
|
ListCommunities,
|
|
|
|
RemoveCommunity,
|
|
|
|
TransferCommunity,
|
|
|
|
},
|
2022-11-28 07:29:33 -07:00
|
|
|
context::LemmyContext,
|
2022-11-18 21:33:54 -07:00
|
|
|
person::{
|
|
|
|
AddAdmin,
|
|
|
|
BanPerson,
|
|
|
|
BlockPerson,
|
|
|
|
ChangePassword,
|
|
|
|
DeleteAccount,
|
|
|
|
GetBannedPersons,
|
|
|
|
GetCaptcha,
|
|
|
|
GetPersonDetails,
|
|
|
|
GetPersonMentions,
|
|
|
|
GetReplies,
|
|
|
|
GetReportCount,
|
|
|
|
GetUnreadCount,
|
|
|
|
Login,
|
|
|
|
MarkAllAsRead,
|
|
|
|
MarkCommentReplyAsRead,
|
|
|
|
MarkPersonMentionAsRead,
|
|
|
|
PasswordChangeAfterReset,
|
|
|
|
PasswordReset,
|
|
|
|
Register,
|
|
|
|
SaveUserSettings,
|
|
|
|
VerifyEmail,
|
|
|
|
},
|
|
|
|
post::{
|
|
|
|
CreatePost,
|
|
|
|
CreatePostLike,
|
|
|
|
CreatePostReport,
|
|
|
|
DeletePost,
|
|
|
|
EditPost,
|
|
|
|
GetPost,
|
|
|
|
GetPosts,
|
|
|
|
GetSiteMetadata,
|
|
|
|
ListPostReports,
|
|
|
|
LockPost,
|
|
|
|
MarkPostAsRead,
|
|
|
|
RemovePost,
|
|
|
|
ResolvePostReport,
|
|
|
|
SavePost,
|
|
|
|
StickyPost,
|
|
|
|
},
|
|
|
|
private_message::{
|
|
|
|
CreatePrivateMessage,
|
|
|
|
CreatePrivateMessageReport,
|
|
|
|
DeletePrivateMessage,
|
|
|
|
EditPrivateMessage,
|
|
|
|
GetPrivateMessages,
|
|
|
|
ListPrivateMessageReports,
|
|
|
|
MarkPrivateMessageAsRead,
|
|
|
|
ResolvePrivateMessageReport,
|
|
|
|
},
|
|
|
|
site::{
|
|
|
|
ApproveRegistrationApplication,
|
|
|
|
CreateSite,
|
|
|
|
EditSite,
|
|
|
|
GetModlog,
|
|
|
|
GetSite,
|
|
|
|
GetUnreadRegistrationApplicationCount,
|
|
|
|
LeaveAdmin,
|
|
|
|
ListRegistrationApplications,
|
|
|
|
PurgeComment,
|
|
|
|
PurgeCommunity,
|
|
|
|
PurgePerson,
|
|
|
|
PurgePost,
|
|
|
|
ResolveObject,
|
|
|
|
Search,
|
|
|
|
},
|
2022-11-25 19:04:46 -07:00
|
|
|
websocket::{
|
|
|
|
routes::chat_route,
|
2022-11-28 07:29:33 -07:00
|
|
|
serialize_websocket_message,
|
2022-11-25 19:04:46 -07:00
|
|
|
structs::{CommunityJoin, ModJoin, PostJoin, UserJoin},
|
2022-11-28 07:29:33 -07:00
|
|
|
UserOperation,
|
|
|
|
UserOperationApub,
|
|
|
|
UserOperationCrud,
|
2022-11-25 19:04:46 -07:00
|
|
|
},
|
2022-09-19 16:58:42 -06:00
|
|
|
};
|
2021-03-25 13:30:15 -06:00
|
|
|
use lemmy_api_crud::PerformCrud;
|
2022-11-28 07:29:33 -07:00
|
|
|
use lemmy_apub::{api::PerformApub, SendActivity};
|
|
|
|
use lemmy_utils::{error::LemmyError, rate_limit::RateLimitCell, ConnectionId};
|
2020-09-03 13:45:12 -06:00
|
|
|
use serde::Deserialize;
|
2022-11-28 07:29:33 -07:00
|
|
|
use std::result;
|
2020-01-15 08:37:25 -07:00
|
|
|
|
2022-11-16 12:06:22 -07:00
|
|
|
pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
|
2020-04-19 21:59:07 -06:00
|
|
|
cfg.service(
|
2021-03-29 09:26:15 -06:00
|
|
|
web::scope("/api/v3")
|
2021-03-29 15:48:37 -06:00
|
|
|
// Websocket
|
|
|
|
.service(web::resource("/ws").to(chat_route))
|
2020-04-19 21:59:07 -06:00
|
|
|
// Site
|
|
|
|
.service(
|
|
|
|
web::scope("/site")
|
|
|
|
.wrap(rate_limit.message())
|
2021-03-25 13:30:15 -06:00
|
|
|
.route("", web::get().to(route_get_crud::<GetSite>))
|
2020-04-19 21:59:07 -06:00
|
|
|
// Admin Actions
|
2021-03-25 13:30:15 -06:00
|
|
|
.route("", web::post().to(route_post_crud::<CreateSite>))
|
2022-06-22 14:24:54 -06:00
|
|
|
.route("", web::put().to(route_post_crud::<EditSite>)),
|
2020-04-19 21:59:07 -06:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/modlog")
|
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route(web::get().to(route_get::<GetModlog>)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/search")
|
2022-03-29 09:46:03 -06:00
|
|
|
.wrap(rate_limit.search())
|
2022-11-28 07:29:33 -07:00
|
|
|
.route(web::get().to(route_get_apub::<Search>)),
|
2020-04-19 21:59:07 -06:00
|
|
|
)
|
2021-07-20 01:00:20 -06:00
|
|
|
.service(
|
|
|
|
web::resource("/resolve_object")
|
|
|
|
.wrap(rate_limit.message())
|
2022-11-28 07:29:33 -07:00
|
|
|
.route(web::get().to(route_get_apub::<ResolveObject>)),
|
2021-07-20 01:00:20 -06:00
|
|
|
)
|
2020-04-19 21:59:07 -06:00
|
|
|
// Community
|
2020-04-20 11:31:22 -06:00
|
|
|
.service(
|
|
|
|
web::resource("/community")
|
|
|
|
.guard(guard::Post())
|
2020-04-20 12:05:07 -06:00
|
|
|
.wrap(rate_limit.register())
|
2021-03-25 13:30:15 -06:00
|
|
|
.route(web::post().to(route_post_crud::<CreateCommunity>)),
|
2020-04-20 11:31:22 -06:00
|
|
|
)
|
2020-04-19 21:59:07 -06:00
|
|
|
.service(
|
|
|
|
web::scope("/community")
|
|
|
|
.wrap(rate_limit.message())
|
2022-11-28 07:29:33 -07:00
|
|
|
.route("", web::get().to(route_get_apub::<GetCommunity>))
|
2021-03-25 13:30:15 -06:00
|
|
|
.route("", web::put().to(route_post_crud::<EditCommunity>))
|
2022-04-28 14:32:32 -06:00
|
|
|
.route("/hide", web::put().to(route_post::<HideCommunity>))
|
2021-03-25 13:30:15 -06:00
|
|
|
.route("/list", web::get().to(route_get_crud::<ListCommunities>))
|
2020-04-19 21:59:07 -06:00
|
|
|
.route("/follow", web::post().to(route_post::<FollowCommunity>))
|
2021-08-19 14:54:15 -06:00
|
|
|
.route("/block", web::post().to(route_post::<BlockCommunity>))
|
2021-03-25 13:30:15 -06:00
|
|
|
.route(
|
|
|
|
"/delete",
|
|
|
|
web::post().to(route_post_crud::<DeleteCommunity>),
|
|
|
|
)
|
2020-04-19 21:59:07 -06:00
|
|
|
// Mod Actions
|
2021-03-25 13:30:15 -06:00
|
|
|
.route(
|
|
|
|
"/remove",
|
|
|
|
web::post().to(route_post_crud::<RemoveCommunity>),
|
|
|
|
)
|
2020-04-19 21:59:07 -06:00
|
|
|
.route("/transfer", web::post().to(route_post::<TransferCommunity>))
|
|
|
|
.route("/ban_user", web::post().to(route_post::<BanFromCommunity>))
|
2020-09-15 13:26:47 -06:00
|
|
|
.route("/mod", web::post().to(route_post::<AddModToCommunity>))
|
2020-10-26 18:25:18 -06:00
|
|
|
.route("/join", web::post().to(route_post::<CommunityJoin>))
|
|
|
|
.route("/mod/join", web::post().to(route_post::<ModJoin>)),
|
2020-04-19 21:59:07 -06: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 13:30:15 -06:00
|
|
|
.route(web::post().to(route_post_crud::<CreatePost>)),
|
2020-04-19 21:59:07 -06:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::scope("/post")
|
|
|
|
.wrap(rate_limit.message())
|
2021-03-25 13:30:15 -06: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 07:15:43 -07:00
|
|
|
.route(
|
|
|
|
"/mark_as_read",
|
|
|
|
web::post().to(route_post::<MarkPostAsRead>),
|
|
|
|
)
|
2020-07-20 21:46:36 -06:00
|
|
|
.route("/lock", web::post().to(route_post::<LockPost>))
|
|
|
|
.route("/sticky", web::post().to(route_post::<StickyPost>))
|
2022-11-28 07:29:33 -07:00
|
|
|
.route("/list", web::get().to(route_get_apub::<GetPosts>))
|
2020-04-19 21:59:07 -06:00
|
|
|
.route("/like", web::post().to(route_post::<CreatePostLike>))
|
2020-09-15 13:26:47 -06:00
|
|
|
.route("/save", web::put().to(route_post::<SavePost>))
|
2020-10-13 17:32:35 -06:00
|
|
|
.route("/join", web::post().to(route_post::<PostJoin>))
|
2020-10-24 20:59:13 -06:00
|
|
|
.route("/report", web::post().to(route_post::<CreatePostReport>))
|
2020-11-03 19:15:11 -07:00
|
|
|
.route(
|
|
|
|
"/report/resolve",
|
|
|
|
web::put().to(route_post::<ResolvePostReport>),
|
|
|
|
)
|
2021-08-19 08:12:49 -06:00
|
|
|
.route("/report/list", web::get().to(route_get::<ListPostReports>))
|
|
|
|
.route(
|
|
|
|
"/site_metadata",
|
|
|
|
web::get().to(route_get::<GetSiteMetadata>),
|
|
|
|
),
|
2020-04-19 21:59:07 -06:00
|
|
|
)
|
|
|
|
// Comment
|
2021-11-11 13:40:25 -07: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-19 21:59:07 -06:00
|
|
|
.service(
|
2020-10-24 20:59:13 -06:00
|
|
|
web::scope("/comment")
|
2020-04-19 21:59:07 -06:00
|
|
|
.wrap(rate_limit.message())
|
2021-11-23 08:53:48 -07:00
|
|
|
.route("", web::get().to(route_get_crud::<GetComment>))
|
2021-03-25 13:30:15 -06: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-20 19:37:44 -06:00
|
|
|
.route(
|
|
|
|
"/mark_as_read",
|
2022-07-29 21:55:59 -06:00
|
|
|
web::post().to(route_post::<MarkCommentReplyAsRead>),
|
2020-07-20 19:37:44 -06:00
|
|
|
)
|
2020-04-19 21:59:07 -06:00
|
|
|
.route("/like", web::post().to(route_post::<CreateCommentLike>))
|
2020-08-20 08:50:26 -06:00
|
|
|
.route("/save", web::put().to(route_post::<SaveComment>))
|
2022-11-28 07:29:33 -07:00
|
|
|
.route("/list", web::get().to(route_get_apub::<GetComments>))
|
2020-10-24 20:59:13 -06:00
|
|
|
.route("/report", web::post().to(route_post::<CreateCommentReport>))
|
2020-11-03 19:15:11 -07:00
|
|
|
.route(
|
|
|
|
"/report/resolve",
|
|
|
|
web::put().to(route_post::<ResolveCommentReport>),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/report/list",
|
|
|
|
web::get().to(route_get::<ListCommentReports>),
|
|
|
|
),
|
2020-04-19 21:59:07 -06:00
|
|
|
)
|
2020-05-05 20:06:24 -06:00
|
|
|
// Private Message
|
|
|
|
.service(
|
|
|
|
web::scope("/private_message")
|
|
|
|
.wrap(rate_limit.message())
|
2021-03-25 13:30:15 -06: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-19 22:29:44 -06:00
|
|
|
.route(
|
|
|
|
"/delete",
|
2021-03-25 13:30:15 -06:00
|
|
|
web::post().to(route_post_crud::<DeletePrivateMessage>),
|
2020-07-19 22:29:44 -06:00
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/mark_as_read",
|
|
|
|
web::post().to(route_post::<MarkPrivateMessageAsRead>),
|
2022-09-19 16:58:42 -06:00
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/report",
|
|
|
|
web::post().to(route_post::<CreatePrivateMessageReport>),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/report/resolve",
|
|
|
|
web::put().to(route_post::<ResolvePrivateMessageReport>),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/report/list",
|
|
|
|
web::get().to(route_get::<ListPrivateMessageReports>),
|
2020-07-19 22:29:44 -06:00
|
|
|
),
|
2020-05-05 20:06:24 -06:00
|
|
|
)
|
2020-04-19 21:59:07 -06: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 13:30:15 -06:00
|
|
|
.route(web::post().to(route_post_crud::<Register>)),
|
2020-04-19 21:59:07 -06:00
|
|
|
)
|
2021-11-25 06:04:19 -07:00
|
|
|
.service(
|
|
|
|
// Handle captcha separately
|
|
|
|
web::resource("/user/get_captcha")
|
|
|
|
.wrap(rate_limit.post())
|
|
|
|
.route(web::get().to(route_get::<GetCaptcha>)),
|
|
|
|
)
|
2020-04-19 21:59:07 -06:00
|
|
|
// User actions
|
|
|
|
.service(
|
|
|
|
web::scope("/user")
|
|
|
|
.wrap(rate_limit.message())
|
2022-11-28 07:29:33 -07:00
|
|
|
.route("", web::get().to(route_get_apub::<GetPersonDetails>))
|
2021-03-10 15:33:55 -07:00
|
|
|
.route("/mention", web::get().to(route_get::<GetPersonMentions>))
|
2020-07-20 08:56:40 -06:00
|
|
|
.route(
|
|
|
|
"/mention/mark_as_read",
|
2021-03-10 15:33:55 -07:00
|
|
|
web::post().to(route_post::<MarkPersonMentionAsRead>),
|
2020-07-20 08:56:40 -06:00
|
|
|
)
|
2020-04-19 21:59:07 -06:00
|
|
|
.route("/replies", web::get().to(route_get::<GetReplies>))
|
2020-08-20 08:50:26 -06:00
|
|
|
.route("/join", web::post().to(route_post::<UserJoin>))
|
2020-04-19 21:59:07 -06:00
|
|
|
// Admin action. I don't like that it's in /user
|
2021-03-10 15:33:55 -07:00
|
|
|
.route("/ban", web::post().to(route_post::<BanPerson>))
|
2021-11-19 13:56:41 -07:00
|
|
|
.route("/banned", web::get().to(route_get::<GetBannedPersons>))
|
2021-08-19 14:54:15 -06:00
|
|
|
.route("/block", web::post().to(route_post::<BlockPerson>))
|
2020-04-19 21:59:07 -06:00
|
|
|
// Account actions. I don't like that they're in /user maybe /accounts
|
|
|
|
.route("/login", web::post().to(route_post::<Login>))
|
2020-04-19 22:47:20 -06:00
|
|
|
.route(
|
|
|
|
"/delete_account",
|
2021-03-25 13:30:15 -06:00
|
|
|
web::post().to(route_post_crud::<DeleteAccount>),
|
2020-04-19 22:47:20 -06:00
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/password_reset",
|
|
|
|
web::post().to(route_post::<PasswordReset>),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/password_change",
|
2022-04-13 12:12:25 -06:00
|
|
|
web::post().to(route_post::<PasswordChangeAfterReset>),
|
2020-04-19 22:47:20 -06:00
|
|
|
)
|
2020-04-19 21:59:07 -06:00
|
|
|
// mark_all_as_read feels off being in this section as well
|
2020-04-19 22:47:20 -06:00
|
|
|
.route(
|
|
|
|
"/mark_all_as_read",
|
|
|
|
web::post().to(route_post::<MarkAllAsRead>),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/save_user_settings",
|
|
|
|
web::put().to(route_post::<SaveUserSettings>),
|
2020-10-24 20:59:13 -06:00
|
|
|
)
|
2021-04-01 15:39:01 -06:00
|
|
|
.route(
|
|
|
|
"/change_password",
|
|
|
|
web::put().to(route_post::<ChangePassword>),
|
|
|
|
)
|
2021-10-16 04:43:41 -06:00
|
|
|
.route("/report_count", web::get().to(route_get::<GetReportCount>))
|
2021-12-15 12:49:59 -07:00
|
|
|
.route("/unread_count", web::get().to(route_get::<GetUnreadCount>))
|
2022-01-26 10:57:16 -07:00
|
|
|
.route("/verify_email", web::post().to(route_post::<VerifyEmail>))
|
|
|
|
.route("/leave_admin", web::post().to(route_post::<LeaveAdmin>)),
|
2020-04-19 21:59:07 -06:00
|
|
|
)
|
|
|
|
// Admin Actions
|
|
|
|
.service(
|
2021-12-15 12:49:59 -07:00
|
|
|
web::scope("/admin")
|
2020-04-19 21:59:07 -06:00
|
|
|
.wrap(rate_limit.message())
|
2021-12-15 12:49:59 -07: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>),
|
|
|
|
),
|
2022-06-13 13:15:04 -06:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::scope("/admin/purge")
|
|
|
|
.wrap(rate_limit.message())
|
|
|
|
.route("/person", web::post().to(route_post::<PurgePerson>))
|
|
|
|
.route("/community", web::post().to(route_post::<PurgeCommunity>))
|
|
|
|
.route("/post", web::post().to(route_post::<PurgePost>))
|
|
|
|
.route("/comment", web::post().to(route_post::<PurgeComment>)),
|
2020-04-19 21:59:07 -06:00
|
|
|
),
|
|
|
|
);
|
2020-01-15 08:37:25 -07:00
|
|
|
}
|
|
|
|
|
2022-11-28 07:29:33 -07:00
|
|
|
async fn perform<'a, Data>(
|
|
|
|
data: Data,
|
2020-08-18 07:43:50 -06:00
|
|
|
context: web::Data<LemmyContext>,
|
2020-04-19 16:08:25 -06:00
|
|
|
) -> Result<HttpResponse, Error>
|
2020-01-15 08:37:25 -07:00
|
|
|
where
|
2022-11-28 07:29:33 -07:00
|
|
|
Data: Perform
|
|
|
|
+ SendActivity<Response = <Data as Perform>::Response>
|
|
|
|
+ Clone
|
|
|
|
+ Deserialize<'a>
|
|
|
|
+ Send
|
|
|
|
+ 'static,
|
2020-01-15 08:37:25 -07:00
|
|
|
{
|
2022-11-28 07:29:33 -07:00
|
|
|
let res = data.perform(&context, None).await?;
|
|
|
|
SendActivity::send_activity(&data, &res, &context).await?;
|
|
|
|
Ok(HttpResponse::Ok().json(res))
|
2020-01-15 08:37:25 -07:00
|
|
|
}
|
|
|
|
|
2020-09-03 13:45:12 -06:00
|
|
|
async fn route_get<'a, Data>(
|
2020-01-23 07:22:17 -07:00
|
|
|
data: web::Query<Data>,
|
2020-08-18 07:43:50 -06:00
|
|
|
context: web::Data<LemmyContext>,
|
2020-01-23 07:22:17 -07:00
|
|
|
) -> Result<HttpResponse, Error>
|
|
|
|
where
|
2022-11-28 07:29:33 -07:00
|
|
|
Data: Perform
|
|
|
|
+ SendActivity<Response = <Data as Perform>::Response>
|
|
|
|
+ Clone
|
|
|
|
+ Deserialize<'a>
|
|
|
|
+ Send
|
|
|
|
+ 'static,
|
2020-01-23 07:22:17 -07:00
|
|
|
{
|
2020-08-18 07:43:50 -06:00
|
|
|
perform::<Data>(data.0, context).await
|
2020-01-23 07:22:17 -07:00
|
|
|
}
|
|
|
|
|
2022-11-28 07:29:33 -07:00
|
|
|
async fn route_get_apub<'a, Data>(
|
|
|
|
data: web::Query<Data>,
|
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse, Error>
|
|
|
|
where
|
|
|
|
Data: PerformApub
|
|
|
|
+ SendActivity<Response = <Data as PerformApub>::Response>
|
|
|
|
+ Clone
|
|
|
|
+ Deserialize<'a>
|
|
|
|
+ Send
|
|
|
|
+ 'static,
|
|
|
|
{
|
|
|
|
let res = data.perform(&context, None).await?;
|
|
|
|
SendActivity::send_activity(&data.0, &res, &context).await?;
|
|
|
|
Ok(HttpResponse::Ok().json(res))
|
|
|
|
}
|
|
|
|
|
2020-09-03 13:45:12 -06:00
|
|
|
async fn route_post<'a, Data>(
|
2020-01-23 07:22:17 -07:00
|
|
|
data: web::Json<Data>,
|
2020-08-18 07:43:50 -06:00
|
|
|
context: web::Data<LemmyContext>,
|
2020-01-23 07:22:17 -07:00
|
|
|
) -> Result<HttpResponse, Error>
|
2020-01-15 08:37:25 -07:00
|
|
|
where
|
2022-11-28 07:29:33 -07:00
|
|
|
Data: Perform
|
|
|
|
+ SendActivity<Response = <Data as Perform>::Response>
|
|
|
|
+ Clone
|
|
|
|
+ Deserialize<'a>
|
|
|
|
+ Send
|
|
|
|
+ 'static,
|
2020-01-15 08:37:25 -07:00
|
|
|
{
|
2020-08-18 07:43:50 -06:00
|
|
|
perform::<Data>(data.0, context).await
|
2020-01-15 08:37:25 -07:00
|
|
|
}
|
2021-03-25 13:30:15 -06:00
|
|
|
|
2022-11-28 07:29:33 -07:00
|
|
|
async fn perform_crud<'a, Data>(
|
|
|
|
data: Data,
|
2021-03-25 13:30:15 -06:00
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse, Error>
|
|
|
|
where
|
2022-11-28 07:29:33 -07:00
|
|
|
Data: PerformCrud
|
|
|
|
+ SendActivity<Response = <Data as PerformCrud>::Response>
|
|
|
|
+ Clone
|
|
|
|
+ Deserialize<'a>
|
|
|
|
+ Send
|
|
|
|
+ 'static,
|
2021-03-25 13:30:15 -06:00
|
|
|
{
|
2022-11-28 07:29:33 -07:00
|
|
|
let res = data.perform(&context, None).await?;
|
|
|
|
SendActivity::send_activity(&data, &res, &context).await?;
|
|
|
|
Ok(HttpResponse::Ok().json(res))
|
2021-03-25 13:30:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn route_get_crud<'a, Data>(
|
|
|
|
data: web::Query<Data>,
|
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse, Error>
|
|
|
|
where
|
2022-11-28 07:29:33 -07:00
|
|
|
Data: PerformCrud
|
|
|
|
+ SendActivity<Response = <Data as PerformCrud>::Response>
|
|
|
|
+ Clone
|
|
|
|
+ Deserialize<'a>
|
|
|
|
+ Send
|
|
|
|
+ 'static,
|
2021-03-25 13:30:15 -06:00
|
|
|
{
|
|
|
|
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
|
2022-11-28 07:29:33 -07:00
|
|
|
Data: PerformCrud
|
|
|
|
+ SendActivity<Response = <Data as PerformCrud>::Response>
|
|
|
|
+ Clone
|
|
|
|
+ Deserialize<'a>
|
|
|
|
+ Send
|
|
|
|
+ 'static,
|
2021-03-25 13:30:15 -06:00
|
|
|
{
|
|
|
|
perform_crud::<Data>(data.0, context).await
|
|
|
|
}
|
2022-11-28 07:29:33 -07:00
|
|
|
|
|
|
|
pub async fn match_websocket_operation_crud(
|
|
|
|
context: LemmyContext,
|
|
|
|
id: ConnectionId,
|
|
|
|
op: UserOperationCrud,
|
|
|
|
data: &str,
|
|
|
|
) -> result::Result<String, LemmyError> {
|
|
|
|
match op {
|
|
|
|
// User ops
|
|
|
|
UserOperationCrud::Register => {
|
|
|
|
do_websocket_operation_crud::<Register>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::DeleteAccount => {
|
|
|
|
do_websocket_operation_crud::<DeleteAccount>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private Message ops
|
|
|
|
UserOperationCrud::CreatePrivateMessage => {
|
|
|
|
do_websocket_operation_crud::<CreatePrivateMessage>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::EditPrivateMessage => {
|
|
|
|
do_websocket_operation_crud::<EditPrivateMessage>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::DeletePrivateMessage => {
|
|
|
|
do_websocket_operation_crud::<DeletePrivateMessage>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::GetPrivateMessages => {
|
|
|
|
do_websocket_operation_crud::<GetPrivateMessages>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Site ops
|
|
|
|
UserOperationCrud::CreateSite => {
|
|
|
|
do_websocket_operation_crud::<CreateSite>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::EditSite => {
|
|
|
|
do_websocket_operation_crud::<EditSite>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::GetSite => {
|
|
|
|
do_websocket_operation_crud::<GetSite>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Community ops
|
|
|
|
UserOperationCrud::ListCommunities => {
|
|
|
|
do_websocket_operation_crud::<ListCommunities>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::CreateCommunity => {
|
|
|
|
do_websocket_operation_crud::<CreateCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::EditCommunity => {
|
|
|
|
do_websocket_operation_crud::<EditCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::DeleteCommunity => {
|
|
|
|
do_websocket_operation_crud::<DeleteCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::RemoveCommunity => {
|
|
|
|
do_websocket_operation_crud::<RemoveCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post ops
|
|
|
|
UserOperationCrud::CreatePost => {
|
|
|
|
do_websocket_operation_crud::<CreatePost>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::GetPost => {
|
|
|
|
do_websocket_operation_crud::<GetPost>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::EditPost => {
|
|
|
|
do_websocket_operation_crud::<EditPost>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::DeletePost => {
|
|
|
|
do_websocket_operation_crud::<DeletePost>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::RemovePost => {
|
|
|
|
do_websocket_operation_crud::<RemovePost>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comment ops
|
|
|
|
UserOperationCrud::CreateComment => {
|
|
|
|
do_websocket_operation_crud::<CreateComment>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::EditComment => {
|
|
|
|
do_websocket_operation_crud::<EditComment>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::DeleteComment => {
|
|
|
|
do_websocket_operation_crud::<DeleteComment>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::RemoveComment => {
|
|
|
|
do_websocket_operation_crud::<RemoveComment>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationCrud::GetComment => {
|
|
|
|
do_websocket_operation_crud::<GetComment>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn do_websocket_operation_crud<'a, 'b, Data>(
|
|
|
|
context: LemmyContext,
|
|
|
|
id: ConnectionId,
|
|
|
|
op: UserOperationCrud,
|
|
|
|
data: &str,
|
|
|
|
) -> result::Result<String, LemmyError>
|
|
|
|
where
|
|
|
|
Data: PerformCrud + SendActivity<Response = <Data as PerformCrud>::Response>,
|
|
|
|
for<'de> Data: Deserialize<'de>,
|
|
|
|
{
|
|
|
|
let parsed_data: Data = serde_json::from_str(data)?;
|
|
|
|
let res = parsed_data
|
|
|
|
.perform(&web::Data::new(context.clone()), Some(id))
|
|
|
|
.await?;
|
|
|
|
SendActivity::send_activity(&parsed_data, &res, &context).await?;
|
|
|
|
serialize_websocket_message(&op, &res)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn match_websocket_operation_apub(
|
|
|
|
context: LemmyContext,
|
|
|
|
id: ConnectionId,
|
|
|
|
op: UserOperationApub,
|
|
|
|
data: &str,
|
|
|
|
) -> result::Result<String, LemmyError> {
|
|
|
|
match op {
|
|
|
|
UserOperationApub::GetPersonDetails => {
|
|
|
|
do_websocket_operation_apub::<GetPersonDetails>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationApub::GetCommunity => {
|
|
|
|
do_websocket_operation_apub::<GetCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationApub::GetComments => {
|
|
|
|
do_websocket_operation_apub::<GetComments>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationApub::GetPosts => {
|
|
|
|
do_websocket_operation_apub::<GetPosts>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationApub::ResolveObject => {
|
|
|
|
do_websocket_operation_apub::<ResolveObject>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperationApub::Search => do_websocket_operation_apub::<Search>(context, id, op, data).await,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn do_websocket_operation_apub<'a, 'b, Data>(
|
|
|
|
context: LemmyContext,
|
|
|
|
id: ConnectionId,
|
|
|
|
op: UserOperationApub,
|
|
|
|
data: &str,
|
|
|
|
) -> result::Result<String, LemmyError>
|
|
|
|
where
|
|
|
|
Data: PerformApub + SendActivity<Response = <Data as PerformApub>::Response>,
|
|
|
|
for<'de> Data: Deserialize<'de>,
|
|
|
|
{
|
|
|
|
let parsed_data: Data = serde_json::from_str(data)?;
|
|
|
|
let res = parsed_data
|
|
|
|
.perform(&web::Data::new(context.clone()), Some(id))
|
|
|
|
.await?;
|
|
|
|
SendActivity::send_activity(&parsed_data, &res, &context).await?;
|
|
|
|
serialize_websocket_message(&op, &res)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn match_websocket_operation(
|
|
|
|
context: LemmyContext,
|
|
|
|
id: ConnectionId,
|
|
|
|
op: UserOperation,
|
|
|
|
data: &str,
|
|
|
|
) -> result::Result<String, LemmyError> {
|
|
|
|
match op {
|
|
|
|
// User ops
|
|
|
|
UserOperation::Login => do_websocket_operation::<Login>(context, id, op, data).await,
|
|
|
|
UserOperation::GetCaptcha => do_websocket_operation::<GetCaptcha>(context, id, op, data).await,
|
|
|
|
UserOperation::GetReplies => do_websocket_operation::<GetReplies>(context, id, op, data).await,
|
|
|
|
UserOperation::AddAdmin => do_websocket_operation::<AddAdmin>(context, id, op, data).await,
|
|
|
|
UserOperation::GetUnreadRegistrationApplicationCount => {
|
|
|
|
do_websocket_operation::<GetUnreadRegistrationApplicationCount>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::ListRegistrationApplications => {
|
|
|
|
do_websocket_operation::<ListRegistrationApplications>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::ApproveRegistrationApplication => {
|
|
|
|
do_websocket_operation::<ApproveRegistrationApplication>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::BanPerson => do_websocket_operation::<BanPerson>(context, id, op, data).await,
|
|
|
|
UserOperation::GetBannedPersons => {
|
|
|
|
do_websocket_operation::<GetBannedPersons>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::BlockPerson => {
|
|
|
|
do_websocket_operation::<BlockPerson>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::GetPersonMentions => {
|
|
|
|
do_websocket_operation::<GetPersonMentions>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::MarkPersonMentionAsRead => {
|
|
|
|
do_websocket_operation::<MarkPersonMentionAsRead>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::MarkCommentReplyAsRead => {
|
|
|
|
do_websocket_operation::<MarkCommentReplyAsRead>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::MarkAllAsRead => {
|
|
|
|
do_websocket_operation::<MarkAllAsRead>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::PasswordReset => {
|
|
|
|
do_websocket_operation::<PasswordReset>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::PasswordChange => {
|
|
|
|
do_websocket_operation::<PasswordChangeAfterReset>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::UserJoin => do_websocket_operation::<UserJoin>(context, id, op, data).await,
|
|
|
|
UserOperation::PostJoin => do_websocket_operation::<PostJoin>(context, id, op, data).await,
|
|
|
|
UserOperation::CommunityJoin => {
|
|
|
|
do_websocket_operation::<CommunityJoin>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::ModJoin => do_websocket_operation::<ModJoin>(context, id, op, data).await,
|
|
|
|
UserOperation::SaveUserSettings => {
|
|
|
|
do_websocket_operation::<SaveUserSettings>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::ChangePassword => {
|
|
|
|
do_websocket_operation::<ChangePassword>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::GetReportCount => {
|
|
|
|
do_websocket_operation::<GetReportCount>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::GetUnreadCount => {
|
|
|
|
do_websocket_operation::<GetUnreadCount>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::VerifyEmail => {
|
|
|
|
do_websocket_operation::<VerifyEmail>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private Message ops
|
|
|
|
UserOperation::MarkPrivateMessageAsRead => {
|
|
|
|
do_websocket_operation::<MarkPrivateMessageAsRead>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::CreatePrivateMessageReport => {
|
|
|
|
do_websocket_operation::<CreatePrivateMessageReport>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::ResolvePrivateMessageReport => {
|
|
|
|
do_websocket_operation::<ResolvePrivateMessageReport>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::ListPrivateMessageReports => {
|
|
|
|
do_websocket_operation::<ListPrivateMessageReports>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Site ops
|
|
|
|
UserOperation::GetModlog => do_websocket_operation::<GetModlog>(context, id, op, data).await,
|
|
|
|
UserOperation::PurgePerson => {
|
|
|
|
do_websocket_operation::<PurgePerson>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::PurgeCommunity => {
|
|
|
|
do_websocket_operation::<PurgeCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::PurgePost => do_websocket_operation::<PurgePost>(context, id, op, data).await,
|
|
|
|
UserOperation::PurgeComment => {
|
|
|
|
do_websocket_operation::<PurgeComment>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::TransferCommunity => {
|
|
|
|
do_websocket_operation::<TransferCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::LeaveAdmin => do_websocket_operation::<LeaveAdmin>(context, id, op, data).await,
|
|
|
|
|
|
|
|
// Community ops
|
|
|
|
UserOperation::FollowCommunity => {
|
|
|
|
do_websocket_operation::<FollowCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::BlockCommunity => {
|
|
|
|
do_websocket_operation::<BlockCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::BanFromCommunity => {
|
|
|
|
do_websocket_operation::<BanFromCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::AddModToCommunity => {
|
|
|
|
do_websocket_operation::<AddModToCommunity>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post ops
|
|
|
|
UserOperation::LockPost => do_websocket_operation::<LockPost>(context, id, op, data).await,
|
|
|
|
UserOperation::StickyPost => do_websocket_operation::<StickyPost>(context, id, op, data).await,
|
|
|
|
UserOperation::CreatePostLike => {
|
|
|
|
do_websocket_operation::<CreatePostLike>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::MarkPostAsRead => {
|
|
|
|
do_websocket_operation::<MarkPostAsRead>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::SavePost => do_websocket_operation::<SavePost>(context, id, op, data).await,
|
|
|
|
UserOperation::CreatePostReport => {
|
|
|
|
do_websocket_operation::<CreatePostReport>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::ListPostReports => {
|
|
|
|
do_websocket_operation::<ListPostReports>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::ResolvePostReport => {
|
|
|
|
do_websocket_operation::<ResolvePostReport>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::GetSiteMetadata => {
|
|
|
|
do_websocket_operation::<GetSiteMetadata>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comment ops
|
|
|
|
UserOperation::SaveComment => {
|
|
|
|
do_websocket_operation::<SaveComment>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::CreateCommentLike => {
|
|
|
|
do_websocket_operation::<CreateCommentLike>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::CreateCommentReport => {
|
|
|
|
do_websocket_operation::<CreateCommentReport>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::ListCommentReports => {
|
|
|
|
do_websocket_operation::<ListCommentReports>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
UserOperation::ResolveCommentReport => {
|
|
|
|
do_websocket_operation::<ResolveCommentReport>(context, id, op, data).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn do_websocket_operation<'a, 'b, Data>(
|
|
|
|
context: LemmyContext,
|
|
|
|
id: ConnectionId,
|
|
|
|
op: UserOperation,
|
|
|
|
data: &str,
|
|
|
|
) -> result::Result<String, LemmyError>
|
|
|
|
where
|
|
|
|
Data: Perform + SendActivity<Response = <Data as Perform>::Response>,
|
|
|
|
for<'de> Data: Deserialize<'de>,
|
|
|
|
{
|
|
|
|
let parsed_data: Data = serde_json::from_str(data)?;
|
|
|
|
let res = parsed_data
|
|
|
|
.perform(&web::Data::new(context.clone()), Some(id))
|
|
|
|
.await?;
|
|
|
|
SendActivity::send_activity(&parsed_data, &res, &context).await?;
|
|
|
|
serialize_websocket_message(&op, &res)
|
|
|
|
}
|