mirror of
https://github.com/discourse/discourse.git
synced 2025-02-08 04:18:23 +00:00
FIX: update categories topic_count when converting topic to a PM and vice versa
This commit is contained in:
parent
e6d07fa6d8
commit
c36e201eb3
@ -24,6 +24,7 @@ class TopicConverter
|
|||||||
@topic.archetype = Archetype.default
|
@topic.archetype = Archetype.default
|
||||||
@topic.save
|
@topic.save
|
||||||
update_user_stats
|
update_user_stats
|
||||||
|
update_category_topic_count_by(1)
|
||||||
|
|
||||||
# TODO: Every post in a PRIVATE MESSAGE looks the same: each is a UserAction::NEW_PRIVATE_MESSAGE.
|
# TODO: Every post in a PRIVATE MESSAGE looks the same: each is a UserAction::NEW_PRIVATE_MESSAGE.
|
||||||
# So we need to remove all those user actions and re-log all the posts.
|
# So we need to remove all those user actions and re-log all the posts.
|
||||||
@ -46,6 +47,7 @@ class TopicConverter
|
|||||||
|
|
||||||
def convert_to_private_message
|
def convert_to_private_message
|
||||||
Topic.transaction do
|
Topic.transaction do
|
||||||
|
update_category_topic_count_by(-1)
|
||||||
@topic.category_id = nil
|
@topic.category_id = nil
|
||||||
@topic.archetype = Archetype.private_message
|
@topic.archetype = Archetype.private_message
|
||||||
add_allowed_users
|
add_allowed_users
|
||||||
@ -92,4 +94,10 @@ class TopicConverter
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update_category_topic_count_by(num)
|
||||||
|
if @topic.category_id.present?
|
||||||
|
Category.where(['id = ?', @topic.category_id]).update_all("topic_count = topic_count " + (num > 0 ? '+' : '') + "#{num}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -5,7 +5,7 @@ describe TopicConverter do
|
|||||||
context 'convert_to_public_topic' do
|
context 'convert_to_public_topic' do
|
||||||
let(:admin) { Fabricate(:admin) }
|
let(:admin) { Fabricate(:admin) }
|
||||||
let(:author) { Fabricate(:user) }
|
let(:author) { Fabricate(:user) }
|
||||||
let(:category) { Fabricate(:category) }
|
let(:category) { Fabricate(:category, topic_count: 1) }
|
||||||
let(:private_message) { Fabricate(:private_message_topic, user: author) } # creates a topic without a first post
|
let(:private_message) { Fabricate(:private_message_topic, user: author) } # creates a topic without a first post
|
||||||
let(:first_post) { Fabricate(:post, topic: private_message, user: author) }
|
let(:first_post) { Fabricate(:post, topic: private_message, user: author) }
|
||||||
let(:other_user) { private_message.topic_allowed_users.find { |u| u.user != author }.user }
|
let(:other_user) { private_message.topic_allowed_users.find { |u| u.user != author }.user }
|
||||||
@ -19,6 +19,7 @@ describe TopicConverter do
|
|||||||
expect(topic).to be_valid
|
expect(topic).to be_valid
|
||||||
expect(topic.archetype).to eq("regular")
|
expect(topic.archetype).to eq("regular")
|
||||||
expect(topic.category_id).to eq(SiteSetting.uncategorized_category_id)
|
expect(topic.category_id).to eq(SiteSetting.uncategorized_category_id)
|
||||||
|
expect(topic.category.topic_count).to eq(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'when uncategorized category is not allowed' do
|
describe 'when uncategorized category is not allowed' do
|
||||||
@ -38,6 +39,7 @@ describe TopicConverter do
|
|||||||
.where(read_restricted: false).order('id asc').first
|
.where(read_restricted: false).order('id asc').first
|
||||||
|
|
||||||
expect(topic.category_id).to eq(first_category.id)
|
expect(topic.category_id).to eq(first_category.id)
|
||||||
|
expect(topic.category.topic_count).to eq(2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -46,6 +48,7 @@ describe TopicConverter do
|
|||||||
topic = TopicConverter.new(private_message, admin).convert_to_public_topic(category.id)
|
topic = TopicConverter.new(private_message, admin).convert_to_public_topic(category.id)
|
||||||
|
|
||||||
expect(topic.reload.category).to eq(category)
|
expect(topic.reload.category).to eq(category)
|
||||||
|
expect(topic.category.topic_count).to eq(2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -86,13 +89,16 @@ describe TopicConverter do
|
|||||||
context 'convert_to_private_message' do
|
context 'convert_to_private_message' do
|
||||||
let(:admin) { Fabricate(:admin) }
|
let(:admin) { Fabricate(:admin) }
|
||||||
let(:author) { Fabricate(:user) }
|
let(:author) { Fabricate(:user) }
|
||||||
let(:topic) { Fabricate(:topic, user: author) }
|
let(:category) { Fabricate(:category) }
|
||||||
|
let(:topic) { Fabricate(:topic, user: author, category_id: category.id) }
|
||||||
|
|
||||||
context 'success' do
|
context 'success' do
|
||||||
it "converts regular topic to private message" do
|
it "converts regular topic to private message" do
|
||||||
private_message = topic.convert_to_private_message(admin)
|
private_message = topic.convert_to_private_message(admin)
|
||||||
expect(private_message).to be_valid
|
expect(private_message).to be_valid
|
||||||
expect(topic.archetype).to eq("private_message")
|
expect(topic.archetype).to eq("private_message")
|
||||||
|
expect(topic.category_id).to eq(nil)
|
||||||
|
expect(category.topic_count).to eq(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "updates user stats" do
|
it "updates user stats" do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user