From 1d5952a92fcc3f73460df04295cfb247a9a6f08f Mon Sep 17 00:00:00 2001 From: Navin Date: Tue, 28 May 2013 16:41:18 +0200 Subject: [PATCH] Extract analysis type methods to a module and refactor Post#raw_links --- app/models/post.rb | 58 ++-------------------------------------- lib/post_analyser.rb | 63 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 56 deletions(-) create mode 100644 lib/post_analyser.rb diff --git a/app/models/post.rb b/app/models/post.rb index 98842c916ce..ae977efcd90 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -4,6 +4,7 @@ require_dependency 'rate_limiter' require_dependency 'post_revisor' require_dependency 'enum' require_dependency 'trashable' +require_dependency 'post_analyser' require 'archetype' require 'digest/sha1' @@ -11,6 +12,7 @@ require 'digest/sha1' class Post < ActiveRecord::Base include RateLimiter::OnCreateRecord include Trashable + include PostAnalyser versioned if: :raw_changed? @@ -114,29 +116,6 @@ class Post < ActiveRecord::Base end.count end - # Returns an array of all links in a post - def raw_links - return [] unless raw.present? - - return @raw_links if @raw_links.present? - - # Don't include @mentions in the link count - @raw_links = [] - cooked_document.search("a[href]").each do |l| - html_class = l.attributes['class'] - url = l.attributes['href'].to_s - if html_class.present? - next if html_class.to_s == 'mention' && l.attributes['href'].to_s =~ /^\/users\// - end - @raw_links << url - end - @raw_links - end - - # How many links are present in the post - def link_count - raw_links.size - end # 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 @@ -168,22 +147,6 @@ class Post < ActiveRecord::Base add_error_if_count_exceeded(:too_many_links, link_count, SiteSetting.newuser_max_links) unless acting_user_is_trusted? 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 = {} - raw_links.each do |u| - uri = URI.parse(u) - host = uri.host - @linked_hosts[host] ||= 1 - end - @linked_hosts - end - def total_hosts_usage hosts = linked_hosts.clone @@ -208,23 +171,6 @@ class Post < ActiveRecord::Base false end - - def raw_mentions - return [] if raw.blank? - - # We don't count mentions in quotes - return @raw_mentions if @raw_mentions.present? - raw_stripped = raw.gsub(/\[quote=(.*)\]([^\[]*?)\[\/quote\]/im, '') - - # Strip pre and code tags - 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!(/^@/, '') } - end - def archetype topic.archetype end diff --git a/lib/post_analyser.rb b/lib/post_analyser.rb new file mode 100644 index 00000000000..e4f0f9d571d --- /dev/null +++ b/lib/post_analyser.rb @@ -0,0 +1,63 @@ +module PostAnalyser + + + def raw_mentions + return [] if raw.blank? + + # We don't count mentions in quotes + return @raw_mentions if @raw_mentions.present? + raw_stripped = raw.gsub(/\[quote=(.*)\]([^\[]*?)\[\/quote\]/im, '') + + # Strip pre and code tags + 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!(/^@/, '') } + 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 = {} + raw_links.each do |u| + uri = URI.parse(u) + host = uri.host + @linked_hosts[host] ||= 1 + end + @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? + + # Don't include @mentions in the link count + @raw_links = [] + cooked_document.search("a[href]").each do |l| + next if link_is_a_mention?(l) + url = l.attributes['href'].to_s + @raw_links << url + end + @raw_links + end + + # How many links are present in the post + def link_count + raw_links.size + end + + private + + 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