fix: media thumbnail calculation and appservice detection

This commit is contained in:
Timo Kösters 2021-03-23 19:46:54 +01:00
parent 3ea7d162db
commit 46d8f36a2c
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
5 changed files with 29 additions and 13 deletions

View File

@ -4,7 +4,7 @@ use crate::{
pdu::{PduBuilder, PduEvent}, pdu::{PduBuilder, PduEvent},
utils, ConduitResult, Database, Error, Result, Ruma, utils, ConduitResult, Database, Error, Result, Ruma,
}; };
use log::{info, warn}; use log::{error, info, warn};
use ruma::{ use ruma::{
api::{ api::{
client::{ client::{
@ -544,8 +544,10 @@ async fn join_room_by_id_helper(
.await?; .await?;
let add_event_id = |pdu: &Raw<Pdu>| -> Result<(EventId, CanonicalJsonObject)> { let add_event_id = |pdu: &Raw<Pdu>| -> Result<(EventId, CanonicalJsonObject)> {
let mut value = serde_json::from_str(pdu.json().get()) let mut value = serde_json::from_str(pdu.json().get()).map_err(|e| {
.expect("converting raw jsons to values always works"); error!("{:?}: {:?}", pdu, e);
Error::BadServerResponse("Invalid PDU in server response")
})?;
let event_id = EventId::try_from(&*format!( let event_id = EventId::try_from(&*format!(
"${}", "${}",
ruma::signatures::reference_hash(&value, &RoomVersionId::Version6) ruma::signatures::reference_hash(&value, &RoomVersionId::Version6)

View File

@ -226,16 +226,17 @@ impl Media {
} }
let thumbnail = if crop { let thumbnail = if crop {
image.resize_to_fill(width, height, FilterType::Triangle) image.resize_to_fill(width, height, FilterType::CatmullRom)
} else { } else {
let (exact_width, exact_height) = { let (exact_width, exact_height) = {
// Copied from image::dynimage::resize_dimensions // Copied from image::dynimage::resize_dimensions
let ratio = u64::from(original_width) * u64::from(height); let ratio = u64::from(original_width) * u64::from(height);
let nratio = u64::from(width) * u64::from(original_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 { 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 { } else {
u64::from(original_width) * u64::from(height) u64::from(original_width) * u64::from(height)
/ u64::from(original_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(); let mut thumbnail_bytes = Vec::new();

View File

@ -1193,6 +1193,9 @@ impl Rooms {
.state_key .state_key
.as_ref() .as_ref()
.map_or(false, |state_key| users.is_match(&state_key)) .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| { let matching_aliases = |aliases: &Regex| {
self.room_aliases(&room_id) self.room_aliases(&room_id)
@ -1201,9 +1204,9 @@ impl Rooms {
}; };
if bridge_user_id.map_or(false, user_is_joined) if bridge_user_id.map_or(false, user_is_joined)
|| users.iter().any(matching_users)
|| aliases.iter().any(matching_aliases) || aliases.iter().any(matching_aliases)
|| rooms.map_or(false, |rooms| rooms.contains(&room_id.as_str().into())) || 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)?; db.sending.send_pdu_appservice(&appservice.0, &pdu_id)?;
} }

View File

@ -1,4 +1,5 @@
use crate::Error; use crate::Error;
use log::error;
use ruma::{ use ruma::{
events::{ events::{
pdu::EventHash, room::member::MemberEventContent, AnyEvent, AnyRoomEvent, AnyStateEvent, 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<String, CanonicalJsonValue>`. /// Returns a tuple of the new `EventId` and the PDU as a `BTreeMap<String, CanonicalJsonValue>`.
pub(crate) fn gen_event_id_canonical_json( pub(crate) fn gen_event_id_canonical_json(
pdu: &Raw<ruma::events::pdu::Pdu>, pdu: &Raw<ruma::events::pdu::Pdu>,
) -> (EventId, CanonicalJsonObject) { ) -> crate::Result<(EventId, CanonicalJsonObject)> {
let value = serde_json::from_str(pdu.json().get()).expect("A Raw<...> is always valid JSON"); 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!( 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"); .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`. /// Build the start of a PDU in order to add it to the `Database`.

View File

@ -556,7 +556,13 @@ pub async fn send_transaction_message_route<'a>(
// 1. Is a valid event, otherwise it is dropped. // 1. Is a valid event, otherwise it is dropped.
// Ruma/PduEvent/StateEvent satisfies this // Ruma/PduEvent/StateEvent satisfies this
// We do not add the event_id field to the pdu here because of signature and hashes checks // 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 // If we have no idea about this room skip the PDU
let room_id = match value let room_id = match value
@ -1138,7 +1144,7 @@ pub(crate) async fn fetch_events(
Ok(res) => { Ok(res) => {
debug!("Got event over federation: {:?}", res); debug!("Got event over federation: {:?}", res);
let (event_id, value) = let (event_id, value) =
crate::pdu::gen_event_id_canonical_json(&res.pdu); crate::pdu::gen_event_id_canonical_json(&res.pdu)?;
let (pdu, _) = let (pdu, _) =
validate_event(db, value, event_id, key_map, origin, auth_cache) validate_event(db, value, event_id, key_map, origin, auth_cache)
.await .await