diff --git a/app/models/post.rb b/app/models/post.rb index 7a8ce9a5731..5863cc49c12 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -222,7 +222,13 @@ class Post < ActiveRecord::Base @post_analyzers[raw_hash] ||= PostAnalyzer.new(raw, topic_id) end - %w{raw_mentions linked_hosts image_count attachment_count link_count raw_links}.each do |attr| + %w{raw_mentions + linked_hosts + image_count + attachment_count + link_count + raw_links + has_oneboxes?}.each do |attr| define_method(attr) do post_analyzer.send(attr) end diff --git a/app/models/post_analyzer.rb b/app/models/post_analyzer.rb index 0d6c2120aef..919b7d42155 100644 --- a/app/models/post_analyzer.rb +++ b/app/models/post_analyzer.rb @@ -13,6 +13,13 @@ class PostAnalyzer @found_oneboxes end + def has_oneboxes? + return false unless @raw.present? + + cooked_stripped + found_oneboxes? + end + # What we use to cook posts def cook(raw, opts = {}) cook_method = opts[:cook_method] @@ -104,7 +111,6 @@ class PostAnalyzer return @raw_links if @raw_links.present? @raw_links = [] - cooked_stripped.css("a[href]").each do |l| # Don't include @mentions in the link count next if l['href'].blank? || link_is_a_mention?(l) diff --git a/lib/validators/post_validator.rb b/lib/validators/post_validator.rb index 37caff0053e..18e8d4330d3 100644 --- a/lib/validators/post_validator.rb +++ b/lib/validators/post_validator.rb @@ -93,7 +93,7 @@ class Validators::PostValidator < ActiveModel::Validator end def can_post_links_validator(post) - return if post.link_count == 0 || + return if (post.link_count == 0 && !post.has_oneboxes?) || Guardian.new(post.acting_user).can_post_link? || private_message?(post) diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 866cfe8867c..8ec72679688 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -355,6 +355,7 @@ describe Post do describe "maximums" do let(:newuser) { Fabricate(:user, trust_level: TrustLevel[0]) } let(:post_one_link) { post_with_body("[sherlock](http://www.bbc.co.uk/programmes/b018ttws)", newuser) } + let(:post_onebox) { post_with_body("http://www.google.com", newuser) } let(:post_two_links) { post_with_body("discourse twitter", newuser) } let(:post_with_mentions) { post_with_body("hello @#{newuser.username} how are you doing?", newuser) } @@ -396,10 +397,18 @@ describe Post do expect(post_two_links).to be_valid end - it "doesn't allow allow links if `min_trust_to_post_links` is not met" do - SiteSetting.min_trust_to_post_links = 2 - post_two_links.user.trust_level = TrustLevel[1] - expect(post_one_link).not_to be_valid + context "min_trust_to_post_links" do + it "considers oneboxes links" do + SiteSetting.min_trust_to_post_links = 3 + post_onebox.user.trust_level = TrustLevel[2] + expect(post_onebox).not_to be_valid + end + + it "doesn't allow allow links if `min_trust_to_post_links` is not met" do + SiteSetting.min_trust_to_post_links = 2 + post_two_links.user.trust_level = TrustLevel[1] + expect(post_one_link).not_to be_valid + end end end