FIX: Link post to uploads in PostCreator.

* This ensures that uploads are linked to their post on creation
  instead of a background job which may be delayed if Sidekiq
  is facing difficulties.
This commit is contained in:
Guo Xiang Tan
2018-09-06 09:58:01 +08:00
parent 5baecffb0d
commit 434035f167
8 changed files with 165 additions and 84 deletions

View File

@@ -4,18 +4,29 @@ require "cooked_post_processor"
describe CookedPostProcessor do
context ".post_process" do
let(:upload) do
Fabricate(:upload,
url: '/uploads/default/original/1X/1/1234567890123456.jpg'
)
end
let(:post) do
Fabricate(:post, raw: <<~RAW)
<img src="#{upload.url}">
RAW
end
let(:post) { build(:post) }
let(:cpp) { CookedPostProcessor.new(post) }
let(:post_process) { sequence("post_process") }
it "post process in sequence" do
cpp.expects(:post_process_oneboxes).in_sequence(post_process)
cpp.expects(:post_process_images).in_sequence(post_process)
cpp.expects(:keep_reverse_index_up_to_date).in_sequence(post_process)
cpp.expects(:optimize_urls).in_sequence(post_process)
cpp.expects(:pull_hotlinked_images).in_sequence(post_process)
cpp.post_process
expect(PostUpload.exists?(post: post, upload: upload)).to eq(true)
end
end
@@ -40,52 +51,6 @@ describe CookedPostProcessor do
end
end
context ".keep_reverse_index_up_to_date" do
let(:video_upload) { Fabricate(:upload, url: '/uploads/default/original/1X/1/1234567890123456.mp4') }
let(:image_upload) { Fabricate(:upload, url: '/uploads/default/original/1X/1/1234567890123456.jpg') }
let(:audio_upload) { Fabricate(:upload, url: '/uploads/default/original/1X/1/1234567890123456.ogg') }
let(:attachment_upload) { Fabricate(:upload, url: '/uploads/default/original/1X/1/1234567890123456.csv') }
let(:raw) do
<<~RAW
<a href="#{attachment_upload.url}">Link</a>
<img src="#{image_upload.url}">
<video width="100%" height="100%" controls>
<source src="http://myforum.com#{video_upload.url}">
<a href="http://myforum.com#{video_upload.url}">http://myforum.com#{video_upload.url}</a>
</video>
<audio controls>
<source src="http://myforum.com#{audio_upload.url}">
<a href="http://myforum.com#{audio_upload.url}">http://myforum.com#{audio_upload.url}</a>
</audio>
RAW
end
let(:post) { Fabricate(:post, raw: raw) }
let(:cpp) { CookedPostProcessor.new(post) }
it "finds all the uploads in the post" do
cpp.keep_reverse_index_up_to_date
expect(PostUpload.where(post: post).map(&:upload_id).sort).to eq(
[video_upload.id, image_upload.id, audio_upload.id, attachment_upload.id].sort
)
end
it "cleans the reverse index up for the current post" do
cpp.keep_reverse_index_up_to_date
post_uploads_ids = post.post_uploads.pluck(:id)
cpp.keep_reverse_index_up_to_date
expect(post.reload.post_uploads.pluck(:id)).to_not eq(post_uploads_ids)
end
end
context ".post_process_images" do
shared_examples "leave dimensions alone" do

View File

@@ -186,11 +186,26 @@ describe PostCreator do
end
it 'queues up post processing job when saved' do
Jobs.expects(:enqueue).with(:feature_topic_users, has_key(:topic_id))
Jobs.expects(:enqueue).with(:process_post, has_key(:post_id))
Jobs.expects(:enqueue).with(:post_alert, has_key(:post_id))
Jobs.expects(:enqueue).with(:notify_mailing_list_subscribers, has_key(:post_id))
creator.create
post = Post.last
post_id = post.id
topic_id = post.topic_id
process_post_args = Jobs::ProcessPost.jobs.first["args"].first
expect(process_post_args["skip_link_post_uploads"]).to eq(true)
expect(process_post_args["post_id"]).to eq(post_id)
feature_topic_users_args = Jobs::FeatureTopicUsers.jobs.first["args"].first
expect(feature_topic_users_args["topic_id"]).to eq(topic_id)
post_alert_args = Jobs::PostAlert.jobs.first["args"].first
expect(post_alert_args["post_id"]).to eq(post_id)
notify_mailing_list_subscribers_args =
Jobs::NotifyMailingListSubscribers.jobs.first["args"].first
expect(notify_mailing_list_subscribers_args["post_id"]).to eq(post_id)
end
it 'passes the invalidate_oneboxes along to the job if present' do