diff --git a/src/session/view/content/room_history/message_row/content.rs b/src/session/view/content/room_history/message_row/content.rs index 20352094..4b2380ff 100644 --- a/src/session/view/content/room_history/message_row/content.rs +++ b/src/session/view/content/room_history/message_row/content.rs @@ -110,6 +110,20 @@ impl MessageContent { self.notify("format"); } + /// Access the widget with the own content of the event. + /// + /// This allows to access the descendant content while discarding the + /// content of a related message, like a replied-to event. + pub fn content_widget(&self) -> Option { + let child = self.child()?; + + if let Some(reply) = child.downcast_ref::() { + reply.content().child() + } else { + Some(child) + } + } + pub fn update_for_event(&self, event: &Event) { let format = self.format(); if format == ContentFormat::Natural { @@ -168,13 +182,9 @@ impl MessageContent { /// Get the texture displayed by this widget, if any. pub fn texture(&self) -> Option { - let mut content = self.child()?; - - if let Some(reply) = content.downcast_ref::() { - content = reply.content().child()?; - } - - content.downcast_ref::()?.texture() + self.content_widget()? + .downcast_ref::()? + .texture() } } diff --git a/src/session/view/content/room_history/message_row/mod.rs b/src/session/view/content/room_history/message_row/mod.rs index fafd2f44..6378b3e9 100644 --- a/src/session/view/content/room_history/message_row/mod.rs +++ b/src/session/view/content/room_history/message_row/mod.rs @@ -15,6 +15,7 @@ use gtk::{ CompositeTemplate, }; use matrix_sdk::ruma::events::room::message::MessageType; +use tracing::warn; pub use self::content::ContentFormat; use self::{content::MessageContent, media::MessageMedia, reaction_list::MessageReactionList}; @@ -244,7 +245,12 @@ impl MessageRow { }; if matches!(message, MessageType::Image(_) | MessageType::Video(_)) { - let media_widget = imp.content.child().and_downcast::().unwrap(); + let Some(media_widget) = imp.content.content_widget().and_downcast::() + else { + warn!("Trying to show media of a non-media message"); + return; + }; + window.session_view().show_media(event, &media_widget); } }