Skip to content

Commit 85c6f00

Browse files
authored
Merge pull request #322 from aaravlu/fix271
2 parents e532590 + 623faaf commit 85c6f00

File tree

1 file changed

+68
-47
lines changed

1 file changed

+68
-47
lines changed

src/home/room_screen.rs

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ use matrix_sdk::{
1212
message::{
1313
AudioMessageEventContent, CustomEventContent, EmoteMessageEventContent, FileMessageEventContent, FormattedBody, ImageMessageEventContent, KeyVerificationRequestEventContent, LocationMessageEventContent, MessageFormat, MessageType, NoticeMessageEventContent, RoomMessageEventContent, ServerNoticeMessageEventContent, TextMessageEventContent, VideoMessageEventContent
1414
}, ImageInfo, MediaSource
15-
}, sticker::StickerEventContent}, matrix_uri::MatrixId, uint, EventId, MatrixToUri, MatrixUri, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, UserId
16-
},
17-
OwnedServerName,
15+
}, sticker::StickerEventContent}, matrix_uri::MatrixId, uint, EventId, MatrixToUri, MatrixUri, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedMxcUri, OwnedRoomId, UserId
16+
}, OwnedServerName
1817
};
1918
use matrix_sdk_ui::timeline::{
2019
self, EventTimelineItem, InReplyToDetails, MemberProfileChange, Profile, ReactionsByKeyBySender, RepliedToInfo, RoomMembershipChange, TimelineDetails, TimelineItem, TimelineItemContent, TimelineItemKind, VirtualTimelineItem
@@ -1155,7 +1154,7 @@ impl Widget for RoomScreen {
11551154
content: { margin: { left: (coords.x), top: (coords.y) } }
11561155
},
11571156
);
1158-
1157+
11591158
if let Some(message_widget_uid) = action.as_widget_action().map(|a| a.widget_uid) {
11601159
message_action_bar_popup.open(cx);
11611160
message_action_bar.initialize_with_data(cx, widget_uid, message_widget_uid, item_id);
@@ -1546,7 +1545,7 @@ impl RoomScreen {
15461545
submit_async_request(MatrixRequest::GetNumberUnreadMessages{ room_id: room_id.clone() });
15471546
}
15481547
}
1549-
1548+
15501549
if clear_cache {
15511550
tl.content_drawn_since_last_update.clear();
15521551
tl.profile_drawn_since_last_update.clear();
@@ -2148,7 +2147,7 @@ impl RoomScreen {
21482147
event_id: last_event_id.to_owned(),
21492148
});
21502149
}
2151-
2150+
21522151
}
21532152
}
21542153
}
@@ -3065,63 +3064,85 @@ fn populate_image_message_content(
30653064
)
30663065
.unwrap_or_default();
30673066

3068-
// If we have a known mimetype and it's not an image (e.g., an animated)
3069-
// then show a message about it being unsupported.
3067+
// If we have a known mimetype and it's not a static image,
3068+
// then show a message about it being unsupported (e.g., for animated gifs).
30703069
if let Some(mime) = mimetype.as_ref() {
30713070
if ImageFormat::from_mimetype(mime).is_none() {
30723071
text_or_image_ref.show_text(format!(
30733072
"{body}\n\nImages/Stickers of type {mime:?} are not yet supported.",
30743073
));
3075-
return true;
3074+
return true; // consider this as fully drawn
30763075
}
30773076
}
30783077

3079-
match image_info_source.map(|(_, source)| source) {
3080-
Some(MediaSource::Plain(mxc_uri)) => {
3081-
// now that we've obtained the image URI and its metadata, try to fetch the image.
3082-
match media_cache.try_get_media_or_fetch(mxc_uri.clone(), None) {
3083-
MediaCacheEntry::Loaded(data) => {
3084-
let show_image_result = text_or_image_ref.show_image(|img| {
3085-
utils::load_png_or_jpg(&img, cx, &data)
3086-
.map(|()| img.size_in_pixels(cx).unwrap())
3087-
});
3088-
if let Err(e) = show_image_result {
3089-
let err_str = format!("{body}\n\nFailed to display image: {e:?}");
3090-
error!("{err_str}");
3091-
text_or_image_ref.show_text(&err_str);
3092-
}
3078+
let mut fully_drawn = false;
30933079

3094-
// We're done drawing the image message content, so mark it as fully drawn.
3095-
true
3096-
}
3097-
MediaCacheEntry::Requested => {
3098-
text_or_image_ref.show_text(format!("{body}\n\nFetching image from {:?}", mxc_uri));
3099-
// Do not consider this image as being fully drawn, as we're still fetching it.
3100-
false
3101-
}
3102-
MediaCacheEntry::Failed => {
3103-
text_or_image_ref
3104-
.show_text(format!("{body}\n\nFailed to fetch image from {:?}", mxc_uri));
3105-
// For now, we consider this as being "complete". In the future, we could support
3106-
// retrying to fetch the image on a user click/tap.
3107-
true
3080+
// A closure that fetches and shows the image from the given `mxc_uri`,
3081+
// marking it as fully drawn if the image was available.
3082+
let mut fetch_and_show_image_uri = |mxc_uri: OwnedMxcUri|
3083+
match media_cache.try_get_media_or_fetch(mxc_uri.clone(), None) {
3084+
MediaCacheEntry::Loaded(data) => {
3085+
let show_image_result = text_or_image_ref.show_image(|img| {
3086+
utils::load_png_or_jpg(&img, cx, &data)
3087+
.map(|()| img.size_in_pixels(cx).unwrap_or_default())
3088+
});
3089+
if let Err(e) = show_image_result {
3090+
let err_str = format!("{body}\n\nFailed to display image: {e:?}");
3091+
error!("{err_str}");
3092+
text_or_image_ref.show_text(&err_str);
31083093
}
3094+
3095+
// We're done drawing the image, so mark it as fully drawn.
3096+
fully_drawn = true;
3097+
}
3098+
MediaCacheEntry::Requested => {
3099+
text_or_image_ref.show_text(format!("{body}\n\nFetching image from {:?}", mxc_uri));
3100+
// Do not consider this thumbnail as being fully drawn, as we're still fetching it.
3101+
fully_drawn = false;
3102+
}
3103+
MediaCacheEntry::Failed => {
3104+
text_or_image_ref
3105+
.show_text(format!("{body}\n\nFailed to fetch image from {:?}", mxc_uri));
3106+
// For now, we consider this as being "complete". In the future, we could support
3107+
// retrying to fetch thumbnail of the image on a user click/tap.
3108+
fully_drawn = true;
3109+
}
3110+
};
3111+
3112+
let mut fetch_and_show_media_source = |media_source: MediaSource| {
3113+
match media_source {
3114+
MediaSource::Encrypted(encrypted) => {
3115+
// We consider this as "fully drawn" since we don't yet support encryption.
3116+
text_or_image_ref.show_text(format!(
3117+
"{body}\n\n[TODO] fetch encrypted image at {:?}",
3118+
encrypted.url
3119+
));
3120+
},
3121+
MediaSource::Plain(mxc_uri) => {
3122+
fetch_and_show_image_uri(mxc_uri)
31093123
}
31103124
}
3111-
Some(MediaSource::Encrypted(encrypted)) => {
3112-
text_or_image_ref.show_text(format!(
3113-
"{body}\n\n[TODO] fetch encrypted image at {:?}",
3114-
encrypted.url
3115-
));
3116-
// We consider this as "fully drawn" since we don't yet support encryption.
3117-
true
3118-
}
3125+
};
3126+
3127+
match image_info_source {
3128+
Some((None, media_source)) => {
3129+
// We fetch the original (full-size) media if it does not have a thumbnail.
3130+
fetch_and_show_media_source(media_source);
3131+
},
3132+
Some((Some(image_info), media_source)) => {
3133+
if let Some(thumbnail_source) = image_info.thumbnail_source {
3134+
fetch_and_show_media_source(thumbnail_source);
3135+
} else {
3136+
fetch_and_show_media_source(media_source);
3137+
}
3138+
},
31193139
None => {
31203140
text_or_image_ref.show_text("{body}\n\nImage message had no source URL.");
3121-
true
3141+
fully_drawn = true;
31223142
}
3123-
31243143
}
3144+
3145+
fully_drawn
31253146
}
31263147

31273148

0 commit comments

Comments
 (0)