Merge pull request #1237 from stephankaag/improve_post_analyzing

Improve post analyzing
This commit is contained in:
Robin Ward 2013-07-23 07:07:49 -07:00
commit adf7c9ad06
4 changed files with 14 additions and 24 deletions

View File

@ -78,27 +78,23 @@ class Post < ActiveRecord::Base
Digest::SHA1.hexdigest(raw.gsub(/\s+/, ""))
end
def reset_cooked
@cooked_document = nil
self.cooked = nil
end
def self.white_listed_image_classes
@white_listed_image_classes ||= ['avatar', 'favicon', 'thumbnail']
end
def post_analyzer
@post_analyzer = PostAnalyzer.new(raw, topic_id)
@post_analyzers ||= {}
@post_analyzers[raw_hash] ||= PostAnalyzer.new(raw, topic_id)
end
%w{raw_mentions linked_hosts image_count attachment_count link_count raw_links}.each do |attr|
define_method(attr) do
PostAnalyzer.new(raw, topic_id).send(attr)
post_analyzer.send(attr)
end
end
def cook(*args)
PostAnalyzer.new(raw, topic_id).cook(*args)
post_analyzer.cook(*args)
end
@ -296,6 +292,7 @@ class Post < ActiveRecord::Base
end
# TODO: move to post-analyzer?
# Determine what posts are quoted by this post
def extract_quoted_post_numbers
temp_collector = []

View File

@ -96,20 +96,18 @@ class PostAlertObserver < ActiveRecord::Observer
display_username: opts[:display_username] || post.user.username }.to_json)
end
# TODO: Move to post-analyzer?
# Returns a list users who have been mentioned
def extract_mentioned_users(post)
User.where(username_lower: post.raw_mentions).where("id <> ?", post.user_id)
end
# TODO: Move to post-analyzer?
# Returns a list of users who were quoted in the post
def extract_quoted_users(post)
result = []
post.raw.scan(/\[quote=\"([^,]+),.+\"\]/).uniq.each do |m|
username = m.first.strip.downcase
user = User.where("username_lower = :username and id != :id", username: username, id: post.user_id).first
result << user if user.present?
end
result
post.raw.scan(/\[quote=\"([^,]+),.+\"\]/).uniq.map do |m|
User.where("username_lower = :username and id != :id", username: m.first.strip.downcase, id: post.user_id).first
end.compact
end
# Notify a bunch of users

View File

@ -1,17 +1,10 @@
class PostAnalyzer
attr_accessor :cooked, :raw
def initialize(raw, topic_id)
@raw = raw
@topic_id = topic_id
end
def cooked_document
@cooked = cook(@raw, topic_id: @topic_id)
@cooked_document = Nokogiri::HTML.fragment(@cooked)
end
# What we use to cook posts
def cook(*args)
cooked = PrettyText.cook(*args)
@ -110,6 +103,10 @@ class PostAnalyzer
private
def cooked_document
@cooked_document ||= Nokogiri::HTML.fragment(cook(@raw, topic_id: @topic_id))
end
def link_is_a_mention?(l)
html_class = l.attributes['class']
return false if html_class.nil?

View File

@ -66,8 +66,6 @@ class PostRevisor
end
def update_post
@post.reset_cooked
@post.raw = @new_raw
@post.updated_by = @user
@post.last_editor_id = @user.id