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},
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<Pdu>| -> 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)

View file

@ -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();

View file

@ -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)?;
}

View file

@ -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<String, CanonicalJsonValue>`.
pub(crate) fn gen_event_id_canonical_json(
pdu: &Raw<ruma::events::pdu::Pdu>,
) -> (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`.

View file

@ -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