2013-10-01 15:04:02 +08:00
|
|
|
require_dependency 'jobs/base'
|
2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'pretty_text'
|
|
|
|
require_dependency 'rate_limiter'
|
2013-02-09 23:33:07 +08:00
|
|
|
require_dependency 'post_revisor'
|
2013-03-19 03:12:31 +08:00
|
|
|
require_dependency 'enum'
|
2013-05-07 12:39:01 +08:00
|
|
|
require_dependency 'trashable'
|
2013-05-31 02:34:44 +08:00
|
|
|
require_dependency 'post_analyzer'
|
2013-06-13 16:18:17 +08:00
|
|
|
require_dependency 'validators/post_validator'
|
2013-10-10 15:45:40 +08:00
|
|
|
require_dependency 'plugin/filter'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
require 'archetype'
|
|
|
|
require 'digest/sha1'
|
|
|
|
|
|
|
|
class Post < ActiveRecord::Base
|
|
|
|
include RateLimiter::OnCreateRecord
|
2013-05-07 12:39:01 +08:00
|
|
|
include Trashable
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-03-14 00:35:55 +08:00
|
|
|
versioned if: :raw_changed?
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
rate_limit
|
2013-10-10 07:32:03 +08:00
|
|
|
rate_limit :limit_posts_per_day
|
2013-02-19 14:57:14 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic, counter_cache: :posts_count
|
2013-03-20 07:51:39 +08:00
|
|
|
belongs_to :reply_to_user, class_name: "User"
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
has_many :post_replies
|
|
|
|
has_many :replies, through: :post_replies
|
|
|
|
has_many :post_actions
|
2013-06-14 01:41:45 +08:00
|
|
|
has_many :topic_links
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-14 05:44:24 +08:00
|
|
|
has_many :post_uploads
|
|
|
|
has_many :uploads, through: :post_uploads
|
2013-06-13 07:43:50 +08:00
|
|
|
|
2013-05-23 03:33:33 +08:00
|
|
|
has_one :post_search_data
|
|
|
|
|
2013-10-15 22:21:30 +08:00
|
|
|
has_many :post_details
|
|
|
|
|
2013-06-13 16:18:17 +08:00
|
|
|
validates_with ::Validators::PostValidator
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-21 23:36:33 +08:00
|
|
|
# We can pass several creating options to a post via attributes
|
2013-09-06 23:50:05 +08:00
|
|
|
attr_accessor :image_sizes, :quoted_post_numbers, :no_bump, :invalidate_oneboxes, :cooking_options, :skip_unique_check
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
SHORT_POST_CHARS = 1200
|
|
|
|
|
2013-06-10 00:48:44 +08:00
|
|
|
scope :by_newest, -> { order('created_at desc, id desc') }
|
|
|
|
scope :by_post_number, -> { order('post_number ASC') }
|
|
|
|
scope :with_user, -> { includes(:user) }
|
2013-04-10 20:54:10 +08: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-17 04:56:18 +08:00
|
|
|
scope :with_topic_subtype, ->(subtype) { joins(:topic).where('topics.subtype = ?', subtype) }
|
2013-02-22 02:20:00 +08:00
|
|
|
|
2013-03-19 02:59:34 +08:00
|
|
|
def self.hidden_reasons
|
2013-05-31 23:41:40 +08:00
|
|
|
@hidden_reasons ||= Enum.new(:flag_threshold_reached, :flag_threshold_reached_again, :new_user_spam_threshold_reached)
|
2013-03-19 02:59:34 +08:00
|
|
|
end
|
|
|
|
|
2013-03-19 04:03:46 +08:00
|
|
|
def self.types
|
|
|
|
@types ||= Enum.new(:regular, :moderator_action)
|
|
|
|
end
|
|
|
|
|
2013-10-15 22:21:30 +08:00
|
|
|
def self.find_by_detail(key, value)
|
|
|
|
includes(:post_details).where( "post_details.key = ? AND " +
|
|
|
|
"post_details.value = ?",
|
|
|
|
key,
|
|
|
|
value ).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_detail(key, value, extra = nil)
|
|
|
|
post_details.build(key: key, value: value, extra: extra)
|
|
|
|
end
|
|
|
|
|
2013-10-10 07:32:03 +08:00
|
|
|
def limit_posts_per_day
|
|
|
|
if user.created_at > 1.day.ago && post_number > 1
|
|
|
|
RateLimiter.new(user, "first-day-replies-per-day:#{Date.today.to_s}", SiteSetting.max_replies_in_first_day, 1.day.to_i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-10 03:20:18 +08:00
|
|
|
def trash!(trashed_by=nil)
|
2013-06-14 01:41:45 +08:00
|
|
|
self.topic_links.each(&:destroy)
|
2013-07-10 03:20:18 +08:00
|
|
|
super(trashed_by)
|
2013-06-14 01:41:45 +08:00
|
|
|
end
|
|
|
|
|
2013-05-07 12:39:01 +08:00
|
|
|
def recover!
|
|
|
|
super
|
|
|
|
update_flagged_posts_count
|
2013-06-14 01:41:45 +08:00
|
|
|
TopicLink.extract_from(self)
|
2013-05-07 12:39:01 +08:00
|
|
|
end
|
|
|
|
|
2013-03-22 18:18:48 +08:00
|
|
|
# The key we use in redis to ensure unique posts
|
2013-02-06 03:16:51 +08:00
|
|
|
def unique_post_key
|
|
|
|
"post-#{user_id}:#{raw_hash}"
|
|
|
|
end
|
|
|
|
|
2013-09-10 04:17:31 +08:00
|
|
|
def store_unique_post_key
|
|
|
|
if SiteSetting.unique_posts_mins > 0
|
|
|
|
$redis.setex(unique_post_key, SiteSetting.unique_posts_mins.minutes.to_i, "1")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches_recent_post?
|
|
|
|
$redis.exists(unique_post_key)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def raw_hash
|
2013-03-01 02:54:12 +08:00
|
|
|
return if raw.blank?
|
2013-06-18 03:57:13 +08:00
|
|
|
Digest::SHA1.hexdigest(raw.gsub(/\s+/, ""))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-12 15:43:48 +08:00
|
|
|
def self.white_listed_image_classes
|
2013-04-12 00:36:45 +08:00
|
|
|
@white_listed_image_classes ||= ['avatar', 'favicon', 'thumbnail']
|
2013-02-12 15:43:48 +08:00
|
|
|
end
|
|
|
|
|
2013-05-31 02:34:44 +08:00
|
|
|
def post_analyzer
|
2013-07-23 04:24:47 +08:00
|
|
|
@post_analyzers ||= {}
|
|
|
|
@post_analyzers[raw_hash] ||= PostAnalyzer.new(raw, topic_id)
|
2013-05-31 02:34:44 +08:00
|
|
|
end
|
2013-02-12 15:43:48 +08:00
|
|
|
|
2013-07-22 08:39:17 +08:00
|
|
|
%w{raw_mentions linked_hosts image_count attachment_count link_count raw_links}.each do |attr|
|
2013-05-31 02:34:44 +08:00
|
|
|
define_method(attr) do
|
2013-07-23 04:24:47 +08:00
|
|
|
post_analyzer.send(attr)
|
2013-05-31 02:34:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cook(*args)
|
2013-10-10 15:45:40 +08:00
|
|
|
Plugin::Filter.apply(:after_post_cook, self, post_analyzer.cook(*args))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-04-06 01:59:00 +08: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-11 04:58:23 +08:00
|
|
|
def total_hosts_usage
|
|
|
|
hosts = linked_hosts.clone
|
|
|
|
|
2013-05-25 03:20:58 +08: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-11 04:58:23 +08: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-06 03:16:51 +08:00
|
|
|
def archetype
|
|
|
|
topic.archetype
|
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.regular_order
|
2013-02-07 23:45:24 +08:00
|
|
|
order(:sort_order, :post_number)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.reverse_order
|
2013-02-07 23:45:24 +08:00
|
|
|
order('sort_order desc, post_number desc')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.best_of
|
2013-03-23 03:43:57 +08:00
|
|
|
where(["(post_number = 1) or (percent_rank <= ?)", SiteSetting.best_of_percent_filter.to_f / 100.0])
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 09:13:41 +08:00
|
|
|
def update_flagged_posts_count
|
|
|
|
PostAction.update_flagged_posts_count
|
|
|
|
end
|
|
|
|
|
2013-03-01 02:54:12 +08:00
|
|
|
def filter_quotes(parent_post = nil)
|
2013-02-06 03:16:51 +08:00
|
|
|
return cooked if parent_post.blank?
|
|
|
|
|
|
|
|
# We only filter quotes when there is exactly 1
|
|
|
|
return cooked unless (quote_count == 1)
|
|
|
|
|
2013-02-16 09:58:33 +08:00
|
|
|
parent_raw = parent_post.raw.sub(/\[quote.+\/quote\]/m, '')
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-03-05 08:42:44 +08:00
|
|
|
if raw[parent_raw] || (parent_raw.size < SHORT_POST_CHARS)
|
2013-02-06 03:16:51 +08:00
|
|
|
return cooked.sub(/\<aside.+\<\/aside\>/m, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
cooked
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
user.username
|
|
|
|
end
|
|
|
|
|
|
|
|
def external_id
|
2013-02-07 23:45:24 +08:00
|
|
|
"#{topic_id}/#{post_number}"
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def quoteless?
|
2013-03-01 02:54:12 +08:00
|
|
|
(quote_count == 0) && (reply_to_post_number.present?)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def reply_notification_target
|
2013-03-01 02:54:12 +08: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-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-04-30 11:25:55 +08:00
|
|
|
def self.excerpt(cooked, maxlength = nil, options = {})
|
2013-02-06 03:16:51 +08:00
|
|
|
maxlength ||= SiteSetting.post_excerpt_maxlength
|
2013-04-30 11:25:55 +08:00
|
|
|
PrettyText.excerpt(cooked, maxlength, options)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Strip out most of the markup
|
2013-04-30 11:25:55 +08:00
|
|
|
def excerpt(maxlength = nil, options = {})
|
|
|
|
Post.excerpt(cooked, maxlength, options)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# A list of versions including the initial version
|
|
|
|
def all_versions
|
|
|
|
result = []
|
2013-03-14 00:35:55 +08:00
|
|
|
result << { number: 1, display_username: user.username, created_at: created_at }
|
2013-02-06 03:16:51 +08:00
|
|
|
versions.order(:number).includes(:user).each do |v|
|
2013-03-14 00:35:55 +08:00
|
|
|
if v.user.present?
|
|
|
|
result << { number: v.number, display_username: v.user.username, created_at: v.created_at }
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-05-26 08:18:04 +08:00
|
|
|
def is_first_post?
|
|
|
|
post_number == 1
|
|
|
|
end
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
def is_flagged?
|
2013-03-01 20:07:44 +08:00
|
|
|
post_actions.where(post_action_type_id: PostActionType.flag_types.values, deleted_at: nil).count != 0
|
2013-02-07 12:15:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def unhide!
|
|
|
|
self.hidden = false
|
|
|
|
self.hidden_reason_id = nil
|
|
|
|
self.topic.update_attributes(visible: true)
|
2013-03-01 02:54:12 +08:00
|
|
|
save
|
2013-02-07 12:15:48 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def url
|
2013-04-24 16:05:35 +08:00
|
|
|
Post.url(topic.slug, topic.id, post_number)
|
2013-04-22 15:45:03 +08:00
|
|
|
end
|
|
|
|
|
2013-04-24 16:05:35 +08:00
|
|
|
def self.url(slug, topic_id, post_number)
|
|
|
|
"/t/#{slug}/#{topic_id}/#{post_number}"
|
2013-04-22 15:45:03 +08: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 16:05:35 +08:00
|
|
|
select(['posts.id as post_id','post_number', 'topics.slug', 'topics.title', 'topics.id']).
|
2013-04-22 15:45:03 +08:00
|
|
|
each do |t|
|
2013-04-24 16:05:35 +08:00
|
|
|
urls[t.post_id.to_i] = url(t.slug, t.id, t.post_number)
|
2013-04-22 15:45:03 +08:00
|
|
|
end
|
|
|
|
urls
|
2013-04-24 16:05:35 +08:00
|
|
|
else
|
2013-04-22 15:45:03 +08:00
|
|
|
{}
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-22 02:20:00 +08:00
|
|
|
def author_readable
|
|
|
|
user.readable_name
|
|
|
|
end
|
|
|
|
|
2013-03-01 02:54:12 +08:00
|
|
|
def revise(updated_by, new_raw, opts = {})
|
2013-02-09 23:33:07 +08:00
|
|
|
PostRevisor.new(self).revise!(updated_by, new_raw, opts)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
before_create do
|
2013-06-10 00:48:44 +08:00
|
|
|
PostCreator.before_create_tasks(self)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# This calculates the geometric mean of the post timings and stores it along with
|
2013-02-07 23:45:24 +08:00
|
|
|
# each post.
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.calculate_avg_time
|
2013-02-12 04:47:28 +08: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
|
2013-09-20 09:34:42 +08:00
|
|
|
AND x.post_number = posts.post_number
|
|
|
|
AND (posts.avg_time <> (x.gmean / 1000)::int OR posts.avg_time IS NULL)")
|
2013-02-12 04:47:28 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
before_save do
|
2013-03-01 02:54:12 +08:00
|
|
|
self.last_editor_id ||= user_id
|
2013-02-06 03:16:51 +08:00
|
|
|
self.cooked = cook(raw, topic_id: topic_id) unless new_record?
|
|
|
|
end
|
|
|
|
|
2013-03-19 03:12:31 +08:00
|
|
|
def advance_draft_sequence
|
|
|
|
return if topic.blank? # could be deleted
|
|
|
|
DraftSequence.next!(last_editor_id, topic.draft_key)
|
|
|
|
end
|
|
|
|
|
2013-05-23 03:45:31 +08:00
|
|
|
|
2013-07-23 04:39:20 +08:00
|
|
|
# TODO: move to post-analyzer?
|
2013-03-19 03:54:08 +08:00
|
|
|
# Determine what posts are quoted by this post
|
2013-02-06 03:16:51 +08:00
|
|
|
def extract_quoted_post_numbers
|
2013-05-23 03:45:31 +08:00
|
|
|
temp_collector = []
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
# Create relationships for the quotes
|
2013-05-23 03:38:45 +08:00
|
|
|
raw.scan(/\[quote=\"([^"]+)"\]/).each do |quote|
|
|
|
|
args = parse_quote_into_arguments(quote)
|
2013-05-23 03:45:31 +08: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-06 03:16:51 +08:00
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-05-23 03:45:31 +08:00
|
|
|
temp_collector.uniq!
|
|
|
|
self.quoted_post_numbers = temp_collector
|
|
|
|
self.quote_count = temp_collector.size
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-05-24 00:09:06 +08:00
|
|
|
|
2013-03-19 03:54:08 +08:00
|
|
|
def save_reply_relationships
|
2013-05-24 00:09:06 +08:00
|
|
|
add_to_quoted_post_numbers(reply_to_post_number)
|
|
|
|
return if self.quoted_post_numbers.blank?
|
2013-03-19 03:54:08 +08:00
|
|
|
|
|
|
|
# Create a reply relationship between quoted posts and this new post
|
2013-05-24 00:09:06 +08: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-19 03:54:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-19 01:55:34 +08:00
|
|
|
# Enqueue post processing for this post
|
2013-02-06 03:16:51 +08:00
|
|
|
def trigger_post_process
|
2013-03-01 02:54:12 +08: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 23:45:24 +08:00
|
|
|
Jobs.enqueue(:process_post, args)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-03-08 00:07:59 +08:00
|
|
|
|
2013-04-17 04:56:18 +08: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-04 01:25:52 +08:00
|
|
|
end
|
|
|
|
|
2013-04-17 04:56:18 +08: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-08 00:07:59 +08:00
|
|
|
end
|
2013-05-18 00:15:21 +08:00
|
|
|
|
2013-08-07 05:42:36 +08:00
|
|
|
|
|
|
|
def reply_history
|
|
|
|
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
|
|
|
|
|
|
|
|
post_ids.map! {|r| r['id'].to_i }.reject! {|post_id| post_id == id}
|
|
|
|
Post.where(id: post_ids).includes(:user, :topic).order(:id).to_a
|
|
|
|
end
|
|
|
|
|
2013-05-18 00:15:21 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
|
2013-05-23 03:45:31 +08:00
|
|
|
|
2013-05-23 03:38:45 +08: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-23 03:45:31 +08:00
|
|
|
|
2013-05-24 00:07:45 +08:00
|
|
|
def add_to_quoted_post_numbers(num)
|
|
|
|
return unless num.present?
|
|
|
|
self.quoted_post_numbers ||= []
|
|
|
|
self.quoted_post_numbers << num
|
|
|
|
end
|
2013-05-24 00:08:24 +08:00
|
|
|
|
|
|
|
def create_reply_relationship_with(post)
|
|
|
|
return if post.nil?
|
|
|
|
post_reply = post.post_replies.new(reply_id: id)
|
|
|
|
if post_reply.save
|
2013-07-02 02:45:52 +08:00
|
|
|
Post.where(id: post.id).update_all ['reply_count = reply_count + 1']
|
2013-05-24 00:08:24 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-05-24 10:48:32 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: posts
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
2013-09-04 05:19:29 +08:00
|
|
|
# user_id :integer
|
2013-05-24 10:48:32 +08:00
|
|
|
# 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
|
2013-06-17 08:48:58 +08:00
|
|
|
# like_score :integer default(0), not null
|
2013-07-14 09:24:16 +08:00
|
|
|
# deleted_by_id :integer
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|