Merge pull request #1237 from stephankaag/improve_post_analyzing
Improve post analyzing
This commit is contained in:
commit
adf7c9ad06
4 changed files with 14 additions and 24 deletions
|
@ -78,27 +78,23 @@ class Post < ActiveRecord::Base
|
||||||
Digest::SHA1.hexdigest(raw.gsub(/\s+/, ""))
|
Digest::SHA1.hexdigest(raw.gsub(/\s+/, ""))
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset_cooked
|
|
||||||
@cooked_document = nil
|
|
||||||
self.cooked = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.white_listed_image_classes
|
def self.white_listed_image_classes
|
||||||
@white_listed_image_classes ||= ['avatar', 'favicon', 'thumbnail']
|
@white_listed_image_classes ||= ['avatar', 'favicon', 'thumbnail']
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_analyzer
|
def post_analyzer
|
||||||
@post_analyzer = PostAnalyzer.new(raw, topic_id)
|
@post_analyzers ||= {}
|
||||||
|
@post_analyzers[raw_hash] ||= PostAnalyzer.new(raw, topic_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
%w{raw_mentions linked_hosts image_count attachment_count link_count raw_links}.each do |attr|
|
%w{raw_mentions linked_hosts image_count attachment_count link_count raw_links}.each do |attr|
|
||||||
define_method(attr) do
|
define_method(attr) do
|
||||||
PostAnalyzer.new(raw, topic_id).send(attr)
|
post_analyzer.send(attr)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def cook(*args)
|
def cook(*args)
|
||||||
PostAnalyzer.new(raw, topic_id).cook(*args)
|
post_analyzer.cook(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -296,6 +292,7 @@ class Post < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: move to post-analyzer?
|
||||||
# Determine what posts are quoted by this post
|
# Determine what posts are quoted by this post
|
||||||
def extract_quoted_post_numbers
|
def extract_quoted_post_numbers
|
||||||
temp_collector = []
|
temp_collector = []
|
||||||
|
|
|
@ -96,20 +96,18 @@ class PostAlertObserver < ActiveRecord::Observer
|
||||||
display_username: opts[:display_username] || post.user.username }.to_json)
|
display_username: opts[:display_username] || post.user.username }.to_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: Move to post-analyzer?
|
||||||
# Returns a list users who have been mentioned
|
# Returns a list users who have been mentioned
|
||||||
def extract_mentioned_users(post)
|
def extract_mentioned_users(post)
|
||||||
User.where(username_lower: post.raw_mentions).where("id <> ?", post.user_id)
|
User.where(username_lower: post.raw_mentions).where("id <> ?", post.user_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: Move to post-analyzer?
|
||||||
# Returns a list of users who were quoted in the post
|
# Returns a list of users who were quoted in the post
|
||||||
def extract_quoted_users(post)
|
def extract_quoted_users(post)
|
||||||
result = []
|
post.raw.scan(/\[quote=\"([^,]+),.+\"\]/).uniq.map do |m|
|
||||||
post.raw.scan(/\[quote=\"([^,]+),.+\"\]/).uniq.each do |m|
|
User.where("username_lower = :username and id != :id", username: m.first.strip.downcase, id: post.user_id).first
|
||||||
username = m.first.strip.downcase
|
end.compact
|
||||||
user = User.where("username_lower = :username and id != :id", username: username, id: post.user_id).first
|
|
||||||
result << user if user.present?
|
|
||||||
end
|
|
||||||
result
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Notify a bunch of users
|
# Notify a bunch of users
|
||||||
|
|
|
@ -1,17 +1,10 @@
|
||||||
class PostAnalyzer
|
class PostAnalyzer
|
||||||
|
|
||||||
attr_accessor :cooked, :raw
|
|
||||||
|
|
||||||
def initialize(raw, topic_id)
|
def initialize(raw, topic_id)
|
||||||
@raw = raw
|
@raw = raw
|
||||||
@topic_id = topic_id
|
@topic_id = topic_id
|
||||||
end
|
end
|
||||||
|
|
||||||
def cooked_document
|
|
||||||
@cooked = cook(@raw, topic_id: @topic_id)
|
|
||||||
@cooked_document = Nokogiri::HTML.fragment(@cooked)
|
|
||||||
end
|
|
||||||
|
|
||||||
# What we use to cook posts
|
# What we use to cook posts
|
||||||
def cook(*args)
|
def cook(*args)
|
||||||
cooked = PrettyText.cook(*args)
|
cooked = PrettyText.cook(*args)
|
||||||
|
@ -110,6 +103,10 @@ class PostAnalyzer
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def cooked_document
|
||||||
|
@cooked_document ||= Nokogiri::HTML.fragment(cook(@raw, topic_id: @topic_id))
|
||||||
|
end
|
||||||
|
|
||||||
def link_is_a_mention?(l)
|
def link_is_a_mention?(l)
|
||||||
html_class = l.attributes['class']
|
html_class = l.attributes['class']
|
||||||
return false if html_class.nil?
|
return false if html_class.nil?
|
||||||
|
|
|
@ -66,8 +66,6 @@ class PostRevisor
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_post
|
def update_post
|
||||||
@post.reset_cooked
|
|
||||||
|
|
||||||
@post.raw = @new_raw
|
@post.raw = @new_raw
|
||||||
@post.updated_by = @user
|
@post.updated_by = @user
|
||||||
@post.last_editor_id = @user.id
|
@post.last_editor_id = @user.id
|
||||||
|
|
Reference in a new issue