2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'jobs'
|
|
|
|
require_dependency 'pretty_text'
|
|
|
|
require_dependency 'rate_limiter'
|
2013-02-09 10:33:07 -05:00
|
|
|
require_dependency 'post_revisor'
|
2013-03-18 15:12:31 -04:00
|
|
|
require_dependency 'enum'
|
2013-05-07 00:39:01 -04:00
|
|
|
require_dependency 'trashable'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
require 'archetype'
|
|
|
|
require 'digest/sha1'
|
|
|
|
|
|
|
|
class Post < ActiveRecord::Base
|
|
|
|
include RateLimiter::OnCreateRecord
|
2013-05-07 00:39:01 -04:00
|
|
|
include Trashable
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-13 12:35:55 -04:00
|
|
|
versioned if: :raw_changed?
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
rate_limit
|
2013-02-19 01:57:14 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic, counter_cache: :posts_count
|
2013-03-19 19:51:39 -04:00
|
|
|
belongs_to :reply_to_user, class_name: "User"
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
has_many :post_replies
|
|
|
|
has_many :replies, through: :post_replies
|
|
|
|
has_many :post_actions
|
|
|
|
|
2013-05-22 15:33:33 -04:00
|
|
|
has_one :post_search_data
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
validates_presence_of :raw, :user_id, :topic_id
|
2013-04-10 08:54:10 -04:00
|
|
|
validates :raw, stripped_length: { in: -> { SiteSetting.post_length } }
|
2013-02-06 20:09:31 -05:00
|
|
|
validate :raw_quality
|
2013-02-05 14:16:51 -05:00
|
|
|
validate :max_mention_validator
|
|
|
|
validate :max_images_validator
|
|
|
|
validate :max_links_validator
|
|
|
|
validate :unique_post_validator
|
|
|
|
|
|
|
|
# We can pass a hash of image sizes when saving to prevent crawling those images
|
|
|
|
attr_accessor :image_sizes, :quoted_post_numbers, :no_bump, :invalidate_oneboxes
|
|
|
|
|
|
|
|
SHORT_POST_CHARS = 1200
|
|
|
|
|
2013-02-21 13:20:00 -05:00
|
|
|
scope :by_newest, order('created_at desc, id desc')
|
2013-05-18 13:35:21 -04:00
|
|
|
scope :by_post_number, order('post_number ASC')
|
2013-02-21 13:20:00 -05:00
|
|
|
scope :with_user, includes(:user)
|
2013-04-10 08:54:10 -04:00
|
|
|
scope :public_posts, -> { joins(:topic).where('topics.archetype <> ?', Archetype.private_message) }
|
|
|
|
scope :private_posts, -> { joins(:topic).where('topics.archetype = ?', Archetype.private_message) }
|
2013-04-16 16:56:18 -04:00
|
|
|
scope :with_topic_subtype, ->(subtype) { joins(:topic).where('topics.subtype = ?', subtype) }
|
2013-02-21 13:20:00 -05:00
|
|
|
|
2013-03-18 14:59:34 -04:00
|
|
|
def self.hidden_reasons
|
|
|
|
@hidden_reasons ||= Enum.new(:flag_threshold_reached, :flag_threshold_reached_again)
|
|
|
|
end
|
|
|
|
|
2013-03-18 16:03:46 -04:00
|
|
|
def self.types
|
|
|
|
@types ||= Enum.new(:regular, :moderator_action)
|
|
|
|
end
|
|
|
|
|
2013-05-07 00:39:01 -04:00
|
|
|
def recover!
|
|
|
|
super
|
|
|
|
update_flagged_posts_count
|
|
|
|
end
|
|
|
|
|
2013-02-06 20:09:31 -05:00
|
|
|
def raw_quality
|
2013-04-10 05:00:50 -04:00
|
|
|
sentinel = TextSentinel.body_sentinel(raw)
|
|
|
|
errors.add(:raw, I18n.t(:is_invalid)) unless sentinel.valid?
|
2013-02-06 20:09:31 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Stop us from posting the same thing too quickly
|
|
|
|
def unique_post_validator
|
|
|
|
return if SiteSetting.unique_posts_mins == 0
|
2013-04-05 15:38:03 -04:00
|
|
|
return if acting_user.admin? || acting_user.moderator?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# If the post is empty, default to the validates_presence_of
|
|
|
|
return if raw.blank?
|
|
|
|
|
|
|
|
if $redis.exists(unique_post_key)
|
|
|
|
errors.add(:raw, I18n.t(:just_posted_that))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-22 06:18:48 -04:00
|
|
|
# The key we use in redis to ensure unique posts
|
2013-02-05 14:16:51 -05:00
|
|
|
def unique_post_key
|
|
|
|
"post-#{user_id}:#{raw_hash}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_hash
|
2013-02-28 13:54:12 -05:00
|
|
|
return if raw.blank?
|
2013-02-05 14:16:51 -05:00
|
|
|
Digest::SHA1.hexdigest(raw.gsub(/\s+/, "").downcase)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cooked_document
|
2013-02-28 13:54:12 -05:00
|
|
|
self.cooked ||= cook(raw, topic_id: topic_id)
|
|
|
|
@cooked_document ||= Nokogiri::HTML.fragment(cooked)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
def reset_cooked
|
|
|
|
@cooked_document = nil
|
|
|
|
self.cooked = nil
|
|
|
|
end
|
|
|
|
|
2013-02-12 02:43:48 -05:00
|
|
|
def self.white_listed_image_classes
|
2013-04-11 12:36:45 -04:00
|
|
|
@white_listed_image_classes ||= ['avatar', 'favicon', 'thumbnail']
|
2013-02-12 02:43:48 -05:00
|
|
|
end
|
|
|
|
|
2013-05-10 16:58:23 -04:00
|
|
|
# How many images are present in the post
|
2013-02-07 10:45:24 -05:00
|
|
|
def image_count
|
2013-02-28 13:54:12 -05:00
|
|
|
return 0 unless raw.present?
|
2013-02-12 02:43:48 -05:00
|
|
|
|
2013-02-28 13:54:12 -05:00
|
|
|
cooked_document.search("img").reject do |t|
|
2013-02-12 02:43:48 -05:00
|
|
|
dom_class = t["class"]
|
|
|
|
if dom_class
|
|
|
|
(Post.white_listed_image_classes & dom_class.split(" ")).count > 0
|
|
|
|
end
|
2013-02-28 13:54:12 -05:00
|
|
|
end.count
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-10 16:58:23 -04:00
|
|
|
# Returns an array of all links in a post
|
|
|
|
def raw_links
|
|
|
|
return [] unless raw.present?
|
|
|
|
|
|
|
|
return @raw_links if @raw_links.present?
|
2013-03-25 13:12:15 -04:00
|
|
|
|
|
|
|
# Don't include @mentions in the link count
|
2013-05-10 16:58:23 -04:00
|
|
|
@raw_links = []
|
2013-03-25 13:12:15 -04:00
|
|
|
cooked_document.search("a[href]").each do |l|
|
|
|
|
html_class = l.attributes['class']
|
2013-05-10 16:58:23 -04:00
|
|
|
url = l.attributes['href'].to_s
|
2013-03-25 13:12:15 -04:00
|
|
|
if html_class.present?
|
|
|
|
next if html_class.to_s == 'mention' && l.attributes['href'].to_s =~ /^\/users\//
|
|
|
|
end
|
2013-05-10 16:58:23 -04:00
|
|
|
@raw_links << url
|
2013-03-25 13:12:15 -04:00
|
|
|
end
|
2013-05-10 16:58:23 -04:00
|
|
|
@raw_links
|
|
|
|
end
|
|
|
|
|
|
|
|
# How many links are present in the post
|
|
|
|
def link_count
|
|
|
|
raw_links.size
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-04-05 13:59:00 -04:00
|
|
|
# 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
|
|
|
|
# restrictions, for example on not posting images.
|
|
|
|
def acting_user
|
|
|
|
@acting_user || user
|
|
|
|
end
|
|
|
|
|
|
|
|
def acting_user=(pu)
|
|
|
|
@acting_user = pu
|
|
|
|
end
|
|
|
|
|
2013-05-10 16:58:23 -04:00
|
|
|
# Ensure maximum amount of mentions in a post
|
2013-02-05 14:16:51 -05:00
|
|
|
def max_mention_validator
|
2013-05-17 12:15:21 -04:00
|
|
|
if acting_user_is_trusted?
|
|
|
|
add_error_if_count_exceeded(:too_many_mentions, raw_mentions.size, SiteSetting.max_mentions_per_post)
|
2013-04-02 11:58:12 -04:00
|
|
|
else
|
2013-05-17 12:15:21 -04:00
|
|
|
add_error_if_count_exceeded(:too_many_mentions_newuser, raw_mentions.size, SiteSetting.newuser_max_mentions_per_post)
|
2013-04-02 11:58:12 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-10 16:58:23 -04:00
|
|
|
# Ensure new users can not put too many images in a post
|
2013-02-05 14:16:51 -05:00
|
|
|
def max_images_validator
|
2013-05-17 12:15:21 -04:00
|
|
|
add_error_if_count_exceeded(:too_many_images, image_count, SiteSetting.newuser_max_images) unless acting_user_is_trusted?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-10 16:58:23 -04:00
|
|
|
# Ensure new users can not put too many links in a post
|
2013-02-05 14:16:51 -05:00
|
|
|
def max_links_validator
|
2013-05-17 12:15:21 -04:00
|
|
|
add_error_if_count_exceeded(:too_many_links, link_count, SiteSetting.newuser_max_links) unless acting_user_is_trusted?
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
|
2013-05-10 16:58:23 -04:00
|
|
|
# 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
|
2013-05-24 15:20:58 -04:00
|
|
|
@linked_hosts[host] ||= 1
|
2013-05-10 16:58:23 -04:00
|
|
|
end
|
|
|
|
@linked_hosts
|
|
|
|
end
|
|
|
|
|
|
|
|
def total_hosts_usage
|
|
|
|
hosts = linked_hosts.clone
|
|
|
|
|
2013-05-24 15:20:58 -04:00
|
|
|
TopicLink.where(domain: hosts.keys, user_id: acting_user.id)
|
|
|
|
.group(:domain, :post_id)
|
|
|
|
.count.keys.each do |tuple|
|
|
|
|
domain = tuple[0]
|
|
|
|
hosts[domain] = (hosts[domain] || 0) + 1
|
2013-05-10 16:58:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
hosts
|
|
|
|
end
|
|
|
|
|
|
|
|
# Prevent new users from posting the same hosts too many times.
|
|
|
|
def has_host_spam?
|
|
|
|
return false if acting_user.present? && acting_user.has_trust_level?(:basic)
|
|
|
|
|
|
|
|
total_hosts_usage.each do |host, count|
|
|
|
|
return true if count >= SiteSetting.newuser_spam_host_threshold
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def raw_mentions
|
2013-02-07 10:45:24 -05:00
|
|
|
return [] if raw.blank?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
# We don't count mentions in quotes
|
|
|
|
return @raw_mentions if @raw_mentions.present?
|
2013-02-05 14:16:51 -05:00
|
|
|
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
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
results = doc.to_html.scan(PrettyText.mention_matcher)
|
2013-02-28 13:54:12 -05:00
|
|
|
@raw_mentions = results.uniq.map { |un| un.first.downcase.gsub!(/^@/, '') }
|
2013-02-07 15:12:55 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def archetype
|
|
|
|
topic.archetype
|
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.regular_order
|
2013-02-07 10:45:24 -05:00
|
|
|
order(:sort_order, :post_number)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.reverse_order
|
2013-02-07 10:45:24 -05:00
|
|
|
order('sort_order desc, post_number desc')
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.best_of
|
2013-03-22 15:43:57 -04:00
|
|
|
where(["(post_number = 1) or (percent_rank <= ?)", SiteSetting.best_of_percent_filter.to_f / 100.0])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 20:13:41 -05:00
|
|
|
def update_flagged_posts_count
|
|
|
|
PostAction.update_flagged_posts_count
|
|
|
|
end
|
|
|
|
|
2013-02-28 13:54:12 -05:00
|
|
|
def filter_quotes(parent_post = nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
return cooked if parent_post.blank?
|
|
|
|
|
|
|
|
# We only filter quotes when there is exactly 1
|
|
|
|
return cooked unless (quote_count == 1)
|
|
|
|
|
2013-02-15 20:58:33 -05:00
|
|
|
parent_raw = parent_post.raw.sub(/\[quote.+\/quote\]/m, '')
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-04 19:42:44 -05:00
|
|
|
if raw[parent_raw] || (parent_raw.size < SHORT_POST_CHARS)
|
2013-02-05 14:16:51 -05:00
|
|
|
return cooked.sub(/\<aside.+\<\/aside\>/m, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
cooked
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
user.username
|
|
|
|
end
|
|
|
|
|
|
|
|
def external_id
|
2013-02-07 10:45:24 -05:00
|
|
|
"#{topic_id}/#{post_number}"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def quoteless?
|
2013-02-28 13:54:12 -05:00
|
|
|
(quote_count == 0) && (reply_to_post_number.present?)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reply_notification_target
|
2013-02-28 13:54:12 -05:00
|
|
|
return if reply_to_post_number.blank?
|
|
|
|
Post.where("topic_id = :topic_id AND post_number = :post_number AND user_id <> :user_id",
|
|
|
|
topic_id: topic_id,
|
|
|
|
post_number: reply_to_post_number,
|
|
|
|
user_id: user_id).first.try(:user)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-04-29 23:25:55 -04:00
|
|
|
def self.excerpt(cooked, maxlength = nil, options = {})
|
2013-02-05 14:16:51 -05:00
|
|
|
maxlength ||= SiteSetting.post_excerpt_maxlength
|
2013-04-29 23:25:55 -04:00
|
|
|
PrettyText.excerpt(cooked, maxlength, options)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Strip out most of the markup
|
2013-04-29 23:25:55 -04:00
|
|
|
def excerpt(maxlength = nil, options = {})
|
|
|
|
Post.excerpt(cooked, maxlength, options)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# What we use to cook posts
|
|
|
|
def cook(*args)
|
2013-02-07 10:45:24 -05:00
|
|
|
cooked = PrettyText.cook(*args)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
# If we have any of the oneboxes in the cache, throw them in right away, don't
|
2013-02-05 14:16:51 -05:00
|
|
|
# wait for the post processor.
|
|
|
|
dirty = false
|
2013-04-10 03:52:38 -04:00
|
|
|
result = Oneboxer.apply(cooked) do |url, elem|
|
|
|
|
Oneboxer.render_from_cache(url)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-04-10 03:52:38 -04:00
|
|
|
cooked = result.to_html if result.changed?
|
2013-02-05 14:16:51 -05:00
|
|
|
cooked
|
|
|
|
end
|
|
|
|
|
|
|
|
# A list of versions including the initial version
|
|
|
|
def all_versions
|
|
|
|
result = []
|
2013-03-13 12:35:55 -04:00
|
|
|
result << { number: 1, display_username: user.username, created_at: created_at }
|
2013-02-05 14:16:51 -05:00
|
|
|
versions.order(:number).includes(:user).each do |v|
|
2013-03-13 12:35:55 -04:00
|
|
|
if v.user.present?
|
|
|
|
result << { number: v.number, display_username: v.user.username, created_at: v.created_at }
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
def is_flagged?
|
2013-03-01 07:07:44 -05:00
|
|
|
post_actions.where(post_action_type_id: PostActionType.flag_types.values, deleted_at: nil).count != 0
|
2013-02-06 23:15:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def unhide!
|
|
|
|
self.hidden = false
|
|
|
|
self.hidden_reason_id = nil
|
|
|
|
self.topic.update_attributes(visible: true)
|
2013-02-28 13:54:12 -05:00
|
|
|
save
|
2013-02-06 23:15:48 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def url
|
2013-04-24 04:05:35 -04:00
|
|
|
Post.url(topic.slug, topic.id, post_number)
|
2013-04-22 03:45:03 -04:00
|
|
|
end
|
|
|
|
|
2013-04-24 04:05:35 -04:00
|
|
|
def self.url(slug, topic_id, post_number)
|
|
|
|
"/t/#{slug}/#{topic_id}/#{post_number}"
|
2013-04-22 03:45:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.urls(post_ids)
|
|
|
|
ids = post_ids.map{|u| u}
|
|
|
|
if ids.length > 0
|
|
|
|
urls = {}
|
|
|
|
Topic.joins(:posts).where('posts.id' => ids).
|
2013-04-24 04:05:35 -04:00
|
|
|
select(['posts.id as post_id','post_number', 'topics.slug', 'topics.title', 'topics.id']).
|
2013-04-22 03:45:03 -04:00
|
|
|
each do |t|
|
2013-04-24 04:05:35 -04:00
|
|
|
urls[t.post_id.to_i] = url(t.slug, t.id, t.post_number)
|
2013-04-22 03:45:03 -04:00
|
|
|
end
|
|
|
|
urls
|
2013-04-24 04:05:35 -04:00
|
|
|
else
|
2013-04-22 03:45:03 -04:00
|
|
|
{}
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-21 13:20:00 -05:00
|
|
|
def author_readable
|
|
|
|
user.readable_name
|
|
|
|
end
|
|
|
|
|
2013-02-28 13:54:12 -05:00
|
|
|
def revise(updated_by, new_raw, opts = {})
|
2013-02-09 10:33:07 -05:00
|
|
|
PostRevisor.new(self).revise!(updated_by, new_raw, opts)
|
|
|
|
end
|
|
|
|
|
2013-03-19 19:51:39 -04:00
|
|
|
|
|
|
|
# TODO: move into PostCreator
|
2013-02-05 14:16:51 -05:00
|
|
|
# Various callbacks
|
|
|
|
before_create do
|
2013-03-19 19:51:39 -04:00
|
|
|
if reply_to_post_number.present?
|
2013-03-21 13:11:54 -04:00
|
|
|
self.reply_to_user_id ||= Post.select(:user_id).where(topic_id: topic_id, post_number: reply_to_post_number).first.try(:user_id)
|
2013-03-19 19:51:39 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
self.post_number ||= Topic.next_post_number(topic_id, reply_to_post_number.present?)
|
|
|
|
self.cooked ||= cook(raw, topic_id: topic_id)
|
|
|
|
self.sort_order = post_number
|
2013-02-07 10:45:24 -05:00
|
|
|
DiscourseEvent.trigger(:before_create_post, self)
|
2013-02-05 14:16:51 -05:00
|
|
|
self.last_version_at ||= Time.now
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Move some of this into an asynchronous job?
|
2013-03-19 19:51:39 -04:00
|
|
|
# TODO: Move into PostCreator
|
2013-02-05 14:16:51 -05:00
|
|
|
after_create do
|
|
|
|
# Update attributes on the topic - featured users and last posted.
|
2013-02-28 13:54:12 -05:00
|
|
|
attrs = {last_posted_at: created_at, last_post_user_id: user_id}
|
|
|
|
attrs[:bumped_at] = created_at unless no_bump
|
2013-02-05 14:16:51 -05:00
|
|
|
topic.update_attributes(attrs)
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
# Update topic user data
|
|
|
|
TopicUser.change(user,
|
2013-02-28 13:54:12 -05:00
|
|
|
topic.id,
|
|
|
|
posted: true,
|
|
|
|
last_read_post_number: post_number,
|
|
|
|
seen_post_count: post_number)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# This calculates the geometric mean of the post timings and stores it along with
|
2013-02-07 10:45:24 -05:00
|
|
|
# each post.
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.calculate_avg_time
|
2013-02-11 15:47:28 -05:00
|
|
|
retry_lock_error do
|
|
|
|
exec_sql("UPDATE posts
|
|
|
|
SET avg_time = (x.gmean / 1000)
|
|
|
|
FROM (SELECT post_timings.topic_id,
|
|
|
|
post_timings.post_number,
|
|
|
|
round(exp(avg(ln(msecs)))) AS gmean
|
|
|
|
FROM post_timings
|
|
|
|
INNER JOIN posts AS p2
|
|
|
|
ON p2.post_number = post_timings.post_number
|
|
|
|
AND p2.topic_id = post_timings.topic_id
|
|
|
|
AND p2.user_id <> post_timings.user_id
|
|
|
|
GROUP BY post_timings.topic_id, post_timings.post_number) AS x
|
|
|
|
WHERE x.topic_id = posts.topic_id
|
|
|
|
AND x.post_number = posts.post_number")
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
before_save do
|
2013-02-28 13:54:12 -05:00
|
|
|
self.last_editor_id ||= user_id
|
2013-02-05 14:16:51 -05:00
|
|
|
self.cooked = cook(raw, topic_id: topic_id) unless new_record?
|
|
|
|
end
|
|
|
|
|
2013-03-18 15:12:31 -04:00
|
|
|
def advance_draft_sequence
|
|
|
|
return if topic.blank? # could be deleted
|
|
|
|
DraftSequence.next!(last_editor_id, topic.draft_key)
|
|
|
|
end
|
|
|
|
|
2013-05-22 15:45:31 -04:00
|
|
|
|
2013-03-18 15:54:08 -04:00
|
|
|
# Determine what posts are quoted by this post
|
2013-02-05 14:16:51 -05:00
|
|
|
def extract_quoted_post_numbers
|
2013-05-22 15:45:31 -04:00
|
|
|
temp_collector = []
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# Create relationships for the quotes
|
2013-05-22 15:38:45 -04:00
|
|
|
raw.scan(/\[quote=\"([^"]+)"\]/).each do |quote|
|
|
|
|
args = parse_quote_into_arguments(quote)
|
2013-05-22 15:45:31 -04:00
|
|
|
# If the topic attribute is present, ensure it's the same topic
|
|
|
|
temp_collector << args[:post] unless (args[:topic].present? && topic_id != args[:topic])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-05-22 15:45:31 -04:00
|
|
|
temp_collector.uniq!
|
|
|
|
self.quoted_post_numbers = temp_collector
|
|
|
|
self.quote_count = temp_collector.size
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-23 12:09:06 -04:00
|
|
|
|
2013-03-18 15:54:08 -04:00
|
|
|
def save_reply_relationships
|
2013-05-23 12:09:06 -04:00
|
|
|
add_to_quoted_post_numbers(reply_to_post_number)
|
|
|
|
return if self.quoted_post_numbers.blank?
|
2013-03-18 15:54:08 -04:00
|
|
|
|
|
|
|
# Create a reply relationship between quoted posts and this new post
|
2013-05-23 12:09:06 -04:00
|
|
|
self.quoted_post_numbers.each do |p|
|
|
|
|
post = Post.where(topic_id: topic_id, post_number: p).first
|
|
|
|
create_reply_relationship_with(post)
|
2013-03-18 15:54:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-18 13:55:34 -04:00
|
|
|
# Enqueue post processing for this post
|
2013-02-05 14:16:51 -05:00
|
|
|
def trigger_post_process
|
2013-02-28 13:54:12 -05:00
|
|
|
args = { post_id: id }
|
|
|
|
args[:image_sizes] = image_sizes if image_sizes.present?
|
|
|
|
args[:invalidate_oneboxes] = true if invalidate_oneboxes.present?
|
2013-02-07 10:45:24 -05:00
|
|
|
Jobs.enqueue(:process_post, args)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-03-07 11:07:59 -05:00
|
|
|
|
2013-04-16 16:56:18 -04:00
|
|
|
def self.public_posts_count_per_day(since_days_ago=30)
|
|
|
|
public_posts.where('posts.created_at > ?', since_days_ago.days.ago).group('date(posts.created_at)').order('date(posts.created_at)').count
|
2013-04-03 13:25:52 -04:00
|
|
|
end
|
|
|
|
|
2013-04-16 16:56:18 -04:00
|
|
|
def self.private_messages_count_per_day(since_days_ago, topic_subtype)
|
|
|
|
private_posts.with_topic_subtype(topic_subtype).where('posts.created_at > ?', since_days_ago.days.ago).group('date(posts.created_at)').order('date(posts.created_at)').count
|
2013-03-07 11:07:59 -05:00
|
|
|
end
|
2013-05-17 12:15:21 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def acting_user_is_trusted?
|
|
|
|
acting_user.present? && acting_user.has_trust_level?(:basic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_error_if_count_exceeded(key_for_translation, current_count, max_count)
|
|
|
|
errors.add(:base, I18n.t(key_for_translation, count: max_count)) if current_count > max_count
|
|
|
|
end
|
2013-05-22 15:45:31 -04:00
|
|
|
|
2013-05-22 15:38:45 -04:00
|
|
|
def parse_quote_into_arguments(quote)
|
|
|
|
return {} unless quote.present?
|
|
|
|
args = {}
|
|
|
|
quote.first.scan(/([a-z]+)\:(\d+)/).each do |arg|
|
|
|
|
args[arg[0].to_sym] = arg[1].to_i
|
|
|
|
end
|
|
|
|
args
|
|
|
|
end
|
2013-05-22 15:45:31 -04:00
|
|
|
|
2013-05-23 12:07:45 -04:00
|
|
|
def add_to_quoted_post_numbers(num)
|
|
|
|
return unless num.present?
|
|
|
|
self.quoted_post_numbers ||= []
|
|
|
|
self.quoted_post_numbers << num
|
|
|
|
end
|
2013-05-23 12:08:24 -04:00
|
|
|
|
|
|
|
def create_reply_relationship_with(post)
|
|
|
|
return if post.nil?
|
|
|
|
post_reply = post.post_replies.new(reply_id: id)
|
|
|
|
if post_reply.save
|
|
|
|
Post.update_all ['reply_count = reply_count + 1'], id: post.id
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: posts
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# topic_id :integer not null
|
|
|
|
# post_number :integer not null
|
|
|
|
# raw :text not null
|
|
|
|
# cooked :text not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# reply_to_post_number :integer
|
|
|
|
# cached_version :integer default(1), not null
|
|
|
|
# reply_count :integer default(0), not null
|
|
|
|
# quote_count :integer default(0), not null
|
|
|
|
# deleted_at :datetime
|
|
|
|
# off_topic_count :integer default(0), not null
|
|
|
|
# like_count :integer default(0), not null
|
|
|
|
# incoming_link_count :integer default(0), not null
|
|
|
|
# bookmark_count :integer default(0), not null
|
|
|
|
# avg_time :integer
|
|
|
|
# score :float
|
|
|
|
# reads :integer default(0), not null
|
|
|
|
# post_type :integer default(1), not null
|
|
|
|
# vote_count :integer default(0), not null
|
|
|
|
# sort_order :integer
|
|
|
|
# last_editor_id :integer
|
|
|
|
# hidden :boolean default(FALSE), not null
|
|
|
|
# hidden_reason_id :integer
|
|
|
|
# notify_moderators_count :integer default(0), not null
|
|
|
|
# spam_count :integer default(0), not null
|
|
|
|
# illegal_count :integer default(0), not null
|
|
|
|
# inappropriate_count :integer default(0), not null
|
|
|
|
# last_version_at :datetime not null
|
|
|
|
# user_deleted :boolean default(FALSE), not null
|
|
|
|
# reply_to_user_id :integer
|
|
|
|
# percent_rank :float default(1.0)
|
|
|
|
# notify_user_count :integer default(0), not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# idx_posts_user_id_deleted_at (user_id)
|
|
|
|
# index_posts_on_reply_to_post_number (reply_to_post_number)
|
|
|
|
# index_posts_on_topic_id_and_post_number (topic_id,post_number) UNIQUE
|
|
|
|
#
|
|
|
|
|