diff options
author | asonix <asonix@asonix.dog> | 2020-04-19 22:59:07 -0500 |
---|---|---|
committer | asonix <asonix@asonix.dog> | 2020-04-19 22:59:07 -0500 |
commit | ac43b86b6063ef1ac876122c390de83d6b34a8e6 (patch) | |
tree | ab329231a106d0f7141973f3cf02bd0faf807e32 /server/src/api/community.rs | |
parent | f300c67a4d9674eef05d180a787cc8352092903d (diff) |
Change RateLimit to act as a middleware
Diffstat (limited to 'server/src/api/community.rs')
-rw-r--r-- | server/src/api/community.rs | 115 |
1 files changed, 27 insertions, 88 deletions
diff --git a/server/src/api/community.rs b/server/src/api/community.rs index 0f437693..df03546c 100644 --- a/server/src/api/community.rs +++ b/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)?; |