diff --git a/plugin.rb b/plugin.rb index fff1419..7421fab 100644 --- a/plugin.rb +++ b/plugin.rb @@ -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? diff --git a/spec/integration/assign_spec.rb b/spec/integration/assign_spec.rb index 6f5d99b..a8c7b0f 100644 --- a/spec/integration/assign_spec.rb +++ b/spec/integration/assign_spec.rb @@ -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