diff --git a/src/client_server.rs b/src/client_server.rs index 4a2f33b..6f9688f 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -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, ) -> ConduitResult { + use ruma::events::room; + let mut chunk = db .rooms .public_rooms() @@ -2060,63 +2070,110 @@ 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| { - Ok(serde_json::from_value::< - Raw, + canonical_alias: state + .get(&(EventType::RoomCanonicalAlias, "".to_owned())) + .map_or(Ok::<_, Error>(None), |s| { + Ok(serde_json::from_value::< + Raw, >(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::>( - s.content.clone(), - ) - .map_err(|_| Error::bad_database("Invalid room name event in database."))? - .deserialize() - .map_err(|_| Error::bad_database("Invalid room name event in database."))? - .name() - .map(|n| n.to_owned())) - })?, + })?, + name: state.get(&(EventType::RoomName, "".to_owned())).map_or( + Ok::<_, Error>(None), + |s| { + Ok(serde_json::from_value::>( + s.content.clone(), + ) + .map_err(|_| Error::bad_database("Invalid room name event in database."))? + .deserialize() + .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, + topic: state.get(&(EventType::RoomTopic, "".to_owned())).map_or( + Ok::<_, Error>(None), + |s| { + Ok(Some( + serde_json::from_value::>( + 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| { + Ok(serde_json::from_value::< + Raw, >(s.content.clone()) - .map_err(|_| Error::bad_database("Invalid room topic event in database."))? + .map_err(|_| { + Error::bad_database( + "Invalid room history visibility 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| { - Ok(serde_json::from_value::< - Raw, - >(s.content.clone()) - .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) - })?, - guest_can_join: state.get(&(EventType::RoomGuestAccess, "".to_owned())).map_or(Ok::<_, Error>(false), |s| { - Ok(serde_json::from_value::< - Raw, - >(s.content.clone()) - .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) - })?, - avatar_url: state.get(&(EventType::RoomAvatar, "".to_owned())).map_or( Ok::<_, Error>(None),|s| { - Ok(Some(serde_json::from_value::< - Raw, - >(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 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, + >(s.content.clone()) + .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, + ) + })?, + avatar_url: state.get(&(EventType::RoomAvatar, "".to_owned())).map_or( + Ok::<_, Error>(None), + |s| { + Ok(Some( + serde_json::from_value::>( + 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, + )) + }, + )?, }; 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", + )); } } } diff --git a/src/database/media.rs b/src/database/media.rs index 0d0820d..cec74a6 100644 --- a/src/database/media.rs +++ b/src/database/media.rs @@ -1,6 +1,8 @@ use crate::{utils, Error, Result}; use std::mem; +pub type FileMeta = (Option, String, Vec); + 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, String, Vec)>> { + pub fn get(&self, mxc: String) -> Result> { 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, String, Vec)>> { + pub fn get_thumbnail(&self, mxc: String, width: u32, height: u32) -> Result> { let mut main_prefix = mxc.as_bytes().to_vec(); main_prefix.push(0xff); diff --git a/src/database/rooms.rs b/src/database/rooms.rs index 7895f02..ae0d084 100644 --- a/src/database/rooms.rs +++ b/src/database/rooms.rs @@ -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::>( - 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::>( - pdu.content.clone(), + Ok( + serde_json::from_value::>(pdu.content) + .expect("Raw::from_value always works.") + .deserialize() + .map_err(|_| Error::bad_database("Invalid Member event in db."))? + .membership, ) - .expect("Raw::from_value always works.") - .deserialize() - .map_err(|_| Error::bad_database("Invalid Member event in db."))? - .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::>( - 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, - >(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,28 +576,24 @@ impl Rooms { self.roomstateid_pdu.insert(key, &*pdu_json.to_string())?; } - match event_type { - EventType::RoomRedaction => { - if let Some(redact_id) = &redacts { - // TODO: Reason - let _reason = - serde_json::from_value::>(content) - .expect("Raw::from_value always works.") - .deserialize() - .map_err(|_| { - Error::BadRequest( - ErrorKind::InvalidParam, - "Invalid redaction event content.", - ) - })? - .reason; + if let EventType::RoomRedaction = event_type { + if let Some(redact_id) = &redacts { + // TODO: Reason + let _reason = + serde_json::from_value::>(content) + .expect("Raw::from_value always works.") + .deserialize() + .map_err(|_| { + Error::BadRequest( + ErrorKind::InvalidParam, + "Invalid redaction event content.", + ) + })? + .reason; - self.redact_pdu(&redact_id)?; - } + self.redact_pdu(&redact_id)?; } - _ => {} } - self.edus.room_read_set(&room_id, &sender, index)?; Ok(pdu.event_id) diff --git a/src/database/uiaa.rs b/src/database/uiaa.rs index 4366eb2..cece8db 100644 --- a/src/database/uiaa.rs +++ b/src/database/uiaa.rs @@ -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 { diff --git a/src/database/users.rs b/src/database/users.rs index c792767..1ec677c 100644 --- a/src/database/users.rs +++ b/src/database/users.rs @@ -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(