2013-06-04 14:13:01 -04:00
|
|
|
class TopicCreator
|
|
|
|
|
|
|
|
attr_accessor :errors
|
|
|
|
|
2013-07-21 21:40:39 -04:00
|
|
|
def self.create(user, guardian, opts)
|
|
|
|
self.new(user, guardian, opts).create
|
|
|
|
end
|
|
|
|
|
2013-06-04 14:13:01 -04:00
|
|
|
def initialize(user, guardian, opts)
|
|
|
|
@user = user
|
|
|
|
@guardian = guardian
|
|
|
|
@opts = opts
|
2014-09-08 11:11:56 -04:00
|
|
|
@added_users = []
|
2013-06-04 14:13:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2014-02-06 14:52:50 -05:00
|
|
|
@topic = Topic.new(setup_topic_params)
|
2013-06-04 14:13:01 -04:00
|
|
|
|
2014-02-06 14:52:50 -05:00
|
|
|
setup_auto_close_time
|
|
|
|
process_private_message
|
2013-06-04 14:13:01 -04:00
|
|
|
save_topic
|
2014-09-08 11:11:56 -04:00
|
|
|
create_warning
|
2013-07-21 21:40:39 -04:00
|
|
|
watch_topic
|
|
|
|
|
2013-06-04 14:13:01 -04:00
|
|
|
@topic
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-09-08 11:11:56 -04:00
|
|
|
def create_warning
|
|
|
|
return unless @opts[:is_warning]
|
|
|
|
|
|
|
|
# We can only attach warnings to PMs
|
|
|
|
unless @topic.private_message?
|
|
|
|
@topic.errors.add(:base, :warning_requires_pm)
|
|
|
|
@errors = @topic.errors
|
|
|
|
raise ActiveRecord::Rollback.new
|
|
|
|
end
|
|
|
|
|
|
|
|
# Don't create it if there is more than one user
|
|
|
|
if @added_users.size != 1
|
|
|
|
@topic.errors.add(:base, :too_many_users)
|
|
|
|
@errors = @topic.errors
|
|
|
|
raise ActiveRecord::Rollback.new
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a warning record
|
|
|
|
Warning.create(topic: @topic, user: @added_users.first, created_by: @user)
|
|
|
|
end
|
|
|
|
|
2013-07-21 21:40:39 -04:00
|
|
|
def watch_topic
|
|
|
|
unless @opts[:auto_track] == false
|
|
|
|
@topic.notifier.watch_topic!(@topic.user_id)
|
|
|
|
end
|
2014-01-22 01:46:52 -05:00
|
|
|
|
2014-06-24 12:31:36 -04: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
|
|
|
|
|
|
|
|
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 01:46:52 -05:00
|
|
|
end
|
|
|
|
|
2014-01-01 20:00:08 -05:00
|
|
|
CategoryUser.auto_watch_new_topic(@topic)
|
2014-06-01 23:55:01 -04:00
|
|
|
CategoryUser.auto_track_new_topic(@topic)
|
2013-07-21 21:40:39 -04:00
|
|
|
end
|
|
|
|
|
2014-02-06 14:52:50 -05:00
|
|
|
def setup_topic_params
|
2014-01-24 06:57:48 -05:00
|
|
|
topic_params = {
|
|
|
|
title: @opts[:title],
|
|
|
|
user_id: @user.id,
|
|
|
|
last_post_user_id: @user.id
|
|
|
|
}
|
|
|
|
|
2014-07-03 14:43:24 -04:00
|
|
|
[:subtype, :archetype, :meta_data, :import_mode].each do |key|
|
2014-02-06 14:52:50 -05:00
|
|
|
topic_params[key] = @opts[key] if @opts[key].present?
|
2014-01-24 06:57:48 -05:00
|
|
|
end
|
|
|
|
|
2014-09-08 13:23:40 -04:00
|
|
|
# Automatically give it a moderator warning subtype if specified
|
|
|
|
topic_params[:subtype] = TopicSubtype.moderator_warning if @opts[:is_warning]
|
|
|
|
|
2014-02-06 14:52:50 -05:00
|
|
|
category = find_category
|
2014-01-24 06:57:48 -05:00
|
|
|
|
2014-03-07 04:44:04 -05:00
|
|
|
@guardian.ensure_can_create!(Topic, category)
|
2014-01-24 06:57:48 -05:00
|
|
|
|
2013-06-04 14:13:01 -04:00
|
|
|
topic_params[:category_id] = category.id if category.present?
|
2014-02-06 14:52:50 -05:00
|
|
|
|
2013-06-04 14:13:01 -04:00
|
|
|
topic_params[:created_at] = Time.zone.parse(@opts[:created_at].to_s) if @opts[:created_at].present?
|
2014-01-24 06:57:48 -05:00
|
|
|
|
2015-01-19 09:00:55 -05:00
|
|
|
topic_params[:pinned_at] = Time.zone.parse(@opts[:pinned_at].to_s) if @opts[:pinned_at].present?
|
2015-03-13 16:24:11 -04:00
|
|
|
topic_params[:pinned_globally] = @opts[:pinned_globally] if @opts[:pinned_globally].present?
|
2015-01-19 09:00:55 -05:00
|
|
|
|
2013-06-04 14:13:01 -04:00
|
|
|
topic_params
|
|
|
|
end
|
|
|
|
|
2014-02-06 14:52:50 -05: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 09:41:59 -04:00
|
|
|
Category.find_by(id: @opts[:category])
|
2014-02-06 14:52:50 -05:00
|
|
|
else
|
2014-08-18 11:07:32 -04:00
|
|
|
Category.find_by(name_lower: @opts[:category].try(:downcase))
|
2014-02-06 14:52:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-26 19:06:20 -05:00
|
|
|
def setup_auto_close_time
|
2014-02-06 14:52:50 -05:00
|
|
|
return unless @opts[:auto_close_time].present?
|
2013-11-28 11:06:04 -05:00
|
|
|
return unless @guardian.can_moderate?(@topic)
|
2013-11-26 19:06:20 -05:00
|
|
|
@topic.set_auto_close(@opts[:auto_close_time], @user)
|
2013-06-04 14:13:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def process_private_message
|
2014-02-06 14:52:50 -05:00
|
|
|
return unless @opts[:archetype] == Archetype.private_message
|
2013-06-04 14:13:01 -04:00
|
|
|
@topic.subtype = TopicSubtype.user_to_user unless @topic.subtype
|
|
|
|
|
|
|
|
unless @opts[:target_usernames].present? || @opts[:target_group_names].present?
|
2014-12-29 21:26:39 -05:00
|
|
|
@topic.errors.add(:base, :no_user_selected)
|
2013-06-04 14:13:01 -04:00
|
|
|
@errors = @topic.errors
|
|
|
|
raise ActiveRecord::Rollback.new
|
|
|
|
end
|
|
|
|
|
|
|
|
add_users(@topic,@opts[:target_usernames])
|
|
|
|
add_groups(@topic,@opts[:target_group_names])
|
|
|
|
@topic.topic_allowed_users.build(user_id: @user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_topic
|
2013-07-01 22:22:56 -04:00
|
|
|
unless @topic.save(validate: !@opts[:skip_validations])
|
2013-06-04 14:13:01 -04:00
|
|
|
@errors = @topic.errors
|
|
|
|
raise ActiveRecord::Rollback.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_users(topic, usernames)
|
|
|
|
return unless usernames
|
|
|
|
User.where(username: usernames.split(',')).each do |user|
|
2014-09-08 11:11:56 -04:00
|
|
|
check_can_send_permission!(topic, user)
|
|
|
|
@added_users << user
|
2013-06-04 14:13:01 -04: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|
|
|
|
|
check_can_send_permission!(topic,group)
|
|
|
|
topic.topic_allowed_groups.build(group_id: group.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_can_send_permission!(topic,item)
|
|
|
|
unless @guardian.can_send_private_message?(item)
|
2014-12-29 21:26:39 -05:00
|
|
|
topic.errors.add(:base, :cant_send_pm)
|
2013-06-04 14:13:01 -04:00
|
|
|
@errors = topic.errors
|
|
|
|
raise ActiveRecord::Rollback.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|