2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'slug'
|
|
|
|
require_dependency 'avatar_lookup'
|
|
|
|
require_dependency 'topic_view'
|
|
|
|
require_dependency 'rate_limiter'
|
2013-02-06 20:09:31 -05:00
|
|
|
require_dependency 'text_sentinel'
|
2013-04-10 05:00:50 -04:00
|
|
|
require_dependency 'text_cleaner'
|
2013-11-06 15:05:06 -05:00
|
|
|
require_dependency 'archetype'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
class Topic < ActiveRecord::Base
|
2013-07-03 15:43:29 -04:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
2013-02-05 14:16:51 -05:00
|
|
|
include RateLimiter::OnCreateRecord
|
2014-04-28 04:31:51 -04:00
|
|
|
include HasCustomFields
|
2013-05-07 00:39:01 -04:00
|
|
|
include Trashable
|
2013-08-30 05:12:44 -04:00
|
|
|
extend Forwardable
|
|
|
|
|
|
|
|
def_delegator :featured_users, :user_ids, :featured_user_ids
|
|
|
|
def_delegator :featured_users, :choose, :feature_topic_users
|
|
|
|
|
|
|
|
def_delegator :notifier, :watch!, :notify_watch!
|
|
|
|
def_delegator :notifier, :tracking!, :notify_tracking!
|
|
|
|
def_delegator :notifier, :regular!, :notify_regular!
|
|
|
|
def_delegator :notifier, :muted!, :notify_muted!
|
|
|
|
def_delegator :notifier, :toggle_mute, :toggle_mute
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-05-12 03:32:49 -04:00
|
|
|
attr_accessor :allowed_user_ids
|
|
|
|
|
2013-03-12 12:33:42 -04:00
|
|
|
def self.max_sort_order
|
|
|
|
2**31 - 1
|
|
|
|
end
|
|
|
|
|
2013-08-26 06:41:56 -04:00
|
|
|
def featured_users
|
|
|
|
@featured_users ||= TopicFeaturedUsers.new(self)
|
2013-03-12 12:33:42 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-07-09 15:20:18 -04:00
|
|
|
def trash!(trashed_by=nil)
|
2013-07-08 15:23:20 -04:00
|
|
|
update_category_topic_count_by(-1) if deleted_at.nil?
|
2013-07-09 15:20:18 -04:00
|
|
|
super(trashed_by)
|
2013-05-07 00:39:01 -04:00
|
|
|
update_flagged_posts_count
|
|
|
|
end
|
|
|
|
|
|
|
|
def recover!
|
2013-07-08 15:23:20 -04:00
|
|
|
update_category_topic_count_by(1) unless deleted_at.nil?
|
2013-05-07 00:39:01 -04:00
|
|
|
super
|
|
|
|
update_flagged_posts_count
|
|
|
|
end
|
2013-02-05 20:13:41 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
rate_limit :default_rate_limiter
|
|
|
|
rate_limit :limit_topics_per_day
|
|
|
|
rate_limit :limit_private_messages_per_day
|
|
|
|
|
2013-05-23 00:52:12 -04:00
|
|
|
validates :title, :presence => true,
|
2013-06-04 17:58:25 -04:00
|
|
|
:topic_title_length => true,
|
2013-05-23 00:52:12 -04:00
|
|
|
:quality_title => { :unless => :private_message? },
|
|
|
|
:unique_among => { :unless => Proc.new { |t| (SiteSetting.allow_duplicate_topic_titles? || t.private_message?) },
|
|
|
|
:message => :has_already_been_used,
|
|
|
|
:allow_blank => true,
|
|
|
|
:case_sensitive => false,
|
|
|
|
:collection => Proc.new{ Topic.listable_topics } }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-04-23 19:19:59 -04:00
|
|
|
validates :category_id,
|
|
|
|
:presence => true,
|
|
|
|
:exclusion => {
|
|
|
|
:in => Proc.new{[SiteSetting.uncategorized_category_id]}
|
|
|
|
},
|
|
|
|
:if => Proc.new { |t|
|
|
|
|
(t.new_record? || t.category_id_changed?) &&
|
|
|
|
!SiteSetting.allow_uncategorized_topics &&
|
|
|
|
(t.archetype.nil? || t.archetype == Archetype.default) &&
|
|
|
|
(!t.user_id || !t.user.staff?)
|
|
|
|
}
|
2013-10-23 19:05:51 -04:00
|
|
|
|
2013-10-08 14:40:31 -04:00
|
|
|
|
2013-05-31 15:22:34 -04:00
|
|
|
before_validation do
|
2014-04-18 15:01:21 -04:00
|
|
|
if SiteSetting.title_sanitize
|
|
|
|
self.title = sanitize(title.to_s, tags: [], attributes: []).strip.presence
|
|
|
|
end
|
2013-05-23 00:52:12 -04:00
|
|
|
self.title = TextCleaner.clean_title(TextSentinel.title_sentinel(title).text) if errors[:title].empty?
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
belongs_to :category
|
|
|
|
has_many :posts
|
|
|
|
has_many :topic_allowed_users
|
2013-05-02 01:15:17 -04:00
|
|
|
has_many :topic_allowed_groups
|
|
|
|
|
|
|
|
has_many :allowed_group_users, through: :allowed_groups, source: :users
|
|
|
|
has_many :allowed_groups, through: :topic_allowed_groups, source: :group
|
2013-02-05 14:16:51 -05:00
|
|
|
has_many :allowed_users, through: :topic_allowed_users, source: :user
|
2013-03-28 13:02:59 -04:00
|
|
|
|
2013-12-23 18:50:36 -05:00
|
|
|
has_one :top_topic
|
2013-02-05 14:16:51 -05:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :last_poster, class_name: 'User', foreign_key: :last_post_user_id
|
|
|
|
belongs_to :featured_user1, class_name: 'User', foreign_key: :featured_user1_id
|
|
|
|
belongs_to :featured_user2, class_name: 'User', foreign_key: :featured_user2_id
|
|
|
|
belongs_to :featured_user3, class_name: 'User', foreign_key: :featured_user3_id
|
|
|
|
belongs_to :featured_user4, class_name: 'User', foreign_key: :featured_user4_id
|
2013-05-07 14:25:41 -04:00
|
|
|
belongs_to :auto_close_user, class_name: 'User', foreign_key: :auto_close_user_id
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
has_many :topic_users
|
|
|
|
has_many :topic_links
|
|
|
|
has_many :topic_invites
|
|
|
|
has_many :invites, through: :topic_invites, source: :invite
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
has_many :revisions, foreign_key: :topic_id, class_name: 'TopicRevision'
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# When we want to temporarily attach some data to a forum topic (usually before serialization)
|
|
|
|
attr_accessor :user_data
|
|
|
|
attr_accessor :posters # TODO: can replace with posters_summary once we remove old list code
|
2014-05-12 03:32:49 -04:00
|
|
|
attr_accessor :participants
|
2013-04-02 16:52:51 -04:00
|
|
|
attr_accessor :topic_list
|
2014-04-25 12:24:22 -04:00
|
|
|
attr_accessor :meta_data
|
2013-10-17 02:44:56 -04:00
|
|
|
attr_accessor :include_last_poster
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# The regular order
|
2014-05-07 13:04:39 -04:00
|
|
|
scope :topic_list_order, -> { order('topics.bumped_at desc') }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# Return private message topics
|
2014-05-07 13:04:39 -04:00
|
|
|
scope :private_messages, -> { where(archetype: Archetype.private_message) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-05-07 13:04:39 -04:00
|
|
|
scope :listable_topics, -> { where('topics.archetype <> ?', [Archetype.private_message]) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-02-27 23:07:55 -05:00
|
|
|
scope :by_newest, -> { order('topics.created_at desc, topics.id desc') }
|
2013-02-27 22:36:12 -05:00
|
|
|
|
2013-07-03 16:04:22 -04:00
|
|
|
scope :visible, -> { where(visible: true) }
|
2013-05-30 07:23:40 -04:00
|
|
|
|
2014-02-27 23:07:55 -05:00
|
|
|
scope :created_since, lambda { |time_ago| where('created_at > ?', time_ago) }
|
2013-05-30 07:23:40 -04:00
|
|
|
|
2014-05-07 13:04:39 -04:00
|
|
|
scope :secured, lambda { |guardian=nil|
|
2013-06-08 09:52:06 -04:00
|
|
|
ids = guardian.secure_category_ids if guardian
|
2013-06-12 13:43:59 -04:00
|
|
|
|
|
|
|
# Query conditions
|
2014-05-07 13:04:39 -04:00
|
|
|
condition = if ids.present?
|
|
|
|
["NOT c.read_restricted or c.id in (:cats)", cats: ids]
|
|
|
|
else
|
|
|
|
["NOT c.read_restricted"]
|
|
|
|
end
|
2013-06-12 13:43:59 -04:00
|
|
|
|
|
|
|
where("category_id IS NULL OR category_id IN (
|
|
|
|
SELECT c.id FROM categories c
|
|
|
|
WHERE #{condition[0]})", condition[1])
|
|
|
|
}
|
2013-06-08 09:52:06 -04:00
|
|
|
|
2014-01-09 16:22:54 -05:00
|
|
|
# Helps us limit how many topics can be starred in a day
|
|
|
|
class StarLimiter < RateLimiter
|
2013-02-05 14:16:51 -05:00
|
|
|
def initialize(user)
|
2014-01-09 16:22:54 -05:00
|
|
|
super(user, "starred:#{Date.today.to_s}", SiteSetting.max_stars_per_day, 1.day.to_i)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before_create do
|
|
|
|
self.bumped_at ||= Time.now
|
2013-02-28 13:54:12 -05:00
|
|
|
self.last_post_user_id ||= user_id
|
2013-12-06 16:39:35 -05:00
|
|
|
if !@ignore_category_auto_close and self.category and self.category.auto_close_hours and self.auto_close_at.nil?
|
|
|
|
set_auto_close(self.category.auto_close_hours)
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-10-23 19:05:51 -04:00
|
|
|
attr_accessor :skip_callbacks
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
after_create do
|
2013-10-23 19:05:51 -04:00
|
|
|
|
2013-12-13 01:04:45 -05:00
|
|
|
unless skip_callbacks
|
|
|
|
changed_to_category(category)
|
|
|
|
if archetype == Archetype.private_message
|
|
|
|
DraftSequence.next!(user, Draft::NEW_PRIVATE_MESSAGE)
|
|
|
|
else
|
|
|
|
DraftSequence.next!(user, Draft::NEW_TOPIC)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-12-13 01:04:45 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-07 14:25:41 -04:00
|
|
|
before_save do
|
2013-10-23 19:05:51 -04:00
|
|
|
|
2013-12-13 01:04:45 -05:00
|
|
|
unless skip_callbacks
|
|
|
|
if (auto_close_at_changed? and !auto_close_at_was.nil?) or (auto_close_user_id_changed? and auto_close_at)
|
|
|
|
self.auto_close_started_at ||= Time.zone.now if auto_close_at
|
|
|
|
Jobs.cancel_scheduled_job(:close_topic, {topic_id: id})
|
|
|
|
true
|
|
|
|
end
|
|
|
|
if category_id.nil? && (archetype.nil? || archetype == Archetype.default)
|
|
|
|
self.category_id = SiteSetting.uncategorized_category_id
|
|
|
|
end
|
2013-10-23 19:05:51 -04:00
|
|
|
end
|
2013-12-13 01:04:45 -05:00
|
|
|
|
2013-05-07 14:25:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after_save do
|
2013-12-11 21:41:34 -05:00
|
|
|
save_revision if should_create_new_version?
|
|
|
|
|
2013-12-13 01:04:45 -05:00
|
|
|
unless skip_callbacks
|
|
|
|
if auto_close_at and (auto_close_at_changed? or auto_close_user_id_changed?)
|
|
|
|
Jobs.enqueue_at(auto_close_at, :close_topic, {topic_id: id, user_id: auto_close_user_id || user_id})
|
|
|
|
end
|
2013-05-07 14:25:41 -04:00
|
|
|
end
|
2013-12-13 01:04:45 -05:00
|
|
|
|
2013-05-07 14:25:41 -04:00
|
|
|
end
|
|
|
|
|
2014-03-07 02:59:47 -05:00
|
|
|
# TODO move into PostRevisor or TopicRevisor
|
2013-12-11 21:41:34 -05:00
|
|
|
def save_revision
|
2014-03-07 02:59:47 -05:00
|
|
|
if first_post_id = posts.where(post_number: 1).pluck(:id).first
|
|
|
|
|
|
|
|
number = PostRevision.where(post_id: first_post_id).count + 2
|
|
|
|
PostRevision.create!(
|
|
|
|
user_id: acting_user.id,
|
|
|
|
post_id: first_post_id,
|
|
|
|
number: number,
|
|
|
|
modifications: changes.extract!(:category_id, :title)
|
|
|
|
)
|
|
|
|
|
2014-03-27 03:00:23 -04:00
|
|
|
Post.where(id: first_post_id).update_all(version: number)
|
2014-03-07 02:59:47 -05:00
|
|
|
end
|
2013-12-11 21:41:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def should_create_new_version?
|
|
|
|
!new_record? && (category_id_changed? || title_changed?)
|
|
|
|
end
|
|
|
|
|
2013-11-13 12:26:32 -05:00
|
|
|
def self.top_viewed(max = 10)
|
2014-02-27 23:07:55 -05:00
|
|
|
Topic.listable_topics.visible.secured.order('views desc').limit(max)
|
2013-11-13 12:26:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.recent(max = 10)
|
2014-02-27 23:07:55 -05:00
|
|
|
Topic.listable_topics.visible.secured.order('created_at desc').limit(max)
|
2013-11-13 12:26:32 -05:00
|
|
|
end
|
|
|
|
|
2013-10-28 02:12:07 -04:00
|
|
|
def self.count_exceeds_minimum?
|
|
|
|
count > SiteSetting.minimum_topics_similar
|
|
|
|
end
|
|
|
|
|
2013-06-03 16:12:24 -04:00
|
|
|
def best_post
|
2014-02-27 23:07:55 -05:00
|
|
|
posts.order('score desc').limit(1).first
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
|
2013-05-02 01:15:17 -04:00
|
|
|
# all users (in groups or directly targetted) that are going to get the pm
|
|
|
|
def all_allowed_users
|
|
|
|
# TODO we should probably change this from 3 queries to 1
|
|
|
|
User.where('id in (?)', allowed_users.select('users.id').to_a + allowed_group_users.select('users.id').to_a)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Additional rate limits on topics: per day and private messages per day
|
|
|
|
def limit_topics_per_day
|
2013-10-16 05:28:18 -04:00
|
|
|
apply_per_day_rate_limit_for("topics", :max_topics_per_day)
|
|
|
|
limit_first_day_topics_per_day if user.added_a_day_ago?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def limit_private_messages_per_day
|
|
|
|
return unless private_message?
|
2013-10-16 05:28:18 -04:00
|
|
|
apply_per_day_rate_limit_for("pms", :max_private_messages_per_day)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
def fancy_title
|
2014-04-18 15:01:21 -04:00
|
|
|
sanitized_title = if SiteSetting.title_sanitize
|
|
|
|
sanitize(title.to_s, tags: [], attributes: []).strip.presence
|
|
|
|
else
|
|
|
|
title.gsub(/['&\"<>]/, {
|
|
|
|
"'" => ''',
|
|
|
|
'&' => '&',
|
|
|
|
'"' => '"',
|
|
|
|
'<' => '<',
|
|
|
|
'>' => '>',
|
|
|
|
})
|
|
|
|
end
|
2014-04-18 00:48:38 -04:00
|
|
|
|
2014-04-18 15:01:21 -04:00
|
|
|
return unless sanitized_title
|
2014-04-18 00:48:38 -04:00
|
|
|
return sanitized_title unless SiteSetting.title_fancy_entities?
|
2013-02-19 16:08:23 -05:00
|
|
|
|
|
|
|
# We don't always have to require this, if fancy is disabled
|
2013-03-12 12:33:42 -04:00
|
|
|
# see: http://meta.discourse.org/t/pattern-for-defer-loading-gems-and-profiling-with-perftools-rb/4629
|
2013-03-10 10:13:52 -04:00
|
|
|
require 'redcarpet' unless defined? Redcarpet
|
2013-02-19 16:08:23 -05:00
|
|
|
|
2014-04-18 00:48:38 -04:00
|
|
|
Redcarpet::Render::SmartyPants.render(sanitized_title)
|
2013-04-21 23:48:05 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def new_version_required?
|
2013-02-26 11:27:59 -05:00
|
|
|
title_changed? || category_id_changed?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-06-03 16:12:24 -04:00
|
|
|
# Returns hot topics since a date for display in email digest.
|
2014-04-17 16:42:40 -04:00
|
|
|
def self.for_digest(user, since, opts=nil)
|
|
|
|
opts = opts || {}
|
2014-04-17 15:14:54 -04:00
|
|
|
score = "#{ListController.best_period_for(since)}_score"
|
|
|
|
|
2013-11-06 15:05:06 -05:00
|
|
|
topics = Topic
|
|
|
|
.visible
|
|
|
|
.secured(Guardian.new(user))
|
2014-04-17 15:21:55 -04:00
|
|
|
.joins("LEFT OUTER JOIN topic_users ON topic_users.topic_id = topics.id AND topic_users.user_id = #{user.id.to_i}")
|
2013-11-06 15:05:06 -05:00
|
|
|
.where(closed: false, archived: false)
|
2014-04-17 15:21:55 -04:00
|
|
|
.where("COALESCE(topic_users.notification_level, 1) <> ?", TopicUser.notification_levels[:muted])
|
2013-11-06 15:05:06 -05:00
|
|
|
.created_since(since)
|
|
|
|
.listable_topics
|
2014-04-17 15:43:24 -04:00
|
|
|
.includes(:category)
|
2014-04-17 16:42:40 -04:00
|
|
|
|
|
|
|
if !!opts[:top_order]
|
|
|
|
topics = topics.joins("LEFT OUTER JOIN top_topics ON top_topics.topic_id = topics.id")
|
|
|
|
.order(TopicQuerySQL.order_top_for(score))
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts[:limit]
|
|
|
|
topics = topics.limit(opts[:limit])
|
|
|
|
end
|
2013-11-06 15:05:06 -05:00
|
|
|
|
2014-04-17 15:14:54 -04:00
|
|
|
# Remove category topics
|
2013-11-06 15:05:06 -05:00
|
|
|
category_topic_ids = Category.pluck(:topic_id).compact!
|
|
|
|
if category_topic_ids.present?
|
2014-04-17 15:14:54 -04:00
|
|
|
topics = topics.where("topics.id NOT IN (?)", category_topic_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove muted categories
|
|
|
|
muted_category_ids = CategoryUser.where(user_id: user.id, notification_level: CategoryUser.notification_levels[:muted]).pluck(:category_id)
|
|
|
|
if muted_category_ids.present?
|
|
|
|
topics = topics.where("topics.category_id NOT IN (?)", muted_category_ids)
|
2013-11-06 15:05:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
topics
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2014-04-17 16:42:40 -04:00
|
|
|
# Using the digest query, figure out what's new for a user since last seen
|
|
|
|
def self.new_since_last_seen(user, since, featured_topic_ids)
|
|
|
|
topics = Topic.for_digest(user, since)
|
|
|
|
topics.where("topics.id NOT IN (?)", featured_topic_ids)
|
|
|
|
end
|
|
|
|
|
2014-04-25 12:24:22 -04:00
|
|
|
def meta_data=(data)
|
|
|
|
custom_fields.replace(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def meta_data
|
|
|
|
custom_fields
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def update_meta_data(data)
|
2014-04-25 12:24:22 -04:00
|
|
|
custom_fields.update(data)
|
2013-02-05 14:16:51 -05:00
|
|
|
save
|
|
|
|
end
|
|
|
|
|
2013-04-21 23:48:05 -04:00
|
|
|
def reload(options=nil)
|
|
|
|
@post_numbers = nil
|
|
|
|
super(options)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def post_numbers
|
|
|
|
@post_numbers ||= posts.order(:post_number).pluck(:post_number)
|
|
|
|
end
|
|
|
|
|
2013-12-06 16:39:35 -05:00
|
|
|
def age_in_minutes
|
|
|
|
((Time.zone.now - created_at) / 1.minute).round
|
2013-05-27 20:58:57 -04:00
|
|
|
end
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
def has_meta_data_boolean?(key)
|
2013-02-05 14:16:51 -05:00
|
|
|
meta_data_string(key) == 'true'
|
|
|
|
end
|
|
|
|
|
|
|
|
def meta_data_string(key)
|
2014-04-25 12:24:22 -04:00
|
|
|
custom_fields[key.to_s]
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-04-03 13:25:52 -04:00
|
|
|
def self.listable_count_per_day(sinceDaysAgo=30)
|
|
|
|
listable_topics.where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count
|
2013-03-07 11:07:59 -05:00
|
|
|
end
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
def private_message?
|
2013-05-24 12:13:31 -04:00
|
|
|
archetype == Archetype.private_message
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-03-14 14:45:29 -04:00
|
|
|
# Search for similar topics
|
2013-06-12 13:43:59 -04:00
|
|
|
def self.similar_to(title, raw, user=nil)
|
2013-03-14 14:45:29 -04:00
|
|
|
return [] unless title.present?
|
|
|
|
return [] unless raw.present?
|
|
|
|
|
2014-04-14 15:48:58 -04:00
|
|
|
similar = Topic.select(sanitize_sql_array(["topics.*, similarity(topics.title, :title) + similarity(p.raw, :raw) AS similarity", title: title, raw: raw]))
|
2014-04-14 15:20:41 -04:00
|
|
|
.visible
|
|
|
|
.where(closed: false, archived: false)
|
|
|
|
.secured(Guardian.new(user))
|
|
|
|
.listable_topics
|
2014-04-14 15:48:58 -04:00
|
|
|
.joins("LEFT OUTER JOIN posts AS p ON p.topic_id = topics.id AND p.post_number = 1")
|
2014-04-14 15:20:41 -04:00
|
|
|
.limit(SiteSetting.max_similar_results)
|
|
|
|
.order('similarity desc')
|
|
|
|
|
|
|
|
# Exclude category definitions from similar topic suggestions
|
|
|
|
exclude_topic_ids = Category.pluck(:topic_id).compact!
|
|
|
|
if exclude_topic_ids.present?
|
|
|
|
similar = similar.where("topics.id NOT IN (?)", exclude_topic_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
similar
|
2013-03-14 14:45:29 -04:00
|
|
|
end
|
2013-03-06 15:17:07 -05:00
|
|
|
|
2013-05-27 21:00:53 -04:00
|
|
|
def update_status(status, enabled, user)
|
|
|
|
TopicStatusUpdate.new(self, user).update! status, enabled
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Atomically creates the next post number
|
2013-02-28 13:54:12 -05:00
|
|
|
def self.next_post_number(topic_id, reply = false)
|
2013-02-05 14:16:51 -05:00
|
|
|
highest = exec_sql("select coalesce(max(post_number),0) as max from posts where topic_id = ?", topic_id).first['max'].to_i
|
|
|
|
|
|
|
|
reply_sql = reply ? ", reply_count = reply_count + 1" : ""
|
2013-02-07 10:45:24 -05:00
|
|
|
result = exec_sql("UPDATE topics SET highest_post_number = ? + 1#{reply_sql}
|
2013-02-05 14:16:51 -05:00
|
|
|
WHERE id = ? RETURNING highest_post_number", highest, topic_id)
|
|
|
|
result.first['highest_post_number'].to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
# If a post is deleted we have to update our highest post counters
|
|
|
|
def self.reset_highest(topic_id)
|
2013-02-07 10:45:24 -05:00
|
|
|
result = exec_sql "UPDATE topics
|
2013-02-05 14:16:51 -05:00
|
|
|
SET highest_post_number = (SELECT COALESCE(MAX(post_number), 0) FROM posts WHERE topic_id = :topic_id AND deleted_at IS NULL),
|
2013-06-17 13:00:34 -04:00
|
|
|
posts_count = (SELECT count(*) FROM posts WHERE deleted_at IS NULL AND topic_id = :topic_id),
|
|
|
|
last_posted_at = (SELECT MAX(created_at) FROM POSTS WHERE topic_id = :topic_id AND deleted_at IS NULL)
|
2013-02-05 14:16:51 -05:00
|
|
|
WHERE id = :topic_id
|
2013-02-07 10:45:24 -05:00
|
|
|
RETURNING highest_post_number", topic_id: topic_id
|
2013-02-05 14:16:51 -05:00
|
|
|
highest_post_number = result.first['highest_post_number'].to_i
|
|
|
|
|
|
|
|
# Update the forum topic user records
|
|
|
|
exec_sql "UPDATE topic_users
|
|
|
|
SET last_read_post_number = CASE
|
|
|
|
WHEN last_read_post_number > :highest THEN :highest
|
|
|
|
ELSE last_read_post_number
|
|
|
|
END,
|
|
|
|
seen_post_count = CASE
|
|
|
|
WHEN seen_post_count > :highest THEN :highest
|
|
|
|
ELSE seen_post_count
|
|
|
|
END
|
|
|
|
WHERE topic_id = :topic_id",
|
|
|
|
highest: highest_post_number,
|
|
|
|
topic_id: topic_id
|
|
|
|
end
|
|
|
|
|
|
|
|
# This calculates the geometric mean of the posts and stores it with the topic
|
2014-02-26 19:45:20 -05:00
|
|
|
def self.calculate_avg_time(min_topic_age=nil)
|
|
|
|
builder = SqlBuilder.new("UPDATE topics
|
2013-02-05 14:16:51 -05:00
|
|
|
SET avg_time = x.gmean
|
2013-02-07 10:45:24 -05:00
|
|
|
FROM (SELECT topic_id,
|
2013-02-05 14:16:51 -05:00
|
|
|
round(exp(avg(ln(avg_time)))) AS gmean
|
|
|
|
FROM posts
|
2013-06-29 11:30:26 -04:00
|
|
|
WHERE avg_time > 0 AND avg_time IS NOT NULL
|
2013-02-05 14:16:51 -05:00
|
|
|
GROUP BY topic_id) AS x
|
2014-02-26 19:45:20 -05:00
|
|
|
/*where*/")
|
|
|
|
|
|
|
|
builder.where("x.topic_id = topics.id AND
|
|
|
|
(topics.avg_time <> x.gmean OR topics.avg_time IS NULL)")
|
|
|
|
|
|
|
|
if min_topic_age
|
|
|
|
builder.where("topics.bumped_at > :bumped_at",
|
|
|
|
bumped_at: min_topic_age)
|
|
|
|
end
|
|
|
|
|
|
|
|
builder.exec
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def changed_to_category(cat)
|
2014-05-06 09:41:59 -04:00
|
|
|
return true if cat.blank? || Category.find_by(topic_id: id).present?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
Topic.transaction do
|
|
|
|
old_category = category
|
|
|
|
|
2013-03-04 19:42:44 -05:00
|
|
|
if category_id.present? && category_id != cat.id
|
2013-07-01 14:45:52 -04:00
|
|
|
Category.where(['id = ?', category_id]).update_all 'topic_count = topic_count - 1'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-10-23 19:05:51 -04:00
|
|
|
success = true
|
|
|
|
if self.category_id != cat.id
|
|
|
|
self.category_id = cat.id
|
|
|
|
success = save
|
|
|
|
end
|
|
|
|
|
|
|
|
if success
|
2013-10-08 14:40:31 -04:00
|
|
|
CategoryFeaturedTopic.feature_topics_for(old_category)
|
|
|
|
Category.where(id: cat.id).update_all 'topic_count = topic_count + 1'
|
|
|
|
CategoryFeaturedTopic.feature_topics_for(cat) unless old_category.try(:id) == cat.try(:id)
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-10-08 14:40:31 -04:00
|
|
|
true
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_moderator_post(user, text, opts={})
|
|
|
|
new_post = nil
|
|
|
|
Topic.transaction do
|
2013-03-28 16:40:54 -04:00
|
|
|
creator = PostCreator.new(user,
|
|
|
|
raw: text,
|
|
|
|
post_type: Post.types[:moderator_action],
|
|
|
|
no_bump: opts[:bump].blank?,
|
|
|
|
topic_id: self.id)
|
|
|
|
new_post = creator.create
|
2013-02-07 10:45:24 -05:00
|
|
|
increment!(:moderator_posts_count)
|
2013-02-05 14:16:51 -05:00
|
|
|
new_post
|
|
|
|
end
|
|
|
|
|
|
|
|
if new_post.present?
|
|
|
|
# If we are moving posts, we want to insert the moderator post where the previous posts were
|
|
|
|
# in the stream, not at the end.
|
|
|
|
new_post.update_attributes(post_number: opts[:post_number], sort_order: opts[:post_number]) if opts[:post_number].present?
|
|
|
|
|
|
|
|
# Grab any links that are present
|
|
|
|
TopicLink.extract_from(new_post)
|
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
new_post
|
|
|
|
end
|
|
|
|
|
|
|
|
# Changes the category to a new name
|
|
|
|
def change_category(name)
|
|
|
|
# If the category name is blank, reset the attribute
|
|
|
|
if name.blank?
|
2014-05-06 09:41:59 -04:00
|
|
|
cat = Category.find_by(id: SiteSetting.uncategorized_category_id)
|
2013-10-23 19:05:51 -04:00
|
|
|
else
|
2014-05-06 09:41:59 -04:00
|
|
|
cat = Category.find_by(name: name)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-10-08 14:40:31 -04:00
|
|
|
return true if cat == category
|
2013-10-23 19:05:51 -04:00
|
|
|
return false unless cat
|
2013-02-05 14:16:51 -05:00
|
|
|
changed_to_category(cat)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-06-18 03:17:01 -04:00
|
|
|
def remove_allowed_user(username)
|
2014-05-06 09:41:59 -04:00
|
|
|
user = User.find_by(username: username)
|
2013-06-18 03:17:01 -04:00
|
|
|
if user
|
2014-05-06 09:41:59 -04:00
|
|
|
topic_user = topic_allowed_users.find_by(user_id: user.id)
|
2013-10-02 13:11:48 -04:00
|
|
|
if topic_user
|
|
|
|
topic_user.destroy
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2013-06-18 03:17:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Invite a user to the topic by username or email. Returns success/failure
|
2014-05-08 21:45:18 -04:00
|
|
|
def invite(invited_by, username_or_email, group_ids=nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
if private_message?
|
|
|
|
# If the user exists, add them to the topic.
|
2013-06-18 20:31:19 -04:00
|
|
|
user = User.find_by_username_or_email(username_or_email)
|
|
|
|
if user && topic_allowed_users.create!(user_id: user.id)
|
|
|
|
|
|
|
|
# Notify the user they've been invited
|
|
|
|
user.notifications.create(notification_type: Notification.types[:invited_to_private_message],
|
|
|
|
topic_id: id,
|
|
|
|
post_number: 1,
|
|
|
|
data: { topic_title: title,
|
|
|
|
display_username: invited_by.username }.to_json)
|
|
|
|
return true
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-06-18 20:31:19 -04:00
|
|
|
if username_or_email =~ /^.+@.+$/
|
|
|
|
# NOTE callers expect an invite object if an invite was sent via email
|
2014-05-08 21:45:18 -04:00
|
|
|
invite_by_email(invited_by, username_or_email, group_ids)
|
2013-06-18 20:31:19 -04:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-05-08 21:45:18 -04:00
|
|
|
def invite_by_email(invited_by, email, group_ids=nil)
|
|
|
|
Invite.invite_by_email(email, invited_by, self, group_ids)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-10-03 17:06:14 -04:00
|
|
|
def email_already_exists_for?(invite)
|
|
|
|
invite.email_already_exists and private_message?
|
|
|
|
end
|
|
|
|
|
|
|
|
def grant_permission_to_user(lower_email)
|
2014-05-06 09:41:59 -04:00
|
|
|
user = User.find_by(email: lower_email)
|
2013-10-03 17:06:14 -04:00
|
|
|
topic_allowed_users.create!(user_id: user.id)
|
|
|
|
end
|
|
|
|
|
2013-05-25 20:37:23 -04:00
|
|
|
def max_post_number
|
|
|
|
posts.maximum(:post_number).to_i
|
|
|
|
end
|
|
|
|
|
2013-05-08 13:33:58 -04:00
|
|
|
def move_posts(moved_by, post_ids, opts)
|
2013-05-25 20:40:33 -04:00
|
|
|
post_mover = PostMover.new(self, moved_by, post_ids)
|
2013-05-08 13:33:58 -04:00
|
|
|
|
2013-05-25 20:40:33 -04:00
|
|
|
if opts[:destination_topic_id]
|
|
|
|
post_mover.to_topic opts[:destination_topic_id]
|
|
|
|
elsif opts[:title]
|
2013-10-29 15:30:06 -04:00
|
|
|
post_mover.to_new_topic(opts[:title], opts[:category_id])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-12 12:33:42 -04:00
|
|
|
# Updates the denormalized statistics of a topic including featured posters. They shouldn't
|
|
|
|
# go out of sync unless you do something drastic live move posts from one topic to another.
|
|
|
|
# this recalculates everything.
|
|
|
|
def update_statistics
|
|
|
|
feature_topic_users
|
|
|
|
update_action_counts
|
|
|
|
Topic.reset_highest(id)
|
|
|
|
end
|
|
|
|
|
2013-02-05 20:13:41 -05:00
|
|
|
def update_flagged_posts_count
|
|
|
|
PostAction.update_flagged_posts_count
|
|
|
|
end
|
|
|
|
|
2013-03-12 12:33:42 -04:00
|
|
|
def update_action_counts
|
|
|
|
PostActionType.types.keys.each do |type|
|
|
|
|
count_field = "#{type}_count"
|
|
|
|
update_column(count_field, Post.where(topic_id: id).sum(count_field))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-23 02:21:19 -04:00
|
|
|
def posters_summary(options = {})
|
|
|
|
@posters_summary ||= TopicPostersSummary.new(self, options).summary
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-05-12 03:32:49 -04:00
|
|
|
def participants_summary(options = {})
|
|
|
|
@participants_summary ||= TopicParticipantsSummary.new(self, options).summary
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Enable/disable the star on the topic
|
|
|
|
def toggle_star(user, starred)
|
|
|
|
Topic.transaction do
|
2013-04-28 16:58:14 -04:00
|
|
|
TopicUser.change(user, id, {starred: starred}.merge( starred ? {starred_at: DateTime.now, unstarred_at: nil} : {unstarred_at: DateTime.now}))
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# Update the star count
|
2013-02-07 10:45:24 -05:00
|
|
|
exec_sql "UPDATE topics
|
|
|
|
SET star_count = (SELECT COUNT(*)
|
|
|
|
FROM topic_users AS ftu
|
2013-02-05 14:16:51 -05:00
|
|
|
WHERE ftu.topic_id = topics.id
|
|
|
|
AND ftu.starred = true)
|
2013-02-28 13:54:12 -05:00
|
|
|
WHERE id = ?", id
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
if starred
|
2014-01-09 16:22:54 -05:00
|
|
|
StarLimiter.new(user).performed!
|
2013-02-05 14:16:51 -05:00
|
|
|
else
|
2014-01-09 16:22:54 -05:00
|
|
|
StarLimiter.new(user).rollback!
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2014-06-16 13:21:21 -04:00
|
|
|
def make_banner!(user)
|
|
|
|
# only one banner at the same time
|
|
|
|
previous_banner = Topic.where(archetype: Archetype.banner).first
|
|
|
|
previous_banner.remove_banner!(user) if previous_banner.present?
|
|
|
|
|
|
|
|
self.archetype = Archetype.banner
|
|
|
|
self.add_moderator_post(user, I18n.t("archetypes.banner.message.make"))
|
|
|
|
self.save
|
2014-06-18 14:04:10 -04:00
|
|
|
|
|
|
|
MessageBus.publish('/site/banner', banner)
|
2014-06-16 13:21:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_banner!(user)
|
|
|
|
self.archetype = Archetype.default
|
|
|
|
self.add_moderator_post(user, I18n.t("archetypes.banner.message.remove"))
|
|
|
|
self.save
|
2014-06-18 14:04:10 -04:00
|
|
|
|
|
|
|
MessageBus.publish('/site/banner', nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def banner
|
|
|
|
post = self.posts.order(:post_number).limit(1).first
|
|
|
|
|
|
|
|
{
|
|
|
|
html: post.cooked,
|
|
|
|
url: self.url,
|
|
|
|
key: self.id
|
|
|
|
}
|
2014-06-16 13:21:21 -04:00
|
|
|
end
|
|
|
|
|
2013-04-18 14:27:22 -04:00
|
|
|
def self.starred_counts_per_day(sinceDaysAgo=30)
|
2013-05-24 12:13:31 -04:00
|
|
|
TopicUser.starred_since(sinceDaysAgo).by_date_starred.count
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-06-07 14:17:12 -04:00
|
|
|
# Even if the slug column in the database is null, topic.slug will return something:
|
2013-02-05 14:16:51 -05:00
|
|
|
def slug
|
2013-04-23 22:46:43 -04:00
|
|
|
unless slug = read_attribute(:slug)
|
|
|
|
return '' unless title.present?
|
|
|
|
slug = Slug.for(title).presence || "topic"
|
|
|
|
if new_record?
|
|
|
|
write_attribute(:slug, slug)
|
|
|
|
else
|
|
|
|
update_column(:slug, slug)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
slug
|
|
|
|
end
|
|
|
|
|
|
|
|
def title=(t)
|
2013-05-24 02:06:38 -04:00
|
|
|
slug = (Slug.for(t.to_s).presence || "topic")
|
2013-04-23 22:46:43 -04:00
|
|
|
write_attribute(:slug, slug)
|
|
|
|
write_attribute(:title,t)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-24 02:06:38 -04:00
|
|
|
# NOTE: These are probably better off somewhere else.
|
|
|
|
# Having a model know about URLs seems a bit strange.
|
2013-02-05 14:16:51 -05:00
|
|
|
def last_post_url
|
|
|
|
"/t/#{slug}/#{id}/#{posts_count}"
|
|
|
|
end
|
|
|
|
|
2013-05-09 03:37:34 -04:00
|
|
|
def self.url(id, slug, post_number=nil)
|
|
|
|
url = "#{Discourse.base_url}/t/#{slug}/#{id}"
|
|
|
|
url << "/#{post_number}" if post_number.to_i > 1
|
|
|
|
url
|
|
|
|
end
|
|
|
|
|
2013-05-25 20:38:15 -04:00
|
|
|
def url(post_number = nil)
|
|
|
|
self.class.url id, slug, post_number
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def relative_url(post_number=nil)
|
|
|
|
url = "/t/#{slug}/#{id}"
|
2013-05-09 03:37:34 -04:00
|
|
|
url << "/#{post_number}" if post_number.to_i > 1
|
2013-02-05 14:16:51 -05:00
|
|
|
url
|
|
|
|
end
|
|
|
|
|
2013-03-06 15:17:07 -05:00
|
|
|
def clear_pin_for(user)
|
|
|
|
return unless user.present?
|
|
|
|
TopicUser.change(user.id, id, cleared_pinned_at: Time.now)
|
|
|
|
end
|
|
|
|
|
2014-04-09 20:56:56 -04:00
|
|
|
def re_pin_for(user)
|
|
|
|
return unless user.present?
|
|
|
|
TopicUser.change(user.id, id, cleared_pinned_at: nil)
|
|
|
|
end
|
|
|
|
|
2014-04-07 02:38:51 -04:00
|
|
|
def update_pinned(status, global=false)
|
2013-03-06 15:17:07 -05:00
|
|
|
update_column(:pinned_at, status ? Time.now : nil)
|
2014-04-07 02:38:51 -04:00
|
|
|
update_column(:pinned_globally, global)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def draft_key
|
2013-02-28 13:54:12 -05:00
|
|
|
"#{Draft::EXISTING_TOPIC}#{id}"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-24 02:06:38 -04:00
|
|
|
def notifier
|
|
|
|
@topic_notifier ||= TopicNotifier.new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def muted?(user)
|
|
|
|
if user && user.id
|
|
|
|
notifier.muted?(user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-06 16:39:35 -05:00
|
|
|
def auto_close_hours=(num_hours)
|
2013-05-15 15:19:41 -04:00
|
|
|
@ignore_category_auto_close = true
|
2013-12-06 16:39:35 -05:00
|
|
|
set_auto_close( num_hours )
|
2013-06-06 17:04:10 -04:00
|
|
|
end
|
|
|
|
|
2013-11-10 18:52:44 -05:00
|
|
|
def self.auto_close
|
2013-12-18 14:09:49 -05:00
|
|
|
Topic.where("NOT closed AND auto_close_at < ? AND auto_close_user_id IS NOT NULL", 1.minute.ago).each do |t|
|
2013-11-10 18:52:44 -05:00
|
|
|
t.auto_close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_close(closer = nil)
|
|
|
|
if auto_close_at && !closed? && !deleted_at && auto_close_at < 5.minutes.from_now
|
|
|
|
closer ||= auto_close_user
|
|
|
|
if Guardian.new(closer).can_moderate?(self)
|
|
|
|
update_status('autoclosed', true, closer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-26 19:06:20 -05:00
|
|
|
# Valid arguments for the auto close time:
|
|
|
|
# * An integer, which is the number of hours from now to close the topic.
|
|
|
|
# * A time, like "12:00", which is the time at which the topic will close in the current day
|
|
|
|
# or the next day if that time has already passed today.
|
|
|
|
# * A timestamp, like "2013-11-25 13:00", when the topic should close.
|
|
|
|
# * A timestamp with timezone in JSON format. (e.g., "2013-11-26T21:00:00.000Z")
|
|
|
|
# * nil, to prevent the topic from automatically closing.
|
|
|
|
def set_auto_close(arg, by_user=nil)
|
2014-03-06 06:44:52 -05:00
|
|
|
if arg.is_a?(String) && matches = /^([\d]{1,2}):([\d]{1,2})$/.match(arg.strip)
|
2013-11-26 19:06:20 -05:00
|
|
|
now = Time.zone.now
|
|
|
|
self.auto_close_at = Time.zone.local(now.year, now.month, now.day, matches[1].to_i, matches[2].to_i)
|
|
|
|
self.auto_close_at += 1.day if self.auto_close_at < now
|
2014-03-06 06:44:52 -05:00
|
|
|
elsif arg.is_a?(String) && arg.include?('-') && timestamp = Time.zone.parse(arg)
|
2013-11-26 19:06:20 -05:00
|
|
|
self.auto_close_at = timestamp
|
|
|
|
self.errors.add(:auto_close_at, :invalid) if timestamp < Time.zone.now
|
|
|
|
else
|
|
|
|
num_hours = arg.to_i
|
|
|
|
self.auto_close_at = (num_hours > 0 ? num_hours.hours.from_now : nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
unless self.auto_close_at.nil?
|
2013-06-06 17:04:10 -04:00
|
|
|
self.auto_close_started_at ||= Time.zone.now
|
2014-03-06 06:44:52 -05:00
|
|
|
if by_user && by_user.staff?
|
2013-06-06 17:04:10 -04:00
|
|
|
self.auto_close_user = by_user
|
|
|
|
else
|
|
|
|
self.auto_close_user ||= (self.user.staff? ? self.user : Discourse.system_user)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.auto_close_started_at = nil
|
|
|
|
end
|
|
|
|
self
|
2013-05-07 14:25:41 -04:00
|
|
|
end
|
|
|
|
|
2013-07-13 21:24:16 -04:00
|
|
|
def read_restricted_category?
|
|
|
|
category && category.read_restricted
|
2013-05-20 02:04:53 -04:00
|
|
|
end
|
2013-07-08 15:23:20 -04:00
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
def acting_user
|
|
|
|
@acting_user || user
|
|
|
|
end
|
|
|
|
|
|
|
|
def acting_user=(u)
|
|
|
|
@acting_user = u
|
|
|
|
end
|
|
|
|
|
2014-03-23 21:19:08 -04:00
|
|
|
def secure_group_ids
|
|
|
|
@secure_group_ids ||= if self.category && self.category.read_restricted?
|
|
|
|
self.category.secure_group_ids
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-01 15:29:15 -04:00
|
|
|
def has_topic_embed?
|
|
|
|
TopicEmbed.where(topic_id: id).exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def expandable_first_post?
|
|
|
|
SiteSetting.embeddable_host.present? && SiteSetting.embed_truncate? && has_topic_embed?
|
|
|
|
end
|
|
|
|
|
2013-07-08 15:23:20 -04:00
|
|
|
private
|
|
|
|
|
2013-10-16 05:28:18 -04:00
|
|
|
def update_category_topic_count_by(num)
|
|
|
|
if category_id.present?
|
|
|
|
Category.where(['id = ?', category_id]).update_all("topic_count = topic_count " + (num > 0 ? '+' : '') + "#{num}")
|
2013-07-08 15:23:20 -04:00
|
|
|
end
|
2013-10-16 05:28:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def limit_first_day_topics_per_day
|
|
|
|
apply_per_day_rate_limit_for("first-day-topics", :max_topics_in_first_day)
|
|
|
|
end
|
|
|
|
|
|
|
|
def apply_per_day_rate_limit_for(key, method_name)
|
|
|
|
RateLimiter.new(user, "#{key}-per-day:#{Date.today.to_s}", SiteSetting.send(method_name), 1.day.to_i)
|
|
|
|
end
|
2013-08-14 00:14:56 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: topics
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# title :string(255) not null
|
|
|
|
# last_posted_at :datetime
|
2014-05-27 21:49:50 -04:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2013-05-23 22:48:32 -04:00
|
|
|
# views :integer default(0), not null
|
|
|
|
# posts_count :integer default(0), not null
|
2013-09-06 03:38:50 -04:00
|
|
|
# user_id :integer
|
2013-05-23 22:48:32 -04:00
|
|
|
# last_post_user_id :integer not null
|
|
|
|
# reply_count :integer default(0), not null
|
|
|
|
# featured_user1_id :integer
|
|
|
|
# featured_user2_id :integer
|
|
|
|
# featured_user3_id :integer
|
|
|
|
# avg_time :integer
|
|
|
|
# deleted_at :datetime
|
|
|
|
# highest_post_number :integer default(0), not null
|
|
|
|
# image_url :string(255)
|
|
|
|
# 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
|
|
|
|
# star_count :integer default(0), not null
|
|
|
|
# category_id :integer
|
|
|
|
# visible :boolean default(TRUE), not null
|
|
|
|
# moderator_posts_count :integer default(0), not null
|
|
|
|
# closed :boolean default(FALSE), not null
|
|
|
|
# archived :boolean default(FALSE), not null
|
|
|
|
# bumped_at :datetime not null
|
2013-11-18 12:48:26 -05:00
|
|
|
# has_summary :boolean default(FALSE), not null
|
2013-05-23 22:48:32 -04:00
|
|
|
# vote_count :integer default(0), not null
|
|
|
|
# archetype :string(255) default("regular"), not null
|
|
|
|
# featured_user4_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
|
|
|
|
# pinned_at :datetime
|
|
|
|
# score :float
|
|
|
|
# percent_rank :float default(1.0), not null
|
|
|
|
# notify_user_count :integer default(0), not null
|
|
|
|
# subtype :string(255)
|
|
|
|
# slug :string(255)
|
|
|
|
# auto_close_at :datetime
|
|
|
|
# auto_close_user_id :integer
|
2013-06-06 17:04:10 -04:00
|
|
|
# auto_close_started_at :datetime
|
2013-07-13 21:24:16 -04:00
|
|
|
# deleted_by_id :integer
|
2013-12-05 01:40:35 -05:00
|
|
|
# participant_count :integer default(1)
|
2014-02-06 19:07:36 -05:00
|
|
|
# word_count :integer
|
2014-03-20 00:35:51 -04:00
|
|
|
# excerpt :string(1000)
|
2014-04-08 11:35:44 -04:00
|
|
|
# pinned_globally :boolean default(FALSE), not null
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2014-03-20 00:35:51 -04:00
|
|
|
# idx_topics_front_page (deleted_at,visible,archetype,category_id,id)
|
|
|
|
# idx_topics_user_id_deleted_at (user_id)
|
2014-05-27 21:49:50 -04:00
|
|
|
# index_topics_on_bumped_at (bumped_at)
|
2014-03-20 00:35:51 -04:00
|
|
|
# index_topics_on_id_and_deleted_at (id,deleted_at)
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|