Skip to content

Commit 70683c1

Browse files
committed
Merge pull request discourse#1034 from chrishunt/rebake-oneboxes
Optionally rebake oneboxes with posts:rebake task
2 parents 14db386 + 9565843 commit 70683c1

File tree

4 files changed

+69
-20
lines changed

4 files changed

+69
-20
lines changed

app/models/post_analyzer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def cook(*args)
2020
# wait for the post processor.
2121
dirty = false
2222
result = Oneboxer.apply(cooked) do |url, elem|
23-
Oneboxer.render_from_cache(url)
23+
Oneboxer.invalidate(url) if args.last[:invalidate_oneboxes]
24+
Oneboxer.onebox url
2425
end
2526

2627
cooked = result.to_html if result.changed?

lib/tasks/posts.rake

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
1-
desc "walk all posts updating cooked with latest markdown"
2-
task "posts:rebake" => :environment do
1+
desc 'Update each post with latest markdown'
2+
task 'posts:rebake' => :environment do
3+
rebake_posts
4+
end
5+
6+
desc 'Update each post with latest markdown and refresh oneboxes'
7+
task 'posts:refresh_oneboxes' => :environment do
8+
rebake_posts invalidate_oneboxes: true
9+
end
10+
11+
def rebake_posts(opts = {})
312
RailsMultisite::ConnectionManagement.each_connection do |db|
413
puts "Re baking post markdown for #{db} , changes are denoted with # , no change with ."
5-
i = 0
6-
Post.select([:id, :user_id, :cooked, :raw, :topic_id, :post_number]).each do |p|
7-
i += 1
8-
cooked = p.cook(p.raw, topic_id: p.topic_id)
9-
if cooked != p.cooked
10-
Post.exec_sql('update posts set cooked = ? where id = ?', cooked, p.id)
11-
p.cooked = cooked
14+
15+
total = Post.select([
16+
:id, :user_id, :cooked, :raw, :topic_id, :post_number
17+
]).inject(0) do |total, post|
18+
cooked = post.cook(
19+
post.raw,
20+
topic_id: post.topic_id,
21+
invalidate_oneboxes: opts.fetch(:invalidate_oneboxes, false)
22+
)
23+
24+
if cooked != post.cooked
25+
Post.exec_sql(
26+
'update posts set cooked = ? where id = ?', cooked, post.id
27+
)
28+
post.cooked = cooked
1229
putc "#"
1330
else
1431
putc "."
1532
end
16-
TopicLink.extract_from(p)
33+
34+
TopicLink.extract_from post
35+
36+
total += 1
1737
end
18-
puts
19-
puts
20-
puts "#{i} posts done!"
21-
puts "-----------------------------------------------"
22-
puts
2338

39+
puts "\n\n#{total} posts done!\n#{'-' * 50}\n"
2440
end
2541
end

spec/models/post_analyzer_spec.rb

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,39 @@
22

33
describe PostAnalyzer do
44

5-
let(:topic) { Fabricate(:topic) }
6-
let(:default_topic_id) { topic.id }
7-
let(:post_args) do
8-
{user: topic.user, topic: topic}
5+
let(:default_topic_id) { 12 }
6+
7+
describe '#cook' do
8+
let(:post_analyzer) { described_class.new nil, nil }
9+
10+
let(:args) { [raw, options] }
11+
let(:raw) { "Here's a tweet:\n#{url}" }
12+
let(:options) { {} }
13+
14+
let(:url) {
15+
'https://twitter.com/evil_trout/status/345954894420787200'
16+
}
17+
18+
before { Oneboxer.stubs(:onebox) }
19+
20+
it 'fetches the onebox for any urls in the post' do
21+
Oneboxer.expects(:onebox).with url
22+
post_analyzer.cook(*args)
23+
end
24+
25+
it 'does not invalidate the onebox cache' do
26+
Oneboxer.expects(:invalidate).with(url).never
27+
post_analyzer.cook(*args)
28+
end
29+
30+
context 'when invalidating oneboxes' do
31+
let(:options) {{ invalidate_oneboxes: true }}
32+
33+
it 'invalidates the oneboxes for urls in the post' do
34+
Oneboxer.expects(:invalidate).with url
35+
post_analyzer.cook(*args)
36+
end
37+
end
938
end
1039

1140
context "links" do
@@ -115,6 +144,7 @@
115144
end
116145

117146
it "finds links from markdown" do
147+
Oneboxer.stubs :onebox
118148
post_analyzer = PostAnalyzer.new(raw_post_one_link_md, default_topic_id)
119149
post_analyzer.link_count.should == 1
120150
end

spec/models/post_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
require_dependency 'post_destroyer'
33

44
describe Post do
5+
before { Oneboxer.stubs :onebox }
6+
57
# Help us build a post with a raw body
68
def post_with_body(body, user=nil)
79
args = post_args.merge(raw: body)

0 commit comments

Comments
 (0)