Class: PostAnalyzer
- Inherits:
-
Object
- Object
- PostAnalyzer
- Defined in:
- app/models/post_analyzer.rb
Class Method Summary collapse
-
.parse_uri_rfc2396(uri) ⇒ Object
from rack …
Instance Method Summary collapse
-
#attachment_count ⇒ Object
How many attachments are present in the post.
-
#cook(raw, opts = {}) ⇒ Object
What we use to cook posts.
- #cooked_stripped ⇒ Object
-
#embedded_media_count ⇒ Object
How many images are present in the post.
- #found_oneboxes? ⇒ Boolean
- #has_oneboxes? ⇒ Boolean
-
#initialize(raw, topic_id) ⇒ PostAnalyzer
constructor
A new instance of PostAnalyzer.
-
#link_count ⇒ Object
How many links are present in the post.
-
#linked_hosts ⇒ Object
Count how many hosts are linked in the post.
-
#raw_links ⇒ Object
Returns an array of all links in a post excluding mentions.
- #raw_mentions ⇒ Object
Constructor Details
#initialize(raw, topic_id) ⇒ PostAnalyzer
Returns a new instance of PostAnalyzer.
4 5 6 7 8 9 |
# File 'app/models/post_analyzer.rb', line 4 def initialize(raw, topic_id) @raw = raw @topic_id = topic_id @onebox_urls = [] @found_oneboxes = false end |
Class Method Details
.parse_uri_rfc2396(uri) ⇒ Object
from rack … compat with ruby 2.2
92 93 94 95 |
# File 'app/models/post_analyzer.rb', line 92 def self.parse_uri_rfc2396(uri) @parser ||= defined?(URI::RFC2396_Parser) ? URI::RFC2396_Parser.new : URI @parser.parse(uri) end |
Instance Method Details
#attachment_count ⇒ Object
How many attachments are present in the post
73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/models/post_analyzer.rb', line 73 def return 0 if @raw.blank? = cooked_stripped.css("a.attachment[href^=\"#{Discourse.store.absolute_base_url}\"]") += cooked_stripped.css( "a.attachment[href^=\"#{Discourse.store.relative_base_url}\"]", ) if Discourse.store.internal? .count end |
#cook(raw, opts = {}) ⇒ Object
What we use to cook posts
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/post_analyzer.rb', line 23 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 limit = SiteSetting.max_oneboxes_per_post result = Oneboxer.apply(cooked, extra_paths: ".inline-onebox-loading") do |url, element| if opts[:invalidate_oneboxes] Oneboxer.invalidate(url) InlineOneboxer.invalidate(url) end next if element["class"] != Oneboxer::ONEBOX_CSS_CLASS next if limit <= 0 limit -= 1 @onebox_urls << url onebox = Oneboxer.cached_onebox(url) @found_oneboxes = true if onebox.present? onebox end if result.changed? PrettyText.sanitize_hotlinked_media(result.doc) cooked = result.to_html end cooked end |
#cooked_stripped ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'app/models/post_analyzer.rb', line 146 def cooked_stripped @cooked_stripped ||= begin cooked = cook(@raw, topic_id: @topic_id) fragment = Nokogiri::HTML5.fragment(cooked) PostStripper.strip(fragment) end end |
#embedded_media_count ⇒ Object
How many images are present in the post
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/models/post_analyzer.rb', line 58 def return 0 if @raw.blank? # TODO - do we need to look for tags other than img, video and audio? cooked_stripped .css("img", "video", "audio") .reject do |t| if dom_class = t["class"] (Post.allowed_image_classes & dom_class.split).count > 0 end end .count end |
#found_oneboxes? ⇒ Boolean
11 12 13 |
# File 'app/models/post_analyzer.rb', line 11 def found_oneboxes? @found_oneboxes end |
#has_oneboxes? ⇒ Boolean
15 16 17 18 19 20 |
# File 'app/models/post_analyzer.rb', line 15 def has_oneboxes? return false if @raw.blank? cooked_stripped found_oneboxes? end |
#link_count ⇒ Object
How many links are present in the post
142 143 144 |
# File 'app/models/post_analyzer.rb', line 142 def link_count raw_links.size + @onebox_urls.size end |
#linked_hosts ⇒ Object
Count how many hosts are linked in the post
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/models/post_analyzer.rb', line 98 def linked_hosts all_links = raw_links + @onebox_urls return {} if all_links.blank? return @linked_hosts if @linked_hosts.present? @linked_hosts = {} all_links.each do |u| begin uri = self.class.parse_uri_rfc2396(u) host = uri.host @linked_hosts[host] ||= 1 unless host.nil? rescue URI::Error # An invalid URI does not count as a host next end end @linked_hosts end |
#raw_links ⇒ Object
Returns an array of all links in a post excluding mentions
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/models/post_analyzer.rb', line 121 def raw_links return [] if @raw.blank? return @raw_links if @raw_links.present? @raw_links = [] cooked_stripped .css("a") .each do |l| # Don't include @mentions in the link count next if link_is_a_mention?(l) # Don't include heading anchor in the link count next if link_is_an_anchor?(l) # Don't include hashtags in the link count next if link_is_a_hashtag?(l) @raw_links << l["href"].to_s end @raw_links end |
#raw_mentions ⇒ Object
85 86 87 88 89 |
# File 'app/models/post_analyzer.rb', line 85 def raw_mentions return [] if @raw.blank? return @raw_mentions if @raw_mentions.present? @raw_mentions = PrettyText.extract_mentions(cooked_stripped) end |