2014-01-28 04:09:09 +08:00
|
|
|
require_dependency 'oneboxer'
|
|
|
|
|
2013-05-31 02:34:44 +08:00
|
|
|
class PostAnalyzer
|
|
|
|
|
|
|
|
def initialize(raw, topic_id)
|
|
|
|
@raw = raw
|
|
|
|
@topic_id = topic_id
|
|
|
|
end
|
|
|
|
|
|
|
|
# What we use to cook posts
|
|
|
|
def cook(*args)
|
|
|
|
cooked = PrettyText.cook(*args)
|
|
|
|
|
2014-08-15 05:54:55 +08:00
|
|
|
result = Oneboxer.apply(cooked) do |url, _|
|
2013-06-19 15:19:42 +08:00
|
|
|
Oneboxer.invalidate(url) if args.last[:invalidate_oneboxes]
|
2014-03-18 10:12:58 +08:00
|
|
|
Oneboxer.cached_onebox url
|
2013-05-31 02:34:44 +08:00
|
|
|
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_document.search("img").reject do |t|
|
|
|
|
dom_class = t["class"]
|
|
|
|
if dom_class
|
|
|
|
(Post.white_listed_image_classes & dom_class.split(" ")).count > 0
|
|
|
|
end
|
|
|
|
end.count
|
|
|
|
end
|
|
|
|
|
2013-07-22 08:39:17 +08:00
|
|
|
# How many attachments are present in the post
|
|
|
|
def attachment_count
|
|
|
|
return 0 unless @raw.present?
|
2013-10-18 00:44:09 +08:00
|
|
|
|
2013-08-01 05:26:34 +08:00
|
|
|
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.count
|
2013-07-22 08:39:17 +08:00
|
|
|
end
|
|
|
|
|
2013-05-31 02:34:44 +08:00
|
|
|
def raw_mentions
|
|
|
|
return [] if @raw.blank?
|
|
|
|
return @raw_mentions if @raw_mentions.present?
|
2013-07-31 06:01:42 +08:00
|
|
|
|
2013-10-18 00:44:09 +08:00
|
|
|
# strip quotes and code blocks
|
|
|
|
cooked_stripped = cooked_document
|
2014-07-01 06:10:46 +08:00
|
|
|
cooked_stripped.css("aside.quote").remove
|
|
|
|
cooked_stripped.css("pre").remove
|
|
|
|
cooked_stripped.css("code").remove
|
2013-05-31 02:34:44 +08:00
|
|
|
|
2013-10-18 00:44:09 +08:00
|
|
|
results = cooked_stripped.to_html.scan(PrettyText.mention_matcher)
|
2013-05-31 02:34:44 +08:00
|
|
|
@raw_mentions = results.uniq.map { |un| un.first.downcase.gsub!(/^@/, '') }
|
|
|
|
end
|
|
|
|
|
2015-03-18 13:25:24 +08:00
|
|
|
# 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
|
|
|
|
|
2013-05-31 02:34:44 +08:00
|
|
|
# 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-18 00:44:09 +08:00
|
|
|
|
2013-05-31 02:34:44 +08:00
|
|
|
raw_links.each do |u|
|
2013-07-15 18:11:23 +08:00
|
|
|
begin
|
2015-03-18 13:25:24 +08:00
|
|
|
uri = self.class.parse_uri_rfc2396(u)
|
2013-07-15 18:11:23 +08:00
|
|
|
host = uri.host
|
2014-03-07 17:44:04 +08:00
|
|
|
@linked_hosts[host] ||= 1 unless host.nil?
|
2013-07-15 18:11:23 +08:00
|
|
|
rescue URI::InvalidURIError
|
|
|
|
# An invalid URI does not count as a raw link.
|
|
|
|
next
|
|
|
|
end
|
2013-05-31 02:34:44 +08:00
|
|
|
end
|
2013-10-18 00:44:09 +08:00
|
|
|
|
2013-05-31 02:34:44 +08: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-18 00:44:09 +08:00
|
|
|
|
2013-10-17 00:19:20 +08:00
|
|
|
cooked_document.search("a").each do |l|
|
2014-03-07 17:44:04 +08:00
|
|
|
# Don't include @mentions in the link count
|
2013-10-17 00:19:20 +08:00
|
|
|
next if l.attributes['href'].nil? || link_is_a_mention?(l)
|
2013-05-31 02:34:44 +08:00
|
|
|
url = l.attributes['href'].to_s
|
|
|
|
@raw_links << url
|
|
|
|
end
|
2013-10-18 00:44:09 +08:00
|
|
|
|
2013-05-31 02:34:44 +08:00
|
|
|
@raw_links
|
|
|
|
end
|
|
|
|
|
|
|
|
# How many links are present in the post
|
|
|
|
def link_count
|
|
|
|
raw_links.size
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-07-23 04:24:47 +08:00
|
|
|
def cooked_document
|
|
|
|
@cooked_document ||= Nokogiri::HTML.fragment(cook(@raw, topic_id: @topic_id))
|
|
|
|
end
|
|
|
|
|
2013-05-31 02:34:44 +08:00
|
|
|
def link_is_a_mention?(l)
|
|
|
|
html_class = l.attributes['class']
|
|
|
|
return false if html_class.nil?
|
|
|
|
html_class.to_s == 'mention' && l.attributes['href'].to_s =~ /^\/users\//
|
|
|
|
end
|
|
|
|
end
|