FIX: Consider oneboxes links wrt to `min_trust_level_to_post_links`

This commit is contained in:
Robin Ward 2018-02-08 18:26:56 -05:00
parent 69bccb9e32
commit 5466389f4e
4 changed files with 28 additions and 7 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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("<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 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