FIX: move post assignment when move post (#229)

When an assigned post is moved to the new topic, it becomes topic assignment.

When an assigned post is moved to an existing topic it stays post assignment
This commit is contained in:
Krzysztof Kotlarek 2021-11-04 08:22:53 +11:00 committed by GitHub
parent a4b1847eff
commit fcd4532c76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View File

@ -737,6 +737,16 @@ after_initialize do
end
end
on(:post_moved) do |post, original_topic_id|
assignment = Assignment.where(topic_id: original_topic_id, target_type: "Post", target_id: post.id).first
next if !assignment
if post.is_first_post?
assignment.update!(topic_id: post.topic_id, target_type: "Topic", target_id: post.topic_id)
else
assignment.update!(topic_id: post.topic_id)
end
end
class ::WebHook
def self.enqueue_assign_hooks(event, payload)
if active_web_hooks('assign').exists?

View File

@ -145,4 +145,30 @@ describe 'integration tests' do
expect(payload["unassigned_to_id"]).to eq(user2.id)
end
end
context 'move post' do
fab!(:old_topic) { Fabricate(:topic) }
fab!(:post) { Fabricate(:post, topic: old_topic) }
fab!(:user) { Fabricate(:user) }
fab!(:assignment) { Assignment.create!(target_id: post.id, target_type: "Post", topic_id: old_topic.id, assigned_by_user: user, assigned_to: user) }
let(:new_topic) { Fabricate(:topic) }
it 'assignment becomes topic assignment when new topic' do
post.update!(topic: new_topic)
DiscourseEvent.trigger(:post_moved, post, old_topic.id)
assignment.reload
expect(assignment.topic_id).to eq(new_topic.id)
expect(assignment.target_type).to eq("Topic")
expect(assignment.target_id).to eq(new_topic.id)
end
it 'assigment is still post assignment when not first post' do
post.update!(topic: new_topic, post_number: "3")
DiscourseEvent.trigger(:post_moved, post, old_topic.id)
assignment.reload
expect(assignment.topic_id).to eq(new_topic.id)
expect(assignment.target_type).to eq("Post")
expect(assignment.target_id).to eq(post.id)
end
end
end