diff --git a/src/admin/check/commands.rs b/src/admin/check/commands.rs index 8e874309..7c331a2f 100644 --- a/src/admin/check/commands.rs +++ b/src/admin/check/commands.rs @@ -13,7 +13,8 @@ pub(super) async fn check_all_users(&self) -> Result { let users = self .services .users - .iter() + .stream() + .map(ToOwned::to_owned) .collect::>() .await; let query_time = timer.elapsed(); diff --git a/src/api/client/directory.rs b/src/api/client/directory.rs index f8218207..453ef82a 100644 --- a/src/api/client/directory.rs +++ b/src/api/client/directory.rs @@ -17,19 +17,15 @@ use ruma::{ federation, }, directory::{Filter, PublicRoomsChunk, RoomNetwork, RoomTypeFilter}, - events::{ - StateEventType, - room::join_rules::{JoinRule, RoomJoinRulesEventContent}, - }, + events::StateEventType, uint, }; use tuwunel_core::{ - Err, Result, err, info, is_true, + Err, Result, err, info, matrix::Event, utils::{ TryFutureExtExt, math::Expected, - result::FlatOk, stream::{IterStream, ReadyExt, WidebandExt}, }, }; @@ -429,17 +425,19 @@ async fn public_rooms_chunk(services: &Services, room_id: OwnedRoomId) -> Public let join_rule = services .state_accessor - .room_state_get_content(&room_id, &StateEventType::RoomJoinRules, "") - .map_ok(|c: RoomJoinRulesEventContent| match c.join_rule { - | JoinRule::Public => "public".into(), - | JoinRule::Knock => "knock".into(), - | JoinRule::KnockRestricted(_) => "knock_restricted".into(), - | _ => "invite".into(), - }); + .get_join_rules(&room_id) + .map(|join_rule| join_rule.kind()); let guest_can_join = services.state_accessor.guest_can_join(&room_id); - let num_joined_members = services.state_cache.room_joined_count(&room_id); + let num_joined_members = services + .state_cache + .room_joined_count(&room_id) + .map(|x| { + x.ok() + .and_then(|x| x.try_into().ok()) + .unwrap_or_else(|| uint!(0)) + }); let ( (avatar_url, canonical_alias, guest_can_join, join_rule, name), @@ -455,13 +453,9 @@ async fn public_rooms_chunk(services: &Services, room_id: OwnedRoomId) -> Public avatar_url: avatar_url.flatten(), canonical_alias, guest_can_join, - join_rule: join_rule.unwrap_or_default(), + join_rule, name, - num_joined_members: num_joined_members - .map(TryInto::try_into) - .map(Result::ok) - .flat_ok() - .unwrap_or_else(|| uint!(0)), + num_joined_members, room_id, room_type, topic, @@ -474,18 +468,17 @@ fn check_server_banned(services: &Services, server: Option<&ServerName>) -> Resu return Ok(()); }; - let conditions = [ - services - .config - .forbidden_remote_room_directory_server_names - .is_match(server.host()), - services + let host = server.host(); + + if services + .config + .forbidden_remote_room_directory_server_names + .is_match(host) + || services .config .forbidden_remote_server_names - .is_match(server.host()), - ]; - - if conditions.iter().any(is_true!()) { + .is_match(host) + { return Err!(Request(Forbidden("Server is banned on this homeserver."))); } diff --git a/src/api/client/message.rs b/src/api/client/message.rs index 6c6d51a0..3eeb8515 100644 --- a/src/api/client/message.rs +++ b/src/api/client/message.rs @@ -257,26 +257,23 @@ where return true; } - let ignored_type = IGNORED_MESSAGE_TYPES + if IGNORED_MESSAGE_TYPES .binary_search(event.kind()) - .is_ok(); + .is_err() + { + return false; + } let ignored_server = services .config .forbidden_remote_server_names .is_match(event.sender().server_name().host()); - if ignored_type - && (ignored_server - || services - .users - .user_is_ignored(event.sender(), user_id) - .await) - { - return true; - } - - false + ignored_server + || services + .users + .user_is_ignored(event.sender(), user_id) + .await } #[inline] diff --git a/src/api/client/user_directory.rs b/src/api/client/user_directory.rs index 4bd58e6b..656918da 100644 --- a/src/api/client/user_directory.rs +++ b/src/api/client/user_directory.rs @@ -32,7 +32,7 @@ pub(crate) async fn search_users_route( ) -> Result { let sender_user = body.sender_user(); let limit = usize::try_from(body.limit) - .map_or(LIMIT_DEFAULT, usize::from) + .unwrap_or(LIMIT_DEFAULT) .min(LIMIT_MAX); let search_term = body.search_term.to_lowercase(); diff --git a/src/core/config/check.rs b/src/core/config/check.rs index 0866c40f..37110b72 100644 --- a/src/core/config/check.rs +++ b/src/core/config/check.rs @@ -128,7 +128,11 @@ pub fn check(config: &Config) -> Result { )); } - if config.emergency_password == Some(String::from("F670$2CP@Hw8mG7RY1$%!#Ic7YA")) { + if config + .emergency_password + .as_ref() + .is_some_and(|emergency_password| emergency_password == "F670$2CP@Hw8mG7RY1$%!#Ic7YA") + { return Err!(Config( "emergency_password", "The public example emergency password is being used, this is insecure. Please \ @@ -136,7 +140,11 @@ pub fn check(config: &Config) -> Result { )); } - if config.emergency_password == Some(String::new()) { + if config + .emergency_password + .as_ref() + .is_some_and(String::is_empty) + { return Err!(Config( "emergency_password", "Emergency password was set to an empty string, this is not valid. Unset \ @@ -145,7 +153,11 @@ pub fn check(config: &Config) -> Result { } // check if the user specified a registration token as `""` - if config.registration_token == Some(String::new()) { + if config + .registration_token + .as_ref() + .is_some_and(String::is_empty) + { return Err!(Config( "registration_token", "Registration token was specified but is empty (\"\")" diff --git a/src/service/users/mod.rs b/src/service/users/mod.rs index 9e9982d4..b8f1be20 100644 --- a/src/service/users/mod.rs +++ b/src/service/users/mod.rs @@ -183,16 +183,6 @@ impl Service { #[inline] pub async fn count(&self) -> usize { self.db.userid_password.count().await } - /// Returns an iterator over all users on this homeserver (offered for - /// compatibility) - #[allow( - clippy::iter_without_into_iter, - clippy::iter_not_returning_iterator - )] - pub fn iter(&self) -> impl Stream + Send + '_ { - self.stream().map(ToOwned::to_owned) - } - /// Returns an iterator over all users on this homeserver. pub fn stream(&self) -> impl Stream + Send { self.db.userid_password.keys().ignore_err()