2015-03-26 15:57:50 -05:00
|
|
|
require_dependency 'has_errors'
|
2013-06-04 13:13:01 -05:00
|
|
|
|
2015-03-26 15:57:50 -05:00
|
|
|
class TopicCreator
|
2015-08-11 00:46:21 -05:00
|
|
|
|
|
|
|
attr_reader :user, :guardian, :opts
|
|
|
|
|
2015-03-26 15:57:50 -05:00
|
|
|
include HasErrors
|
2013-06-04 13:13:01 -05:00
|
|
|
|
2013-07-21 20:40:39 -05:00
|
|
|
def self.create(user, guardian, opts)
|
|
|
|
self.new(user, guardian, opts).create
|
|
|
|
end
|
|
|
|
|
2013-06-04 13:13:01 -05:00
|
|
|
def initialize(user, guardian, opts)
|
|
|
|
@user = user
|
|
|
|
@guardian = guardian
|
|
|
|
@opts = opts
|
2014-09-08 10:11:56 -05:00
|
|
|
@added_users = []
|
2013-06-04 13:13:01 -05:00
|
|
|
end
|
|
|
|
|
2015-03-26 15:57:50 -05:00
|
|
|
def valid?
|
|
|
|
topic = Topic.new(setup_topic_params)
|
2015-08-11 00:46:21 -05:00
|
|
|
# validate? will clear the error hash
|
|
|
|
# so we fire the validation event after
|
|
|
|
# this allows us to add errors
|
|
|
|
valid = topic.valid?
|
2016-04-25 14:55:15 -05:00
|
|
|
|
2015-08-11 00:46:21 -05:00
|
|
|
DiscourseEvent.trigger(:after_validate_topic, topic, self)
|
|
|
|
valid &&= topic.errors.empty?
|
|
|
|
|
|
|
|
add_errors_from(topic) unless valid
|
|
|
|
|
|
|
|
valid
|
2015-03-26 15:57:50 -05:00
|
|
|
end
|
|
|
|
|
2013-06-04 13:13:01 -05:00
|
|
|
def create
|
2015-03-26 15:57:50 -05:00
|
|
|
topic = Topic.new(setup_topic_params)
|
2016-05-04 13:02:47 -05:00
|
|
|
setup_tags(topic)
|
|
|
|
|
2015-08-11 00:46:21 -05:00
|
|
|
DiscourseEvent.trigger(:before_create_topic, topic, self)
|
2013-06-04 13:13:01 -05:00
|
|
|
|
2015-03-26 15:57:50 -05:00
|
|
|
setup_auto_close_time(topic)
|
|
|
|
process_private_message(topic)
|
|
|
|
save_topic(topic)
|
|
|
|
create_warning(topic)
|
|
|
|
watch_topic(topic)
|
2018-03-13 14:59:12 -05:00
|
|
|
create_shared_draft(topic)
|
2013-07-21 20:40:39 -05:00
|
|
|
|
2015-03-26 15:57:50 -05:00
|
|
|
topic
|
2013-06-04 13:13:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-03-13 14:59:12 -05:00
|
|
|
def create_shared_draft(topic)
|
|
|
|
return unless @opts[:shared_draft] && @opts[:category].present?
|
|
|
|
SharedDraft.create(topic_id: topic.id, category_id: @opts[:category])
|
|
|
|
end
|
|
|
|
|
2015-03-26 15:57:50 -05:00
|
|
|
def create_warning(topic)
|
2014-09-08 10:11:56 -05:00
|
|
|
return unless @opts[:is_warning]
|
|
|
|
|
|
|
|
# We can only attach warnings to PMs
|
2015-03-26 15:57:50 -05:00
|
|
|
rollback_with!(topic, :warning_requires_pm) unless topic.private_message?
|
2014-09-08 10:11:56 -05:00
|
|
|
|
|
|
|
# Don't create it if there is more than one user
|
2015-03-26 15:57:50 -05:00
|
|
|
rollback_with!(topic, :too_many_users) if @added_users.size != 1
|
2014-09-08 10:11:56 -05:00
|
|
|
|
|
|
|
# Create a warning record
|
2017-04-14 23:11:02 -05:00
|
|
|
UserWarning.create(topic: topic, user: @added_users.first, created_by: @user)
|
2014-09-08 10:11:56 -05:00
|
|
|
end
|
|
|
|
|
2015-03-26 15:57:50 -05:00
|
|
|
def watch_topic(topic)
|
2013-07-21 20:40:39 -05:00
|
|
|
unless @opts[:auto_track] == false
|
2015-03-26 15:57:50 -05:00
|
|
|
topic.notifier.watch_topic!(topic.user_id)
|
2013-07-21 20:40:39 -05:00
|
|
|
end
|
2014-01-22 00:46:52 -06:00
|
|
|
|
2017-08-30 23:06:56 -05:00
|
|
|
topic.reload.topic_allowed_users.each do |tau|
|
2015-12-14 16:17:09 -06:00
|
|
|
next if tau.user_id == -1 || tau.user_id == topic.user_id
|
|
|
|
topic.notifier.watch!(tau.user_id)
|
|
|
|
end
|
2014-06-24 11:31:36 -05:00
|
|
|
|
2017-08-30 23:06:56 -05:00
|
|
|
topic.reload.topic_allowed_groups.each do |tag|
|
2015-12-14 16:17:09 -06:00
|
|
|
tag.group.group_users.each do |gu|
|
|
|
|
next if gu.user_id == -1 || gu.user_id == topic.user_id
|
2017-07-27 20:20:09 -05:00
|
|
|
|
|
|
|
action =
|
|
|
|
case gu.notification_level
|
|
|
|
when TopicUser.notification_levels[:tracking] then "track!"
|
|
|
|
when TopicUser.notification_levels[:regular] then "regular!"
|
|
|
|
when TopicUser.notification_levels[:muted] then "mute!"
|
|
|
|
when TopicUser.notification_levels[:watching] then "watch!"
|
|
|
|
else "track!"
|
|
|
|
end
|
|
|
|
|
2015-12-14 16:17:09 -06:00
|
|
|
topic.notifier.send(action, gu.user_id)
|
|
|
|
end
|
2014-01-22 00:46:52 -06:00
|
|
|
end
|
2013-07-21 20:40:39 -05:00
|
|
|
end
|
|
|
|
|
2014-02-06 13:52:50 -06:00
|
|
|
def setup_topic_params
|
2016-07-27 04:50:13 -05:00
|
|
|
@opts[:visible] = true if @opts[:visible].nil?
|
|
|
|
|
2014-01-24 05:57:48 -06:00
|
|
|
topic_params = {
|
|
|
|
title: @opts[:title],
|
|
|
|
user_id: @user.id,
|
2016-07-27 04:50:13 -05:00
|
|
|
last_post_user_id: @user.id,
|
|
|
|
visible: @opts[:visible]
|
2014-01-24 05:57:48 -06:00
|
|
|
}
|
|
|
|
|
2014-07-03 13:43:24 -05:00
|
|
|
[:subtype, :archetype, :meta_data, :import_mode].each do |key|
|
2014-02-06 13:52:50 -06:00
|
|
|
topic_params[key] = @opts[key] if @opts[key].present?
|
2014-01-24 05:57:48 -06:00
|
|
|
end
|
|
|
|
|
2015-07-16 01:11:20 -05:00
|
|
|
if topic_params[:import_mode] && @opts[:views].to_i > 0
|
|
|
|
topic_params[:views] = @opts[:views].to_i
|
|
|
|
end
|
|
|
|
|
2014-09-08 12:23:40 -05:00
|
|
|
# Automatically give it a moderator warning subtype if specified
|
|
|
|
topic_params[:subtype] = TopicSubtype.moderator_warning if @opts[:is_warning]
|
|
|
|
|
2014-02-06 13:52:50 -06:00
|
|
|
category = find_category
|
2014-01-24 05:57:48 -06:00
|
|
|
|
2015-10-17 02:48:56 -05:00
|
|
|
@guardian.ensure_can_create!(Topic, category) unless (@opts[:skip_validations] || @opts[:archetype] == Archetype.private_message)
|
2014-01-24 05:57:48 -06:00
|
|
|
|
2013-06-04 13:13:01 -05:00
|
|
|
topic_params[:category_id] = category.id if category.present?
|
2014-02-06 13:52:50 -06:00
|
|
|
|
2013-06-04 13:13:01 -05:00
|
|
|
topic_params[:created_at] = Time.zone.parse(@opts[:created_at].to_s) if @opts[:created_at].present?
|
2014-01-24 05:57:48 -06:00
|
|
|
|
2015-01-19 08:00:55 -06:00
|
|
|
topic_params[:pinned_at] = Time.zone.parse(@opts[:pinned_at].to_s) if @opts[:pinned_at].present?
|
2015-03-13 15:24:11 -05:00
|
|
|
topic_params[:pinned_globally] = @opts[:pinned_globally] if @opts[:pinned_globally].present?
|
2015-01-19 08:00:55 -06:00
|
|
|
|
2016-12-05 06:31:43 -06:00
|
|
|
if SiteSetting.topic_featured_link_enabled && @opts[:featured_link].present? && @guardian.can_edit_featured_link?(topic_params[:category_id])
|
|
|
|
topic_params[:featured_link] = @opts[:featured_link]
|
|
|
|
end
|
|
|
|
|
2013-06-04 13:13:01 -05:00
|
|
|
topic_params
|
|
|
|
end
|
|
|
|
|
2014-02-06 13:52:50 -06:00
|
|
|
def find_category
|
|
|
|
# PM can't have a category
|
|
|
|
@opts.delete(:category) if @opts[:archetype].present? && @opts[:archetype] == Archetype.private_message
|
|
|
|
|
2018-03-13 14:59:12 -05:00
|
|
|
if @opts[:shared_draft]
|
|
|
|
return Category.find(SiteSetting.shared_drafts_category)
|
|
|
|
end
|
|
|
|
|
2014-02-06 13:52:50 -06: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.
|
|
|
|
if (@opts[:category].is_a? Integer) || (@opts[:category] =~ /^\d+$/)
|
2014-05-06 08:41:59 -05:00
|
|
|
Category.find_by(id: @opts[:category])
|
2014-02-06 13:52:50 -06:00
|
|
|
else
|
2014-08-18 10:07:32 -05:00
|
|
|
Category.find_by(name_lower: @opts[:category].try(:downcase))
|
2014-02-06 13:52:50 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-04 13:02:47 -05:00
|
|
|
def setup_tags(topic)
|
2018-03-28 13:40:26 -05:00
|
|
|
if @opts[:tags].blank?
|
2018-04-06 12:45:33 -05:00
|
|
|
unless @guardian.is_staff? || !guardian.can_tag?(topic)
|
2018-03-28 13:40:26 -05:00
|
|
|
# Validate minimum required tags for a category
|
|
|
|
category = find_category
|
|
|
|
if category.present? && category.minimum_required_tags > 0
|
|
|
|
topic.errors[:base] << I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags)
|
|
|
|
rollback_from_errors!(topic)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
valid_tags = DiscourseTagging.tag_topic_by_names(topic, @guardian, @opts[:tags])
|
|
|
|
rollback_from_errors!(topic) unless valid_tags
|
|
|
|
end
|
2016-05-04 13:02:47 -05:00
|
|
|
end
|
|
|
|
|
2015-03-26 15:57:50 -05:00
|
|
|
def setup_auto_close_time(topic)
|
2014-02-06 13:52:50 -06:00
|
|
|
return unless @opts[:auto_close_time].present?
|
2015-03-26 15:57:50 -05:00
|
|
|
return unless @guardian.can_moderate?(topic)
|
2017-07-27 20:20:09 -05:00
|
|
|
topic.set_auto_close(@opts[:auto_close_time], by_user: @user)
|
2013-06-04 13:13:01 -05:00
|
|
|
end
|
|
|
|
|
2015-03-26 15:57:50 -05:00
|
|
|
def process_private_message(topic)
|
2014-02-06 13:52:50 -06:00
|
|
|
return unless @opts[:archetype] == Archetype.private_message
|
2015-03-26 15:57:50 -05:00
|
|
|
topic.subtype = TopicSubtype.user_to_user unless topic.subtype
|
2013-06-04 13:13:01 -05:00
|
|
|
|
2017-08-28 11:07:30 -05:00
|
|
|
unless @opts[:target_usernames].present? || @opts[:target_emails].present? || @opts[:target_group_names].present?
|
2015-03-26 15:57:50 -05:00
|
|
|
rollback_with!(topic, :no_user_selected)
|
2013-06-04 13:13:01 -05:00
|
|
|
end
|
|
|
|
|
2017-08-28 11:07:30 -05:00
|
|
|
if @opts[:target_emails].present? && !@guardian.cand_send_private_messages_to_email? then
|
|
|
|
rollback_with!(topic, :reply_by_email_disabled)
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
add_users(topic, @opts[:target_usernames])
|
2017-08-28 11:07:30 -05:00
|
|
|
add_emails(topic, @opts[:target_emails])
|
2017-07-27 20:20:09 -05:00
|
|
|
add_groups(topic, @opts[:target_group_names])
|
2015-03-26 15:57:50 -05:00
|
|
|
topic.topic_allowed_users.build(user_id: @user.id)
|
2013-06-04 13:13:01 -05:00
|
|
|
end
|
|
|
|
|
2015-03-26 15:57:50 -05:00
|
|
|
def save_topic(topic)
|
2015-04-21 15:48:39 -05:00
|
|
|
topic.disable_rate_limits! if @opts[:skip_validations]
|
|
|
|
|
2015-03-26 15:57:50 -05:00
|
|
|
unless topic.save(validate: !@opts[:skip_validations])
|
|
|
|
rollback_from_errors!(topic)
|
2013-06-04 13:13:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_users(topic, usernames)
|
|
|
|
return unless usernames
|
2015-12-01 22:49:43 -06:00
|
|
|
|
|
|
|
names = usernames.split(',').flatten
|
|
|
|
len = 0
|
|
|
|
|
2017-10-06 02:56:58 -05:00
|
|
|
User.includes(:user_option).where(username: names).find_each do |user|
|
2014-09-08 10:11:56 -05:00
|
|
|
check_can_send_permission!(topic, user)
|
|
|
|
@added_users << user
|
2013-06-04 13:13:01 -05:00
|
|
|
topic.topic_allowed_users.build(user_id: user.id)
|
2015-12-01 22:49:43 -06:00
|
|
|
len += 1
|
2013-06-04 13:13:01 -05:00
|
|
|
end
|
2015-12-01 22:49:43 -06:00
|
|
|
|
|
|
|
rollback_with!(topic, :target_user_not_found) unless len == names.length
|
2013-06-04 13:13:01 -05:00
|
|
|
end
|
|
|
|
|
2017-08-28 11:07:30 -05:00
|
|
|
def add_emails(topic, emails)
|
|
|
|
return unless emails
|
|
|
|
|
2017-09-27 23:25:42 -05:00
|
|
|
begin
|
|
|
|
emails = emails.split(',').flatten
|
|
|
|
len = 0
|
|
|
|
|
|
|
|
emails.each do |email|
|
|
|
|
display_name = email.split("@").first
|
|
|
|
|
|
|
|
if user = find_or_create_user(email, display_name)
|
|
|
|
@added_users << user
|
|
|
|
topic.topic_allowed_users.build(user_id: user.id)
|
|
|
|
len += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
rollback_with!(topic, :target_user_not_found) unless len == emails.length
|
2017-08-28 11:07:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-04 13:13:01 -05:00
|
|
|
def add_groups(topic, groups)
|
|
|
|
return unless groups
|
2015-12-07 10:01:08 -06:00
|
|
|
names = groups.split(',').flatten
|
2015-12-01 22:49:43 -06:00
|
|
|
len = 0
|
|
|
|
|
|
|
|
Group.where(name: names).each do |group|
|
2015-03-26 15:57:50 -05:00
|
|
|
check_can_send_permission!(topic, group)
|
2013-06-04 13:13:01 -05:00
|
|
|
topic.topic_allowed_groups.build(group_id: group.id)
|
2015-12-01 22:49:43 -06:00
|
|
|
len += 1
|
2015-12-20 00:21:31 -06:00
|
|
|
group.update_columns(has_messages: true) unless group.has_messages
|
2013-06-04 13:13:01 -05:00
|
|
|
end
|
2015-12-01 22:49:43 -06:00
|
|
|
|
|
|
|
rollback_with!(topic, :target_group_not_found) unless len == names.length
|
2013-06-04 13:13:01 -05:00
|
|
|
end
|
|
|
|
|
2015-03-26 15:57:50 -05:00
|
|
|
def check_can_send_permission!(topic, obj)
|
2016-01-18 17:57:55 -06:00
|
|
|
rollback_with!(topic, :cant_send_pm) unless @opts[:skip_validations] || @guardian.can_send_private_message?(obj)
|
2013-06-04 13:13:01 -05:00
|
|
|
end
|
2017-08-28 11:07:30 -05:00
|
|
|
|
|
|
|
def find_or_create_user(email, display_name)
|
|
|
|
user = User.find_by_email(email)
|
|
|
|
|
2017-09-27 23:25:42 -05:00
|
|
|
if !user && SiteSetting.enable_staged_users
|
2017-08-28 11:07:30 -05:00
|
|
|
username = UserNameSuggester.sanitize_username(display_name) if display_name.present?
|
2017-09-27 23:25:42 -05:00
|
|
|
|
2017-08-28 11:07:30 -05:00
|
|
|
user = User.create!(
|
|
|
|
email: email,
|
|
|
|
username: UserNameSuggester.suggest(username.presence || email),
|
|
|
|
name: display_name.presence || User.suggest_name(email),
|
|
|
|
staged: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2013-06-04 13:13:01 -05:00
|
|
|
end
|