-
Notifications
You must be signed in to change notification settings - Fork 34
Display link previews beneath a message containing a link #556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Add url-preview dependency to Cargo.toml - Create new LinkPreviewCard widget in src/room/link_preview_card.rs - Integrate LinkPreviewCard into HTML message rendering system - Add link preview state management and async fetching support - Update RobrixHtmlLink to display preview cards below links - Add URL preview service initialization in sliding_sync module - Extend MatrixRequest enum with GetLinkPreviewDetails variant
- Replace url_preview::Preview with custom LinkPreviewData struct - Add support for fetching and displaying preview images - Integrate image loading using utils::load_png_or_jpg - Add separate image view container for preview images - Update async worker to fetch images from preview URLs - Validate image types (PNG/JPEG) before processing - Remove unused IMG_APP_LOGO dependency from link preview card - Improve error handling for image fetch operations
- Add LinkPreviewCard widget to room message display layout - Implement link extraction with domain filtering (matrix.to, matrix.io) - Add click handling to open URLs using robius_open - Simplify LinkPreviewData structure (remove unused fields) - Move link preview functionality from HTML links to message level - Add proper state management with equality checks to prevent redundant updates - Include linkify dependency for URL detection in message content - Add populate_link_preview_card_content function for message processing
- Integrate LinkPreviewCard widget into Message and CondensedMessage layouts - Add populate_link_preview_card_content import for handling link preview data - Position link preview cards below message content with consistent margins - Support link preview display for both regular and condensed message views
Best aspect to use for image preview is 1.91:1. It should support cropping or least the image looks squashed. |
yep, i will try. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
match client.http_client() | ||
.get(preview_image_url) | ||
.send() | ||
.await | ||
.map_err(|e| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Link preview got nothing to do with Matrix Request. In addition, you are using Matrix HTTP Client. You should be using cx.http_request
. I don't think it is a good idea to abuse submit_async_request(MatrixRequest) just to spawn a new thread.
It is not optimal, because you are sending message to a channel before spawning new thread. cx.spawn_thread
will do so directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correction - there is no need to spawn new thread. using cx.http_request.
let image = self.image(id!(link_thumbnail_preview_view.image)); | ||
let _ = utils::load_png_or_jpg(&image, cx, raw_image); | ||
link_thumbnail_preview_view.set_visible(cx, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need to preserve the drawn state. utils::load_png_or_jpg is operation intensive to be operated in draw_walk without checking if it is drawn.
}) { | ||
Ok(response) => { | ||
let raw_data = response.bytes().await.unwrap().to_vec(); | ||
match imghdr::from_bytes(&raw_data) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imghdr::from_bytes is being called twice. Once here, and utils::load_png_or_jpg. Use response.headers().get("content-type") to check if it is an image.
Ok(response) => { | ||
let raw_data = response.bytes().await.unwrap().to_vec(); | ||
match imghdr::from_bytes(&raw_data) { | ||
Some(imghdr::Type::Png) | Some(imghdr::Type::Jpeg) => Some(Arc::new(raw_data)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need for Arc, as you are not Cx::post_action(image.clone()) multiple times.
fn handle_event(&mut self, cx: &mut Cx, event: &Event, _scope: &mut Scope) { | ||
if let Event::Actions(actions) = event { | ||
for action in actions { | ||
if let Some(loaded @ LinkPreviewCardState::Loaded { url, .. }) = action.downcast_ref() { | ||
if self.url == *url && self.state != *loaded { | ||
self.state = loaded.clone(); | ||
self.redraw(cx); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than handling LinkPreviewCardState::Loaded action, use MatchEvent's handle_network_responses to handle the url's html and the og:image response.
|
||
impl LinkPreviewCard { | ||
pub fn set_card_info(&mut self, cx: &mut Cx, url: String) { | ||
if self.url != url { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String comparison can be expensive, usually we do LiveId / RoomId / UserId comparison.
Fixes #81
PR content
Display link previews beneath a message containing a link.
Basic style:
Related pr and issue