mirror of
https://github.com/discourse/discourse.git
synced 2025-03-25 09:45:51 +08:00
Merge pull request #1034 from chrishunt/rebake-oneboxes
Optionally rebake oneboxes with posts:rebake task
This commit is contained in:
commit
70683c1f09
@ -20,7 +20,8 @@ class PostAnalyzer
|
|||||||
# wait for the post processor.
|
# wait for the post processor.
|
||||||
dirty = false
|
dirty = false
|
||||||
result = Oneboxer.apply(cooked) do |url, elem|
|
result = Oneboxer.apply(cooked) do |url, elem|
|
||||||
Oneboxer.render_from_cache(url)
|
Oneboxer.invalidate(url) if args.last[:invalidate_oneboxes]
|
||||||
|
Oneboxer.onebox url
|
||||||
end
|
end
|
||||||
|
|
||||||
cooked = result.to_html if result.changed?
|
cooked = result.to_html if result.changed?
|
||||||
|
@ -1,25 +1,41 @@
|
|||||||
desc "walk all posts updating cooked with latest markdown"
|
desc 'Update each post with latest markdown'
|
||||||
task "posts:rebake" => :environment do
|
task 'posts:rebake' => :environment do
|
||||||
|
rebake_posts
|
||||||
|
end
|
||||||
|
|
||||||
|
desc 'Update each post with latest markdown and refresh oneboxes'
|
||||||
|
task 'posts:refresh_oneboxes' => :environment do
|
||||||
|
rebake_posts invalidate_oneboxes: true
|
||||||
|
end
|
||||||
|
|
||||||
|
def rebake_posts(opts = {})
|
||||||
RailsMultisite::ConnectionManagement.each_connection do |db|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
||||||
puts "Re baking post markdown for #{db} , changes are denoted with # , no change with ."
|
puts "Re baking post markdown for #{db} , changes are denoted with # , no change with ."
|
||||||
i = 0
|
|
||||||
Post.select([:id, :user_id, :cooked, :raw, :topic_id, :post_number]).each do |p|
|
total = Post.select([
|
||||||
i += 1
|
:id, :user_id, :cooked, :raw, :topic_id, :post_number
|
||||||
cooked = p.cook(p.raw, topic_id: p.topic_id)
|
]).inject(0) do |total, post|
|
||||||
if cooked != p.cooked
|
cooked = post.cook(
|
||||||
Post.exec_sql('update posts set cooked = ? where id = ?', cooked, p.id)
|
post.raw,
|
||||||
p.cooked = cooked
|
topic_id: post.topic_id,
|
||||||
|
invalidate_oneboxes: opts.fetch(:invalidate_oneboxes, false)
|
||||||
|
)
|
||||||
|
|
||||||
|
if cooked != post.cooked
|
||||||
|
Post.exec_sql(
|
||||||
|
'update posts set cooked = ? where id = ?', cooked, post.id
|
||||||
|
)
|
||||||
|
post.cooked = cooked
|
||||||
putc "#"
|
putc "#"
|
||||||
else
|
else
|
||||||
putc "."
|
putc "."
|
||||||
end
|
end
|
||||||
TopicLink.extract_from(p)
|
|
||||||
end
|
|
||||||
puts
|
|
||||||
puts
|
|
||||||
puts "#{i} posts done!"
|
|
||||||
puts "-----------------------------------------------"
|
|
||||||
puts
|
|
||||||
|
|
||||||
|
TopicLink.extract_from post
|
||||||
|
|
||||||
|
total += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "\n\n#{total} posts done!\n#{'-' * 50}\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -2,10 +2,39 @@ require 'spec_helper'
|
|||||||
|
|
||||||
describe PostAnalyzer do
|
describe PostAnalyzer do
|
||||||
|
|
||||||
let(:topic) { Fabricate(:topic) }
|
let(:default_topic_id) { 12 }
|
||||||
let(:default_topic_id) { topic.id }
|
|
||||||
let(:post_args) do
|
describe '#cook' do
|
||||||
{user: topic.user, topic: topic}
|
let(:post_analyzer) { described_class.new nil, nil }
|
||||||
|
|
||||||
|
let(:args) { [raw, options] }
|
||||||
|
let(:raw) { "Here's a tweet:\n#{url}" }
|
||||||
|
let(:options) { {} }
|
||||||
|
|
||||||
|
let(:url) {
|
||||||
|
'https://twitter.com/evil_trout/status/345954894420787200'
|
||||||
|
}
|
||||||
|
|
||||||
|
before { Oneboxer.stubs(:onebox) }
|
||||||
|
|
||||||
|
it 'fetches the onebox for any urls in the post' do
|
||||||
|
Oneboxer.expects(:onebox).with url
|
||||||
|
post_analyzer.cook(*args)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not invalidate the onebox cache' do
|
||||||
|
Oneboxer.expects(:invalidate).with(url).never
|
||||||
|
post_analyzer.cook(*args)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when invalidating oneboxes' do
|
||||||
|
let(:options) {{ invalidate_oneboxes: true }}
|
||||||
|
|
||||||
|
it 'invalidates the oneboxes for urls in the post' do
|
||||||
|
Oneboxer.expects(:invalidate).with url
|
||||||
|
post_analyzer.cook(*args)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "links" do
|
context "links" do
|
||||||
@ -115,6 +144,7 @@ describe PostAnalyzer do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "finds links from markdown" do
|
it "finds links from markdown" do
|
||||||
|
Oneboxer.stubs :onebox
|
||||||
post_analyzer = PostAnalyzer.new(raw_post_one_link_md, default_topic_id)
|
post_analyzer = PostAnalyzer.new(raw_post_one_link_md, default_topic_id)
|
||||||
post_analyzer.link_count.should == 1
|
post_analyzer.link_count.should == 1
|
||||||
end
|
end
|
||||||
|
@ -2,6 +2,8 @@ require 'spec_helper'
|
|||||||
require_dependency 'post_destroyer'
|
require_dependency 'post_destroyer'
|
||||||
|
|
||||||
describe Post do
|
describe Post do
|
||||||
|
before { Oneboxer.stubs :onebox }
|
||||||
|
|
||||||
# Help us build a post with a raw body
|
# Help us build a post with a raw body
|
||||||
def post_with_body(body, user=nil)
|
def post_with_body(body, user=nil)
|
||||||
args = post_args.merge(raw: body)
|
args = post_args.merge(raw: body)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user