mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 02:19:27 +08:00
FIX: Delete internal links when moderator deletes a post (#13233)
This commit is contained in:
parent
fd64268b0d
commit
4d4c3fe1e4
|
@ -108,7 +108,7 @@ class TopicLink < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def self.extract_from(post)
|
||||
return if post.blank? || post.whisper? || post.user_id.blank?
|
||||
return if post.blank? || post.whisper? || post.user_id.blank? || post.deleted_at.present?
|
||||
|
||||
current_urls = []
|
||||
reflected_ids = []
|
||||
|
|
|
@ -168,6 +168,7 @@ class PostDestroyer
|
|||
permanent? ? @post.topic.destroy! : @post.topic.trash!(@user)
|
||||
PublishedPage.unpublish!(@user, @post.topic) if @post.topic.published_page
|
||||
end
|
||||
TopicLink.where(link_post_id: @post.id).destroy_all
|
||||
update_associated_category_latest_topic
|
||||
update_user_counts
|
||||
TopicUser.update_post_action_cache(post_id: @post.id)
|
||||
|
|
|
@ -938,6 +938,43 @@ describe PostDestroyer do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'internal links' do
|
||||
fab!(:topic) { Fabricate(:topic) }
|
||||
let!(:second_post) { Fabricate(:post, topic: topic) }
|
||||
fab!(:other_topic) { Fabricate(:topic) }
|
||||
let!(:other_post) { Fabricate(:post, topic: other_topic) }
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
let!(:base_url) { URI.parse(Discourse.base_url) }
|
||||
let!(:guardian) { Guardian.new }
|
||||
let!(:url) { "http://#{base_url.host}/t/#{other_topic.slug}/#{other_topic.id}/#{other_post.post_number}" }
|
||||
|
||||
it 'should destroy internal links when user deletes own post' do
|
||||
new_post = Post.create!(user: user, topic: topic, raw: "Link to other topic:\n\n#{url}\n")
|
||||
TopicLink.extract_from(new_post)
|
||||
|
||||
link_counts = TopicLink.counts_for(guardian, other_topic.reload, [other_post])
|
||||
expect(link_counts.count).to eq(1)
|
||||
|
||||
PostDestroyer.new(user, new_post).destroy
|
||||
|
||||
updated_link_counts = TopicLink.counts_for(guardian, other_topic.reload, [other_post])
|
||||
expect(updated_link_counts.count).to eq(0)
|
||||
end
|
||||
|
||||
it 'should destroy internal links when moderator deletes post' do
|
||||
new_post = Post.create!(user: user, topic: topic, raw: "Link to other topic:\n\n#{url}\n")
|
||||
TopicLink.extract_from(new_post)
|
||||
link_counts = TopicLink.counts_for(guardian, other_topic.reload, [other_post])
|
||||
expect(link_counts.count).to eq(1)
|
||||
|
||||
PostDestroyer.new(moderator, new_post).destroy
|
||||
TopicLink.extract_from(new_post)
|
||||
updated_link_counts = TopicLink.counts_for(guardian, other_topic, [other_post])
|
||||
|
||||
expect(updated_link_counts.count).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#delete_with_replies' do
|
||||
let(:reporter) { Discourse.system_user }
|
||||
fab!(:post) { Fabricate(:post) }
|
||||
|
|
|
@ -107,6 +107,7 @@ describe TopicLink do
|
|||
fab!(:other_topic) do
|
||||
Fabricate(:topic, user: user)
|
||||
end
|
||||
fab!(:moderator) { Fabricate(:moderator) }
|
||||
|
||||
let(:post) do
|
||||
other_topic.posts.create(user: user, raw: "some content")
|
||||
|
@ -185,6 +186,21 @@ describe TopicLink do
|
|||
expect(reflection.link_post_id).to eq(linked_post.id)
|
||||
expect(reflection.user_id).to eq(link.user_id)
|
||||
end
|
||||
|
||||
it "doesn't work for a deleted post" do
|
||||
post
|
||||
url = "http://#{test_uri.host}/t/#{other_topic.slug}/#{other_topic.id}"
|
||||
|
||||
topic.posts.create(user: user, raw: 'initial post')
|
||||
linked_post = topic.posts.create(user: user, raw: "Link to another topic: #{url}")
|
||||
TopicLink.extract_from(linked_post)
|
||||
expect(other_topic.reload.topic_links.where(link_post_id: linked_post.id).count).to eq(1)
|
||||
|
||||
PostDestroyer.new(moderator, linked_post).destroy
|
||||
TopicLink.extract_from(linked_post)
|
||||
expect(other_topic.reload.topic_links.where(link_post_id: linked_post.id)).to be_blank
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
context "link to a user on discourse" do
|
||||
|
|
Loading…
Reference in New Issue
Block a user