mirror of
https://github.com/discourse/discourse.git
synced 2025-03-22 20:32:02 +08:00
FIX: Consider oneboxes links wrt to min_trust_level_to_post_links
This commit is contained in:
parent
69bccb9e32
commit
5466389f4e
@ -222,7 +222,13 @@ class Post < ActiveRecord::Base
|
|||||||
@post_analyzers[raw_hash] ||= PostAnalyzer.new(raw, topic_id)
|
@post_analyzers[raw_hash] ||= PostAnalyzer.new(raw, topic_id)
|
||||||
end
|
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
|
define_method(attr) do
|
||||||
post_analyzer.send(attr)
|
post_analyzer.send(attr)
|
||||||
end
|
end
|
||||||
|
@ -13,6 +13,13 @@ class PostAnalyzer
|
|||||||
@found_oneboxes
|
@found_oneboxes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def has_oneboxes?
|
||||||
|
return false unless @raw.present?
|
||||||
|
|
||||||
|
cooked_stripped
|
||||||
|
found_oneboxes?
|
||||||
|
end
|
||||||
|
|
||||||
# What we use to cook posts
|
# What we use to cook posts
|
||||||
def cook(raw, opts = {})
|
def cook(raw, opts = {})
|
||||||
cook_method = opts[:cook_method]
|
cook_method = opts[:cook_method]
|
||||||
@ -104,7 +111,6 @@ class PostAnalyzer
|
|||||||
return @raw_links if @raw_links.present?
|
return @raw_links if @raw_links.present?
|
||||||
|
|
||||||
@raw_links = []
|
@raw_links = []
|
||||||
|
|
||||||
cooked_stripped.css("a[href]").each do |l|
|
cooked_stripped.css("a[href]").each do |l|
|
||||||
# Don't include @mentions in the link count
|
# Don't include @mentions in the link count
|
||||||
next if l['href'].blank? || link_is_a_mention?(l)
|
next if l['href'].blank? || link_is_a_mention?(l)
|
||||||
|
@ -93,7 +93,7 @@ class Validators::PostValidator < ActiveModel::Validator
|
|||||||
end
|
end
|
||||||
|
|
||||||
def can_post_links_validator(post)
|
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? ||
|
Guardian.new(post.acting_user).can_post_link? ||
|
||||||
private_message?(post)
|
private_message?(post)
|
||||||
|
|
||||||
|
@ -355,6 +355,7 @@ describe Post do
|
|||||||
describe "maximums" do
|
describe "maximums" do
|
||||||
let(:newuser) { Fabricate(:user, trust_level: TrustLevel[0]) }
|
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_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_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) }
|
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
|
expect(post_two_links).to be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't allow allow links if `min_trust_to_post_links` is not met" do
|
context "min_trust_to_post_links" do
|
||||||
SiteSetting.min_trust_to_post_links = 2
|
it "considers oneboxes links" do
|
||||||
post_two_links.user.trust_level = TrustLevel[1]
|
SiteSetting.min_trust_to_post_links = 3
|
||||||
expect(post_one_link).not_to be_valid
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user