From 03818e642a1ae871bffdc0c39c10f05f0b8b0398 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 5 May 2020 09:07:50 +0100 Subject: [PATCH] FEATURE: Include optimized thumbnails for topics (#9215) This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this: - Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact - A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake - Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued - Topic lists and topics now include a `thumbnails` key, which includes all the available images: ``` "thumbnails": [ { "max_width": null, "max_height": null, "url": "//example.com/original-image.png", "width": 1380, "height": 1840 }, { "max_width": 1024, "max_height": 1024, "url": "//example.com/optimized-image.png", "width": 768, "height": 1024 } ] ``` - Themes can request additional thumbnail sizes by using a modifier in their `about.json` file: ``` "modifiers": { "topic_thumbnail_sizes": [ [200, 200], [800, 800] ], ... ``` Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated - Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link` --- .../app/templates/list/topic-list-item.hbr | 3 + .../templates/mobile/list/topic-list-item.hbr | 2 + app/jobs/regular/generate_topic_thumbnails.rb | 18 ++++ app/jobs/scheduled/ensure_db_consistency.rb | 3 +- app/models/post.rb | 8 ++ app/models/theme_modifier_set.rb | 35 +++++-- app/models/topic.rb | 82 ++++++++++++++++ app/models/topic_thumbnail.rb | 60 ++++++++++++ app/models/upload.rb | 1 + app/serializers/listable_topic_serializer.rb | 12 ++- app/serializers/topic_view_serializer.rb | 6 ++ ...9095034_add_topic_thumbnail_information.rb | 19 ++++ ...35_migrate_image_url_to_image_upload_id.rb | 95 +++++++++++++++++++ lib/cooked_post_processor.rb | 26 +++-- lib/search.rb | 2 +- lib/topic_query.rb | 6 ++ spec/components/cooked_post_processor_spec.rb | 43 ++++++--- spec/components/search_spec.rb | 4 +- spec/components/topic_view_spec.rb | 21 ++-- spec/fabricators/post_fabricator.rb | 2 +- spec/integration/topic_thumbnail_spec.rb | 88 +++++++++++++++++ spec/lib/db_helper_spec.rb | 9 -- spec/models/topic_thumbnail_spec.rb | 46 +++++++++ .../serializers/topic_view_serializer_spec.rb | 28 +++++- .../web_hook_topic_view_serializer_spec.rb | 1 + 25 files changed, 563 insertions(+), 57 deletions(-) create mode 100644 app/jobs/regular/generate_topic_thumbnails.rb create mode 100644 app/models/topic_thumbnail.rb create mode 100644 db/migrate/20200429095034_add_topic_thumbnail_information.rb create mode 100644 db/migrate/20200429095035_migrate_image_url_to_image_upload_id.rb create mode 100644 spec/integration/topic_thumbnail_spec.rb create mode 100644 spec/models/topic_thumbnail_spec.rb diff --git a/app/assets/javascripts/discourse/app/templates/list/topic-list-item.hbr b/app/assets/javascripts/discourse/app/templates/list/topic-list-item.hbr index ab05872ece0..03ea153bb09 100644 --- a/app/assets/javascripts/discourse/app/templates/list/topic-list-item.hbr +++ b/app/assets/javascripts/discourse/app/templates/list/topic-list-item.hbr @@ -1,3 +1,5 @@ +{{~raw-plugin-outlet name="topic-list-before-columns"}} + {{#if bulkSelectEnabled}} @@ -12,6 +14,7 @@ at the end of the link, preventing it from line wrapping onto its own line. --}} + {{~raw-plugin-outlet name="topic-list-before-link"}} {{~raw-plugin-outlet name="topic-list-before-status"}} {{~raw "topic-status" topic=topic}} diff --git a/app/assets/javascripts/discourse/app/templates/mobile/list/topic-list-item.hbr b/app/assets/javascripts/discourse/app/templates/mobile/list/topic-list-item.hbr index c126de09242..6c4663b83db 100644 --- a/app/assets/javascripts/discourse/app/templates/mobile/list/topic-list-item.hbr +++ b/app/assets/javascripts/discourse/app/templates/mobile/list/topic-list-item.hbr @@ -1,4 +1,5 @@ + {{~raw-plugin-outlet name="topic-list-before-columns"}} {{~#unless expandPinned}}
{{avatar topic.lastPosterUser imageSize="large"}} @@ -14,6 +15,7 @@ This causes the topic-post-badge to be considered the same word as "text" at the end of the link, preventing it from line wrapping onto its own line. --}} + {{~raw-plugin-outlet name="topic-list-before-link"}}