-
Notifications
You must be signed in to change notification settings - Fork 19
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
feat: generate multi-image thumbnails for multi-image tweets #373
Merged
pajlada
merged 6 commits into
Chatterino:master
from
leon-richardt:feat/twitter/image-collage
Oct 15, 2022
Merged
feat: generate multi-image thumbnails for multi-image tweets #373
pajlada
merged 6 commits into
Chatterino:master
from
leon-richardt:feat/twitter/image-collage
Oct 15, 2022
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This will be needed for the next commit. Different components may need to know the cache key format of a specific cache. Without extracting the functionality into an own interface, users can run into a circular dependency. (This happened to me.) Split into its own commit to allow for easier reviewing.
leon-richardt
force-pushed
the
feat/twitter/image-collage
branch
from
October 15, 2022 11:59
5d991cd
to
cea016d
Compare
leon-richardt
changed the title
Feat/twitter/image collage
feat: generate multi-image thumbnail for multi-image tweets
Oct 15, 2022
Codecov Report
@@ Coverage Diff @@
## master #373 +/- ##
==========================================
- Coverage 48.74% 45.97% -2.78%
==========================================
Files 98 99 +1
Lines 3159 3589 +430
==========================================
+ Hits 1540 1650 +110
- Misses 1576 1893 +317
- Partials 43 46 +3
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Split into its own commit for easier reviewing. See PR description for more context.
Various formatting changes from my autoformatter. Extracted into its own commit for easier reviewing.
leon-richardt
force-pushed
the
feat/twitter/image-collage
branch
from
October 15, 2022 12:08
cea016d
to
8dd5797
Compare
pajlada
requested changes
Oct 15, 2022
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.
Only nitpick I found during code review, will give it a test as well
leon-richardt
changed the title
feat: generate multi-image thumbnail for multi-image tweets
feat: generate multi-image thumbnails for multi-image tweets
Oct 15, 2022
pajlada
approved these changes
Oct 15, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull request checklist:
CHANGELOG.md
was updated, if applicableFeature Description
This PR closes #347.
For tweets with multiple images, link preview thumbnails no longer just display the first image but a collage of all images. The images are arranged in a 2×2 grid.
Reading Twitter's API documentation, I would have expected to receive thumbnail URLs for attached videos as well but it seems that these are not returned from the endpoint. Thus, no thumbnails for videos will be included in the collage. Maybe we will be able to change this with the v2 endpoint.
Design Decisions
Implementing this feature required some larger-scale design decisions. I will describe and justify these decisions in this section.
Serving Generated Values
Previous to this PR, thumbnails served by the Chatterino API referred to an image resource identified by some URL. The only "contribution" of the API was scaling the image down to the dimensions specified in the configuration file. With this PR, we want to serve an image that does not exist anywhere else: a collage of multiple images. In my opinion, this did not fit into the existing architecture very well.
The solution I opted for consists of introducing an additional route:
generated/
. This route serves data that has been generated by the Chatterino API itself and does not directly reference resources elsewhere.cache.DependentCache
To implement the above, I defined a new type of cache:
cache.DependentCache
. ADependentCache
stores[]byte
values under a key. However, this key is directly associated with a parent key in a regularcache.Cache
. When the parent key is deleted, all child keys in registered dependent caches are deleted as well.Currently, the only
DependentCache
implementation is PostgreSQL-based. I decided not to implement the interface for the in-memory cache since it is currently not used anywhere in the code, and I would rather not spend time implementing the interface if it ends up never being used. If you require the interface for an in-memory cache as well, let me know and I may be able to provide assistance in implementing it.Commit/Rollback Mechanism
Child keys may be inserted into the dependent cache before their parents exist. (This is the case for collages, for example.) If an error occurs before the parent key is inserted in the parent cache, the child keys may be orphaned. This would mean they would never get cleaned up. The commit/rollback mechanism makes sure that child keys only remain in the cache if the parent is successfully inserted.
Additionally, a mechanism has been implemented that cleans up any values in dependent caches that have been inserted longer than 24 hours ago. This is meant as fail-safe to prevent caller errors from clogging up the database.
Collage Layout
Ideally, I would have liked to use
vips_join()
for the collage. However, at the point of writing, govips does not support all parameters of the C library in its binding. Thus, it is not much better than usingImageRef.ArrayJoin()
. This has the effect that tweets with three images lead to somewhat ugly thumbnails. In a future govips version, usingImageRef.Join()
could yield a nice QoL improvement.Review Notes
I hope the random formatting changes sprinkled throughout are okay; they were introduced by my autoformatter.
Points a reviewer can focus on:
dependent_values
table and thePostgreSQLDependentCache
overall?TweetLoader
?Testing
Make sure you provide a valid Twitter API token.
Tweets to test this with:
Thank you!