FEATURE: Can create warnings for users via PM

This commit is contained in:
Robin Ward
2014-09-08 10:27:06 -04:00
parent a8e4c4caa1
commit b0bfc1f93f
37 changed files with 243 additions and 18 deletions
+25 -1
View File
@@ -10,6 +10,7 @@ class TopicCreator
@user = user
@guardian = guardian
@opts = opts
@added_users = []
end
def create
@@ -18,6 +19,7 @@ class TopicCreator
setup_auto_close_time
process_private_message
save_topic
create_warning
watch_topic
@topic
@@ -25,6 +27,27 @@ class TopicCreator
private
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
def watch_topic
unless @opts[:auto_track] == false
@topic.notifier.watch_topic!(@topic.user_id)
@@ -108,7 +131,8 @@ class TopicCreator
def add_users(topic, usernames)
return unless usernames
User.where(username: usernames.split(',')).each do |user|
check_can_send_permission!(topic,user)
check_can_send_permission!(topic, user)
@added_users << user
topic.topic_allowed_users.build(user_id: user.id)
end
end