diff options
author | Felix Ableitner <me@nutomic.com> | 2020-01-02 12:30:00 +0100 |
---|---|---|
committer | Felix Ableitner <me@nutomic.com> | 2020-01-02 12:30:00 +0100 |
commit | d08e09fbdc9d0e50e4b81410ae513b034170c36b (patch) | |
tree | a2cb974941f730960594dd9c66e72ed52f85c580 /server/src/websocket | |
parent | 081d8ddd127ccd1ed58b275424837b24eef52070 (diff) |
Apply changes suggested by cargo clippy (fixes #395)
Diffstat (limited to 'server/src/websocket')
-rw-r--r-- | server/src/websocket/server.rs | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/server/src/websocket/server.rs b/server/src/websocket/server.rs index 56fff275..4fcbc241 100644 --- a/server/src/websocket/server.rs +++ b/server/src/websocket/server.rs @@ -91,7 +91,7 @@ impl Default for ChatServer { ChatServer { sessions: HashMap::new(), rate_limits: HashMap::new(), - rooms: rooms, + rooms, rng: rand::thread_rng(), } } @@ -99,8 +99,8 @@ impl Default for ChatServer { impl ChatServer { /// Send message to all users in the room - fn send_room_message(&self, room: &i32, message: &str, skip_id: usize) { - if let Some(sessions) = self.rooms.get(room) { + fn send_room_message(&self, room: i32, message: &str, skip_id: usize) { + if let Some(sessions) = self.rooms.get(&room) { for id in sessions { if *id != skip_id { if let Some(info) = self.sessions.get(id) { @@ -113,7 +113,7 @@ impl ChatServer { fn join_room(&mut self, room_id: i32, id: usize) { // remove session from all rooms - for (_n, sessions) in &mut self.rooms { + for sessions in self.rooms.values_mut() { sessions.remove(&id); } @@ -122,12 +122,12 @@ impl ChatServer { self.rooms.insert(room_id, HashSet::new()); } - &self.rooms.get_mut(&room_id).unwrap().insert(id); + self.rooms.get_mut(&room_id).unwrap().insert(id); } fn send_community_message( &self, - community_id: &i32, + community_id: i32, message: &str, skip_id: usize, ) -> Result<(), Error> { @@ -138,12 +138,12 @@ impl ChatServer { let posts = PostQueryBuilder::create(&conn) .listing_type(ListingType::Community) .sort(&SortType::New) - .for_community_id(*community_id) + .for_community_id(community_id) .limit(9999) .list()?; for post in posts { - self.send_room_message(&post.id, message, skip_id); + self.send_room_message(post.id, message, skip_id); } Ok(()) @@ -173,6 +173,7 @@ impl ChatServer { ) } + #[allow(clippy::float_cmp)] fn check_rate_limit_full(&mut self, id: usize, rate: i32, per: i32) -> Result<(), Error> { if let Some(info) = self.sessions.get(&id) { if let Some(rate_limit) = self.rate_limits.get_mut(&info.ip) { @@ -194,10 +195,13 @@ impl ChatServer { "Rate limited IP: {}, time_passed: {}, allowance: {}", &info.ip, time_passed, rate_limit.allowance ); - Err(APIError { - op: "Rate Limit".to_string(), - message: format!("Too many requests. {} per {} seconds", rate, per), - })? + Err( + APIError { + op: "Rate Limit".to_string(), + message: format!("Too many requests. {} per {} seconds", rate, per), + } + .into(), + ) } else { rate_limit.allowance -= 1.0; Ok(()) @@ -264,7 +268,7 @@ impl Handler<Disconnect> for ChatServer { // remove address if self.sessions.remove(&msg.id).is_some() { // remove session from all rooms - for (_id, sessions) in &mut self.rooms { + for sessions in self.rooms.values_mut() { if sessions.remove(&msg.id) { // rooms.push(*id); } @@ -292,7 +296,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str let data = &json["data"].to_string(); let op = &json["op"].as_str().ok_or(APIError { op: "Unknown op type".to_string(), - message: format!("Unknown op type"), + message: "Unknown op type".to_string(), })?; let user_operation: UserOperation = UserOperation::from_str(&op)?; @@ -374,7 +378,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str community_sent.community.user_id = None; community_sent.community.subscribed = None; let community_sent_str = serde_json::to_string(&community_sent)?; - chat.send_community_message(&community_sent.community.id, &community_sent_str, msg.id)?; + chat.send_community_message(community_sent.community.id, &community_sent_str, msg.id)?; Ok(serde_json::to_string(&res)?) } UserOperation::FollowCommunity => { @@ -392,7 +396,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str let community_id = ban_from_community.community_id; let res = Oper::new(user_operation, ban_from_community).perform()?; let res_str = serde_json::to_string(&res)?; - chat.send_community_message(&community_id, &res_str, msg.id)?; + chat.send_community_message(community_id, &res_str, msg.id)?; Ok(res_str) } UserOperation::AddModToCommunity => { @@ -400,7 +404,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str let community_id = mod_add_to_community.community_id; let res = Oper::new(user_operation, mod_add_to_community).perform()?; let res_str = serde_json::to_string(&res)?; - chat.send_community_message(&community_id, &res_str, msg.id)?; + chat.send_community_message(community_id, &res_str, msg.id)?; Ok(res_str) } UserOperation::ListCategories => { @@ -437,7 +441,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str let mut post_sent = res.clone(); post_sent.post.my_vote = None; let post_sent_str = serde_json::to_string(&post_sent)?; - chat.send_room_message(&post_sent.post.id, &post_sent_str, msg.id); + chat.send_room_message(post_sent.post.id, &post_sent_str, msg.id); Ok(serde_json::to_string(&res)?) } UserOperation::SavePost => { @@ -454,7 +458,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str comment_sent.comment.my_vote = None; comment_sent.comment.user_id = None; let comment_sent_str = serde_json::to_string(&comment_sent)?; - chat.send_room_message(&post_id, &comment_sent_str, msg.id); + chat.send_room_message(post_id, &comment_sent_str, msg.id); Ok(serde_json::to_string(&res)?) } UserOperation::EditComment => { @@ -465,7 +469,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str comment_sent.comment.my_vote = None; comment_sent.comment.user_id = None; let comment_sent_str = serde_json::to_string(&comment_sent)?; - chat.send_room_message(&post_id, &comment_sent_str, msg.id); + chat.send_room_message(post_id, &comment_sent_str, msg.id); Ok(serde_json::to_string(&res)?) } UserOperation::SaveComment => { @@ -482,7 +486,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str comment_sent.comment.my_vote = None; comment_sent.comment.user_id = None; let comment_sent_str = serde_json::to_string(&comment_sent)?; - chat.send_room_message(&post_id, &comment_sent_str, msg.id); + chat.send_room_message(post_id, &comment_sent_str, msg.id); Ok(serde_json::to_string(&res)?) } UserOperation::GetModlog => { |