Browse Source

Merge branch 'asonix-abstract_websocket_sends' into abstract_websocket_sends

abstract_websocket_sends
Dessalines 2 years ago
parent
commit
c0f4d5260f
  1. 2
      server/Cargo.lock
  2. 2
      server/Cargo.toml
  3. 60
      server/src/api/comment.rs
  4. 115
      server/src/api/community.rs
  5. 10
      server/src/api/mod.rs
  6. 79
      server/src/api/post.rs
  7. 115
      server/src/api/site.rs
  8. 204
      server/src/api/user.rs
  9. 7
      server/src/lib.rs
  10. 19
      server/src/main.rs
  11. 184
      server/src/rate_limit/mod.rs
  12. 32
      server/src/rate_limit/rate_limiter.rs
  13. 221
      server/src/routes/api.rs
  14. 2
      server/src/routes/mod.rs
  15. 15
      server/src/routes/websocket.rs
  16. 2
      server/src/websocket/mod.rs
  17. 868
      server/src/websocket/server.rs

2
server/Cargo.lock

@ -1410,6 +1410,7 @@ dependencies = [
"dotenv 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"hjson 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"htmlescape 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"isahc 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1427,6 +1428,7 @@ dependencies = [
"sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"strum 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
"strum_macros 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]

2
server/Cargo.toml

@ -37,3 +37,5 @@ hjson = "0.8.2"
percent-encoding = "2.1.0"
isahc = "0.9"
comrak = "0.7"
tokio = "0.2.18"
futures = "0.3.4"

60
server/src/api/comment.rs

@ -59,12 +59,13 @@ pub struct GetCommentsResponse {
comments: Vec<CommentView>,
}
impl Perform<CommentResponse> for Oper<CreateComment> {
impl Perform for Oper<CreateComment> {
type Response = CommentResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<CommentResponse, Error> {
let data: &CreateComment = &self.data;
@ -77,13 +78,6 @@ impl Perform<CommentResponse> for Oper<CreateComment> {
let hostname = &format!("https://{}", Settings::get().hostname);
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// Check for a community ban
@ -253,12 +247,13 @@ impl Perform<CommentResponse> for Oper<CreateComment> {
}
}
impl Perform<CommentResponse> for Oper<EditComment> {
impl Perform for Oper<EditComment> {
type Response = CommentResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<CommentResponse, Error> {
let data: &EditComment = &self.data;
@ -269,13 +264,6 @@ impl Perform<CommentResponse> for Oper<EditComment> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let orig_comment = CommentView::read(&conn, data.edit_id, None)?;
@ -411,12 +399,13 @@ impl Perform<CommentResponse> for Oper<EditComment> {
}
}
impl Perform<CommentResponse> for Oper<SaveComment> {
impl Perform for Oper<SaveComment> {
type Response = CommentResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<CommentResponse, Error> {
let data: &SaveComment = &self.data;
@ -432,13 +421,6 @@ impl Perform<CommentResponse> for Oper<SaveComment> {
user_id,
};
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
if data.save {
@ -462,12 +444,13 @@ impl Perform<CommentResponse> for Oper<SaveComment> {
}
}
impl Perform<CommentResponse> for Oper<CreateCommentLike> {
impl Perform for Oper<CreateCommentLike> {
type Response = CommentResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<CommentResponse, Error> {
let data: &CreateCommentLike = &self.data;
@ -480,13 +463,6 @@ impl Perform<CommentResponse> for Oper<CreateCommentLike> {
let mut recipient_ids = Vec::new();
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// Don't do a downvote if site has downvotes disabled
@ -567,12 +543,13 @@ impl Perform<CommentResponse> for Oper<CreateCommentLike> {
}
}
impl Perform<GetCommentsResponse> for Oper<GetComments> {
impl Perform for Oper<GetComments> {
type Response = GetCommentsResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetCommentsResponse, Error> {
let data: &GetComments = &self.data;
@ -592,13 +569,6 @@ impl Perform<GetCommentsResponse> for Oper<GetComments> {
let type_ = ListingType::from_str(&data.type_)?;
let sort = SortType::from_str(&data.sort)?;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let comments = match CommentQueryBuilder::create(&conn)

115
server/src/api/community.rs

@ -111,12 +111,13 @@ pub struct TransferCommunity {
auth: String,
}
impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
impl Perform for Oper<GetCommunity> {
type Response = GetCommunityResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetCommunityResponse, Error> {
let data: &GetCommunity = &self.data;
@ -131,13 +132,6 @@ impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
None => None,
};
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let community_id = match data.id {
@ -197,12 +191,13 @@ impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
}
}
impl Perform<CommunityResponse> for Oper<CreateCommunity> {
impl Perform for Oper<CreateCommunity> {
type Response = CommunityResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<CommunityResponse, Error> {
let data: &CreateCommunity = &self.data;
@ -227,13 +222,6 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> {
let user_id = claims.id;
if let Some(rl) = &rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_register(&rl.ip, true)?;
}
let conn = pool.get()?;
// Check for a site ban
@ -283,25 +271,19 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> {
let community_view = CommunityView::read(&conn, inserted_community.id, Some(user_id))?;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_register(&rl.ip, false)?;
}
Ok(CommunityResponse {
community: community_view,
})
}
}
impl Perform<CommunityResponse> for Oper<EditCommunity> {
impl Perform for Oper<EditCommunity> {
type Response = CommunityResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<CommunityResponse, Error> {
let data: &EditCommunity = &self.data;
@ -326,13 +308,6 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// Check for a site ban
@ -410,12 +385,13 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> {
}
}
impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
impl Perform for Oper<ListCommunities> {
type Response = ListCommunitiesResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<ListCommunitiesResponse, Error> {
let data: &ListCommunities = &self.data;
@ -439,13 +415,6 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
let sort = SortType::from_str(&data.sort)?;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let communities = CommunityQueryBuilder::create(&conn)
@ -461,12 +430,13 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
}
}
impl Perform<CommunityResponse> for Oper<FollowCommunity> {
impl Perform for Oper<FollowCommunity> {
type Response = CommunityResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<CommunityResponse, Error> {
let data: &FollowCommunity = &self.data;
@ -482,13 +452,6 @@ impl Perform<CommunityResponse> for Oper<FollowCommunity> {
user_id,
};
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
if data.follow {
@ -511,12 +474,13 @@ impl Perform<CommunityResponse> for Oper<FollowCommunity> {
}
}
impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
impl Perform for Oper<GetFollowedCommunities> {
type Response = GetFollowedCommunitiesResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetFollowedCommunitiesResponse, Error> {
let data: &GetFollowedCommunities = &self.data;
@ -527,13 +491,6 @@ impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let communities: Vec<CommunityFollowerView> =
@ -547,12 +504,13 @@ impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
}
}
impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> {
impl Perform for Oper<BanFromCommunity> {
type Response = BanFromCommunityResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<BanFromCommunityResponse, Error> {
let data: &BanFromCommunity = &self.data;
@ -568,13 +526,6 @@ impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> {
user_id: data.user_id,
};
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
if data.ban {
@ -625,12 +576,13 @@ impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> {
}
}
impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> {
impl Perform for Oper<AddModToCommunity> {
type Response = AddModToCommunityResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<AddModToCommunityResponse, Error> {
let data: &AddModToCommunity = &self.data;
@ -646,13 +598,6 @@ impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> {
user_id: data.user_id,
};
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
if data.added {
@ -693,12 +638,13 @@ impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> {
}
}
impl Perform<GetCommunityResponse> for Oper<TransferCommunity> {
impl Perform for Oper<TransferCommunity> {
type Response = GetCommunityResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetCommunityResponse, Error> {
let data: &TransferCommunity = &self.data;
@ -709,13 +655,6 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let read_community = Community::read(&conn, data.community_id)?;

10
server/src/api/mod.rs

@ -22,7 +22,6 @@ use crate::{
naive_now, remove_slurs, send_email, slur_check, slurs_vec_to_str,
};
use crate::rate_limit::RateLimitInfo;
use crate::settings::Settings;
use crate::websocket::UserOperation;
use crate::websocket::{
@ -69,13 +68,12 @@ impl<T> Oper<T> {
}
}
pub trait Perform<T> {
pub trait Perform {
type Response: serde::ser::Serialize;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<T, Error>
where
T: Sized;
) -> Result<Self::Response, Error>;
}

79
server/src/api/post.rs

@ -77,12 +77,13 @@ pub struct SavePost {
auth: String,
}
impl Perform<PostResponse> for Oper<CreatePost> {
impl Perform for Oper<CreatePost> {
type Response = PostResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<PostResponse, Error> {
let data: &CreatePost = &self.data;
@ -103,13 +104,6 @@ impl Perform<PostResponse> for Oper<CreatePost> {
let user_id = claims.id;
if let Some(rl) = &rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_post(&rl.ip, true)?;
}
let conn = pool.get()?;
// Check for a community ban
@ -176,13 +170,6 @@ impl Perform<PostResponse> for Oper<CreatePost> {
Err(_e) => return Err(APIError::err("couldnt_find_post").into()),
};
if let Some(rl) = &rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_post(&rl.ip, false)?;
}
let res = PostResponse { post: post_view };
if let Some(ws) = websocket_info {
@ -197,12 +184,13 @@ impl Perform<PostResponse> for Oper<CreatePost> {
}
}
impl Perform<GetPostResponse> for Oper<GetPost> {
impl Perform for Oper<GetPost> {
type Response = GetPostResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetPostResponse, Error> {
let data: &GetPost = &self.data;
@ -217,13 +205,6 @@ impl Perform<GetPostResponse> for Oper<GetPost> {
None => None,
};
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let post_view = match PostView::read(&conn, data.id, user_id) {
@ -277,12 +258,13 @@ impl Perform<GetPostResponse> for Oper<GetPost> {
}
}
impl Perform<GetPostsResponse> for Oper<GetPosts> {
impl Perform for Oper<GetPosts> {
type Response = GetPostsResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetPostsResponse, Error> {
let data: &GetPosts = &self.data;
@ -307,13 +289,6 @@ impl Perform<GetPostsResponse> for Oper<GetPosts> {
let type_ = ListingType::from_str(&data.type_)?;
let sort = SortType::from_str(&data.sort)?;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let posts = match PostQueryBuilder::create(&conn)
@ -348,12 +323,13 @@ impl Perform<GetPostsResponse> for Oper<GetPosts> {
}
}
impl Perform<PostResponse> for Oper<CreatePostLike> {
impl Perform for Oper<CreatePostLike> {
type Response = PostResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<PostResponse, Error> {
let data: &CreatePostLike = &self.data;
@ -364,13 +340,6 @@ impl Perform<PostResponse> for Oper<CreatePostLike> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// Don't do a downvote if site has downvotes disabled
@ -429,12 +398,13 @@ impl Perform<PostResponse> for Oper<CreatePostLike> {
}
}
impl Perform<PostResponse> for Oper<EditPost> {
impl Perform for Oper<EditPost> {
type Response = PostResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<PostResponse, Error> {
let data: &EditPost = &self.data;
@ -455,13 +425,6 @@ impl Perform<PostResponse> for Oper<EditPost> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// Verify its the creator or a mod or admin
@ -567,12 +530,13 @@ impl Perform<PostResponse> for Oper<EditPost> {
}
}
impl Perform<PostResponse> for Oper<SavePost> {
impl Perform for Oper<SavePost> {
type Response = PostResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<PostResponse, Error> {
let data: &SavePost = &self.data;
@ -588,13 +552,6 @@ impl Perform<PostResponse> for Oper<SavePost> {
user_id,
};
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
if data.save {

115
server/src/api/site.rs

@ -108,22 +108,16 @@ pub struct SaveSiteConfig {
auth: String,
}
impl Perform<ListCategoriesResponse> for Oper<ListCategories> {
impl Perform for Oper<ListCategories> {
type Response = ListCategoriesResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<ListCategoriesResponse, Error> {
let _data: &ListCategories = &self.data;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let categories: Vec<Category> = Category::list_all(&conn)?;
@ -133,22 +127,16 @@ impl Perform<ListCategoriesResponse> for Oper<ListCategories> {
}
}
impl Perform<GetModlogResponse> for Oper<GetModlog> {
impl Perform for Oper<GetModlog> {
type Response = GetModlogResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetModlogResponse, Error> {
let data: &GetModlog = &self.data;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let removed_posts = ModRemovePostView::list(
@ -220,12 +208,13 @@ impl Perform<GetModlogResponse> for Oper<GetModlog> {
}
}
impl Perform<SiteResponse> for Oper<CreateSite> {
impl Perform for Oper<CreateSite> {
type Response = SiteResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<SiteResponse, Error> {
let data: &CreateSite = &self.data;
@ -246,13 +235,6 @@ impl Perform<SiteResponse> for Oper<CreateSite> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// Make sure user is an admin
@ -281,12 +263,12 @@ impl Perform<SiteResponse> for Oper<CreateSite> {
}
}
impl Perform<SiteResponse> for Oper<EditSite> {
impl Perform for Oper<EditSite> {
type Response = SiteResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<SiteResponse, Error> {
let data: &EditSite = &self.data;
@ -307,13 +289,6 @@ impl Perform<SiteResponse> for Oper<EditSite> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// Make sure user is an admin
@ -354,22 +329,16 @@ impl Perform<SiteResponse> for Oper<EditSite> {
}
}
impl Perform<GetSiteResponse> for Oper<GetSite> {
impl Perform for Oper<GetSite> {
type Response = GetSiteResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetSiteResponse, Error> {
let _data: &GetSite = &self.data;
if let Some(rl) = &rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// TODO refactor this a little
@ -385,11 +354,7 @@ impl Perform<GetSiteResponse> for Oper<GetSite> {
admin: true,
show_nsfw: true,
};
let login_response = Oper::new(register).perform(
pool.clone(),
websocket_info.clone(),
rate_limit_info.clone(),
)?;
let login_response = Oper::new(register).perform(pool.clone(), websocket_info.clone())?;
info!("Admin {} created", setup.admin_username);
let create_site = CreateSite {
@ -400,7 +365,7 @@ impl Perform<GetSiteResponse> for Oper<GetSite> {
enable_nsfw: false,
auth: login_response.jwt,
};
Oper::new(create_site).perform(pool, websocket_info.clone(), rate_limit_info)?;
Oper::new(create_site).perform(pool, websocket_info.clone())?;
info!("Site {} created", setup.site_name);
Some(SiteView::read(&conn)?)
} else {
@ -437,12 +402,13 @@ impl Perform<GetSiteResponse> for Oper<GetSite> {
}
}
impl Perform<SearchResponse> for Oper<Search> {
impl Perform for Oper<Search> {
type Response = SearchResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<SearchResponse, Error> {
let data: &Search = &self.data;
@ -467,13 +433,6 @@ impl Perform<SearchResponse> for Oper<Search> {
// TODO no clean / non-nsfw searching rn
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
match type_ {
@ -569,12 +528,13 @@ impl Perform<SearchResponse> for Oper<Search> {
}
}
impl Perform<GetSiteResponse> for Oper<TransferSite> {
impl Perform for Oper<TransferSite> {
type Response = GetSiteResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetSiteResponse, Error> {
let data: &TransferSite = &self.data;
@ -585,13 +545,6 @@ impl Perform<GetSiteResponse> for Oper<TransferSite> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let read_site = Site::read(&conn, 1)?;
@ -646,12 +599,13 @@ impl Perform<GetSiteResponse> for Oper<TransferSite> {
}
}
impl Perform<GetSiteConfigResponse> for Oper<GetSiteConfig> {
impl Perform for Oper<GetSiteConfig> {
type Response = GetSiteConfigResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetSiteConfigResponse, Error> {
let data: &GetSiteConfig = &self.data;
@ -662,13 +616,6 @@ impl Perform<GetSiteConfigResponse> for Oper<GetSiteConfig> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// Only let admins read this
@ -685,12 +632,13 @@ impl Perform<GetSiteConfigResponse> for Oper<GetSiteConfig> {
}
}
impl Perform<GetSiteConfigResponse> for Oper<SaveSiteConfig> {
impl Perform for Oper<SaveSiteConfig> {
type Response = GetSiteConfigResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetSiteConfigResponse, Error> {
let data: &SaveSiteConfig = &self.data;
@ -701,13 +649,6 @@ impl Perform<GetSiteConfigResponse> for Oper<SaveSiteConfig> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// Only let admins read this

204
server/src/api/user.rs

@ -199,22 +199,16 @@ pub struct UserJoinResponse {
pub user_id: i32,
}
impl Perform<LoginResponse> for Oper<Login> {
impl Perform for Oper<Login> {
type Response = LoginResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<LoginResponse, Error> {
let data: &Login = &self.data;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// Fetch that username / email
@ -234,22 +228,16 @@ impl Perform<LoginResponse> for Oper<Login> {
}
}
impl Perform<LoginResponse> for Oper<Register> {
impl Perform for Oper<Register> {
type Response = LoginResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<LoginResponse, Error> {
let data: &Register = &self.data;
if let Some(rl) = &rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_register(&rl.ip, true)?;
}
let conn = pool.get()?;
// Make sure site has open registration
@ -355,13 +343,6 @@ impl Perform<LoginResponse> for Oper<Register> {
};
}
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_register(&rl.ip, false)?;
}
// Return the jwt
Ok(LoginResponse {
jwt: inserted_user.jwt(),
@ -369,12 +350,13 @@ impl Perform<LoginResponse> for Oper<Register> {
}
}
impl Perform<LoginResponse> for Oper<SaveUserSettings> {
impl Perform for Oper<SaveUserSettings> {
type Response = LoginResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<LoginResponse, Error> {
let data: &SaveUserSettings = &self.data;
@ -385,13 +367,6 @@ impl Perform<LoginResponse> for Oper<SaveUserSettings> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let read_user = User_::read(&conn, user_id)?;
@ -471,22 +446,16 @@ impl Perform<LoginResponse> for Oper<SaveUserSettings> {
}
}
impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> {
impl Perform for Oper<GetUserDetails> {
type Response = GetUserDetailsResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetUserDetailsResponse, Error> {
let data: &GetUserDetails = &self.data;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let user_claims: Option<Claims> = match &data.auth {
@ -582,12 +551,13 @@ impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> {
}
}
impl Perform<AddAdminResponse> for Oper<AddAdmin> {
impl Perform for Oper<AddAdmin> {
type Response = AddAdminResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<AddAdminResponse, Error> {
let data: &AddAdmin = &self.data;
@ -598,13 +568,6 @@ impl Perform<AddAdminResponse> for Oper<AddAdmin> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// Make sure user is an admin
@ -669,12 +632,13 @@ impl Perform<AddAdminResponse> for Oper<AddAdmin> {
}
}
impl Perform<BanUserResponse> for Oper<BanUser> {
impl Perform for Oper<BanUser> {
type Response = BanUserResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<BanUserResponse, Error> {
let data: &BanUser = &self.data;
@ -685,13 +649,6 @@ impl Perform<BanUserResponse> for Oper<BanUser> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// Make sure user is an admin
@ -762,12 +719,13 @@ impl Perform<BanUserResponse> for Oper<BanUser> {
}
}
impl Perform<GetRepliesResponse> for Oper<GetReplies> {
impl Perform for Oper<GetReplies> {
type Response = GetRepliesResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetRepliesResponse, Error> {
let data: &GetReplies = &self.data;
@ -780,13 +738,6 @@ impl Perform<GetRepliesResponse> for Oper<GetReplies> {
let sort = SortType::from_str(&data.sort)?;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let replies = ReplyQueryBuilder::create(&conn, user_id)
@ -800,12 +751,13 @@ impl Perform<GetRepliesResponse> for Oper<GetReplies> {
}
}
impl Perform<GetUserMentionsResponse> for Oper<GetUserMentions> {
impl Perform for Oper<GetUserMentions> {
type Response = GetUserMentionsResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetUserMentionsResponse, Error> {
let data: &GetUserMentions = &self.data;
@ -818,13 +770,6 @@ impl Perform<GetUserMentionsResponse> for Oper<GetUserMentions> {
let sort = SortType::from_str(&data.sort)?;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let mentions = UserMentionQueryBuilder::create(&conn, user_id)
@ -838,12 +783,13 @@ impl Perform<GetUserMentionsResponse> for Oper<GetUserMentions> {
}
}
impl Perform<UserMentionResponse> for Oper<EditUserMention> {
impl Perform for Oper<EditUserMention> {
type Response = UserMentionResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<UserMentionResponse, Error> {
let data: &EditUserMention = &self.data;
@ -854,13 +800,6 @@ impl Perform<UserMentionResponse> for Oper<EditUserMention> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let user_mention = UserMention::read(&conn, data.user_mention_id)?;
@ -885,12 +824,13 @@ impl Perform<UserMentionResponse> for Oper<EditUserMention> {
}
}
impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> {
impl Perform for Oper<MarkAllAsRead> {
type Response = GetRepliesResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<GetRepliesResponse, Error> {
let data: &MarkAllAsRead = &self.data;
@ -901,13 +841,6 @@ impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let replies = ReplyQueryBuilder::create(&conn, user_id)
@ -983,12 +916,13 @@ impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> {
}
}
impl Perform<LoginResponse> for Oper<DeleteAccount> {
impl Perform for Oper<DeleteAccount> {
type Response = LoginResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<LoginResponse, Error> {
let data: &DeleteAccount = &self.data;
@ -999,13 +933,6 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> {
let user_id = claims.id;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
let user: User_ = User_::read(&conn, user_id)?;
@ -1078,22 +1005,16 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> {
}
}
impl Perform<PasswordResetResponse> for Oper<PasswordReset> {
impl Perform for Oper<PasswordReset> {
type Response = PasswordResetResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<PasswordResetResponse, Error> {
let data: &PasswordReset = &self.data;
if let Some(rl) = rate_limit_info {
rl.rate_limiter
.lock()
.unwrap()
.check_rate_limit_message(&rl.ip, false)?;
}
let conn = pool.get()?;
// Fetch that email
@ -1123,22 +1044,16 @@ impl Perform<PasswordResetResponse> for Oper<PasswordReset> {
}
}
impl Perform<LoginResponse> for Oper<PasswordChange> {
impl Perform for Oper<PasswordChange> {
type Response = LoginResponse;
fn perform(
&self,
pool: Pool<ConnectionManager<PgConnection>>,
_websocket_info: Option<WebsocketInfo>,
rate_limit_info: Option<RateLimitInfo>,
) -> Result<LoginResponse, Error> {
let data: &PasswordChange = &self.data;