mirror of
https://github.com/discourse/discourse.git
synced 2025-06-03 19:39:30 +08:00
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`
This commit is contained in:
@ -751,16 +751,16 @@ describe CookedPostProcessor do
|
||||
end
|
||||
|
||||
context "topic image" do
|
||||
let(:post) { Fabricate(:post_with_uploaded_image) }
|
||||
fab!(:post) { Fabricate(:post_with_uploaded_image) }
|
||||
let(:cpp) { CookedPostProcessor.new(post) }
|
||||
|
||||
it "adds a topic image if there's one in the first post" do
|
||||
FastImage.stubs(:size)
|
||||
expect(post.topic.image_url).to eq(nil)
|
||||
expect(post.topic.image_upload_id).to eq(nil)
|
||||
|
||||
cpp.post_process
|
||||
post.topic.reload
|
||||
expect(post.topic.image_url).to be_present
|
||||
expect(post.topic.image_upload_id).to be_present
|
||||
end
|
||||
|
||||
it "removes image if post is edited and no longer has an image" do
|
||||
@ -768,14 +768,14 @@ describe CookedPostProcessor do
|
||||
|
||||
cpp.post_process
|
||||
post.topic.reload
|
||||
expect(post.topic.image_url).to be_present
|
||||
expect(post.image_url).to be_present
|
||||
expect(post.topic.image_upload_id).to be_present
|
||||
expect(post.image_upload_id).to be_present
|
||||
|
||||
post.update!(raw: "This post no longer has an image.")
|
||||
CookedPostProcessor.new(post).post_process
|
||||
post.topic.reload
|
||||
expect(post.topic.image_url).not_to be_present
|
||||
expect(post.image_url).not_to be_present
|
||||
expect(post.topic.image_upload_id).not_to be_present
|
||||
expect(post.image_upload_id).not_to be_present
|
||||
end
|
||||
|
||||
it "won't remove the original image if another post doesn't have an image" do
|
||||
@ -784,15 +784,32 @@ describe CookedPostProcessor do
|
||||
|
||||
cpp.post_process
|
||||
topic.reload
|
||||
expect(topic.image_url).to be_present
|
||||
expect(post.image_url).to be_present
|
||||
expect(topic.image_upload_id).to be_present
|
||||
expect(post.image_upload_id).to be_present
|
||||
|
||||
post = Fabricate(:post, topic: topic, raw: "this post doesn't have an image")
|
||||
CookedPostProcessor.new(post).post_process
|
||||
topic.reload
|
||||
|
||||
expect(post.topic.image_url).to be_present
|
||||
expect(post.image_url).to be_blank
|
||||
expect(post.topic.image_upload_id).to be_present
|
||||
expect(post.image_upload_id).to be_blank
|
||||
end
|
||||
|
||||
it "generates thumbnails correctly" do
|
||||
FastImage.expects(:size).returns([1750, 2000])
|
||||
|
||||
topic = post.topic
|
||||
cpp.post_process
|
||||
topic.reload
|
||||
expect(topic.image_upload_id).to be_present
|
||||
expect(post.image_upload_id).to be_present
|
||||
|
||||
post = Fabricate(:post, topic: topic, raw: "this post doesn't have an image")
|
||||
CookedPostProcessor.new(post).post_process
|
||||
topic.reload
|
||||
|
||||
expect(post.topic.image_upload_id).to be_present
|
||||
expect(post.image_upload_id).to be_blank
|
||||
end
|
||||
end
|
||||
|
||||
@ -802,10 +819,10 @@ describe CookedPostProcessor do
|
||||
|
||||
it "adds a post image if there's one in the post" do
|
||||
FastImage.stubs(:size)
|
||||
expect(reply.image_url).to eq(nil)
|
||||
expect(reply.image_upload_id).to eq(nil)
|
||||
cpp.post_process
|
||||
reply.reload
|
||||
expect(reply.image_url).to be_present
|
||||
expect(reply.image_upload_id).to be_present
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1134,13 +1134,11 @@ describe Search do
|
||||
|
||||
it 'can find posts with images' do
|
||||
post_uploaded = Fabricate(:post_with_uploaded_image)
|
||||
post_with_image_urls = Fabricate(:post_with_image_urls)
|
||||
Fabricate(:post)
|
||||
|
||||
CookedPostProcessor.new(post_uploaded).update_post_image
|
||||
CookedPostProcessor.new(post_with_image_urls).update_post_image
|
||||
|
||||
expect(Search.execute('with:images').posts.map(&:id)).to contain_exactly(post_uploaded.id, post_with_image_urls.id)
|
||||
expect(Search.execute('with:images').posts.map(&:id)).to contain_exactly(post_uploaded.id)
|
||||
end
|
||||
|
||||
it 'can find by latest' do
|
||||
|
@ -708,9 +708,12 @@ describe TopicView do
|
||||
end
|
||||
|
||||
describe '#image_url' do
|
||||
let!(:post1) { Fabricate(:post, topic: topic) }
|
||||
let!(:post2) { Fabricate(:post, topic: topic) }
|
||||
let!(:post3) { Fabricate(:post, topic: topic).tap { |p| p.update_column(:image_url, "post3_image.png") }.reload }
|
||||
fab!(:op_upload) { Fabricate(:image_upload) }
|
||||
fab!(:post3_upload) { Fabricate(:image_upload) }
|
||||
|
||||
fab!(:post1) { Fabricate(:post, topic: topic) }
|
||||
fab!(:post2) { Fabricate(:post, topic: topic) }
|
||||
fab!(:post3) { Fabricate(:post, topic: topic).tap { |p| p.update_column(:image_upload_id, post3_upload.id) }.reload }
|
||||
|
||||
def topic_view_for_post(post_number)
|
||||
TopicView.new(topic.id, evil_trout, post_number: post_number)
|
||||
@ -718,14 +721,14 @@ describe TopicView do
|
||||
|
||||
context "when op has an image" do
|
||||
before do
|
||||
topic.update_column(:image_url, "op_image.png")
|
||||
post1.update_column(:image_url, "op_image.png")
|
||||
topic.update_column(:image_upload_id, op_upload.id)
|
||||
post1.update_column(:image_upload_id, op_upload.id)
|
||||
end
|
||||
|
||||
it "uses the topic image as a fallback when posts have no image" do
|
||||
expect(topic_view_for_post(1).image_url).to eq("op_image.png")
|
||||
expect(topic_view_for_post(2).image_url).to eq("op_image.png")
|
||||
expect(topic_view_for_post(3).image_url).to eq("post3_image.png")
|
||||
expect(topic_view_for_post(1).image_url).to eq(op_upload.url)
|
||||
expect(topic_view_for_post(2).image_url).to eq(op_upload.url)
|
||||
expect(topic_view_for_post(3).image_url).to eq(post3_upload.url)
|
||||
end
|
||||
end
|
||||
|
||||
@ -733,7 +736,7 @@ describe TopicView do
|
||||
it "returns nil when posts have no image" do
|
||||
expect(topic_view_for_post(1).image_url).to eq(nil)
|
||||
expect(topic_view_for_post(2).image_url).to eq(nil)
|
||||
expect(topic_view_for_post(3).image_url).to eq("post3_image.png")
|
||||
expect(topic_view_for_post(3).image_url).to eq(post3_upload.url)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user