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`
2020-05-05 16:07:50 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe "Topic Thumbnails" do
|
|
|
|
before { SiteSetting.create_thumbnails = true }
|
|
|
|
|
|
|
|
fab!(:image) { Fabricate(:image_upload, width: 5000, height: 5000) }
|
|
|
|
fab!(:topic) { Fabricate(:topic, image_upload_id: image.id) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
context 'latest' do
|
|
|
|
def get_topic
|
2020-05-23 12:56:13 +08:00
|
|
|
Discourse.redis.del(topic.thumbnail_job_redis_key(Topic.thumbnail_sizes))
|
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`
2020-05-05 16:07:50 +08:00
|
|
|
get '/latest.json'
|
|
|
|
response.parsed_body["topic_list"]["topics"][0]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes thumbnails" do
|
|
|
|
topic_json = nil
|
|
|
|
expect do
|
|
|
|
topic_json = get_topic
|
|
|
|
end.to change { Jobs::GenerateTopicThumbnails.jobs.size }.by(1)
|
|
|
|
|
|
|
|
thumbnails = topic_json["thumbnails"]
|
|
|
|
|
|
|
|
# Original only. Optimized not yet generated
|
|
|
|
expect(thumbnails.length).to eq(1)
|
|
|
|
|
|
|
|
# Original
|
|
|
|
expect(thumbnails[0]["max_width"]).to eq(nil)
|
|
|
|
expect(thumbnails[0]["max_height"]).to eq(nil)
|
|
|
|
expect(thumbnails[0]["width"]).to eq(image.width)
|
|
|
|
expect(thumbnails[0]["height"]).to eq(image.height)
|
2020-05-15 20:35:20 +08:00
|
|
|
expect(thumbnails[0]["url"]).to end_with(image.url)
|
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`
2020-05-05 16:07:50 +08:00
|
|
|
|
|
|
|
# Run the job
|
|
|
|
args = Jobs::GenerateTopicThumbnails.jobs.last["args"].first
|
|
|
|
Jobs::GenerateTopicThumbnails.new.execute(args.with_indifferent_access)
|
|
|
|
|
|
|
|
# Re-request
|
|
|
|
expect do
|
|
|
|
topic_json = get_topic
|
|
|
|
end.to change { Jobs::GenerateTopicThumbnails.jobs.size }.by(0)
|
|
|
|
thumbnails = topic_json["thumbnails"]
|
|
|
|
|
|
|
|
expect(thumbnails[1]["max_width"]).to eq(Topic.share_thumbnail_size[0])
|
|
|
|
expect(thumbnails[1]["max_height"]).to eq(Topic.share_thumbnail_size[1])
|
|
|
|
expect(thumbnails[1]["width"]).to eq(1024)
|
|
|
|
expect(thumbnails[1]["height"]).to eq(1024)
|
|
|
|
expect(thumbnails[1]["url"]).to include("/optimized/")
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a theme" do
|
|
|
|
before do
|
|
|
|
theme = Fabricate(:theme)
|
|
|
|
theme.theme_modifier_set.topic_thumbnail_sizes = [
|
|
|
|
[100, 100],
|
|
|
|
[200, 200],
|
|
|
|
[300, 300]
|
|
|
|
]
|
|
|
|
theme.theme_modifier_set.save!
|
|
|
|
theme.set_default!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes the theme specified resolutions" do
|
|
|
|
topic_json = nil
|
|
|
|
|
|
|
|
expect do
|
|
|
|
topic_json = get_topic
|
|
|
|
end.to change { Jobs::GenerateTopicThumbnails.jobs.size }.by(1)
|
|
|
|
|
|
|
|
# Run the job
|
|
|
|
args = Jobs::GenerateTopicThumbnails.jobs.last["args"].first
|
|
|
|
Jobs::GenerateTopicThumbnails.new.execute(args.with_indifferent_access)
|
|
|
|
|
|
|
|
# Request again
|
|
|
|
expect do
|
|
|
|
topic_json = get_topic
|
|
|
|
end.to change { Jobs::GenerateTopicThumbnails.jobs.size }.by(0)
|
|
|
|
|
|
|
|
thumbnails = topic_json["thumbnails"]
|
|
|
|
|
|
|
|
# Original + Optimized + 3 theme requests
|
|
|
|
expect(thumbnails.length).to eq(5)
|
|
|
|
end
|
|
|
|
end
|
2020-05-23 12:56:13 +08:00
|
|
|
|
|
|
|
context "with a plugin" do
|
|
|
|
before do
|
|
|
|
plugin = Plugin::Instance.new
|
|
|
|
plugin.register_topic_thumbnail_size [512, 512]
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
DiscoursePluginRegistry.reset!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes the theme specified resolutions" do
|
|
|
|
topic_json = nil
|
|
|
|
|
|
|
|
expect do
|
|
|
|
topic_json = get_topic
|
|
|
|
end.to change { Jobs::GenerateTopicThumbnails.jobs.size }.by(1)
|
|
|
|
|
|
|
|
# Run the job
|
|
|
|
args = Jobs::GenerateTopicThumbnails.jobs.last["args"].first
|
|
|
|
Jobs::GenerateTopicThumbnails.new.execute(args.with_indifferent_access)
|
|
|
|
|
|
|
|
# Request again
|
|
|
|
expect do
|
|
|
|
topic_json = get_topic
|
|
|
|
end.to change { Jobs::GenerateTopicThumbnails.jobs.size }.by(0)
|
|
|
|
|
|
|
|
thumbnails = topic_json["thumbnails"]
|
|
|
|
|
|
|
|
# Original + Optimized + 1 plugin request
|
|
|
|
expect(thumbnails.length).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
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`
2020-05-05 16:07:50 +08:00
|
|
|
end
|
|
|
|
end
|