Topic can have null user_id when user was nuked

This commit is contained in:
Neil Lalonde 2013-09-04 15:35:10 -04:00
parent 117fc8db58
commit d76486a48b
4 changed files with 37 additions and 5 deletions

View File

@ -196,10 +196,13 @@ ORDER BY p.created_at desc
group_ids = topic.category.groups.pluck("groups.id") group_ids = topic.category.groups.pluck("groups.id")
end end
MessageBus.publish("/users/#{action.user.username.downcase}", if action.user
action.id, MessageBus.publish("/users/#{action.user.username.downcase}",
user_ids: [user_id], action.id,
group_ids: group_ids ) user_ids: [user_id],
group_ids: group_ids )
end
action action
rescue ActiveRecord::RecordNotUnique rescue ActiveRecord::RecordNotUnique

View File

@ -0,0 +1,9 @@
class AllowNullUserIdOnTopics < ActiveRecord::Migration
def up
change_column :topics, :user_id, :integer, null: true
end
def down
change_column :topics, :user_id, :integer, null: false
end
end

View File

@ -25,6 +25,9 @@ class UserDestroyer
end end
end end
PostDestroyer.new(@staff, post).destroy PostDestroyer.new(@staff, post).destroy
if post.topic and post.post_number == 1
Topic.unscoped.where(id: post.topic.id).update_all(user_id: nil)
end
end end
raise PostsExistError if user.reload.post_count != 0 raise PostsExistError if user.reload.post_count != 0
end end

View File

@ -87,7 +87,10 @@ describe UserDestroyer do
end end
context 'user has posts' do context 'user has posts' do
let!(:post) { Fabricate(:post, user: @user) } let!(:topic_starter) { Fabricate(:user) }
let!(:topic) { Fabricate(:topic, user: topic_starter) }
let!(:first_post) { Fabricate(:post, user: topic_starter, topic: topic) }
let!(:post) { Fabricate(:post, user: @user, topic: topic) }
context "delete_posts is false" do context "delete_posts is false" do
subject(:destroy) { UserDestroyer.new(@admin).destroy(@user) } subject(:destroy) { UserDestroyer.new(@admin).destroy(@user) }
@ -123,6 +126,20 @@ describe UserDestroyer do
post.reload.deleted_at.should_not be_nil post.reload.deleted_at.should_not be_nil
post.user_id.should be_nil post.user_id.should be_nil
end end
it "does not delete topics started by others in which the user has replies" do
destroy
topic.reload.deleted_at.should be_nil
topic.user_id.should_not be_nil
end
it "deletes topics started by the deleted user" do
spammer_topic = Fabricate(:topic, user: @user)
spammer_post = Fabricate(:post, user: @user, topic: spammer_topic)
destroy
spammer_topic.reload.deleted_at.should_not be_nil
spammer_topic.user_id.should be_nil
end
end end
end end