Revert "Revert "FEATURE: Can create warnings for users via PM""

This reverts commit 1c7559380c145136726028ae9f2aeea6f351eb78.
This commit is contained in:
Robin Ward
2014-09-08 11:11:56 -04:00
parent 1c7559380c
commit 334e21a03a
37 changed files with 243 additions and 18 deletions

View File

@ -28,6 +28,7 @@ class PostCreator
# When creating a topic:
# title - New topic title
# archetype - Topic archetype
# is_warning - Is the topic a warning?
# category - Category to assign to topic
# target_usernames - comma delimited list of usernames for membership (private message)
# target_group_names - comma delimited list of groups for membership (private message)

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

View File

@ -165,7 +165,7 @@ class TopicQuery
options.reverse_merge!(per_page: SiteSetting.topics_per_page)
# Start with a list of all topics
result = Topic.includes(:allowed_users)
result = Topic.includes(:allowed_users).includes(:warning)
.where("topics.id IN (SELECT topic_id FROM topic_allowed_users WHERE user_id = #{user.id.to_i})")
.joins("LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = #{user.id.to_i})")
.order(TopicQuerySQL.order_nocategory_basic_bumped)