diff --git a/src/client_server/membership.rs b/src/client_server/membership.rs index d571eaa..71be6ac 100644 --- a/src/client_server/membership.rs +++ b/src/client_server/membership.rs @@ -4,7 +4,7 @@ use crate::{ pdu::{PduBuilder, PduEvent}, utils, ConduitResult, Database, Error, Result, Ruma, }; -use log::{info, warn}; +use log::{error, info, warn}; use ruma::{ api::{ client::{ @@ -544,8 +544,10 @@ async fn join_room_by_id_helper( .await?; let add_event_id = |pdu: &Raw| -> Result<(EventId, CanonicalJsonObject)> { - let mut value = serde_json::from_str(pdu.json().get()) - .expect("converting raw jsons to values always works"); + let mut value = serde_json::from_str(pdu.json().get()).map_err(|e| { + error!("{:?}: {:?}", pdu, e); + Error::BadServerResponse("Invalid PDU in server response") + })?; let event_id = EventId::try_from(&*format!( "${}", ruma::signatures::reference_hash(&value, &RoomVersionId::Version6) diff --git a/src/database/media.rs b/src/database/media.rs index 448d071..f958dc8 100644 --- a/src/database/media.rs +++ b/src/database/media.rs @@ -226,16 +226,17 @@ impl Media { } let thumbnail = if crop { - image.resize_to_fill(width, height, FilterType::Triangle) + image.resize_to_fill(width, height, FilterType::CatmullRom) } else { let (exact_width, exact_height) = { // Copied from image::dynimage::resize_dimensions let ratio = u64::from(original_width) * u64::from(height); let nratio = u64::from(width) * u64::from(original_height); - let use_width = nratio > ratio; + let use_width = nratio <= ratio; let intermediate = if use_width { - u64::from(original_height) * u64::from(width) / u64::from(width) + u64::from(original_height) * u64::from(width) + / u64::from(original_width) } else { u64::from(original_width) * u64::from(height) / u64::from(original_height) @@ -261,7 +262,7 @@ impl Media { } }; - image.thumbnail_exact(exact_width, exact_height) + image.thumbnail_exact(dbg!(exact_width), dbg!(exact_height)) }; let mut thumbnail_bytes = Vec::new(); diff --git a/src/database/rooms.rs b/src/database/rooms.rs index 2144340..3bf72d0 100644 --- a/src/database/rooms.rs +++ b/src/database/rooms.rs @@ -1193,6 +1193,9 @@ impl Rooms { .state_key .as_ref() .map_or(false, |state_key| users.is_match(&state_key)) + || db.rooms.room_members(&room_id).any(|userid| { + userid.map_or(false, |userid| users.is_match(userid.as_str())) + }) }; let matching_aliases = |aliases: &Regex| { self.room_aliases(&room_id) @@ -1201,9 +1204,9 @@ impl Rooms { }; if bridge_user_id.map_or(false, user_is_joined) - || users.iter().any(matching_users) || aliases.iter().any(matching_aliases) || rooms.map_or(false, |rooms| rooms.contains(&room_id.as_str().into())) + || users.iter().any(matching_users) { db.sending.send_pdu_appservice(&appservice.0, &pdu_id)?; } diff --git a/src/pdu.rs b/src/pdu.rs index 6085581..009fde6 100644 --- a/src/pdu.rs +++ b/src/pdu.rs @@ -1,4 +1,5 @@ use crate::Error; +use log::error; use ruma::{ events::{ pdu::EventHash, room::member::MemberEventContent, AnyEvent, AnyRoomEvent, AnyStateEvent, @@ -322,8 +323,11 @@ impl Ord for PduEvent { /// Returns a tuple of the new `EventId` and the PDU as a `BTreeMap`. pub(crate) fn gen_event_id_canonical_json( pdu: &Raw, -) -> (EventId, CanonicalJsonObject) { - let value = serde_json::from_str(pdu.json().get()).expect("A Raw<...> is always valid JSON"); +) -> crate::Result<(EventId, CanonicalJsonObject)> { + let value = serde_json::from_str(pdu.json().get()).map_err(|e| { + error!("{:?}: {:?}", pdu, e); + Error::BadServerResponse("Invalid PDU in server response") + })?; let event_id = EventId::try_from(&*format!( "${}", @@ -332,7 +336,7 @@ pub(crate) fn gen_event_id_canonical_json( )) .expect("ruma's reference hashes are valid event ids"); - (event_id, value) + Ok((event_id, value)) } /// Build the start of a PDU in order to add it to the `Database`. diff --git a/src/server_server.rs b/src/server_server.rs index 3c364db..fa5706d 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -556,7 +556,13 @@ pub async fn send_transaction_message_route<'a>( // 1. Is a valid event, otherwise it is dropped. // Ruma/PduEvent/StateEvent satisfies this // We do not add the event_id field to the pdu here because of signature and hashes checks - let (event_id, value) = crate::pdu::gen_event_id_canonical_json(pdu); + let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(pdu) { + Ok(t) => t, + Err(_) => { + // Event could not be converted to canonical json + return None; + } + }; // If we have no idea about this room skip the PDU let room_id = match value @@ -1138,7 +1144,7 @@ pub(crate) async fn fetch_events( Ok(res) => { debug!("Got event over federation: {:?}", res); let (event_id, value) = - crate::pdu::gen_event_id_canonical_json(&res.pdu); + crate::pdu::gen_event_id_canonical_json(&res.pdu)?; let (pdu, _) = validate_event(db, value, event_id, key_map, origin, auth_cache) .await