Fix all clippy warnings, shorten line len in client_server

next
Devin Ragotzy 2020-07-25 23:56:50 -04:00 committed by timokoesters
parent 3ccbd02081
commit 62df9ca580
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
5 changed files with 155 additions and 100 deletions

View File

@ -230,7 +230,7 @@ pub fn register_route(
Ok(register::Response {
access_token: Some(token),
user_id,
device_id: Some(device_id.into()),
device_id: Some(device_id),
}
.into())
}
@ -257,11 +257,22 @@ pub fn login_route(
if let (login::UserInfo::MatrixId(username), login::LoginInfo::Password { password }) =
(body.user.clone(), body.login_info.clone())
{
let user_id = UserId::parse_with_server_name(username, db.globals.server_name()).map_err(|_| Error::BadRequest(ErrorKind::InvalidUsername, "Username is invalid."))?;
let hash = db.users.password_hash(&user_id)?.ok_or(Error::BadRequest(ErrorKind::Forbidden, "Wrong username or password."))?;
let user_id = UserId::parse_with_server_name(username, db.globals.server_name())
.map_err(|_| Error::BadRequest(
ErrorKind::InvalidUsername,
"Username is invalid."
))?;
let hash = db.users.password_hash(&user_id)?
.ok_or(Error::BadRequest(
ErrorKind::Forbidden,
"Wrong username or password."
))?;
if hash.is_empty() {
return Err(Error::BadRequest(ErrorKind::UserDeactivated, "The user has been deactivated"));
return Err(Error::BadRequest(
ErrorKind::UserDeactivated,
"The user has been deactivated"
));
}
let hash_matches =
@ -298,7 +309,7 @@ pub fn login_route(
user_id,
access_token: token,
home_server: Some(db.globals.server_name().to_owned()),
device_id: device_id.into(),
device_id,
well_known: None,
}
.into())
@ -1702,8 +1713,7 @@ pub fn leave_room_route(
ErrorKind::BadState,
"Cannot leave a room you are not a member of.",
))?
.content
.clone(),
.content,
)
.map_err(|_| Error::bad_database("Invalid member event in database."))?
.deserialize()
@ -1746,8 +1756,7 @@ pub fn kick_user_route(
ErrorKind::BadState,
"Cannot kick member that's not in the room.",
))?
.content
.clone(),
.content,
)
.expect("Raw::from_value always works")
.deserialize()
@ -1882,8 +1891,7 @@ pub fn unban_user_route(
ErrorKind::BadState,
"Cannot unban a user who is not banned.",
))?
.content
.clone(),
.content,
)
.map_err(|_| Error::bad_database("Invalid member event in database."))?
.deserialize()
@ -2049,6 +2057,8 @@ pub async fn get_public_rooms_filtered_route(
db: State<'_, Database>,
_body: Ruma<get_public_rooms_filtered::Request>,
) -> ConduitResult<get_public_rooms_filtered::Response> {
use ruma::events::room;
let mut chunk = db
.rooms
.public_rooms()
@ -2060,17 +2070,25 @@ pub async fn get_public_rooms_filtered_route(
let chunk = directory::PublicRoomsChunk {
aliases: Vec::new(),
canonical_alias: state.get(&(EventType::RoomCanonicalAlias, "".to_owned())).map_or(Ok::<_, Error>(None), |s| {
canonical_alias: state
.get(&(EventType::RoomCanonicalAlias, "".to_owned()))
.map_or(Ok::<_, Error>(None), |s| {
Ok(serde_json::from_value::<
Raw<ruma::events::room::canonical_alias::CanonicalAliasEventContent>,
Raw<room::canonical_alias::CanonicalAliasEventContent>,
>(s.content.clone())
.map_err(|_| Error::bad_database("Invalid canonical alias event in database."))?
.map_err(|_| {
Error::bad_database("Invalid canonical alias event in database.")
})?
.deserialize()
.map_err(|_| Error::bad_database("Invalid canonical alias event in database."))?
.map_err(|_| {
Error::bad_database("Invalid canonical alias event in database.")
})?
.alias)
})?,
name: state.get(&(EventType::RoomName, "".to_owned())).map_or(Ok::<_, Error>(None), |s| {
Ok(serde_json::from_value::<Raw<ruma::events::room::name::NameEventContent>>(
name: state.get(&(EventType::RoomName, "".to_owned())).map_or(
Ok::<_, Error>(None),
|s| {
Ok(serde_json::from_value::<Raw<room::name::NameEventContent>>(
s.content.clone(),
)
.map_err(|_| Error::bad_database("Invalid room name event in database."))?
@ -2078,45 +2096,84 @@ pub async fn get_public_rooms_filtered_route(
.map_err(|_| Error::bad_database("Invalid room name event in database."))?
.name()
.map(|n| n.to_owned()))
})?,
},
)?,
num_joined_members: (db.rooms.room_members(&room_id).count() as u32).into(),
room_id,
topic: state.get(&(EventType::RoomTopic, "".to_owned())).map_or(Ok::<_, Error>(None), |s| {
Ok(Some(serde_json::from_value::<
Raw<ruma::events::room::topic::TopicEventContent>,
>(s.content.clone())
.map_err(|_| Error::bad_database("Invalid room topic event in database."))?
topic: state.get(&(EventType::RoomTopic, "".to_owned())).map_or(
Ok::<_, Error>(None),
|s| {
Ok(Some(
serde_json::from_value::<Raw<room::topic::TopicEventContent>>(
s.content.clone(),
)
.map_err(|_| {
Error::bad_database("Invalid room topic event in database.")
})?
.deserialize()
.map_err(|_| Error::bad_database("Invalid room topic event in database."))?
.topic))
})?,
world_readable: state.get(&(EventType::RoomHistoryVisibility, "".to_owned())).map_or(Ok::<_, Error>(false), |s| {
.map_err(|_| {
Error::bad_database("Invalid room topic event in database.")
})?
.topic,
))
},
)?,
world_readable: state
.get(&(EventType::RoomHistoryVisibility, "".to_owned()))
.map_or(Ok::<_, Error>(false), |s| {
Ok(serde_json::from_value::<
Raw<ruma::events::room::history_visibility::HistoryVisibilityEventContent>,
Raw<room::history_visibility::HistoryVisibilityEventContent>,
>(s.content.clone())
.map_err(|_| Error::bad_database("Invalid room history visibility event in database."))?
.map_err(|_| {
Error::bad_database(
"Invalid room history visibility event in database.",
)
})?
.deserialize()
.map_err(|_| Error::bad_database("Invalid room history visibility event in database."))?
.history_visibility == history_visibility::HistoryVisibility::WorldReadable)
.map_err(|_| {
Error::bad_database(
"Invalid room history visibility event in database.",
)
})?
.history_visibility
== history_visibility::HistoryVisibility::WorldReadable)
})?,
guest_can_join: state.get(&(EventType::RoomGuestAccess, "".to_owned())).map_or(Ok::<_, Error>(false), |s| {
Ok(serde_json::from_value::<
Raw<ruma::events::room::guest_access::GuestAccessEventContent>,
guest_can_join: state
.get(&(EventType::RoomGuestAccess, "".to_owned()))
.map_or(Ok::<_, Error>(false), |s| {
Ok(
serde_json::from_value::<
Raw<room::guest_access::GuestAccessEventContent>,
>(s.content.clone())
.map_err(|_| Error::bad_database("Invalid room guest access event in database."))?
.map_err(|_| {
Error::bad_database("Invalid room guest access event in database.")
})?
.deserialize()
.map_err(|_| Error::bad_database("Invalid room guest access event in database."))?
.guest_access == guest_access::GuestAccess::CanJoin)
.map_err(|_| {
Error::bad_database("Invalid room guest access event in database.")
})?
.guest_access
== guest_access::GuestAccess::CanJoin,
)
})?,
avatar_url: state.get(&(EventType::RoomAvatar, "".to_owned())).map_or( Ok::<_, Error>(None),|s| {
Ok(Some(serde_json::from_value::<
Raw<ruma::events::room::avatar::AvatarEventContent>,
>(s.content.clone())
.map_err(|_| Error::bad_database("Invalid room avatar event in database."))?
avatar_url: state.get(&(EventType::RoomAvatar, "".to_owned())).map_or(
Ok::<_, Error>(None),
|s| {
Ok(Some(
serde_json::from_value::<Raw<room::avatar::AvatarEventContent>>(
s.content.clone(),
)
.map_err(|_| {
Error::bad_database("Invalid room avatar event in database.")
})?
.deserialize()
.map_err(|_| Error::bad_database("Invalid room avatar event in database."))?
.url))
})?,
.map_err(|_| {
Error::bad_database("Invalid room avatar event in database.")
})?
.url,
))
},
)?,
};
Ok::<_, Error>(chunk)
})
@ -2338,7 +2395,11 @@ pub fn create_state_event_for_key_route(
.filter(|room| room == &body.room_id) // Make sure it's the right room
.is_none()
{
return Err(Error::BadRequest(ErrorKind::Forbidden, "You are only allowed to send canonical_alias events when it's aliases already exists"));
return Err(Error::BadRequest(
ErrorKind::Forbidden,
"You are only allowed to send canonical_alias \
events when it's aliases already exists",
));
}
}
}

View File

@ -1,6 +1,8 @@
use crate::{utils, Error, Result};
use std::mem;
pub type FileMeta = (Option<String>, String, Vec<u8>);
pub struct Media {
pub(super) mediaid_file: sled::Tree, // MediaId = MXC + WidthHeight + Filename + ContentType
}
@ -29,7 +31,7 @@ impl Media {
}
/// Downloads a file.
pub fn get(&self, mxc: String) -> Result<Option<(Option<String>, String, Vec<u8>)>> {
pub fn get(&self, mxc: String) -> Result<Option<FileMeta>> {
let mut prefix = mxc.as_bytes().to_vec();
prefix.push(0xff);
prefix.extend_from_slice(&0_u32.to_be_bytes()); // Width = 0 if it's not a thumbnail
@ -66,12 +68,7 @@ impl Media {
}
/// Downloads a file's thumbnail.
pub fn get_thumbnail(
&self,
mxc: String,
width: u32,
height: u32,
) -> Result<Option<(Option<String>, String, Vec<u8>)>> {
pub fn get_thumbnail(&self, mxc: String, width: u32, height: u32) -> Result<Option<FileMeta>> {
let mut main_prefix = mxc.as_bytes().to_vec();
main_prefix.push(0xff);

View File

@ -250,6 +250,7 @@ impl Rooms {
}
/// Creates a new persisted data unit and adds it to a room.
#[allow(clippy::too_many_arguments, clippy::blocks_in_if_conditions)]
pub fn append_pdu(
&self,
room_id: RoomId,
@ -288,7 +289,7 @@ impl Rooms {
},
|power_levels| {
Ok(serde_json::from_value::<Raw<PowerLevelsEventContent>>(
power_levels.content.clone(),
power_levels.content,
)
.expect("Raw::from_value always works.")
.deserialize()
@ -298,13 +299,13 @@ impl Rooms {
let sender_membership = self
.room_state_get(&room_id, &EventType::RoomMember, &sender.to_string())?
.map_or(Ok::<_, Error>(member::MembershipState::Leave), |pdu| {
Ok(serde_json::from_value::<Raw<member::MemberEventContent>>(
pdu.content.clone(),
)
Ok(
serde_json::from_value::<Raw<member::MemberEventContent>>(pdu.content)
.expect("Raw::from_value always works.")
.deserialize()
.map_err(|_| Error::bad_database("Invalid Member event in db."))?
.membership)
.membership,
)
})?;
let sender_power = power_levels.users.get(&sender).map_or_else(
@ -341,7 +342,7 @@ impl Rooms {
)?
.map_or(Ok::<_, Error>(member::MembershipState::Leave), |pdu| {
Ok(serde_json::from_value::<Raw<member::MemberEventContent>>(
pdu.content.clone(),
pdu.content,
)
.expect("Raw::from_value always works.")
.deserialize()
@ -373,7 +374,7 @@ impl Rooms {
.map_or(Ok::<_, Error>(join_rules::JoinRule::Public), |pdu| {
Ok(serde_json::from_value::<
Raw<join_rules::JoinRulesEventContent>,
>(pdu.content.clone())
>(pdu.content)
.expect("Raw::from_value always works.")
.deserialize()
.map_err(|_| {
@ -501,7 +502,7 @@ impl Rooms {
let mut unsigned = unsigned.unwrap_or_default();
if let Some(state_key) = &state_key {
if let Some(prev_pdu) = self.room_state_get(&room_id, &event_type, &state_key)? {
unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone());
unsigned.insert("prev_content".to_owned(), prev_pdu.content);
unsigned.insert(
"prev_sender".to_owned(),
serde_json::to_value(prev_pdu.sender).expect("UserId::to_value always works"),
@ -575,8 +576,7 @@ impl Rooms {
self.roomstateid_pdu.insert(key, &*pdu_json.to_string())?;
}
match event_type {
EventType::RoomRedaction => {
if let EventType::RoomRedaction = event_type {
if let Some(redact_id) = &redacts {
// TODO: Reason
let _reason =
@ -594,9 +594,6 @@ impl Rooms {
self.redact_pdu(&redact_id)?;
}
}
_ => {}
}
self.edus.room_read_set(&room_id, &sender, index)?;
Ok(pdu.event_id)

View File

@ -42,7 +42,7 @@ impl Uiaa {
.map(|session| {
Ok::<_, Error>(self.get_uiaa_session(&user_id, &device_id, session)?)
})
.unwrap_or(Ok(uiaainfo.clone()))?;
.unwrap_or_else(|| Ok(uiaainfo.clone()))?;
// Find out what the user completed
match &**kind {

View File

@ -502,7 +502,7 @@ impl Users {
));
}
let mut user_signing_key_key = prefix.clone();
let mut user_signing_key_key = prefix;
user_signing_key_key.extend_from_slice(user_signing_key_id.as_bytes());
self.keyid_key.insert(