A platform for community discussion. Free, open, simple.
FIX: Consider oneboxes links wrt to `min_trust_level_to_post_links`
Robin Ward5466389f4e2a
diff --git a/app/models/post.rb b/app/models/post.rbindex 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) dopost_analyzer.send(attr)enddiff --git a/app/models/post_analyzer.rb b/app/models/post_analyzer.rbindex 0d6c2120aef..919b7d42155 100644--- a/app/models/post_analyzer.rb+++ b/app/models/post_analyzer.rb@@ -13,6 +13,13 @@ class PostAnalyzer@found_oneboxesend+ def has_oneboxes?+ return false unless @raw.present?++ cooked_stripped+ found_oneboxes?+ end+# What we use to cook postsdef cook(raw, opts = {})cook_method = opts[:cook_method]@@ -104,7 +111,6 @@ class PostAnalyzerreturn @raw_links if @raw_links.present?@raw_links = []-cooked_stripped.css("a[href]").each do |l|# Don't include @mentions in the link countnext if l['href'].blank? || link_is_a_mention?(l)diff --git a/lib/validators/post_validator.rb b/lib/validators/post_validator.rbindex 37caff0053e..18e8d4330d3 100644--- a/lib/validators/post_validator.rb+++ b/lib/validators/post_validator.rb@@ -93,7 +93,7 @@ class Validators::PostValidator < ActiveModel::Validatorenddef 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.rbindex 866cfe8867c..8ec72679688 100644--- a/spec/models/post_spec.rb+++ b/spec/models/post_spec.rb@@ -355,6 +355,7 @@ describe Post dodescribe "maximums" dolet(: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("<a href='http://discourse.org'>discourse</a> <a href='http://twitter.com'>twitter</a>", newuser) }let(:post_with_mentions) { post_with_body("hello @#{newuser.username} how are you doing?", newuser) }@@ -396,10 +397,18 @@ describe Post doexpect(post_two_links).to be_validend- 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+ endendend