FIX: Private message can be set to publish in the future.

This commit is contained in:
Guo Xiang Tan 2017-04-11 20:43:33 +08:00
parent 3861bd2793
commit 73180c8a19
4 changed files with 78 additions and 5 deletions

View File

@ -8,7 +8,13 @@ module Jobs
return if topic.blank?
PostTimestampChanger.new(timestamp: Time.zone.now, topic: topic).change! do
topic.change_category_to_id(topic_status_update.category_id)
if topic.private_message?
topic = TopicConverter.new(topic, Discourse.system_user)
.convert_to_public_topic(topic_status_update.category_id)
else
topic.change_category_to_id(topic_status_update.category_id)
end
topic.update_columns(visible: true)
topic_status_update.trash!(Discourse.system_user)
end

View File

@ -7,9 +7,19 @@ class TopicConverter
@user = user
end
def convert_to_public_topic
def convert_to_public_topic(category_id = nil)
Topic.transaction do
@topic.category_id = SiteSetting.allow_uncategorized_topics ? SiteSetting.uncategorized_category_id : Category.where(read_restricted: false).first.id
@topic.category_id =
if category_id
category_id
elsif SiteSetting.allow_uncategorized_topics
SiteSetting.uncategorized_category_id
else
Category.where(read_restricted: false)
.where.not(id: SiteSetting.uncategorized_category_id)
.first.id
end
@topic.archetype = Archetype.default
@topic.save
update_user_stats

View File

@ -37,7 +37,7 @@ RSpec.describe Jobs::PublishTopicToCategory do
end
end
it 'should publish the topic to the new category correctly' do
it 'should publish the topic to the new category' do
Timecop.travel(1.hour.ago) { topic.update!(visible: false) }
message = MessageBus.track_publish do
@ -56,4 +56,32 @@ RSpec.describe Jobs::PublishTopicToCategory do
expect(message.data[:reload_topic]).to be_present
expect(message.data[:refresh_stream]).to be_present
end
describe 'when topic is a private message' do
before do
expect { topic.convert_to_private_message(Discourse.system_user) }
.to change { topic.private_message? }.to(true)
end
it 'should publish the topic to the new category' do
described_class.new.execute(topic_status_update_id: topic.topic_status_update.id)
message = MessageBus.track_publish do
described_class.new.execute(topic_status_update_id: topic.topic_status_update.id)
end.first
topic.reload
expect(topic.category).to eq(another_category)
expect(topic.visible).to eq(true)
expect(topic.private_message?).to eq(false)
%w{created_at bumped_at updated_at last_posted_at}.each do |attribute|
expect(topic.public_send(attribute)).to be_within(1.second).of(Time.zone.now)
end
expect(message.data[:reload_topic]).to be_present
expect(message.data[:refresh_stream]).to be_present
end
end
end

View File

@ -5,13 +5,42 @@ describe TopicConverter do
context 'convert_to_public_topic' do
let(:admin) { Fabricate(:admin) }
let(:author) { Fabricate(:user) }
let(:category) { Fabricate(:category) }
let(:private_message) { Fabricate(:private_message_topic, user: author) }
context 'success' do
it "converts private message to regular topic" do
topic = private_message.convert_to_public_topic(admin)
SiteSetting.allow_uncategorized_topics = true
topic = described_class.new(private_message, admin).convert_to_public_topic
topic.reload
expect(topic).to be_valid
expect(topic.archetype).to eq("regular")
expect(topic.category_id).to eq(SiteSetting.uncategorized_category_id)
end
describe 'when uncategorized category is not allowed' do
before do
SiteSetting.allow_uncategorized_topics = false
category.update!(read_restricted: false)
end
it 'should convert private message into the right category' do
topic = described_class.new(private_message, admin).convert_to_public_topic
topic.reload
expect(topic).to be_valid
expect(topic.archetype).to eq("regular")
expect(topic.category_id).to eq(category.id)
end
end
describe 'when a custom category_id is given' do
it 'should convert private message into the right category' do
topic = described_class.new(private_message, admin).convert_to_public_topic(category.id)
expect(topic.reload.category).to eq(category)
end
end
it "updates user stats" do