Skip to content

Commit

Permalink
use macro for error constructions
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Volk <[email protected]>
  • Loading branch information
jevolk authored and girlbossceo committed Dec 18, 2024
1 parent 4d46df2 commit 60a9525
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions src/service/rooms/alias/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ use std::sync::Arc;
use conduwuit::{
err,
utils::{stream::TryIgnore, ReadyExt},
Err, Error, Result,
Err, Result,
};
use database::{Deserialized, Ignore, Interfix, Map};
use futures::{Stream, StreamExt};
use futures::{Stream, StreamExt, TryFutureExt};
use ruma::{
api::client::error::ErrorKind,
events::{
room::power_levels::{RoomPowerLevels, RoomPowerLevelsEventContent},
StateEventType,
Expand Down Expand Up @@ -72,10 +71,7 @@ impl Service {
if alias == self.services.globals.admin_alias
&& user_id != self.services.globals.server_user
{
return Err(Error::BadRequest(
ErrorKind::forbidden(),
"Only the server user can set this alias",
));
return Err!(Request(Forbidden("Only the server user can set this alias")));
}

// Comes first as we don't want a stuck alias
Expand Down Expand Up @@ -220,18 +216,20 @@ impl Service {
}

// Checking whether the user is able to change canonical aliases of the room
if let Ok(content) = self
if let Ok(power_levels) = self
.services
.state_accessor
.room_state_get_content::<RoomPowerLevelsEventContent>(
&room_id,
&StateEventType::RoomPowerLevels,
"",
)
.map_ok(RoomPowerLevels::from)
.await
{
return Ok(RoomPowerLevels::from(content)
.user_can_send_state(user_id, StateEventType::RoomCanonicalAlias));
return Ok(
power_levels.user_can_send_state(user_id, StateEventType::RoomCanonicalAlias)
);
}

// If there is no power levels event, only the room creator can change
Expand Down Expand Up @@ -291,29 +289,20 @@ impl Service {
.globals
.server_is_ours(room_alias.server_name())
{
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Alias is from another server.",
));
return Err!(Request(InvalidParam("Alias is from another server.")));
}

if let Some(ref info) = appservice_info {
if let Some(info) = appservice_info {
if !info.aliases.is_match(room_alias.as_str()) {
return Err(Error::BadRequest(
ErrorKind::Exclusive,
"Room alias is not in namespace.",
));
return Err!(Request(Exclusive("Room alias is not in namespace.")));
}
} else if self
.services
.appservice
.is_exclusive_alias(room_alias)
.await
{
return Err(Error::BadRequest(
ErrorKind::Exclusive,
"Room alias reserved by appservice.",
));
return Err!(Request(Exclusive("Room alias reserved by appservice.")));
}

Ok(())
Expand Down

0 comments on commit 60a9525

Please sign in to comment.