discourse/app/models/post_analyzer.rb

140 lines
3.4 KiB
Ruby
Raw Normal View History

require_dependency 'oneboxer'
require_dependency 'email_cook'
class PostAnalyzer
def initialize(raw, topic_id)
2017-07-27 20:20:09 -05:00
@raw = raw
@topic_id = topic_id
2016-04-12 13:09:59 -05:00
@found_oneboxes = false
end
def found_oneboxes?
@found_oneboxes
end
# What we use to cook posts
def cook(raw, opts = {})
cook_method = opts[:cook_method]
return raw if cook_method == Post.cook_methods[:raw_html]
if cook_method == Post.cook_methods[:email]
cooked = EmailCook.new(raw).cook(opts)
else
cooked = PrettyText.cook(raw, opts)
end
2015-09-25 05:14:38 -05:00
result = Oneboxer.apply(cooked, topic_id: @topic_id) do |url, _|
2016-04-12 13:09:59 -05:00
@found_oneboxes = true
Oneboxer.invalidate(url) if opts[:invalidate_oneboxes]
Oneboxer.cached_onebox(url)
end
cooked = result.to_html if result.changed?
cooked
end
# How many images are present in the post
def image_count
return 0 unless @raw.present?
cooked_stripped.css("img").reject do |t|
if dom_class = t["class"]
2017-08-23 19:54:21 -05:00
(Post.white_listed_image_classes & dom_class.split).count > 0
end
end.count
end
2013-07-21 19:39:17 -05:00
# How many attachments are present in the post
def attachment_count
return 0 unless @raw.present?
2013-10-17 11:44:09 -05:00
attachments = cooked_stripped.css("a.attachment[href^=\"#{Discourse.store.absolute_base_url}\"]")
attachments += cooked_stripped.css("a.attachment[href^=\"#{Discourse.store.relative_base_url}\"]") if Discourse.store.internal?
attachments.count
2013-07-21 19:39:17 -05:00
end
def raw_mentions
return [] if @raw.blank?
return @raw_mentions if @raw_mentions.present?
2013-07-30 17:01:42 -05:00
2016-02-24 02:09:18 -06:00
raw_mentions = cooked_stripped.css('.mention, .mention-group').map do |e|
2017-07-27 20:20:09 -05:00
if name = e.inner_text
name = name[1..-1]
name.downcase! if name
name
end
end
raw_mentions.compact!
raw_mentions.uniq!
@raw_mentions = raw_mentions
end
# from rack ... compat with ruby 2.2
def self.parse_uri_rfc2396(uri)
@parser ||= defined?(URI::RFC2396_Parser) ? URI::RFC2396_Parser.new : URI
@parser.parse(uri)
end
# Count how many hosts are linked in the post
def linked_hosts
return {} if raw_links.blank?
return @linked_hosts if @linked_hosts.present?
@linked_hosts = {}
2013-10-17 11:44:09 -05:00
raw_links.each do |u|
2013-07-15 05:11:23 -05:00
begin
uri = self.class.parse_uri_rfc2396(u)
2013-07-15 05:11:23 -05:00
host = uri.host
@linked_hosts[host] ||= 1 unless host.nil?
2013-07-15 05:11:23 -05:00
rescue URI::InvalidURIError
# An invalid URI does not count as a raw link.
next
end
end
2013-10-17 11:44:09 -05:00
@linked_hosts
end
# Returns an array of all links in a post excluding mentions
def raw_links
return [] unless @raw.present?
return @raw_links if @raw_links.present?
@raw_links = []
2013-10-17 11:44:09 -05:00
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)
@raw_links << l['href'].to_s
end
2013-10-17 11:44:09 -05:00
@raw_links
end
# How many links are present in the post
def link_count
raw_links.size
end
private
def cooked_stripped
@cooked_stripped ||= begin
doc = Nokogiri::HTML.fragment(cook(@raw, topic_id: @topic_id))
doc.css("pre, code, aside.quote, .onebox, .elided").remove
doc
end
end
def link_is_a_mention?(l)
html_class = l['class']
return false if html_class.blank?
href = l['href'].to_s
html_class.to_s['mention'] && href[/^\/u\//] || href[/^\/users\//]
end
end