Skip to content
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

sdk: Improvements around generate_image_thumbnail #3415

Merged
merged 8 commits into from
May 17, 2024

Conversation

zecakeh
Copy link
Collaborator

@zecakeh zecakeh commented May 15, 2024

The main improvement here is to be able to choose the format of the generated thumbnail to avoid errors because the image crate cannot encode to the same format as the original.

We also allow to send the attachment even if the generation fails, because thumbnails are optional after all.

See commits messages for details.

  • Public API changes documented in changelogs (optional)

@zecakeh zecakeh requested a review from a team as a code owner May 15, 2024 11:33
@zecakeh zecakeh requested review from bnjbvr and removed request for a team May 15, 2024 11:33
We have already all the data for it.
Also fixes an error where the thumbnail format was assumed to always be
JPEG.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
Sending an attachment could often fail if the image crate
cannot encode the thumbnail to the same format as the original.
This allows to select a known supported format to always
be able to generate a thumbnail.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
Since the thumbnail is optional, failing to generate it should not
stop us from sending the attachment.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
Copy link

codecov bot commented May 15, 2024

Codecov Report

Attention: Patch coverage is 87.50000% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 83.16%. Comparing base (7ae0bce) to head (9c2b0a8).
Report is 9 commits behind head on main.

Files Patch % Lines
crates/matrix-sdk/src/room/futures.rs 75.00% 2 Missing ⚠️
crates/matrix-sdk/src/attachment.rs 93.75% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3415      +/-   ##
==========================================
+ Coverage   82.98%   83.16%   +0.17%     
==========================================
  Files         247      247              
  Lines       25019    25014       -5     
==========================================
+ Hits        20763    20802      +39     
+ Misses       4256     4212      -44     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet, that makes sense, thanks!

I think a few more tests might be nice to have, but not mandatory. I've got a few nit requests below that I'd like to see addressed before merging, though.

Comment on lines 397 to 398
let content_type = mime::Mime::from_str(image_format.to_mime_type())
.expect("image should give a valid mimetype");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid the expect and return an error instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

data_slot = thumbnail_data;
Some(Thumbnail {
data: data_slot,
content_type: mime::IMAGE_JPEG,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. Is it worth adding a regression test, or too much hassle?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added tests that check this.

@@ -12,6 +12,9 @@ Breaking changes:
- A custom sliding sync proxy set with `ClientBuilder::sliding_sync_proxy` now takes precedence over a discovered proxy.
- `Client::get_profile` was moved to `Account` and renamed to `Account::fetch_user_profile_of`. `Account::get_profile` was renamed to `Account::fetch_user_profile`.
- `generate_image_thumbnail` now returns a `Thumbnail`.
- It is know possible to select the format of a generated thumbnail.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: now, not know

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks

let thumbnail_format = match format {
ThumbnailFormat::Always(format) => format,
ThumbnailFormat::Fallback(format) if !image_format.writing_enabled() => format,
_ => image_format,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the explicit last variant instead of _ here, so that if we add a new variant to ThumbnailFormat, the compiler warns us here, and we can then decide what's the best thing to do?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

Err(error) => {
tracing::error!("Failed to generate thumbnail: {error}");
None
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this; can we remove the previous match arm that handles two ImageError variants, so they're also logged?

Also, can we use warn! instead of error!, since these are non-critical?

Copy link
Collaborator Author

@zecakeh zecakeh May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that ThumbnailBiggerThanOriginal should be logged, or at least not at the warn level.

It could allow the users to be lazy and always use generate_thumbnail without having to check the image's size. It is more an optimization of the code than a real error so it's not something users should care to fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Would it make sense to log the other error at the warn level, though?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, definitely.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
@zecakeh
Copy link
Collaborator Author

zecakeh commented May 16, 2024

I took care of the reviews, merged main to solve conflicts, and added integration tests.

For the integration tests I needed a real image so I took the avatar from #matrix-rust-sdk:matrix.org. It's also difficult to trigger the ImageFormat::Fallback(format) path because there are only 2 formats that are not writable and they generate really big images (~5MB for the same avatar).

@poljar
Copy link
Contributor

poljar commented May 16, 2024

For the integration tests I needed a real image so I took the avatar from #matrix-rust-sdk:matrix.org.

Nice 🦀.

@bnjbvr bnjbvr self-requested a review May 16, 2024 12:15
Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tests!

.await
.unwrap();

assert_eq!(event_id!("$h29iv0s8:example.com"), response.event_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and in the 3 other tests: can we replace the raw event_id with test_json::EVENT_ID too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_json::EVENT_ID is a serde_json::Value containing a JSON object, so we can't use it for asserting the event ID, afaiu.

We would probably need a matrix_sdk_test::DEFAULT_TEST_EVENT_ID, like we have for the room ID for that.

@@ -93,7 +93,7 @@ macro_rules! init_tracing_for_tests {
// Since tracing_subscriber does prefix matching, the `matrix_sdk=` directive
// takes effect for all the main crates (`matrix_sdk_base`, `matrix_sdk_crypto`
// and so on).
"info,matrix_sdk=debug".into()
"info,wiremock=debug,matrix_sdk=debug".into()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we revert this change? wiremock can be verbose in the output…

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right sorry. I had enabled it locally to understand why an event wasn't matching for an endpoint.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undone.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
@bnjbvr bnjbvr merged commit 6c18bcf into matrix-org:main May 17, 2024
36 checks passed
@zecakeh zecakeh deleted the generate-thumbnail branch May 17, 2024 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants