-
Notifications
You must be signed in to change notification settings - Fork 218
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
Conversation
116193a
to
c2152ad
Compare
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>
c2152ad
to
01fe95a
Compare
Codecov ReportAttention: Patch coverage is
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. |
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.
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.
crates/matrix-sdk/src/attachment.rs
Outdated
let content_type = mime::Mime::from_str(image_format.to_mime_type()) | ||
.expect("image should give a valid mimetype"); |
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.
Can we avoid the expect
and return an error instead?
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.
Changed
data_slot = thumbnail_data; | ||
Some(Thumbnail { | ||
data: data_slot, | ||
content_type: mime::IMAGE_JPEG, |
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.
Nice catch. Is it worth adding a regression test, or too much hassle?
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 added tests that check this.
crates/matrix-sdk/CHANGELOG.md
Outdated
@@ -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. |
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.
nit: now, not know
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.
Right, thanks
crates/matrix-sdk/src/attachment.rs
Outdated
let thumbnail_format = match format { | ||
ThumbnailFormat::Always(format) => format, | ||
ThumbnailFormat::Fallback(format) if !image_format.writing_enabled() => format, | ||
_ => image_format, |
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.
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?
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.
Changed
Err(error) => { | ||
tracing::error!("Failed to generate thumbnail: {error}"); | ||
None | ||
} |
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 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?
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 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.
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.
Ok. Would it make sense to log the other error at the warn level, though?
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.
Yes, definitely.
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.
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>
I took care of the reviews, merged 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 |
Nice 🦀. |
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.
Thanks for the tests!
.await | ||
.unwrap(); | ||
|
||
assert_eq!(event_id!("$h29iv0s8:example.com"), response.event_id) |
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.
Here and in the 3 other tests: can we replace the raw event_id
with test_json::EVENT_ID
too?
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.
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.
testing/matrix-sdk-test/src/lib.rs
Outdated
@@ -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() |
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.
Can we revert this change? wiremock can be verbose in the output…
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.
Ah right sorry. I had enabled it locally to understand why an event wasn't matching for an endpoint.
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.
Undone.
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
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.