mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
4% speedup on our test suite
This commit is contained in:
parent
71571b9316
commit
82dd9009e3
@ -125,7 +125,6 @@ class Post < ActiveRecord::Base
|
|||||||
Plugin::Filter.apply(:after_post_cook, self, post_analyzer.cook(*args))
|
Plugin::Filter.apply(:after_post_cook, self, post_analyzer.cook(*args))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# Sometimes the post is being edited by someone else, for example, a mod.
|
# Sometimes the post is being edited by someone else, for example, a mod.
|
||||||
# If that's the case, they should not be bound by the original poster's
|
# If that's the case, they should not be bound by the original poster's
|
||||||
# restrictions, for example on not posting images.
|
# restrictions, for example on not posting images.
|
||||||
|
@ -42,6 +42,7 @@ class PostAnalyzer
|
|||||||
# How many attachments are present in the post
|
# How many attachments are present in the post
|
||||||
def attachment_count
|
def attachment_count
|
||||||
return 0 unless @raw.present?
|
return 0 unless @raw.present?
|
||||||
|
|
||||||
attachments = cooked_document.css("a.attachment[href^=\"#{Discourse.store.absolute_base_url}\"]")
|
attachments = cooked_document.css("a.attachment[href^=\"#{Discourse.store.absolute_base_url}\"]")
|
||||||
attachments += cooked_document.css("a.attachment[href^=\"#{Discourse.store.relative_base_url}\"]") if Discourse.store.internal?
|
attachments += cooked_document.css("a.attachment[href^=\"#{Discourse.store.relative_base_url}\"]") if Discourse.store.internal?
|
||||||
attachments.count
|
attachments.count
|
||||||
@ -49,30 +50,25 @@ class PostAnalyzer
|
|||||||
|
|
||||||
def raw_mentions
|
def raw_mentions
|
||||||
return [] if @raw.blank?
|
return [] if @raw.blank?
|
||||||
|
|
||||||
# We don't count mentions in quotes
|
|
||||||
return @raw_mentions if @raw_mentions.present?
|
return @raw_mentions if @raw_mentions.present?
|
||||||
raw_stripped = @raw.gsub(/\[quote=(.*)\]([^\[]*?)\[\/quote\]/im, '')
|
|
||||||
|
|
||||||
# Process markdown so that code blocks can be generated and subsequently ignored
|
# strip quotes and code blocks
|
||||||
raw_stripped = PrettyText.markdown(raw_stripped)
|
cooked_stripped = cooked_document
|
||||||
|
cooked_stripped.search("aside.quote").remove
|
||||||
|
cooked_stripped.search("pre").remove
|
||||||
|
cooked_stripped.search("code").remove
|
||||||
|
|
||||||
# Strip pre and code tags
|
results = cooked_stripped.to_html.scan(PrettyText.mention_matcher)
|
||||||
doc = Nokogiri::HTML.fragment(raw_stripped)
|
|
||||||
doc.search("pre").remove
|
|
||||||
doc.search("code").remove
|
|
||||||
|
|
||||||
results = doc.to_html.scan(PrettyText.mention_matcher)
|
|
||||||
@raw_mentions = results.uniq.map { |un| un.first.downcase.gsub!(/^@/, '') }
|
@raw_mentions = results.uniq.map { |un| un.first.downcase.gsub!(/^@/, '') }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Count how many hosts are linked in the post
|
# Count how many hosts are linked in the post
|
||||||
def linked_hosts
|
def linked_hosts
|
||||||
return {} if raw_links.blank?
|
return {} if raw_links.blank?
|
||||||
|
|
||||||
return @linked_hosts if @linked_hosts.present?
|
return @linked_hosts if @linked_hosts.present?
|
||||||
|
|
||||||
@linked_hosts = {}
|
@linked_hosts = {}
|
||||||
|
|
||||||
raw_links.each do |u|
|
raw_links.each do |u|
|
||||||
begin
|
begin
|
||||||
uri = URI.parse(u)
|
uri = URI.parse(u)
|
||||||
@ -83,23 +79,24 @@ class PostAnalyzer
|
|||||||
next
|
next
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@linked_hosts
|
@linked_hosts
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns an array of all links in a post excluding mentions
|
# Returns an array of all links in a post excluding mentions
|
||||||
def raw_links
|
def raw_links
|
||||||
|
|
||||||
return [] unless @raw.present?
|
return [] unless @raw.present?
|
||||||
|
|
||||||
return @raw_links if @raw_links.present?
|
return @raw_links if @raw_links.present?
|
||||||
|
|
||||||
# Don't include @mentions in the link count
|
# Don't include @mentions in the link count
|
||||||
@raw_links = []
|
@raw_links = []
|
||||||
|
|
||||||
cooked_document.search("a").each do |l|
|
cooked_document.search("a").each do |l|
|
||||||
next if l.attributes['href'].nil? || link_is_a_mention?(l)
|
next if l.attributes['href'].nil? || link_is_a_mention?(l)
|
||||||
url = l.attributes['href'].to_s
|
url = l.attributes['href'].to_s
|
||||||
@raw_links << url
|
@raw_links << url
|
||||||
end
|
end
|
||||||
|
|
||||||
@raw_links
|
@raw_links
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user