remove some unnecessary loops

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-04-07 23:24:38 -04:00 committed by June
parent 40596634c4
commit 839a89c968
4 changed files with 42 additions and 61 deletions

View file

@ -117,14 +117,13 @@ pub(crate) async fn get_alias_helper(room_alias: OwnedRoomAliasId) -> Result<get
servers.push(room_alias.server_name().into());
// find active servers in room state cache to suggest
for extra_servers in services()
.rooms
.state_cache
.room_servers(&room_id)
.filter_map(Result::ok)
{
servers.push(extra_servers);
}
servers.extend(
services()
.rooms
.state_cache
.room_servers(&room_id)
.filter_map(Result::ok),
);
servers.sort_unstable();
servers.dedup();
@ -186,17 +185,13 @@ pub(crate) async fn get_alias_helper(room_alias: OwnedRoomAliasId) -> Result<get
return Err(Error::BadRequest(ErrorKind::NotFound, "Room with alias not found."));
};
let mut servers: Vec<OwnedServerName> = Vec::new();
// find active servers in room state cache to suggest
for extra_servers in services()
let mut servers: Vec<OwnedServerName> = services()
.rooms
.state_cache
.room_servers(&room_id)
.filter_map(Result::ok)
{
servers.push(extra_servers);
}
.collect();
servers.sort_unstable();
servers.dedup();

View file

@ -55,26 +55,20 @@ pub async fn join_room_by_id_route(body: Ruma<join_room_by_id::v3::Request>) ->
));
}
let mut servers = Vec::new(); // There is no body.server_name for /roomId/join
servers.extend(
services()
.rooms
.state_cache
.invite_state(sender_user, &body.room_id)?
.unwrap_or_default()
.iter()
.filter_map(|event| serde_json::from_str(event.json().get()).ok())
.filter_map(|event: serde_json::Value| event.get("sender").cloned())
.filter_map(|sender| sender.as_str().map(ToOwned::to_owned))
.filter_map(|sender| UserId::parse(sender).ok())
.map(|user| user.server_name().to_owned()),
);
// There is no body.server_name for /roomId/join
let mut servers = services()
.rooms
.state_cache
.invite_state(sender_user, &body.room_id)?
.unwrap_or_default()
.iter()
.filter_map(|event| serde_json::from_str(event.json().get()).ok())
.filter_map(|event: serde_json::Value| event.get("sender").cloned())
.filter_map(|sender| sender.as_str().map(ToOwned::to_owned))
.filter_map(|sender| UserId::parse(sender).ok())
.map(|user| user.server_name().to_owned())
.collect::<Vec<_>>();
// server names being permanently attached to room IDs may be potentally removed
// in the future (see MSC4051). for future compatibility with this, and just
// because it makes sense, we shouldn't fail if the room ID doesn't have a
// server name with it and just use at least the server name from the initial
// invite above
if let Some(server) = body.room_id.server_name() {
servers.push(server.into());
}
@ -113,6 +107,7 @@ pub async fn join_room_by_id_or_alias_route(
}
let mut servers = body.server_name.clone();
servers.extend(
services()
.rooms
@ -127,11 +122,6 @@ pub async fn join_room_by_id_or_alias_route(
.map(|user| user.server_name().to_owned()),
);
// server names being permanently attached to room IDs may be potentally removed
// in the future (see MSC4051). for future compatibility with this, and just
// because it makes sense, we shouldn't fail if the room ID doesn't have a
// server name with it and just use at least the server name from the initial
// invite above
if let Some(server) = room_id.server_name() {
servers.push(server.into());
}

View file

@ -1429,7 +1429,7 @@ pub async fn get_room_information_route(
.state_cache
.room_servers(&room_id)
.filter_map(Result::ok)
.collect::<Vec<_>>();
.collect();
servers.sort_unstable();
servers.dedup();

View file

@ -1069,17 +1069,15 @@ impl Service {
let mut servers: Vec<OwnedServerName> = vec![];
// add server names of any trusted key servers if they're in the room
for server in services()
.rooms
.state_cache
.room_servers(room_id)
.filter_map(Result::ok)
.filter(|server| services().globals.trusted_servers().contains(server))
{
if server != services().globals.server_name() {
servers.push(server);
}
}
servers.extend(
services()
.rooms
.state_cache
.room_servers(room_id)
.filter_map(Result::ok)
.filter(|server| services().globals.trusted_servers().contains(server))
.filter(|server| server != services().globals.server_name()),
);
// add server names from room aliases on the room ID
let room_aliases = services()
@ -1114,17 +1112,15 @@ impl Service {
.unwrap_or_default();
// add server names of the list of admins in the room for backfill server
for server in power_levels
.users
.iter()
.filter(|(_, level)| **level > power_levels.users_default)
.map(|(user_id, _)| user_id.server_name())
.collect::<Vec<_>>()
{
if server != services().globals.server_name() {
servers.push(server.to_owned());
}
}
servers.extend(
power_levels
.users
.iter()
.filter(|(_, level)| **level > power_levels.users_default)
.map(|(user_id, _)| user_id.server_name())
.filter(|server| server != &services().globals.server_name())
.map(ToOwned::to_owned),
);
// don't backfill from ourselves (might be noop if we checked it above already)
if let Some(server_index) = servers