2015-03-27 04:57:50 +08:00
|
|
|
require_dependency 'has_errors'
|
2013-06-05 02:13:01 +08:00
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
class TopicCreator
|
2015-08-11 13:46:21 +08:00
|
|
|
|
|
|
|
attr_reader :user, :guardian, :opts
|
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
include HasErrors
|
2013-06-05 02:13:01 +08:00
|
|
|
|
2013-07-22 09:40:39 +08:00
|
|
|
def self.create(user, guardian, opts)
|
|
|
|
self.new(user, guardian, opts).create
|
|
|
|
end
|
|
|
|
|
2013-06-05 02:13:01 +08:00
|
|
|
def initialize(user, guardian, opts)
|
|
|
|
@user = user
|
|
|
|
@guardian = guardian
|
|
|
|
@opts = opts
|
2014-09-08 23:11:56 +08:00
|
|
|
@added_users = []
|
2013-06-05 02:13:01 +08:00
|
|
|
end
|
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
def valid?
|
|
|
|
topic = Topic.new(setup_topic_params)
|
2015-08-11 13:46:21 +08:00
|
|
|
# validate? will clear the error hash
|
|
|
|
# so we fire the validation event after
|
|
|
|
# this allows us to add errors
|
|
|
|
valid = topic.valid?
|
|
|
|
DiscourseEvent.trigger(:after_validate_topic, topic, self)
|
|
|
|
valid &&= topic.errors.empty?
|
|
|
|
|
|
|
|
add_errors_from(topic) unless valid
|
|
|
|
|
|
|
|
valid
|
2015-03-27 04:57:50 +08:00
|
|
|
end
|
|
|
|
|
2013-06-05 02:13:01 +08:00
|
|
|
def create
|
2015-03-27 04:57:50 +08:00
|
|
|
topic = Topic.new(setup_topic_params)
|
2015-08-11 13:46:21 +08:00
|
|
|
DiscourseEvent.trigger(:before_create_topic, topic, self)
|
2013-06-05 02:13:01 +08:00
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
setup_auto_close_time(topic)
|
|
|
|
process_private_message(topic)
|
|
|
|
save_topic(topic)
|
|
|
|
create_warning(topic)
|
|
|
|
watch_topic(topic)
|
2013-07-22 09:40:39 +08:00
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
topic
|
2013-06-05 02:13:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
def create_warning(topic)
|
2014-09-08 23:11:56 +08:00
|
|
|
return unless @opts[:is_warning]
|
|
|
|
|
|
|
|
# We can only attach warnings to PMs
|
2015-03-27 04:57:50 +08:00
|
|
|
rollback_with!(topic, :warning_requires_pm) unless topic.private_message?
|
2014-09-08 23:11:56 +08:00
|
|
|
|
|
|
|
# Don't create it if there is more than one user
|
2015-03-27 04:57:50 +08:00
|
|
|
rollback_with!(topic, :too_many_users) if @added_users.size != 1
|
2014-09-08 23:11:56 +08:00
|
|
|
|
|
|
|
# Create a warning record
|
2015-03-27 04:57:50 +08:00
|
|
|
Warning.create(topic: topic, user: @added_users.first, created_by: @user)
|
2014-09-08 23:11:56 +08:00
|
|
|
end
|
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
def watch_topic(topic)
|
2013-07-22 09:40:39 +08:00
|
|
|
unless @opts[:auto_track] == false
|
2015-03-27 04:57:50 +08:00
|
|
|
topic.notifier.watch_topic!(topic.user_id)
|
2013-07-22 09:40:39 +08:00
|
|
|
end
|
2014-01-22 14:46:52 +08:00
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
user_ids = topic.topic_allowed_users(true).pluck(:user_id)
|
|
|
|
user_ids += topic.topic_allowed_groups(true).map { |t| t.group.users.pluck(:id) }.flatten
|
2014-06-25 00:31:36 +08:00
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
user_ids.uniq.reject{ |id| id == topic.user_id }.each do |user_id|
|
|
|
|
topic.notifier.watch_topic!(user_id, nil) unless user_id == -1
|
2014-01-22 14:46:52 +08:00
|
|
|
end
|
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
CategoryUser.auto_watch_new_topic(topic)
|
|
|
|
CategoryUser.auto_track_new_topic(topic)
|
2013-07-22 09:40:39 +08:00
|
|
|
end
|
|
|
|
|
2014-02-07 03:52:50 +08:00
|
|
|
def setup_topic_params
|
2014-01-24 19:57:48 +08:00
|
|
|
topic_params = {
|
|
|
|
title: @opts[:title],
|
|
|
|
user_id: @user.id,
|
|
|
|
last_post_user_id: @user.id
|
|
|
|
}
|
|
|
|
|
2014-07-04 02:43:24 +08:00
|
|
|
[:subtype, :archetype, :meta_data, :import_mode].each do |key|
|
2014-02-07 03:52:50 +08:00
|
|
|
topic_params[key] = @opts[key] if @opts[key].present?
|
2014-01-24 19:57:48 +08:00
|
|
|
end
|
|
|
|
|
2015-07-16 14:11:20 +08:00
|
|
|
if topic_params[:import_mode] && @opts[:views].to_i > 0
|
|
|
|
topic_params[:views] = @opts[:views].to_i
|
|
|
|
end
|
|
|
|
|
2014-09-09 01:23:40 +08:00
|
|
|
# Automatically give it a moderator warning subtype if specified
|
|
|
|
topic_params[:subtype] = TopicSubtype.moderator_warning if @opts[:is_warning]
|
|
|
|
|
2014-02-07 03:52:50 +08:00
|
|
|
category = find_category
|
2014-01-24 19:57:48 +08:00
|
|
|
|
2015-10-17 15:48:56 +08:00
|
|
|
@guardian.ensure_can_create!(Topic, category) unless (@opts[:skip_validations] || @opts[:archetype] == Archetype.private_message)
|
2014-01-24 19:57:48 +08:00
|
|
|
|
2013-06-05 02:13:01 +08:00
|
|
|
topic_params[:category_id] = category.id if category.present?
|
2014-02-07 03:52:50 +08:00
|
|
|
|
2013-06-05 02:13:01 +08:00
|
|
|
topic_params[:created_at] = Time.zone.parse(@opts[:created_at].to_s) if @opts[:created_at].present?
|
2014-01-24 19:57:48 +08:00
|
|
|
|
2015-01-19 22:00:55 +08:00
|
|
|
topic_params[:pinned_at] = Time.zone.parse(@opts[:pinned_at].to_s) if @opts[:pinned_at].present?
|
2015-03-14 04:24:11 +08:00
|
|
|
topic_params[:pinned_globally] = @opts[:pinned_globally] if @opts[:pinned_globally].present?
|
2015-01-19 22:00:55 +08:00
|
|
|
|
2013-06-05 02:13:01 +08:00
|
|
|
topic_params
|
|
|
|
end
|
|
|
|
|
2014-02-07 03:52:50 +08:00
|
|
|
def find_category
|
|
|
|
# PM can't have a category
|
|
|
|
@opts.delete(:category) if @opts[:archetype].present? && @opts[:archetype] == Archetype.private_message
|
|
|
|
|
|
|
|
# Temporary fix to allow older clients to create topics.
|
|
|
|
# When all clients are updated the category variable should
|
|
|
|
# be set directly to the contents of the if statement.
|
|
|
|
if (@opts[:category].is_a? Integer) || (@opts[:category] =~ /^\d+$/)
|
2014-05-06 21:41:59 +08:00
|
|
|
Category.find_by(id: @opts[:category])
|
2014-02-07 03:52:50 +08:00
|
|
|
else
|
2014-08-18 23:07:32 +08:00
|
|
|
Category.find_by(name_lower: @opts[:category].try(:downcase))
|
2014-02-07 03:52:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
def setup_auto_close_time(topic)
|
2014-02-07 03:52:50 +08:00
|
|
|
return unless @opts[:auto_close_time].present?
|
2015-03-27 04:57:50 +08:00
|
|
|
return unless @guardian.can_moderate?(topic)
|
2015-05-28 00:22:34 +08:00
|
|
|
topic.set_auto_close(@opts[:auto_close_time], {by_user: @user})
|
2013-06-05 02:13:01 +08:00
|
|
|
end
|
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
def process_private_message(topic)
|
2014-02-07 03:52:50 +08:00
|
|
|
return unless @opts[:archetype] == Archetype.private_message
|
2015-03-27 04:57:50 +08:00
|
|
|
topic.subtype = TopicSubtype.user_to_user unless topic.subtype
|
2013-06-05 02:13:01 +08:00
|
|
|
|
|
|
|
unless @opts[:target_usernames].present? || @opts[:target_group_names].present?
|
2015-03-27 04:57:50 +08:00
|
|
|
rollback_with!(topic, :no_user_selected)
|
2013-06-05 02:13:01 +08:00
|
|
|
end
|
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
add_users(topic,@opts[:target_usernames])
|
|
|
|
add_groups(topic,@opts[:target_group_names])
|
|
|
|
topic.topic_allowed_users.build(user_id: @user.id)
|
2013-06-05 02:13:01 +08:00
|
|
|
end
|
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
def save_topic(topic)
|
2015-04-22 04:48:39 +08:00
|
|
|
topic.disable_rate_limits! if @opts[:skip_validations]
|
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
unless topic.save(validate: !@opts[:skip_validations])
|
|
|
|
rollback_from_errors!(topic)
|
2013-06-05 02:13:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_users(topic, usernames)
|
|
|
|
return unless usernames
|
2015-09-23 17:16:17 +08:00
|
|
|
User.where(username: usernames.split(',').flatten).each do |user|
|
2014-09-08 23:11:56 +08:00
|
|
|
check_can_send_permission!(topic, user)
|
|
|
|
@added_users << user
|
2013-06-05 02:13:01 +08:00
|
|
|
topic.topic_allowed_users.build(user_id: user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_groups(topic, groups)
|
|
|
|
return unless groups
|
|
|
|
Group.where(name: groups.split(',')).each do |group|
|
2015-03-27 04:57:50 +08:00
|
|
|
check_can_send_permission!(topic, group)
|
2013-06-05 02:13:01 +08:00
|
|
|
topic.topic_allowed_groups.build(group_id: group.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-27 04:57:50 +08:00
|
|
|
def check_can_send_permission!(topic, obj)
|
|
|
|
rollback_with!(topic, :cant_send_pm) unless @guardian.can_send_private_message?(obj)
|
2013-06-05 02:13:01 +08:00
|
|
|
end
|
|
|
|
end
|