2013-10-01 03:04:02 -04:00
|
|
|
require_dependency 'jobs/base'
|
2013-02-05 14:16:51 -05:00
|
|
|
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-30 14:34:44 -04:00
|
|
|
require_dependency 'post_analyzer'
|
2013-06-13 04:18:17 -04:00
|
|
|
require_dependency 'validators/post_validator'
|
2013-10-10 03:45:40 -04:00
|
|
|
require_dependency 'plugin/filter'
|
2015-06-05 11:46:21 -04:00
|
|
|
require_dependency 'email_cook'
|
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
|
2014-04-28 04:31:51 -04:00
|
|
|
include HasCustomFields
|
2015-02-25 14:53:21 -05:00
|
|
|
include LimitedEdit
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-05-30 00:45:39 -04:00
|
|
|
# increase this number to force a system wide post rebake
|
|
|
|
BAKED_VERSION = 1
|
2014-05-27 22:30:43 -04:00
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
rate_limit
|
2013-10-09 19:32:03 -04:00
|
|
|
rate_limit :limit_posts_per_day
|
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
|
2014-07-16 15:04:55 -04:00
|
|
|
|
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-06-13 13:41:45 -04:00
|
|
|
has_many :topic_links
|
2015-12-01 00:52:43 -05:00
|
|
|
has_many :group_mentions, dependent: :destroy
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-13 17:44:24 -04:00
|
|
|
has_many :post_uploads
|
|
|
|
has_many :uploads, through: :post_uploads
|
2013-06-12 19:43:50 -04:00
|
|
|
|
2013-05-22 15:33:33 -04:00
|
|
|
has_one :post_search_data
|
2015-08-03 00:29:04 -04:00
|
|
|
has_one :post_stat
|
2013-05-22 15:33:33 -04:00
|
|
|
|
2013-10-15 10:21:30 -04:00
|
|
|
has_many :post_details
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
has_many :post_revisions
|
|
|
|
has_many :revisions, foreign_key: :post_id, class_name: 'PostRevision'
|
|
|
|
|
2014-07-16 15:04:55 -04:00
|
|
|
has_many :user_actions, foreign_key: :target_post_id
|
|
|
|
|
2013-06-13 04:18:17 -04:00
|
|
|
validates_with ::Validators::PostValidator
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-21 11:36:33 -04:00
|
|
|
# We can pass several creating options to a post via attributes
|
2013-09-06 11:50:05 -04:00
|
|
|
attr_accessor :image_sizes, :quoted_post_numbers, :no_bump, :invalidate_oneboxes, :cooking_options, :skip_unique_check
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
SHORT_POST_CHARS = 1200
|
|
|
|
|
2013-06-09 12:48:44 -04:00
|
|
|
scope :by_newest, -> { order('created_at desc, id desc') }
|
|
|
|
scope :by_post_number, -> { order('post_number ASC') }
|
|
|
|
scope :with_user, -> { includes(:user) }
|
2013-12-13 15:15:51 -05:00
|
|
|
scope :created_since, lambda { |time_ago| where('posts.created_at > ?', time_ago) }
|
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) }
|
2014-06-26 13:48:07 -04:00
|
|
|
scope :visible, -> { joins(:topic).where('topics.visible = true').where(hidden: false) }
|
2015-09-24 20:15:58 -04:00
|
|
|
scope :secured, lambda { |guardian| where('posts.post_type in (?)', Topic.visible_post_types(guardian && guardian.user))}
|
2014-03-07 04:44:04 -05:00
|
|
|
|
2014-01-14 11:15:35 -05:00
|
|
|
delegate :username, to: :user
|
2014-03-07 04:44:04 -05:00
|
|
|
|
2013-03-18 14:59:34 -04:00
|
|
|
def self.hidden_reasons
|
2016-01-08 05:53:52 -05:00
|
|
|
@hidden_reasons ||= Enum.new(flag_threshold_reached: 1,
|
|
|
|
flag_threshold_reached_again: 2,
|
|
|
|
new_user_spam_threshold_reached: 3,
|
|
|
|
flagged_by_tl3_user: 4)
|
2013-03-18 14:59:34 -04:00
|
|
|
end
|
|
|
|
|
2013-03-18 16:03:46 -04:00
|
|
|
def self.types
|
2016-01-08 05:53:52 -05:00
|
|
|
@types ||= Enum.new(regular: 1,
|
|
|
|
moderator_action: 2,
|
|
|
|
small_action: 3,
|
|
|
|
whisper: 4)
|
2013-03-18 16:03:46 -04:00
|
|
|
end
|
|
|
|
|
2013-12-31 14:37:43 -05:00
|
|
|
def self.cook_methods
|
2016-01-08 05:53:52 -05:00
|
|
|
@cook_methods ||= Enum.new(regular: 1,
|
|
|
|
raw_html: 2,
|
|
|
|
email: 3)
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
2013-10-15 10:21:30 -04:00
|
|
|
def self.find_by_detail(key, value)
|
2014-05-06 09:41:59 -04:00
|
|
|
includes(:post_details).find_by(post_details: { key: key, value: value })
|
2013-10-15 10:21:30 -04:00
|
|
|
end
|
|
|
|
|
2016-01-11 11:47:17 -05:00
|
|
|
def whisper?
|
|
|
|
post_type == Post.types[:whisper]
|
|
|
|
end
|
|
|
|
|
2013-10-15 10:21:30 -04:00
|
|
|
def add_detail(key, value, extra = nil)
|
|
|
|
post_details.build(key: key, value: value, extra: extra)
|
|
|
|
end
|
|
|
|
|
2013-10-09 19:32:03 -04:00
|
|
|
def limit_posts_per_day
|
2015-09-23 01:24:30 -04:00
|
|
|
if user && user.first_day_user? && post_number && post_number > 1
|
2015-02-11 01:45:46 -05:00
|
|
|
RateLimiter.new(user, "first-day-replies-per-day", SiteSetting.max_replies_in_first_day, 1.day.to_i)
|
2013-10-09 19:32:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-07 10:29:01 -04:00
|
|
|
def publish_change_to_clients!(type, options = {})
|
2015-09-10 16:01:23 -04:00
|
|
|
# special failsafe for posts missing topics consistency checks should fix, but message
|
2014-11-10 05:24:54 -05:00
|
|
|
# is safe to skip
|
2015-09-10 16:01:23 -04:00
|
|
|
return unless topic
|
|
|
|
|
2015-09-21 18:50:52 -04:00
|
|
|
channel = "/topic/#{topic_id}"
|
|
|
|
msg = {
|
|
|
|
id: id,
|
|
|
|
post_number: post_number,
|
|
|
|
updated_at: Time.now,
|
2015-10-11 21:45:04 -04:00
|
|
|
user_id: user_id,
|
|
|
|
last_editor_id: last_editor_id,
|
2015-09-21 18:50:52 -04:00
|
|
|
type: type
|
2016-04-07 10:29:01 -04:00
|
|
|
}.merge(options)
|
2015-09-21 18:50:52 -04:00
|
|
|
|
2015-09-24 20:15:58 -04:00
|
|
|
if Topic.visible_post_types.include?(post_type)
|
|
|
|
MessageBus.publish(channel, msg, group_ids: topic.secure_group_ids)
|
|
|
|
else
|
2015-09-10 16:01:23 -04:00
|
|
|
user_ids = User.where('admin or moderator or id = ?', user_id).pluck(:id)
|
|
|
|
MessageBus.publish(channel, msg, user_ids: user_ids)
|
|
|
|
end
|
2014-08-28 23:34:32 -04:00
|
|
|
end
|
|
|
|
|
2013-07-09 15:20:18 -04:00
|
|
|
def trash!(trashed_by=nil)
|
2013-06-13 13:41:45 -04:00
|
|
|
self.topic_links.each(&:destroy)
|
2013-07-09 15:20:18 -04:00
|
|
|
super(trashed_by)
|
2013-06-13 13:41:45 -04:00
|
|
|
end
|
|
|
|
|
2013-05-07 00:39:01 -04:00
|
|
|
def recover!
|
|
|
|
super
|
|
|
|
update_flagged_posts_count
|
2013-06-13 13:41:45 -04:00
|
|
|
TopicLink.extract_from(self)
|
2014-07-15 03:47:24 -04:00
|
|
|
QuotedPost.extract_from(self)
|
2013-10-23 19:05:51 -04:00
|
|
|
if topic && topic.category_id && topic.category
|
2013-10-17 02:44:56 -04:00
|
|
|
topic.category.update_latest
|
|
|
|
end
|
2013-05-07 00:39:01 -04:00
|
|
|
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
|
2015-02-02 12:44:21 -05:00
|
|
|
"unique-post-#{user_id}:#{raw_hash}"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-09-09 16:17:31 -04:00
|
|
|
def store_unique_post_key
|
|
|
|
if SiteSetting.unique_posts_mins > 0
|
2014-03-07 02:23:15 -05:00
|
|
|
$redis.setex(unique_post_key, SiteSetting.unique_posts_mins.minutes.to_i, id)
|
2013-09-09 16:17:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches_recent_post?
|
2014-03-07 02:23:15 -05:00
|
|
|
post_id = $redis.get(unique_post_key)
|
2015-02-02 12:44:21 -05:00
|
|
|
post_id != nil and post_id.to_i != id
|
2013-09-09 16:17:31 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def raw_hash
|
2013-02-28 13:54:12 -05:00
|
|
|
return if raw.blank?
|
2014-06-15 22:14:06 -04:00
|
|
|
Digest::SHA1.hexdigest(raw)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-12 02:43:48 -05:00
|
|
|
def self.white_listed_image_classes
|
2013-12-12 10:42:27 -05:00
|
|
|
@white_listed_image_classes ||= ['avatar', 'favicon', 'thumbnail']
|
2013-02-12 02:43:48 -05:00
|
|
|
end
|
|
|
|
|
2013-05-30 14:34:44 -04:00
|
|
|
def post_analyzer
|
2013-07-22 16:24:47 -04:00
|
|
|
@post_analyzers ||= {}
|
|
|
|
@post_analyzers[raw_hash] ||= PostAnalyzer.new(raw, topic_id)
|
2013-05-30 14:34:44 -04:00
|
|
|
end
|
2013-02-12 02:43:48 -05:00
|
|
|
|
2013-07-21 20:39:17 -04:00
|
|
|
%w{raw_mentions linked_hosts image_count attachment_count link_count raw_links}.each do |attr|
|
2013-05-30 14:34:44 -04:00
|
|
|
define_method(attr) do
|
2013-07-22 16:24:47 -04:00
|
|
|
post_analyzer.send(attr)
|
2013-05-30 14:34:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cook(*args)
|
2013-12-31 14:37:43 -05:00
|
|
|
# For some posts, for example those imported via RSS, we support raw HTML. In that
|
|
|
|
# case we can skip the rendering pipeline.
|
|
|
|
return raw if cook_method == Post.cook_methods[:raw_html]
|
|
|
|
|
2015-06-05 11:46:21 -04:00
|
|
|
cooked = nil
|
|
|
|
if cook_method == Post.cook_methods[:email]
|
|
|
|
cooked = EmailCook.new(raw).cook
|
|
|
|
else
|
|
|
|
cooked = if !self.user || SiteSetting.tl3_links_no_follow || !self.user.has_trust_level?(TrustLevel[3])
|
|
|
|
post_analyzer.cook(*args)
|
|
|
|
else
|
|
|
|
# At trust level 3, we don't apply nofollow to links
|
|
|
|
cloned = args.dup
|
|
|
|
cloned[1] ||= {}
|
|
|
|
cloned[1][:omit_nofollow] = true
|
|
|
|
post_analyzer.cook(*cloned)
|
|
|
|
end
|
|
|
|
end
|
2014-11-23 18:34:29 -05:00
|
|
|
|
|
|
|
new_cooked = Plugin::Filter.apply(:after_post_cook, self, cooked)
|
|
|
|
|
2015-07-29 14:54:33 -04:00
|
|
|
if post_type == Post.types[:regular]
|
|
|
|
if new_cooked != cooked && new_cooked.blank?
|
2016-03-23 11:44:55 -04:00
|
|
|
Rails.logger.debug("Plugin is blanking out post: #{self.url}\nraw: #{self.raw}")
|
2015-07-29 14:54:33 -04:00
|
|
|
elsif new_cooked.blank?
|
2016-03-23 11:44:55 -04:00
|
|
|
Rails.logger.debug("Blank post detected post: #{self.url}\nraw: #{self.raw}")
|
2015-07-29 14:54:33 -04:00
|
|
|
end
|
2014-11-23 18:34:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
new_cooked
|
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
|
|
|
|
|
2016-03-08 15:26:06 -05:00
|
|
|
def last_editor
|
|
|
|
self.last_editor_id ? (User.find_by_id(self.last_editor_id) || user) : user
|
|
|
|
end
|
|
|
|
|
2014-02-26 23:43:45 -05:00
|
|
|
def whitelisted_spam_hosts
|
|
|
|
|
|
|
|
hosts = SiteSetting
|
|
|
|
.white_listed_spam_host_domains
|
2014-03-29 19:50:44 -04:00
|
|
|
.split('|')
|
2014-02-26 23:43:45 -05:00
|
|
|
.map{|h| h.strip}
|
2014-03-29 19:50:44 -04:00
|
|
|
.reject{|h| !h.include?('.')}
|
2014-02-26 23:43:45 -05:00
|
|
|
|
|
|
|
hosts << GlobalSetting.hostname
|
2014-04-28 10:37:28 -04:00
|
|
|
hosts << RailsMultisite::ConnectionManagement.current_hostname
|
2014-02-26 23:43:45 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-05-10 16:58:23 -04:00
|
|
|
def total_hosts_usage
|
|
|
|
hosts = linked_hosts.clone
|
2014-02-26 23:43:45 -05:00
|
|
|
whitelisted = whitelisted_spam_hosts
|
|
|
|
|
|
|
|
hosts.reject! do |h|
|
|
|
|
whitelisted.any? do |w|
|
|
|
|
h.end_with?(w)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return hosts if hosts.length == 0
|
2013-05-10 16:58:23 -04:00
|
|
|
|
2013-05-24 15:20:58 -04:00
|
|
|
TopicLink.where(domain: hosts.keys, user_id: acting_user.id)
|
|
|
|
.group(:domain, :post_id)
|
2015-04-18 07:53:53 -04:00
|
|
|
.count.each_key do |tuple|
|
2013-05-24 15:20:58 -04:00
|
|
|
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?
|
2014-09-05 01:20:39 -04:00
|
|
|
return false if acting_user.present? && acting_user.has_trust_level?(TrustLevel[1])
|
2013-05-10 16:58:23 -04:00
|
|
|
|
2014-08-14 17:54:55 -04:00
|
|
|
total_hosts_usage.each do |_, count|
|
2013-05-10 16:58:23 -04:00
|
|
|
return true if count >= SiteSetting.newuser_spam_host_threshold
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
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
|
|
|
|
|
2015-01-30 01:19:42 -05:00
|
|
|
def self.summary(topic_id=nil)
|
|
|
|
# PERF: if you pass in nil it is WAY slower
|
|
|
|
# pg chokes getting a reasonable plan
|
|
|
|
topic_id = topic_id ? topic_id.to_i : "posts.topic_id"
|
|
|
|
|
|
|
|
# percent rank has tons of ties
|
|
|
|
where(["post_number = 1 or id in (
|
|
|
|
SELECT p1.id
|
|
|
|
FROM posts p1
|
|
|
|
WHERE p1.percent_rank <= ? AND
|
|
|
|
p1.topic_id = #{topic_id}
|
|
|
|
ORDER BY p1.percent_rank
|
|
|
|
LIMIT ?
|
|
|
|
)",
|
|
|
|
SiteSetting.summary_percent_filter.to_f / 100.0,
|
|
|
|
SiteSetting.summary_max_results
|
|
|
|
])
|
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 external_id
|
2013-02-07 10:45:24 -05:00
|
|
|
"#{topic_id}/#{post_number}"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-01-03 12:52:24 -05:00
|
|
|
def reply_to_post
|
|
|
|
return if reply_to_post_number.blank?
|
2014-05-06 09:41:59 -04:00
|
|
|
@reply_to_post ||= Post.find_by("topic_id = :topic_id AND post_number = :post_number", topic_id: topic_id, post_number: reply_to_post_number)
|
2014-01-03 12:52:24 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def reply_notification_target
|
2013-02-28 13:54:12 -05:00
|
|
|
return if reply_to_post_number.blank?
|
2014-05-06 09:41:59 -04:00
|
|
|
Post.find_by("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).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
|
|
|
|
|
2013-05-25 20:18:04 -04:00
|
|
|
def is_first_post?
|
2015-04-23 13:33:29 -04:00
|
|
|
post_number.blank? ?
|
|
|
|
topic.try(:highest_post_number) == 0 :
|
|
|
|
post_number == 1
|
2013-05-25 20:18:04 -04:00
|
|
|
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
|
|
|
|
|
2016-03-30 13:27:34 -04:00
|
|
|
def has_active_flag?
|
|
|
|
post_actions.active.where(post_action_type_id: PostActionType.flag_types.values).count != 0
|
|
|
|
end
|
|
|
|
|
2013-02-06 23:15:48 -05:00
|
|
|
def unhide!
|
2015-12-29 16:59:26 -05:00
|
|
|
self.update_attributes(hidden: false)
|
2015-04-23 13:33:29 -04:00
|
|
|
self.topic.update_attributes(visible: true) if is_first_post?
|
2014-08-11 04:48:00 -04:00
|
|
|
save(validate: false)
|
2014-09-22 12:55:13 -04:00
|
|
|
publish_change_to_clients!(:acted)
|
2013-02-06 23:15:48 -05:00
|
|
|
end
|
|
|
|
|
2016-01-12 12:38:49 -05:00
|
|
|
def full_url
|
|
|
|
"#{Discourse.base_url}#{url}"
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def url
|
2015-08-11 17:28:36 -04:00
|
|
|
if topic
|
|
|
|
Post.url(topic.slug, topic.id, post_number)
|
|
|
|
else
|
|
|
|
"/404"
|
|
|
|
end
|
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
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def revise(updated_by, changes={}, opts={})
|
|
|
|
PostRevisor.new(self).revise!(updated_by, changes, opts)
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2014-05-27 22:30:43 -04:00
|
|
|
def self.rebake_old(limit)
|
2014-07-17 16:22:46 -04:00
|
|
|
problems = []
|
2014-05-30 00:45:39 -04:00
|
|
|
Post.where('baked_version IS NULL OR baked_version < ?', BAKED_VERSION)
|
2014-05-27 22:30:43 -04:00
|
|
|
.limit(limit).each do |p|
|
|
|
|
begin
|
|
|
|
p.rebake!
|
|
|
|
rescue => e
|
2014-07-17 16:22:46 -04:00
|
|
|
problems << {post: p, ex: e}
|
2014-05-27 22:30:43 -04:00
|
|
|
end
|
|
|
|
end
|
2014-07-17 16:22:46 -04:00
|
|
|
problems
|
2014-05-27 22:30:43 -04:00
|
|
|
end
|
|
|
|
|
2015-03-26 16:57:50 -04:00
|
|
|
def rebake!(opts=nil)
|
|
|
|
opts ||= {}
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
new_cooked = cook(raw, topic_id: topic_id, invalidate_oneboxes: opts.fetch(:invalidate_oneboxes, false))
|
2014-05-27 22:30:43 -04:00
|
|
|
old_cooked = cooked
|
|
|
|
|
2014-05-30 00:45:39 -04:00
|
|
|
update_columns(cooked: new_cooked, baked_at: Time.new, baked_version: BAKED_VERSION)
|
2014-05-27 22:30:43 -04:00
|
|
|
|
|
|
|
# Extracts urls from the body
|
2014-07-15 03:47:24 -04:00
|
|
|
TopicLink.extract_from(self)
|
|
|
|
QuotedPost.extract_from(self)
|
|
|
|
|
2014-05-27 22:30:43 -04:00
|
|
|
# make sure we trigger the post process
|
|
|
|
trigger_post_process(true)
|
|
|
|
|
2014-09-22 12:55:13 -04:00
|
|
|
publish_change_to_clients!(:rebaked)
|
|
|
|
|
2014-05-27 22:30:43 -04:00
|
|
|
new_cooked != old_cooked
|
|
|
|
end
|
|
|
|
|
2014-03-27 21:28:14 -04:00
|
|
|
def set_owner(new_user, actor)
|
2014-10-27 17:06:43 -04:00
|
|
|
return if user_id == new_user.id
|
|
|
|
|
|
|
|
edit_reason = I18n.t('change_owner.post_revision_text',
|
2015-07-15 18:22:01 -04:00
|
|
|
old_user: (self.user.username_lower rescue nil) || I18n.t('change_owner.deleted_user'),
|
2014-10-27 17:06:43 -04:00
|
|
|
new_user: new_user.username_lower
|
|
|
|
)
|
|
|
|
|
2016-03-16 17:32:27 -04:00
|
|
|
revise(actor, {raw: self.raw, user_id: new_user.id, edit_reason: edit_reason}, bypass_bump: true)
|
2016-03-16 08:49:27 -04:00
|
|
|
|
|
|
|
if post_number == topic.highest_post_number
|
|
|
|
topic.update_columns(last_post_user_id: new_user.id)
|
|
|
|
end
|
|
|
|
|
2014-03-27 21:28:14 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
before_create do
|
2013-06-09 12:48:44 -04:00
|
|
|
PostCreator.before_create_tasks(self)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2016-03-29 03:50:17 -04:00
|
|
|
def self.estimate_posts_per_day
|
|
|
|
val = $redis.get("estimated_posts_per_day")
|
|
|
|
return val.to_i if val
|
|
|
|
|
|
|
|
posts_per_day = Topic.listable_topics.secured.joins(:posts).merge(Post.created_since(30.days.ago)).count / 30
|
|
|
|
$redis.setex("estimated_posts_per_day", 1.day.to_i, posts_per_day.to_s)
|
|
|
|
posts_per_day
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# This calculates the geometric mean of the post timings and stores it along with
|
2013-02-07 10:45:24 -05:00
|
|
|
# each post.
|
2014-02-26 19:45:20 -05:00
|
|
|
def self.calculate_avg_time(min_topic_age=nil)
|
2013-02-11 15:47:28 -05:00
|
|
|
retry_lock_error do
|
2014-02-26 19:45:20 -05:00
|
|
|
builder = SqlBuilder.new("UPDATE posts
|
2013-02-11 15:47:28 -05:00
|
|
|
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
|
2014-02-26 19:45:20 -05:00
|
|
|
/*where*/")
|
|
|
|
|
|
|
|
builder.where("x.topic_id = posts.topic_id
|
2013-09-19 21:34:42 -04:00
|
|
|
AND x.post_number = posts.post_number
|
|
|
|
AND (posts.avg_time <> (x.gmean / 1000)::int OR posts.avg_time IS NULL)")
|
2014-02-26 19:45:20 -05:00
|
|
|
|
|
|
|
if min_topic_age
|
|
|
|
builder.where("posts.topic_id IN (SELECT id FROM topics where bumped_at > :bumped_at)",
|
|
|
|
bumped_at: min_topic_age)
|
|
|
|
end
|
|
|
|
|
|
|
|
builder.exec
|
2013-02-11 15:47:28 -05:00
|
|
|
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?
|
2014-05-27 22:30:43 -04:00
|
|
|
self.baked_at = Time.new
|
2014-05-30 00:45:39 -04:00
|
|
|
self.baked_version = BAKED_VERSION
|
2013-02-05 14:16:51 -05:00
|
|
|
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-07-22 16:39:20 -04:00
|
|
|
# TODO: move to post-analyzer?
|
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|
|
2014-05-06 09:41:59 -04:00
|
|
|
post = Post.find_by(topic_id: topic_id, post_number: p)
|
2013-05-23 12:09:06 -04:00
|
|
|
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-11-21 19:52:26 -05:00
|
|
|
def trigger_post_process(bypass_bump = false)
|
|
|
|
args = {
|
|
|
|
post_id: id,
|
|
|
|
bypass_bump: bypass_bump
|
|
|
|
}
|
2013-02-28 13:54:12 -05:00
|
|
|
args[:image_sizes] = image_sizes if image_sizes.present?
|
|
|
|
args[:invalidate_oneboxes] = true if invalidate_oneboxes.present?
|
2015-09-29 12:51:26 -04:00
|
|
|
args[:cooking_options] = self.cooking_options
|
2013-02-07 10:45:24 -05:00
|
|
|
Jobs.enqueue(:process_post, args)
|
2015-09-03 23:35:25 -04:00
|
|
|
DiscourseEvent.trigger(:after_trigger_post_process, self)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-03-07 11:07:59 -05:00
|
|
|
|
2015-06-24 09:19:39 -04:00
|
|
|
def self.public_posts_count_per_day(start_date, end_date, category_id=nil)
|
|
|
|
result = public_posts.where('posts.created_at >= ? AND posts.created_at <= ?', start_date, end_date)
|
|
|
|
result = result.where('topics.category_id = ?', category_id) if category_id
|
|
|
|
result.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
|
|
|
|
2015-09-24 20:15:58 -04:00
|
|
|
def reply_history(max_replies=100, guardian=nil)
|
2013-08-06 17:42:36 -04:00
|
|
|
post_ids = Post.exec_sql("WITH RECURSIVE breadcrumb(id, reply_to_post_number) AS (
|
|
|
|
SELECT p.id, p.reply_to_post_number FROM posts AS p
|
|
|
|
WHERE p.id = :post_id
|
|
|
|
UNION
|
|
|
|
SELECT p.id, p.reply_to_post_number FROM posts AS p, breadcrumb
|
|
|
|
WHERE breadcrumb.reply_to_post_number = p.post_number
|
|
|
|
AND p.topic_id = :topic_id
|
|
|
|
) SELECT id from breadcrumb ORDER by id", post_id: id, topic_id: topic_id).to_a
|
|
|
|
|
2014-10-26 18:44:42 -04:00
|
|
|
post_ids.map! {|r| r['id'].to_i }
|
|
|
|
.reject! {|post_id| post_id == id}
|
|
|
|
|
|
|
|
# [1,2,3][-10,-1] => nil
|
|
|
|
post_ids = (post_ids[(0-max_replies)..-1] || post_ids)
|
|
|
|
|
2015-09-24 20:15:58 -04:00
|
|
|
Post.secured(guardian).where(id: post_ids).includes(:user, :topic).order(:id).to_a
|
2013-08-06 17:42:36 -04:00
|
|
|
end
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
def revert_to(number)
|
|
|
|
return if number >= version
|
2014-05-06 09:41:59 -04:00
|
|
|
post_revision = PostRevision.find_by(post_id: id, number: (number + 1))
|
2013-12-11 21:41:34 -05:00
|
|
|
post_revision.modifications.each do |attribute, change|
|
|
|
|
attribute = "version" if attribute == "cached_version"
|
|
|
|
write_attribute(attribute, change[0])
|
|
|
|
end
|
|
|
|
end
|
2013-05-17 12:15:21 -04:00
|
|
|
|
2015-04-24 05:14:10 -04:00
|
|
|
def self.rebake_all_quoted_posts(user_id)
|
|
|
|
return if user_id.blank?
|
|
|
|
|
|
|
|
Post.exec_sql <<-SQL
|
|
|
|
WITH user_quoted_posts AS (
|
|
|
|
SELECT post_id
|
|
|
|
FROM quoted_posts
|
|
|
|
WHERE quoted_post_id IN (SELECT id FROM posts WHERE user_id = #{user_id})
|
|
|
|
)
|
|
|
|
UPDATE posts
|
|
|
|
SET baked_version = NULL
|
|
|
|
WHERE baked_version IS NOT NULL
|
|
|
|
AND id IN (SELECT post_id FROM user_quoted_posts)
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2016-01-26 20:19:49 -05:00
|
|
|
def seen?(user)
|
|
|
|
PostTiming.where(topic_id: topic_id, post_number: post_number, user_id: user.id).exists?
|
|
|
|
end
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
private
|
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?
|
2014-08-17 23:00:02 -04:00
|
|
|
args = HashWithIndifferentAccess.new
|
2013-05-22 15:38:45 -04:00
|
|
|
quote.first.scan(/([a-z]+)\:(\d+)/).each do |arg|
|
2014-08-17 23:00:02 -04:00
|
|
|
args[arg[0]] = arg[1].to_i
|
2013-05-22 15:38:45 -04:00
|
|
|
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
|
2015-09-24 20:15:58 -04:00
|
|
|
if Topic.visible_post_types.include?(self.post_type)
|
|
|
|
Post.where(id: post.id).update_all ['reply_count = reply_count + 1']
|
|
|
|
end
|
2013-05-23 12:08:24 -04:00
|
|
|
end
|
|
|
|
end
|
2013-12-11 21:41:34 -05:00
|
|
|
|
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
|
2013-09-03 17:19:29 -04:00
|
|
|
# user_id :integer
|
2013-05-23 22:48:32 -04:00
|
|
|
# topic_id :integer not null
|
|
|
|
# post_number :integer not null
|
|
|
|
# raw :text not null
|
|
|
|
# cooked :text not null
|
2014-08-27 01:30:17 -04:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-05-23 22:48:32 -04:00
|
|
|
# reply_to_post_number :integer
|
|
|
|
# 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
|
2013-06-16 20:48:58 -04:00
|
|
|
# like_score :integer default(0), not null
|
2013-07-13 21:24:16 -04:00
|
|
|
# deleted_by_id :integer
|
2016-02-22 18:33:53 -05:00
|
|
|
# edit_reason :string
|
2014-02-06 19:07:36 -05:00
|
|
|
# word_count :integer
|
|
|
|
# version :integer default(1), not null
|
|
|
|
# cook_method :integer default(1), not null
|
2014-05-21 19:00:38 -04:00
|
|
|
# wiki :boolean default(FALSE), not null
|
2014-05-29 00:59:14 -04:00
|
|
|
# baked_at :datetime
|
2014-07-03 03:29:44 -04:00
|
|
|
# baked_version :integer
|
|
|
|
# hidden_at :datetime
|
2014-07-14 21:29:44 -04:00
|
|
|
# self_edits :integer default(0), not null
|
2014-07-30 23:14:40 -04:00
|
|
|
# reply_quoted :boolean default(FALSE), not null
|
2014-11-19 22:53:15 -05:00
|
|
|
# via_email :boolean default(FALSE), not null
|
|
|
|
# raw_email :text
|
|
|
|
# public_version :integer default(1), not null
|
2016-02-22 18:33:53 -05:00
|
|
|
# action_code :string
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2014-03-20 00:35:51 -04:00
|
|
|
# idx_posts_created_at_topic_id (created_at,topic_id)
|
2013-05-23 22:48:32 -04:00
|
|
|
# idx_posts_user_id_deleted_at (user_id)
|
|
|
|
# index_posts_on_reply_to_post_number (reply_to_post_number)
|
2014-07-17 02:10:44 -04:00
|
|
|
# index_posts_on_topic_id_and_post_number (topic_id,post_number) UNIQUE
|
2015-02-03 23:09:00 -05:00
|
|
|
# index_posts_on_user_id_and_created_at (user_id,created_at)
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|