2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-03-20 02:15:08 +08:00
|
|
|
require 'post_destroyer'
|
|
|
|
|
|
|
|
describe PostDestroyer do
|
|
|
|
|
2013-05-14 09:59:55 +08:00
|
|
|
before do
|
2016-12-22 12:03:40 +08:00
|
|
|
UserActionCreator.enable
|
2013-05-14 09:59:55 +08:00
|
|
|
end
|
|
|
|
|
2013-03-20 02:15:08 +08:00
|
|
|
let(:moderator) { Fabricate(:moderator) }
|
2013-07-23 23:50:58 +08:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
2013-07-22 13:06:53 +08:00
|
|
|
let(:post) { create_post }
|
2013-03-20 02:15:08 +08:00
|
|
|
|
2014-09-26 01:51:00 +08:00
|
|
|
describe "destroy_old_hidden_posts" do
|
|
|
|
|
|
|
|
it "destroys posts that have been hidden for 30 days" do
|
|
|
|
Fabricate(:admin)
|
|
|
|
|
|
|
|
now = Time.now
|
|
|
|
|
|
|
|
freeze_time(now - 60.days)
|
|
|
|
topic = post.topic
|
|
|
|
reply1 = create_post(topic: topic)
|
|
|
|
|
|
|
|
freeze_time(now - 40.days)
|
|
|
|
reply2 = create_post(topic: topic)
|
|
|
|
PostAction.hide_post!(reply2, PostActionType.types[:off_topic])
|
|
|
|
|
|
|
|
freeze_time(now - 20.days)
|
|
|
|
reply3 = create_post(topic: topic)
|
|
|
|
PostAction.hide_post!(reply3, PostActionType.types[:off_topic])
|
|
|
|
|
|
|
|
freeze_time(now - 10.days)
|
|
|
|
reply4 = create_post(topic: topic)
|
|
|
|
|
|
|
|
freeze_time(now)
|
|
|
|
PostDestroyer.destroy_old_hidden_posts
|
|
|
|
|
|
|
|
reply1.reload
|
|
|
|
reply2.reload
|
|
|
|
reply3.reload
|
|
|
|
reply4.reload
|
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(reply1.deleted_at).to eq(nil)
|
|
|
|
expect(reply2.deleted_at).not_to eq(nil)
|
|
|
|
expect(reply3.deleted_at).to eq(nil)
|
|
|
|
expect(reply4.deleted_at).to eq(nil)
|
2014-09-26 01:51:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-07-22 15:48:24 +08:00
|
|
|
describe 'destroy_old_stubs' do
|
|
|
|
it 'destroys stubs for deleted by user posts' do
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.delete_removed_posts_after = 24
|
2013-07-22 15:48:24 +08:00
|
|
|
Fabricate(:admin)
|
2013-07-23 14:11:44 +08:00
|
|
|
topic = post.topic
|
|
|
|
reply1 = create_post(topic: topic)
|
|
|
|
reply2 = create_post(topic: topic)
|
|
|
|
reply3 = create_post(topic: topic)
|
2013-07-22 15:48:24 +08:00
|
|
|
|
|
|
|
PostDestroyer.new(reply1.user, reply1).destroy
|
|
|
|
PostDestroyer.new(reply2.user, reply2).destroy
|
|
|
|
|
|
|
|
reply2.update_column(:updated_at, 2.days.ago)
|
|
|
|
|
|
|
|
PostDestroyer.destroy_stubs
|
|
|
|
|
|
|
|
reply1.reload
|
|
|
|
reply2.reload
|
|
|
|
reply3.reload
|
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(reply1.deleted_at).to eq(nil)
|
|
|
|
expect(reply2.deleted_at).not_to eq(nil)
|
|
|
|
expect(reply3.deleted_at).to eq(nil)
|
2013-07-22 15:48:24 +08:00
|
|
|
|
2013-07-23 14:11:44 +08:00
|
|
|
# if topic is deleted we should still be able to destroy stubs
|
|
|
|
|
|
|
|
topic.trash!
|
|
|
|
reply1.update_column(:updated_at, 2.days.ago)
|
|
|
|
PostDestroyer.destroy_stubs
|
|
|
|
|
|
|
|
reply1.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(reply1.deleted_at).to eq(nil)
|
2013-07-23 14:11:44 +08:00
|
|
|
|
|
|
|
# flag the post, it should not nuke the stub anymore
|
|
|
|
topic.recover!
|
|
|
|
PostAction.act(Fabricate(:coding_horror), reply1, PostActionType.types[:spam])
|
|
|
|
|
|
|
|
PostDestroyer.destroy_stubs
|
|
|
|
|
|
|
|
reply1.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(reply1.deleted_at).to eq(nil)
|
2013-07-23 14:11:44 +08:00
|
|
|
|
2018-08-10 02:54:23 +08:00
|
|
|
# defer the flag, we should be able to delete the stub
|
|
|
|
PostAction.defer_flags!(reply1, Discourse.system_user)
|
|
|
|
PostDestroyer.destroy_stubs
|
|
|
|
|
|
|
|
reply1.reload
|
|
|
|
expect(reply1.deleted_at).to_not eq(nil)
|
2013-07-22 15:48:24 +08:00
|
|
|
end
|
2013-08-03 01:35:42 +08:00
|
|
|
|
|
|
|
it 'uses the delete_removed_posts_after site setting' do
|
|
|
|
Fabricate(:admin)
|
|
|
|
topic = post.topic
|
|
|
|
reply1 = create_post(topic: topic)
|
|
|
|
reply2 = create_post(topic: topic)
|
|
|
|
|
|
|
|
PostDestroyer.new(reply1.user, reply1).destroy
|
|
|
|
PostDestroyer.new(reply2.user, reply2).destroy
|
|
|
|
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.delete_removed_posts_after = 1
|
2013-08-03 01:35:42 +08:00
|
|
|
|
|
|
|
reply2.update_column(:updated_at, 70.minutes.ago)
|
|
|
|
|
|
|
|
PostDestroyer.destroy_stubs
|
|
|
|
|
|
|
|
reply1.reload
|
|
|
|
reply2.reload
|
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(reply1.deleted_at).to eq(nil)
|
|
|
|
expect(reply2.deleted_at).not_to eq(nil)
|
2013-08-03 01:35:42 +08:00
|
|
|
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.delete_removed_posts_after = 72
|
2013-08-03 01:35:42 +08:00
|
|
|
|
|
|
|
reply1.update_column(:updated_at, 2.days.ago)
|
|
|
|
|
|
|
|
PostDestroyer.destroy_stubs
|
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(reply1.reload.deleted_at).to eq(nil)
|
2013-08-03 01:35:42 +08:00
|
|
|
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.delete_removed_posts_after = 47
|
2013-08-03 01:35:42 +08:00
|
|
|
|
|
|
|
PostDestroyer.destroy_stubs
|
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(reply1.reload.deleted_at).not_to eq(nil)
|
2013-08-03 01:35:42 +08:00
|
|
|
end
|
2014-10-07 04:29:20 +08:00
|
|
|
|
|
|
|
it "deletes posts immediately if delete_removed_posts_after is 0" do
|
|
|
|
Fabricate(:admin)
|
|
|
|
topic = post.topic
|
|
|
|
reply1 = create_post(topic: topic)
|
|
|
|
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.delete_removed_posts_after = 0
|
2014-10-07 04:29:20 +08:00
|
|
|
|
|
|
|
PostDestroyer.new(reply1.user, reply1).destroy
|
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(reply1.reload.deleted_at).not_to eq(nil)
|
2014-10-07 04:29:20 +08:00
|
|
|
end
|
2013-07-22 15:48:24 +08:00
|
|
|
end
|
|
|
|
|
2015-09-19 00:48:43 +08:00
|
|
|
describe "recovery and user actions" do
|
|
|
|
it "recreates user actions" do
|
|
|
|
reply = create_post(topic: post.topic)
|
|
|
|
author = reply.user
|
|
|
|
|
|
|
|
post_action = author.user_actions.where(action_type: UserAction::REPLY, target_post_id: reply.id).first
|
|
|
|
expect(post_action).to be_present
|
|
|
|
|
|
|
|
PostDestroyer.new(moderator, reply).destroy
|
|
|
|
|
|
|
|
# User Action is removed
|
|
|
|
post_action = author.user_actions.where(action_type: UserAction::REPLY, target_post_id: reply.id).first
|
|
|
|
expect(post_action).to be_blank
|
|
|
|
|
|
|
|
PostDestroyer.new(moderator, reply).recover
|
|
|
|
|
|
|
|
# On recovery, the user action is recreated
|
|
|
|
post_action = author.user_actions.where(action_type: UserAction::REPLY, target_post_id: reply.id).first
|
|
|
|
expect(post_action).to be_present
|
|
|
|
end
|
2016-06-13 11:25:06 +08:00
|
|
|
|
|
|
|
describe "post_count recovery" do
|
|
|
|
before do
|
|
|
|
post
|
|
|
|
@user = post.user
|
2017-11-10 07:05:53 +08:00
|
|
|
@reply = create_post(topic: post.topic, user: @user)
|
2016-06-13 11:25:06 +08:00
|
|
|
expect(@user.user_stat.post_count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "recovered by user" do
|
|
|
|
it "should increment the user's post count" do
|
2017-11-10 07:05:53 +08:00
|
|
|
PostDestroyer.new(@user, @reply).destroy
|
|
|
|
expect(@user.user_stat.topic_count).to eq(1)
|
2016-06-13 11:25:06 +08:00
|
|
|
expect(@user.user_stat.post_count).to eq(1)
|
|
|
|
|
2017-11-10 07:05:53 +08:00
|
|
|
PostDestroyer.new(@user, @reply.reload).recover
|
|
|
|
expect(@user.user_stat.topic_count).to eq(1)
|
2016-06-13 11:25:06 +08:00
|
|
|
expect(@user.reload.user_stat.post_count).to eq(1)
|
2017-11-10 07:05:53 +08:00
|
|
|
|
|
|
|
expect(UserAction.where(target_topic_id: post.topic_id, action_type: UserAction::NEW_TOPIC).count).to eq(1)
|
|
|
|
expect(UserAction.where(target_topic_id: post.topic_id, action_type: UserAction::REPLY).count).to eq(1)
|
2016-06-13 11:25:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "recovered by admin" do
|
|
|
|
it "should increment the user's post count" do
|
2017-11-10 07:05:53 +08:00
|
|
|
PostDestroyer.new(moderator, @reply).destroy
|
|
|
|
expect(@user.reload.user_stat.topic_count).to eq(1)
|
|
|
|
expect(@user.user_stat.post_count).to eq(0)
|
|
|
|
|
|
|
|
PostDestroyer.new(admin, @reply).recover
|
|
|
|
expect(@user.reload.user_stat.topic_count).to eq(1)
|
|
|
|
expect(@user.user_stat.post_count).to eq(1)
|
|
|
|
|
2016-06-13 11:25:06 +08:00
|
|
|
PostDestroyer.new(moderator, post).destroy
|
2017-11-10 07:05:53 +08:00
|
|
|
expect(@user.reload.user_stat.topic_count).to eq(0)
|
2016-06-13 11:25:06 +08:00
|
|
|
expect(@user.user_stat.post_count).to eq(0)
|
|
|
|
|
|
|
|
PostDestroyer.new(admin, post).recover
|
2017-11-10 07:05:53 +08:00
|
|
|
expect(@user.reload.user_stat.topic_count).to eq(1)
|
|
|
|
expect(@user.user_stat.post_count).to eq(1)
|
|
|
|
|
|
|
|
expect(UserAction.where(target_topic_id: post.topic_id, action_type: UserAction::NEW_TOPIC).count).to eq(1)
|
|
|
|
expect(UserAction.where(target_topic_id: post.topic_id, action_type: UserAction::REPLY).count).to eq(1)
|
2016-06-13 11:25:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-09-19 00:48:43 +08:00
|
|
|
end
|
|
|
|
|
2018-10-02 23:25:08 +08:00
|
|
|
describe "recovery and post actions" do
|
|
|
|
let(:codinghorror) { Fabricate(:coding_horror) }
|
|
|
|
let!(:like) { PostAction.act(codinghorror, post, PostActionType.types[:like]) }
|
|
|
|
let!(:another_like) { PostAction.act(moderator, post, PostActionType.types[:like]) }
|
|
|
|
|
|
|
|
it "restores public post actions" do
|
|
|
|
PostDestroyer.new(moderator, post).destroy
|
|
|
|
expect(PostAction.exists?(id: like.id)).to eq(false)
|
|
|
|
|
|
|
|
PostDestroyer.new(moderator, post).recover
|
|
|
|
expect(PostAction.exists?(id: like.id)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not recover previously-deleted actions" do
|
|
|
|
PostAction.remove_act(codinghorror, post, PostActionType.types[:like])
|
|
|
|
expect(PostAction.exists?(id: like.id)).to eq(false)
|
|
|
|
|
|
|
|
PostDestroyer.new(moderator, post).destroy
|
|
|
|
PostDestroyer.new(moderator, post).recover
|
|
|
|
expect(PostAction.exists?(id: another_like.id)).to eq(true)
|
|
|
|
expect(PostAction.exists?(id: like.id)).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates post like count" do
|
|
|
|
PostDestroyer.new(moderator, post).destroy
|
|
|
|
PostDestroyer.new(moderator, post).recover
|
|
|
|
post.reload
|
|
|
|
expect(post.like_count).to eq(2)
|
|
|
|
expect(post.custom_fields["deleted_public_actions"]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-20 02:15:08 +08:00
|
|
|
describe 'basic destroying' do
|
2013-09-06 23:50:05 +08:00
|
|
|
it "as the creator of the post, doesn't delete the post" do
|
2016-09-05 15:58:04 +08:00
|
|
|
begin
|
|
|
|
post2 = create_post
|
2017-11-03 04:43:09 +08:00
|
|
|
user_stat = post2.user.user_stat
|
2016-09-05 15:58:04 +08:00
|
|
|
|
|
|
|
called = 0
|
|
|
|
topic_destroyed = -> (topic, user) do
|
|
|
|
expect(topic).to eq(post2.topic)
|
|
|
|
expect(user).to eq(post2.user)
|
|
|
|
called += 1
|
|
|
|
end
|
2013-09-06 23:50:05 +08:00
|
|
|
|
2016-09-05 15:58:04 +08:00
|
|
|
DiscourseEvent.on(:topic_destroyed, &topic_destroyed)
|
2013-09-06 23:50:05 +08:00
|
|
|
|
2016-09-05 15:58:04 +08:00
|
|
|
@orig = post2.cooked
|
2017-11-03 03:33:40 +08:00
|
|
|
# Guardian.new(post2.user).can_delete_post?(post2) == false
|
2016-09-05 15:58:04 +08:00
|
|
|
PostDestroyer.new(post2.user, post2).destroy
|
|
|
|
post2.reload
|
2013-09-06 23:50:05 +08:00
|
|
|
|
2016-09-05 15:58:04 +08:00
|
|
|
expect(post2.deleted_at).to be_blank
|
|
|
|
expect(post2.deleted_by).to be_blank
|
|
|
|
expect(post2.user_deleted).to eq(true)
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(post2.raw).to eq(I18n.t('js.post.deleted_by_author', count: 24))
|
2016-09-05 15:58:04 +08:00
|
|
|
expect(post2.version).to eq(2)
|
|
|
|
expect(called).to eq(1)
|
2017-11-10 07:05:53 +08:00
|
|
|
expect(user_stat.reload.post_count).to eq(0)
|
|
|
|
expect(user_stat.reload.topic_count).to eq(1)
|
2016-06-29 10:00:07 +08:00
|
|
|
|
2016-09-05 15:58:04 +08:00
|
|
|
called = 0
|
|
|
|
topic_recovered = -> (topic, user) do
|
|
|
|
expect(topic).to eq(post2.topic)
|
|
|
|
expect(user).to eq(post2.user)
|
|
|
|
called += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
DiscourseEvent.on(:topic_recovered, &topic_recovered)
|
|
|
|
|
|
|
|
# lets try to recover
|
|
|
|
PostDestroyer.new(post2.user, post2).recover
|
|
|
|
post2.reload
|
|
|
|
expect(post2.version).to eq(3)
|
|
|
|
expect(post2.user_deleted).to eq(false)
|
|
|
|
expect(post2.cooked).to eq(@orig)
|
|
|
|
expect(called).to eq(1)
|
2017-11-10 07:05:53 +08:00
|
|
|
expect(user_stat.reload.post_count).to eq(0)
|
|
|
|
expect(user_stat.reload.topic_count).to eq(1)
|
2016-09-05 15:58:04 +08:00
|
|
|
ensure
|
|
|
|
DiscourseEvent.off(:topic_destroyed, &topic_destroyed)
|
|
|
|
DiscourseEvent.off(:topic_recovered, &topic_recovered)
|
|
|
|
end
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|
|
|
|
|
2017-11-03 03:33:40 +08:00
|
|
|
it "when topic is destroyed, it updates user_stats correctly" do
|
2018-07-14 00:15:59 +08:00
|
|
|
SiteSetting.min_topic_title_length = 5
|
|
|
|
post.topic.update_column(:title, "xyz")
|
|
|
|
|
2017-11-03 03:33:40 +08:00
|
|
|
user1 = post.user
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
reply = create_post(topic_id: post.topic_id, user: user2)
|
|
|
|
reply2 = create_post(topic_id: post.topic_id, user: user1)
|
|
|
|
expect(user1.user_stat.topic_count).to eq(1)
|
2017-11-10 07:05:53 +08:00
|
|
|
expect(user1.user_stat.post_count).to eq(1)
|
2017-11-03 03:33:40 +08:00
|
|
|
expect(user2.user_stat.topic_count).to eq(0)
|
|
|
|
expect(user2.user_stat.post_count).to eq(1)
|
2018-07-14 00:15:59 +08:00
|
|
|
|
2017-11-03 03:33:40 +08:00
|
|
|
PostDestroyer.new(Fabricate(:admin), post).destroy
|
|
|
|
user1.reload
|
|
|
|
user2.reload
|
|
|
|
expect(user1.user_stat.topic_count).to eq(0)
|
|
|
|
expect(user1.user_stat.post_count).to eq(0)
|
|
|
|
expect(user2.user_stat.topic_count).to eq(0)
|
|
|
|
expect(user2.user_stat.post_count).to eq(0)
|
|
|
|
end
|
|
|
|
|
2017-06-16 11:27:51 +08:00
|
|
|
it "accepts a delete_removed_posts_after option" do
|
|
|
|
SiteSetting.delete_removed_posts_after = 0
|
|
|
|
|
|
|
|
PostDestroyer.new(post.user, post, delete_removed_posts_after: 1).destroy
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
expect(post.deleted_at).to eq(nil)
|
|
|
|
expect(post.user_deleted).to eq(true)
|
|
|
|
|
|
|
|
expect(post.raw).to eq(I18n.t('js.post.deleted_by_author', count: 1))
|
|
|
|
end
|
|
|
|
|
2013-03-20 02:15:08 +08:00
|
|
|
context "as a moderator" do
|
|
|
|
it "deletes the post" do
|
2015-09-19 00:48:43 +08:00
|
|
|
author = post.user
|
2017-11-10 07:05:53 +08:00
|
|
|
reply = create_post(topic_id: post.topic_id, user: author)
|
2015-09-19 00:48:43 +08:00
|
|
|
|
|
|
|
post_count = author.post_count
|
|
|
|
history_count = UserHistory.count
|
|
|
|
|
2017-11-10 07:05:53 +08:00
|
|
|
PostDestroyer.new(moderator, reply).destroy
|
2015-09-19 00:48:43 +08:00
|
|
|
|
2017-11-10 07:05:53 +08:00
|
|
|
expect(reply.deleted_at).to be_present
|
|
|
|
expect(reply.deleted_by).to eq(moderator)
|
2014-10-01 23:40:13 +08:00
|
|
|
|
2015-09-19 00:48:43 +08:00
|
|
|
author.reload
|
|
|
|
expect(author.post_count).to eq(post_count - 1)
|
|
|
|
expect(UserHistory.count).to eq(history_count + 1)
|
2014-10-01 23:40:13 +08:00
|
|
|
end
|
2014-08-15 03:21:10 +08:00
|
|
|
end
|
2013-05-03 15:56:23 +08:00
|
|
|
|
2014-08-15 03:21:10 +08:00
|
|
|
context "as an admin" do
|
2013-05-03 15:56:23 +08:00
|
|
|
it "deletes the post" do
|
2014-08-15 03:21:10 +08:00
|
|
|
PostDestroyer.new(admin, post).destroy
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.deleted_at).to be_present
|
|
|
|
expect(post.deleted_by).to eq(admin)
|
2013-05-03 15:56:23 +08:00
|
|
|
end
|
2014-08-15 03:21:10 +08:00
|
|
|
|
2017-11-10 07:05:53 +08:00
|
|
|
it "updates the user's topic_count for first post" do
|
2014-08-15 03:21:10 +08:00
|
|
|
author = post.user
|
|
|
|
expect {
|
|
|
|
PostDestroyer.new(admin, post).destroy
|
|
|
|
author.reload
|
2017-11-10 07:05:53 +08:00
|
|
|
}.to change { author.topic_count }.by(-1)
|
|
|
|
expect(author.user_stat.post_count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the user's post_count for reply" do
|
|
|
|
author = post.user
|
|
|
|
reply = create_post(topic: post.topic, user: author)
|
|
|
|
expect {
|
|
|
|
PostDestroyer.new(admin, reply).destroy
|
|
|
|
author.reload
|
2014-08-15 03:21:10 +08:00
|
|
|
}.to change { author.post_count }.by(-1)
|
2017-11-10 07:05:53 +08:00
|
|
|
expect(author.user_stat.topic_count).to eq(1)
|
2014-08-15 03:21:10 +08:00
|
|
|
end
|
2017-11-03 05:11:08 +08:00
|
|
|
|
|
|
|
it "doesn't count whispers" do
|
|
|
|
user_stat = admin.user_stat
|
|
|
|
whisper = PostCreator.new(
|
|
|
|
admin,
|
|
|
|
topic_id: post.topic.id,
|
|
|
|
reply_to_post_number: 1,
|
|
|
|
post_type: Post.types[:whisper],
|
|
|
|
raw: 'this is a whispered reply'
|
|
|
|
).create
|
|
|
|
expect(user_stat.reload.post_count).to eq(0)
|
|
|
|
expect {
|
|
|
|
PostDestroyer.new(admin, whisper).destroy
|
|
|
|
}.to_not change { user_stat.reload.post_count }
|
|
|
|
end
|
2013-05-03 15:56:23 +08:00
|
|
|
end
|
2013-03-20 02:15:08 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-07-04 10:50:51 +08:00
|
|
|
context 'private message' do
|
|
|
|
let(:author) { Fabricate(:user) }
|
|
|
|
let(:private_message) { Fabricate(:private_message_topic, user: author) }
|
|
|
|
let!(:first_post) { Fabricate(:post, topic: private_message, user: author) }
|
2018-07-04 21:42:31 +08:00
|
|
|
let!(:second_post) { Fabricate(:post, topic: private_message, user: author, post_number: 2) }
|
2018-07-04 10:50:51 +08:00
|
|
|
|
|
|
|
it "doesn't update post_count for a reply" do
|
|
|
|
expect {
|
|
|
|
PostDestroyer.new(admin, second_post).destroy
|
|
|
|
author.reload
|
|
|
|
}.to_not change { author.post_count }
|
|
|
|
|
|
|
|
expect {
|
|
|
|
PostDestroyer.new(admin, second_post).recover
|
|
|
|
}.to_not change { author.post_count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't update topic_count for first post" do
|
|
|
|
expect {
|
|
|
|
PostDestroyer.new(admin, first_post).destroy
|
|
|
|
author.reload
|
|
|
|
}.to_not change { author.topic_count }
|
|
|
|
expect(author.post_count).to eq(0) # also unchanged
|
|
|
|
end
|
2018-10-05 16:53:59 +08:00
|
|
|
|
|
|
|
it 'triggers the extensibility events' do
|
|
|
|
events = DiscourseEvent.track_events { PostDestroyer.new(admin, first_post).destroy }.last(2)
|
|
|
|
|
|
|
|
expect(events[0][:event_name]).to eq(:post_destroyed)
|
|
|
|
expect(events[0][:params].first).to eq(first_post)
|
|
|
|
|
|
|
|
expect(events[1][:event_name]).to eq(:topic_destroyed)
|
|
|
|
expect(events[1][:params].first).to eq(first_post.topic)
|
|
|
|
end
|
2018-07-04 10:50:51 +08:00
|
|
|
end
|
|
|
|
|
2013-03-20 02:15:08 +08:00
|
|
|
context 'deleting the second post in a topic' do
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
2013-07-22 13:06:53 +08:00
|
|
|
let!(:post) { create_post(user: user) }
|
2018-07-14 00:15:59 +08:00
|
|
|
let(:topic) { post.topic }
|
2013-03-20 02:15:08 +08:00
|
|
|
let(:second_user) { Fabricate(:coding_horror) }
|
2013-07-22 13:06:53 +08:00
|
|
|
let!(:second_post) { create_post(topic: topic, user: second_user) }
|
2013-03-20 02:15:08 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
PostDestroyer.new(moderator, second_post).destroy
|
2018-07-14 00:15:59 +08:00
|
|
|
topic.reload
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets the last_poster_id back to the OP' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic.last_post_user_id).to eq(user.id)
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets the last_posted_at back to the OP' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic.last_posted_at.to_i).to eq(post.created_at.to_i)
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|
|
|
|
|
2018-07-14 00:15:59 +08:00
|
|
|
it 'resets the highest_post_number' do
|
|
|
|
expect(topic.highest_post_number).to eq(post.post_number)
|
|
|
|
end
|
|
|
|
|
2013-03-20 02:15:08 +08:00
|
|
|
context 'topic_user' do
|
|
|
|
|
2014-05-06 21:41:59 +08:00
|
|
|
let(:topic_user) { second_user.topic_users.find_by(topic_id: topic.id) }
|
2013-03-20 02:15:08 +08:00
|
|
|
|
|
|
|
it 'clears the posted flag for the second user' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic_user.posted?).to eq(false)
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the second user's last_read_post_number back to 1" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic_user.last_read_post_number).to eq(1)
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the second user's last_read_post_number back to 1" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic_user.highest_seen_post_number).to eq(1)
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-23 23:50:58 +08:00
|
|
|
context "deleting a post belonging to a deleted topic" do
|
|
|
|
let!(:topic) { post.topic }
|
2017-11-03 04:43:09 +08:00
|
|
|
let(:author) { post.user }
|
2013-07-23 23:50:58 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
topic.trash!(admin)
|
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
context "as a moderator" do
|
|
|
|
before do
|
|
|
|
PostDestroyer.new(moderator, post).destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes the post" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.deleted_at).to be_present
|
|
|
|
expect(post.deleted_by).to eq(moderator)
|
2017-11-03 04:43:09 +08:00
|
|
|
expect(author.user_stat.post_count).to eq(0)
|
2013-07-23 23:50:58 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "as an admin" do
|
2017-11-03 04:43:09 +08:00
|
|
|
subject { PostDestroyer.new(admin, post).destroy }
|
2013-07-23 23:50:58 +08:00
|
|
|
|
|
|
|
it "deletes the post" do
|
2017-11-03 04:43:09 +08:00
|
|
|
subject
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.deleted_at).to be_present
|
|
|
|
expect(post.deleted_by).to eq(admin)
|
2013-07-23 23:50:58 +08:00
|
|
|
end
|
2014-10-01 23:40:13 +08:00
|
|
|
|
|
|
|
it "creates a new user history entry" do
|
2017-11-03 04:43:09 +08:00
|
|
|
expect { subject }.to change { UserHistory.count }.by(1)
|
|
|
|
end
|
2018-10-05 16:53:59 +08:00
|
|
|
|
|
|
|
it 'triggers a extensibility event' do
|
|
|
|
events = DiscourseEvent.track_events { subject }
|
|
|
|
|
|
|
|
expect(events[0][:event_name]).to eq(:post_destroyed)
|
|
|
|
expect(events[0][:params].first).to eq(post)
|
|
|
|
end
|
2017-11-03 04:43:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "deleting a reply belonging to a deleted topic" do
|
|
|
|
let!(:topic) { post.topic }
|
|
|
|
let!(:reply) { create_post(topic_id: topic.id, user: post.user) }
|
|
|
|
let(:author) { reply.user }
|
|
|
|
|
|
|
|
before do
|
|
|
|
topic.trash!(admin)
|
|
|
|
post.reload
|
|
|
|
reply.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
context "as a moderator" do
|
|
|
|
subject { PostDestroyer.new(moderator, reply).destroy }
|
|
|
|
|
|
|
|
it "deletes the reply" do
|
|
|
|
subject
|
|
|
|
expect(reply.deleted_at).to be_present
|
|
|
|
expect(reply.deleted_by).to eq(moderator)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't decrement post_count again" do
|
|
|
|
expect { subject }.to_not change { author.user_stat.post_count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "as an admin" do
|
|
|
|
subject { PostDestroyer.new(admin, reply).destroy }
|
|
|
|
|
|
|
|
it "deletes the post" do
|
|
|
|
subject
|
|
|
|
expect(reply.deleted_at).to be_present
|
|
|
|
expect(reply.deleted_by).to eq(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't decrement post_count again" do
|
|
|
|
expect { subject }.to_not change { author.user_stat.post_count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new user history entry" do
|
|
|
|
expect { subject }.to change { UserHistory.count }.by(1)
|
2014-10-01 23:40:13 +08:00
|
|
|
end
|
2013-07-23 23:50:58 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-22 22:08:37 +08:00
|
|
|
it "deletes a post belonging to a non-existent topic" do
|
|
|
|
DB.exec("DELETE FROM topics WHERE id = ?", post.topic_id)
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
PostDestroyer.new(admin, post).destroy
|
|
|
|
|
|
|
|
expect(post.deleted_at).to be_present
|
|
|
|
expect(post.deleted_by).to eq(admin)
|
|
|
|
end
|
|
|
|
|
2013-03-20 02:15:08 +08:00
|
|
|
describe 'after delete' do
|
|
|
|
|
|
|
|
let!(:coding_horror) { Fabricate(:coding_horror) }
|
|
|
|
let!(:post) { Fabricate(:post, raw: "Hello @CodingHorror") }
|
|
|
|
|
|
|
|
it "should feature the users again (in case they've changed)" do
|
2015-04-06 15:27:05 +08:00
|
|
|
Jobs.expects(:enqueue).with(:feature_topic_users, has_entries(topic_id: post.topic_id))
|
2013-03-20 02:15:08 +08:00
|
|
|
PostDestroyer.new(moderator, post).destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with a reply' do
|
|
|
|
|
|
|
|
let!(:reply) { Fabricate(:basic_reply, user: coding_horror, topic: post.topic) }
|
|
|
|
let!(:post_reply) { PostReply.create(post_id: post.id, reply_id: reply.id) }
|
|
|
|
|
|
|
|
it 'changes the post count of the topic' do
|
|
|
|
post.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
expect {
|
2013-03-20 02:15:08 +08:00
|
|
|
PostDestroyer.new(moderator, reply).destroy
|
|
|
|
post.topic.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
}.to change(post.topic, :posts_count).by(-1)
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'lowers the reply_count when the reply is deleted' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect {
|
2013-03-20 02:15:08 +08:00
|
|
|
PostDestroyer.new(moderator, reply).destroy
|
2015-01-10 00:34:37 +08:00
|
|
|
}.to change(post.post_replies, :count).by(-1)
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should increase the post_number when there are deletion gaps' do
|
|
|
|
PostDestroyer.new(moderator, reply).destroy
|
|
|
|
p = Fabricate(:post, user: post.user, topic: post.topic)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(p.post_number).to eq(3)
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context '@mentions' do
|
|
|
|
it 'removes notifications when deleted' do
|
2019-03-14 22:47:38 +08:00
|
|
|
Jobs.run_immediately!
|
2014-03-18 12:22:39 +08:00
|
|
|
user = Fabricate(:evil_trout)
|
|
|
|
post = create_post(raw: 'Hello @eviltrout')
|
2015-01-10 00:34:37 +08:00
|
|
|
expect {
|
2014-03-18 12:22:39 +08:00
|
|
|
PostDestroyer.new(Fabricate(:moderator), post).destroy
|
2015-01-10 00:34:37 +08:00
|
|
|
}.to change(user.notifications, :count).by(-1)
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-06 04:00:45 +08:00
|
|
|
describe "post actions" do
|
|
|
|
let(:second_post) { Fabricate(:post, topic_id: post.topic_id) }
|
2014-07-29 01:17:37 +08:00
|
|
|
let!(:bookmark) { PostAction.act(moderator, second_post, PostActionType.types[:bookmark]) }
|
|
|
|
let!(:flag) { PostAction.act(moderator, second_post, PostActionType.types[:off_topic]) }
|
2013-03-20 02:15:08 +08:00
|
|
|
|
2018-07-25 07:25:00 +08:00
|
|
|
before do
|
2018-11-30 01:14:18 +08:00
|
|
|
Jobs::SendSystemMessage.clear
|
2018-07-25 07:25:00 +08:00
|
|
|
end
|
2018-07-25 05:17:47 +08:00
|
|
|
|
2018-07-25 07:25:00 +08:00
|
|
|
it "should delete public post actions and agree with flags" do
|
2019-01-21 22:51:09 +08:00
|
|
|
url = second_post.url
|
2014-07-29 01:17:37 +08:00
|
|
|
second_post.expects(:update_flagged_posts_count)
|
2013-06-06 04:00:45 +08:00
|
|
|
|
2018-11-28 12:51:11 +08:00
|
|
|
PostDestroyer.new(moderator, second_post).destroy
|
2013-06-06 04:00:45 +08:00
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(PostAction.find_by(id: bookmark.id)).to eq(nil)
|
2014-07-29 01:17:37 +08:00
|
|
|
|
|
|
|
off_topic = PostAction.find_by(id: flag.id)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(off_topic).not_to eq(nil)
|
|
|
|
expect(off_topic.agreed_at).not_to eq(nil)
|
2014-07-29 01:17:37 +08:00
|
|
|
|
2013-06-06 04:00:45 +08:00
|
|
|
second_post.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(second_post.bookmark_count).to eq(0)
|
|
|
|
expect(second_post.off_topic_count).to eq(1)
|
2018-07-25 05:17:47 +08:00
|
|
|
|
2019-01-21 14:57:23 +08:00
|
|
|
jobs = Jobs::SendSystemMessage.jobs
|
|
|
|
expect(jobs.size).to eq(1)
|
|
|
|
|
|
|
|
Jobs::SendSystemMessage.new.execute(jobs[0]["args"][0].with_indifferent_access)
|
|
|
|
|
2019-01-21 22:51:09 +08:00
|
|
|
expect(Post.last.raw).to eq(I18n.t(
|
|
|
|
"system_messages.flags_agreed_and_post_deleted.text_body_template",
|
|
|
|
flagged_post_raw_content: second_post.raw,
|
|
|
|
url: url,
|
|
|
|
flag_reason: I18n.t(
|
|
|
|
"flag_reasons.#{PostActionType.flag_types[off_topic.post_action_type_id]}",
|
|
|
|
locale: SiteSetting.default_locale,
|
|
|
|
base_path: Discourse.base_path
|
|
|
|
),
|
|
|
|
site_name: SiteSetting.title,
|
|
|
|
base_url: Discourse.base_url
|
|
|
|
).strip)
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|
2018-07-25 07:25:00 +08:00
|
|
|
|
|
|
|
it "should not send the flags_agreed_and_post_deleted message if it was deleted by system" do
|
2018-11-30 01:14:18 +08:00
|
|
|
expect(PostAction.flagged_posts_count).to eq(1)
|
2018-11-28 12:51:11 +08:00
|
|
|
PostDestroyer.new(Discourse.system_user, second_post).destroy
|
2018-11-30 01:14:18 +08:00
|
|
|
expect(Jobs::SendSystemMessage.jobs.size).to eq(0)
|
|
|
|
expect(PostAction.flagged_posts_count).to eq(0)
|
2018-07-25 07:25:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not send the flags_agreed_and_post_deleted message if it was deleted by author" do
|
|
|
|
SiteSetting.delete_removed_posts_after = 0
|
2018-11-30 01:14:18 +08:00
|
|
|
expect(PostAction.flagged_posts_count).to eq(1)
|
2018-11-28 12:51:11 +08:00
|
|
|
PostDestroyer.new(second_post.user, second_post).destroy
|
2018-11-30 01:14:18 +08:00
|
|
|
expect(Jobs::SendSystemMessage.jobs.size).to eq(0)
|
|
|
|
expect(PostAction.flagged_posts_count).to eq(0)
|
2018-07-25 07:25:00 +08:00
|
|
|
end
|
2018-07-27 03:12:12 +08:00
|
|
|
|
|
|
|
it "should not send the flags_agreed_and_post_deleted message if flags were deferred" do
|
2018-11-30 01:14:18 +08:00
|
|
|
expect(PostAction.flagged_posts_count).to eq(1)
|
2018-07-27 03:12:12 +08:00
|
|
|
PostAction.defer_flags!(second_post, moderator)
|
|
|
|
second_post.reload
|
2018-11-30 01:14:18 +08:00
|
|
|
expect(PostAction.flagged_posts_count).to eq(0)
|
|
|
|
|
2018-11-28 12:51:11 +08:00
|
|
|
PostDestroyer.new(moderator, second_post).destroy
|
2018-11-30 01:14:18 +08:00
|
|
|
expect(Jobs::SendSystemMessage.jobs.size).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not send the flags_agreed_and_post_deleted message if defer_flags is true" do
|
|
|
|
expect(PostAction.flagged_posts_count).to eq(1)
|
|
|
|
PostDestroyer.new(moderator, second_post, defer_flags: true).destroy
|
|
|
|
expect(Jobs::SendSystemMessage.jobs.size).to eq(0)
|
|
|
|
expect(PostAction.flagged_posts_count).to eq(0)
|
2018-07-27 03:12:12 +08:00
|
|
|
end
|
2018-10-02 23:25:08 +08:00
|
|
|
|
|
|
|
it "should set the deleted_public_actions custom field" do
|
|
|
|
PostDestroyer.new(moderator, second_post).destroy
|
|
|
|
expect(second_post.custom_fields["deleted_public_actions"]).to eq("#{bookmark.id}")
|
|
|
|
end
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|
|
|
|
|
2014-06-04 23:41:11 +08:00
|
|
|
describe "user actions" do
|
|
|
|
let(:codinghorror) { Fabricate(:coding_horror) }
|
|
|
|
let(:second_post) { Fabricate(:post, topic_id: post.topic_id) }
|
|
|
|
|
|
|
|
def create_user_action(action_type)
|
2017-07-28 09:20:09 +08:00
|
|
|
UserAction.log_action!(action_type: action_type,
|
|
|
|
user_id: codinghorror.id,
|
|
|
|
acting_user_id: codinghorror.id,
|
|
|
|
target_topic_id: second_post.topic_id,
|
|
|
|
target_post_id: second_post.id)
|
2014-06-04 23:41:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should delete the user actions" do
|
|
|
|
bookmark = create_user_action(UserAction::BOOKMARK)
|
|
|
|
like = create_user_action(UserAction::LIKE)
|
|
|
|
|
|
|
|
PostDestroyer.new(moderator, second_post).destroy
|
|
|
|
|
2014-09-26 01:51:00 +08:00
|
|
|
expect(UserAction.find_by(id: bookmark.id)).to be_nil
|
|
|
|
expect(UserAction.find_by(id: like.id)).to be_nil
|
2014-06-04 23:41:11 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-14 01:41:45 +08:00
|
|
|
describe 'topic links' do
|
|
|
|
let!(:first_post) { Fabricate(:post) }
|
|
|
|
let!(:topic) { first_post.topic }
|
|
|
|
let!(:second_post) { Fabricate(:post_with_external_links, topic: topic) }
|
|
|
|
|
|
|
|
before { TopicLink.extract_from(second_post) }
|
|
|
|
|
|
|
|
it 'should destroy the topic links when moderator destroys the post' do
|
|
|
|
PostDestroyer.new(moderator, second_post.reload).destroy
|
|
|
|
expect(topic.topic_links.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should destroy the topic links when the user destroys the post' do
|
|
|
|
PostDestroyer.new(second_post.user, second_post.reload).destroy
|
|
|
|
expect(topic.topic_links.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-20 02:15:08 +08:00
|
|
|
end
|