]> git.openstreetmap.org Git - rails.git/commitdiff
Make spam scoring work with new rich text system
authorTom Hughes <tom@compton.nu>
Mon, 5 Mar 2012 22:46:28 +0000 (22:46 +0000)
committerTom Hughes <tom@compton.nu>
Sat, 17 Mar 2012 16:36:58 +0000 (16:36 +0000)
app/models/user.rb
lib/osm.rb
lib/rich_text.rb

index 3b55040c63cd486906d0e52e1ab0a4f6e9cddf15..cebea94685711f8b972db57191444498a6a60717 100644 (file)
@@ -204,10 +204,10 @@ class User < ActiveRecord::Base
   def spam_score
     changeset_score = self.changesets.limit(10).length * 50
     trace_score = self.traces.limit(10).length * 50
-    diary_entry_score = self.diary_entries.inject(0) { |s,e| s += OSM.spam_score(e.body) }
-    diary_comment_score = self.diary_comments.inject(0) { |s,e| s += OSM.spam_score(e.body) }
+    diary_entry_score = self.diary_entries.inject(0) { |s,e| s += e.body.spam_score }
+    diary_comment_score = self.diary_comments.inject(0) { |s,c| s += c.body.spam_score }
 
-    score = OSM.spam_score(self.description)
+    score = self.description.spam_score
     score += diary_entry_score / self.diary_entries.length if self.diary_entries.length > 0
     score += diary_comment_score / self.diary_comments.length if self.diary_comments.length > 0
     score -= changeset_score
index c85abc0f3cf5c426a11f2f7b337d414192161caa..1a22af93a67b2806dbf4515f126a4e3f11fe6454 100644 (file)
@@ -6,7 +6,6 @@ module OSM
   require 'rexml/text'
   require 'xml/libxml'
   require 'digest/md5'
-  require 'nokogiri'
 
   if defined?(SystemTimer)
     Timer = SystemTimer
@@ -511,27 +510,6 @@ module OSM
                       "AND #{prefix}longitude BETWEEN #{bbox.min_lon} AND #{bbox.max_lon}"
   end
 
-  # Return a spam score for a chunk of text
-  def self.spam_score(text)
-    link_count = 0
-    link_size = 0
-
-    doc = Nokogiri::HTML(Rinku.auto_link(text, :urls))
-
-    if doc.content.length > 0
-      doc.xpath("//a").each do |link|
-        link_count += 1
-        link_size += link.content.length
-      end
-
-      link_proportion = link_size.to_f / doc.content.length.to_f
-    else
-      link_proportion = 0
-    end
-
-    return [link_proportion - 0.2, 0.0].max * 200 + link_count * 20
-  end
-
   def self.legal_text_for_country(country_code)
     file_name = File.join(Rails.root, "config", "legales", country_code.to_s + ".yml")
     file_name = File.join(Rails.root, "config", "legales", DEFAULT_LEGALE + ".yml") unless File.exist? file_name
index ec5e9e473789a9d70c7e8fb770cac51e14be8137..f2a558e6af8dcc92aae9aa588ce8216edb0ab248 100644 (file)
@@ -7,7 +7,29 @@ module RichText
     end
   end
 
-  class HTML < String
+  class Base < String
+    def spam_score
+      link_count = 0
+      link_size = 0
+
+      doc = Nokogiri::HTML(to_html)
+
+      if doc.content.length > 0
+        doc.xpath("//a").each do |link|
+          link_count += 1
+          link_size += link.content.length
+        end
+
+        link_proportion = link_size.to_f / doc.content.length.to_f
+      else
+        link_proportion = 0
+      end
+
+      return [link_proportion - 0.2, 0.0].max * 200 + link_count * 20
+    end
+  end
+
+  class HTML < Base
     include ActionView::Helpers::TextHelper
     include ActionView::Helpers::TagHelper
 
@@ -34,7 +56,7 @@ module RichText
     end
   end
 
-  class Markdown < String
+  class Markdown < Base
     def to_html
       html_parser.render(self).html_safe
     end