2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-30 14:34:44 -04:00
|
|
|
class PostAnalyzer
|
|
|
|
|
|
|
|
def initialize(raw, topic_id)
|
2017-07-27 21:20:09 -04:00
|
|
|
@raw = raw
|
2013-05-30 14:34:44 -04:00
|
|
|
@topic_id = topic_id
|
2018-06-13 14:57:32 -04:00
|
|
|
@onebox_urls = []
|
2019-09-10 06:59:48 -04:00
|
|
|
@found_oneboxes = false
|
2016-04-12 14:09:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def found_oneboxes?
|
2019-09-10 06:59:48 -04:00
|
|
|
@found_oneboxes
|
2013-05-30 14:34:44 -04:00
|
|
|
end
|
|
|
|
|
2018-02-08 18:26:56 -05:00
|
|
|
def has_oneboxes?
|
|
|
|
return false unless @raw.present?
|
|
|
|
|
|
|
|
cooked_stripped
|
|
|
|
found_oneboxes?
|
|
|
|
end
|
|
|
|
|
2013-05-30 14:34:44 -04:00
|
|
|
# What we use to cook posts
|
2017-10-17 14:37:51 -04:00
|
|
|
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]
|
2017-11-15 10:39:29 -05:00
|
|
|
cooked = EmailCook.new(raw).cook(opts)
|
2017-10-17 14:37:51 -04:00
|
|
|
else
|
|
|
|
cooked = PrettyText.cook(raw, opts)
|
|
|
|
end
|
2013-05-30 14:34:44 -04:00
|
|
|
|
2018-02-13 18:39:44 -05:00
|
|
|
result = Oneboxer.apply(cooked) do |url|
|
2018-06-13 14:57:32 -04:00
|
|
|
@onebox_urls << url
|
2017-10-17 14:37:51 -04:00
|
|
|
Oneboxer.invalidate(url) if opts[:invalidate_oneboxes]
|
2019-09-10 06:59:48 -04:00
|
|
|
onebox = Oneboxer.cached_onebox(url)
|
|
|
|
@found_oneboxes = true if onebox.present?
|
|
|
|
onebox
|
2013-05-30 14:34:44 -04: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?
|
|
|
|
|
2016-12-05 09:19:15 -05:00
|
|
|
cooked_stripped.css("img").reject do |t|
|
|
|
|
if dom_class = t["class"]
|
2017-08-23 20:54:21 -04:00
|
|
|
(Post.white_listed_image_classes & dom_class.split).count > 0
|
2013-05-30 14:34:44 -04:00
|
|
|
end
|
|
|
|
end.count
|
|
|
|
end
|
|
|
|
|
2013-07-21 20:39:17 -04:00
|
|
|
# How many attachments are present in the post
|
|
|
|
def attachment_count
|
|
|
|
return 0 unless @raw.present?
|
2013-10-17 12:44:09 -04:00
|
|
|
|
2016-12-05 09:19:15 -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?
|
2013-07-31 17:26:34 -04:00
|
|
|
attachments.count
|
2013-07-21 20:39:17 -04:00
|
|
|
end
|
|
|
|
|
2013-05-30 14:34:44 -04:00
|
|
|
def raw_mentions
|
|
|
|
return [] if @raw.blank?
|
|
|
|
return @raw_mentions if @raw_mentions.present?
|
2013-07-30 18:01:42 -04:00
|
|
|
|
2016-02-24 03:09:18 -05:00
|
|
|
raw_mentions = cooked_stripped.css('.mention, .mention-group').map do |e|
|
2017-07-27 21:20:09 -04:00
|
|
|
if name = e.inner_text
|
|
|
|
name = name[1..-1]
|
2019-04-23 06:22:47 -04:00
|
|
|
name = User.normalize_username(name)
|
2017-07-27 21:20:09 -04:00
|
|
|
name
|
|
|
|
end
|
2016-02-24 01:47:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
raw_mentions.compact!
|
|
|
|
raw_mentions.uniq!
|
2017-09-11 14:01:24 -04:00
|
|
|
@raw_mentions = raw_mentions
|
2013-05-30 14:34:44 -04:00
|
|
|
end
|
|
|
|
|
2015-03-18 01:25:24 -04: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-30 14:34:44 -04:00
|
|
|
# Count how many hosts are linked in the post
|
|
|
|
def linked_hosts
|
2018-06-13 14:57:32 -04:00
|
|
|
all_links = raw_links + @onebox_urls
|
|
|
|
|
|
|
|
return {} if all_links.blank?
|
2013-05-30 14:34:44 -04:00
|
|
|
return @linked_hosts if @linked_hosts.present?
|
|
|
|
|
|
|
|
@linked_hosts = {}
|
2013-10-17 12:44:09 -04:00
|
|
|
|
2018-06-13 14:57:32 -04:00
|
|
|
all_links.each do |u|
|
2013-07-15 06:11:23 -04:00
|
|
|
begin
|
2015-03-18 01:25:24 -04:00
|
|
|
uri = self.class.parse_uri_rfc2396(u)
|
2013-07-15 06:11:23 -04:00
|
|
|
host = uri.host
|
2014-03-07 04:44:04 -05:00
|
|
|
@linked_hosts[host] ||= 1 unless host.nil?
|
2018-08-14 06:23:32 -04:00
|
|
|
rescue URI::Error
|
2018-06-13 14:57:32 -04:00
|
|
|
# An invalid URI does not count as a host
|
2013-07-15 06:11:23 -04:00
|
|
|
next
|
|
|
|
end
|
2013-05-30 14:34:44 -04:00
|
|
|
end
|
2013-10-17 12:44:09 -04:00
|
|
|
|
2013-05-30 14:34:44 -04: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 = []
|
2018-03-28 12:32:16 -04:00
|
|
|
cooked_stripped.css("a").each do |l|
|
2014-03-07 04:44:04 -05:00
|
|
|
# Don't include @mentions in the link count
|
2018-03-28 12:32:16 -04:00
|
|
|
next if link_is_a_mention?(l)
|
2016-12-05 09:19:15 -05:00
|
|
|
@raw_links << l['href'].to_s
|
2013-05-30 14:34:44 -04:00
|
|
|
end
|
2013-10-17 12:44:09 -04:00
|
|
|
|
2013-05-30 14:34:44 -04:00
|
|
|
@raw_links
|
|
|
|
end
|
|
|
|
|
|
|
|
# How many links are present in the post
|
|
|
|
def link_count
|
2019-09-10 06:59:48 -04:00
|
|
|
raw_links.size + @onebox_urls.size
|
2013-05-30 14:34:44 -04:00
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def cooked_stripped
|
|
|
|
@cooked_stripped ||= begin
|
|
|
|
doc = Nokogiri::HTML.fragment(cook(@raw, topic_id: @topic_id))
|
2020-02-10 12:31:42 -05:00
|
|
|
doc.css("pre .mention, aside.quote > .title, aside.quote .mention, aside.quote .mention-group, .onebox, .elided").remove
|
2018-06-07 01:28:18 -04:00
|
|
|
doc
|
2016-12-05 09:19:15 -05:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2016-12-05 09:19:15 -05:00
|
|
|
|
2018-09-13 04:34:32 -04:00
|
|
|
private
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
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
|
2013-07-22 16:24:47 -04:00
|
|
|
|
2013-05-30 14:34:44 -04:00
|
|
|
end
|