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
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
topic_params = setup
|
|
|
|
@topic = Topic.new(topic_params)
|
|
|
|
|
2013-11-27 20:41:04 -05:00
|
|
|
setup_auto_close_time if @opts[:auto_close_time].present?
|
2013-06-04 14:13:01 -04:00
|
|
|
|
|
|
|
process_private_message if @opts[:archetype] == Archetype.private_message
|
|
|
|
save_topic
|
|
|
|
|
2013-07-21 21:40:39 -04:00
|
|
|
watch_topic
|
2014-01-01 20:00:08 -05:00
|
|
|
auto_mute_topic
|
2013-07-21 21:40:39 -04:00
|
|
|
|
2013-06-04 14:13:01 -04:00
|
|
|
@topic
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-01-01 20:00:08 -05:00
|
|
|
def auto_mute_topic
|
|
|
|
CategoryUser.auto_mute_new_topic(@topic)
|
|
|
|
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
|
|
|
|
|
|
|
@topic.topic_allowed_users.pluck(:user_id).reject{|id| id == @topic.user_id}.each do |id|
|
|
|
|
@topic.notifier.watch_topic!(id, nil)
|
|
|
|
end
|
|
|
|
|
2013-12-29 21:02:12 -05:00
|
|
|
TopicUser.auto_watch_new_topic(@topic.id)
|
2014-01-01 20:00:08 -05:00
|
|
|
CategoryUser.auto_watch_new_topic(@topic)
|
2013-07-21 21:40:39 -04:00
|
|
|
end
|
|
|
|
|
2013-06-04 14:13:01 -04:00
|
|
|
def setup
|
2014-01-24 06:57:48 -05:00
|
|
|
topic_params = {
|
|
|
|
title: @opts[:title],
|
|
|
|
user_id: @user.id,
|
|
|
|
last_post_user_id: @user.id
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:13:01 -04:00
|
|
|
topic_params[:subtype] = @opts[:subtype] if @opts[:subtype].present?
|
|
|
|
|
2014-01-24 06:57:48 -05:00
|
|
|
if @opts[:archetype].present?
|
|
|
|
topic_params[:archetype] = @opts[:archetype]
|
|
|
|
# PM can't have a category
|
|
|
|
@opts.delete(:category) if topic_params[:archetype] == Archetype.private_message
|
|
|
|
end
|
|
|
|
|
2013-07-17 18:58:25 -04:00
|
|
|
# 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.
|
|
|
|
category = if (@opts[:category].is_a? Integer) || (@opts[:category] =~ /^\d+$/)
|
|
|
|
Category.where(id: @opts[:category]).first
|
|
|
|
else
|
|
|
|
Category.where(name: @opts[:category]).first
|
|
|
|
end
|
2014-01-24 06:57:48 -05:00
|
|
|
|
2013-07-16 01:44:07 -04: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?
|
|
|
|
topic_params[:meta_data] = @opts[:meta_data] if @opts[:meta_data].present?
|
|
|
|
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
|
|
|
|
2013-06-04 14:13:01 -04:00
|
|
|
topic_params
|
|
|
|
end
|
|
|
|
|
2013-11-26 19:06:20 -05:00
|
|
|
def setup_auto_close_time
|
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
|
|
|
|
@topic.subtype = TopicSubtype.user_to_user unless @topic.subtype
|
|
|
|
|
|
|
|
unless @opts[:target_usernames].present? || @opts[:target_group_names].present?
|
|
|
|
@topic.errors.add(:archetype, :cant_send_pm)
|
|
|
|
@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|
|
|
|
|
check_can_send_permission!(topic,user)
|
|
|
|
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)
|
|
|
|
topic.errors.add(:archetype, :cant_send_pm)
|
|
|
|
@errors = topic.errors
|
|
|
|
raise ActiveRecord::Rollback.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|