2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe TopicConverter do
|
2022-07-27 11:14:14 -05:00
|
|
|
describe 'convert_to_public_topic' do
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:author) { Fabricate(:user) }
|
|
|
|
fab!(:category) { Fabricate(:category, topic_count: 1) }
|
|
|
|
fab!(:private_message) { Fabricate(:private_message_topic, user: author) } # creates a topic without a first post
|
2022-09-30 13:20:21 -05:00
|
|
|
let(:first_post) { create_post(user: author, topic: private_message, allow_uncategorized_topics: false) }
|
2017-11-09 14:33:26 -06:00
|
|
|
let(:other_user) { private_message.topic_allowed_users.find { |u| u.user != author }.user }
|
2016-05-01 06:48:43 -05:00
|
|
|
|
2019-04-16 02:16:23 -05:00
|
|
|
let(:uncategorized_category) do
|
|
|
|
Category.find(SiteSetting.uncategorized_category_id)
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context 'with success' do
|
2016-05-01 06:48:43 -05:00
|
|
|
it "converts private message to regular topic" do
|
2017-04-11 07:43:33 -05:00
|
|
|
SiteSetting.allow_uncategorized_topics = true
|
2019-04-16 02:16:23 -05:00
|
|
|
topic = nil
|
|
|
|
|
2022-02-10 19:00:58 -06:00
|
|
|
_pm_post_2 = Fabricate(:post, topic: private_message, user: author)
|
|
|
|
_pm_post_3 = Fabricate(:post, topic: private_message, user: author)
|
|
|
|
|
|
|
|
other_pm = Fabricate(:private_message_post).topic
|
|
|
|
other_pm_post = Fabricate(:private_message_post, topic: other_pm)
|
|
|
|
other_pm_post_2 = Fabricate(:private_message_post, topic: other_pm, user: other_pm_post.user)
|
|
|
|
|
2019-04-16 02:16:23 -05:00
|
|
|
expect do
|
|
|
|
topic = TopicConverter.new(first_post.topic, admin).convert_to_public_topic
|
|
|
|
topic.reload
|
|
|
|
end.to change { uncategorized_category.reload.topic_count }.by(1)
|
2022-02-10 19:00:58 -06:00
|
|
|
.and change { author.reload.topic_count }.from(0).to(1)
|
|
|
|
.and change { author.reload.post_count }.from(0).to(2)
|
|
|
|
|
|
|
|
# Ensure query does not affect users from other topics or posts as DB query to update count is quite complex.
|
|
|
|
expect(other_pm.user.topic_count).to eq(0)
|
|
|
|
expect(other_pm.user.post_count).to eq(0)
|
|
|
|
expect(other_pm_post.user.topic_count).to eq(0)
|
|
|
|
expect(other_pm_post.user.post_count).to eq(0)
|
2017-04-11 07:43:33 -05:00
|
|
|
|
2016-05-01 06:48:43 -05:00
|
|
|
expect(topic).to be_valid
|
|
|
|
expect(topic.archetype).to eq("regular")
|
2017-04-11 07:43:33 -05:00
|
|
|
expect(topic.category_id).to eq(SiteSetting.uncategorized_category_id)
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context 'when uncategorized category is not allowed' do
|
2017-04-11 07:43:33 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.allow_uncategorized_topics = false
|
|
|
|
category.update!(read_restricted: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should convert private message into the right category' do
|
2019-04-16 02:16:23 -05:00
|
|
|
topic = TopicConverter.new(first_post.topic, admin).convert_to_public_topic
|
2017-04-11 07:43:33 -05:00
|
|
|
topic.reload
|
|
|
|
|
|
|
|
expect(topic).to be_valid
|
|
|
|
expect(topic.archetype).to eq("regular")
|
2017-07-24 08:17:42 -05:00
|
|
|
|
|
|
|
first_category = Category.where.not(id: SiteSetting.uncategorized_category_id)
|
|
|
|
.where(read_restricted: false).order('id asc').first
|
|
|
|
|
|
|
|
expect(topic.category_id).to eq(first_category.id)
|
2018-04-04 01:31:56 -05:00
|
|
|
expect(topic.category.topic_count).to eq(2)
|
2017-04-11 07:43:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context 'when a custom category_id is given' do
|
2017-04-11 07:43:33 -05:00
|
|
|
it 'should convert private message into the right category' do
|
2019-04-16 02:16:23 -05:00
|
|
|
topic = TopicConverter.new(first_post.topic, admin).convert_to_public_topic(category.id)
|
2017-04-11 07:43:33 -05:00
|
|
|
|
|
|
|
expect(topic.reload.category).to eq(category)
|
2018-04-04 01:31:56 -05:00
|
|
|
expect(topic.category.topic_count).to eq(2)
|
2017-04-11 07:43:33 -05:00
|
|
|
end
|
2016-05-01 06:48:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates user stats" do
|
2017-11-09 14:33:26 -06:00
|
|
|
first_post
|
2019-01-03 11:03:01 -06:00
|
|
|
topic_user = TopicUser.find_by(user_id: author.id, topic_id: private_message.id)
|
2016-05-01 06:48:43 -05:00
|
|
|
expect(private_message.user.user_stat.topic_count).to eq(0)
|
2018-07-03 21:50:51 -05:00
|
|
|
expect(private_message.user.user_stat.post_count).to eq(0)
|
2016-05-01 06:48:43 -05:00
|
|
|
private_message.convert_to_public_topic(admin)
|
|
|
|
expect(private_message.reload.user.user_stat.topic_count).to eq(1)
|
2022-02-06 21:23:34 -06:00
|
|
|
expect(private_message.user.user_stat.post_count).to eq(0)
|
2016-05-01 06:48:43 -05:00
|
|
|
expect(topic_user.reload.notification_level).to eq(TopicUser.notification_levels[:watching])
|
|
|
|
end
|
2017-11-09 14:33:26 -06:00
|
|
|
|
|
|
|
context "with a reply" do
|
|
|
|
before do
|
2019-01-03 11:03:01 -06:00
|
|
|
Jobs.run_immediately!
|
|
|
|
UserActionManager.enable
|
2017-11-09 14:33:26 -06:00
|
|
|
first_post
|
|
|
|
create_post(topic: private_message, user: other_user)
|
|
|
|
private_message.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates UserActions" do
|
|
|
|
TopicConverter.new(private_message, admin).convert_to_public_topic
|
|
|
|
expect(author.user_actions.where(action_type: UserAction::NEW_PRIVATE_MESSAGE).count).to eq(0)
|
|
|
|
expect(author.user_actions.where(action_type: UserAction::NEW_TOPIC).count).to eq(1)
|
2019-01-03 11:03:01 -06:00
|
|
|
expect(other_user.user_actions.where(action_type: UserAction::NEW_PRIVATE_MESSAGE).count).to eq(0)
|
|
|
|
expect(other_user.user_actions.where(action_type: UserAction::GOT_PRIVATE_MESSAGE).count).to eq(0)
|
|
|
|
expect(other_user.user_actions.where(action_type: UserAction::REPLY).count).to eq(1)
|
2017-11-09 14:33:26 -06:00
|
|
|
end
|
|
|
|
end
|
2019-07-22 12:02:21 -05:00
|
|
|
|
|
|
|
it "deletes notifications for users not allowed to see the topic" do
|
|
|
|
staff_category = Fabricate(:private_category, group: Group[:staff])
|
|
|
|
user_notification = Fabricate(:mentioned_notification, post: first_post, user: Fabricate(:user))
|
|
|
|
admin_notification = Fabricate(:mentioned_notification, post: first_post, user: Fabricate(:admin))
|
|
|
|
|
|
|
|
Jobs.run_immediately!
|
|
|
|
TopicConverter.new(first_post.topic, admin).convert_to_public_topic(staff_category.id)
|
|
|
|
|
|
|
|
expect(Notification.exists?(user_notification.id)).to eq(false)
|
|
|
|
expect(Notification.exists?(admin_notification.id)).to eq(true)
|
|
|
|
end
|
2016-05-01 06:48:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe 'convert_to_private_message' do
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:author) { Fabricate(:user) }
|
|
|
|
fab!(:category) { Fabricate(:category) }
|
|
|
|
fab!(:topic) { Fabricate(:topic, user: author, category_id: category.id) }
|
2022-02-10 19:00:58 -06:00
|
|
|
fab!(:post) { Fabricate(:post, topic: topic, user: topic.user) }
|
2016-05-01 06:48:43 -05:00
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context 'with success' do
|
2016-05-01 06:48:43 -05:00
|
|
|
it "converts regular topic to private message" do
|
2019-10-11 01:44:29 -05:00
|
|
|
private_message = topic.convert_to_private_message(post.user)
|
2016-05-01 06:48:43 -05:00
|
|
|
expect(private_message).to be_valid
|
|
|
|
expect(topic.archetype).to eq("private_message")
|
2018-04-04 01:31:56 -05:00
|
|
|
expect(topic.category_id).to eq(nil)
|
2018-04-04 01:50:50 -05:00
|
|
|
expect(category.reload.topic_count).to eq(0)
|
2016-05-01 06:48:43 -05:00
|
|
|
end
|
|
|
|
|
2022-02-10 19:00:58 -06:00
|
|
|
it "converts unlisted topic to private message" do
|
|
|
|
topic.update_status('visible', false, admin)
|
|
|
|
private_message = topic.convert_to_private_message(post.user)
|
|
|
|
|
|
|
|
expect(private_message).to be_valid
|
|
|
|
expect(topic.archetype).to eq("private_message")
|
|
|
|
expect(topic.category_id).to eq(nil)
|
|
|
|
expect(topic.user.post_count).to eq(0)
|
|
|
|
expect(topic.user.topic_count).to eq(0)
|
|
|
|
expect(category.reload.topic_count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates user stats when converting topic to private message" do
|
|
|
|
_post_2 = Fabricate(:post, topic: topic, user: author)
|
|
|
|
_post_3 = Fabricate(:post, topic: topic, user: author)
|
|
|
|
|
|
|
|
other_topic = Fabricate(:post).topic
|
|
|
|
other_post = Fabricate(:post, topic: other_topic)
|
|
|
|
|
2016-05-01 06:48:43 -05:00
|
|
|
topic_user = TopicUser.create!(user_id: author.id, topic_id: topic.id, posted: true)
|
2022-02-10 19:00:58 -06:00
|
|
|
|
|
|
|
expect do
|
|
|
|
topic.convert_to_private_message(admin)
|
|
|
|
end.to change { author.reload.post_count }.from(2).to(0)
|
|
|
|
.and change { author.reload.topic_count }.from(1).to(0)
|
|
|
|
|
|
|
|
# Ensure query does not affect users from other topics or posts as DB query to update count is quite complex.
|
|
|
|
expect(other_topic.user.post_count).to eq(0)
|
|
|
|
expect(other_topic.user.topic_count).to eq(1)
|
|
|
|
expect(other_post.user.post_count).to eq(1)
|
|
|
|
expect(other_post.user.topic_count).to eq(0)
|
2016-05-01 06:48:43 -05:00
|
|
|
|
|
|
|
expect(topic.reload.topic_allowed_users.where(user_id: author.id).count).to eq(1)
|
|
|
|
expect(topic_user.reload.notification_level).to eq(TopicUser.notification_levels[:watching])
|
|
|
|
end
|
2017-11-09 14:33:26 -06:00
|
|
|
|
2022-08-29 07:52:54 -05:00
|
|
|
it "invites only users with regular posts" do
|
|
|
|
post2 = Fabricate(:post, topic: topic)
|
|
|
|
whipser_post = Fabricate(:post, topic: topic, post_type: Post.types[:whisper])
|
|
|
|
small_action_post = Fabricate(:post, topic: topic, post_type: Post.types[:small_action])
|
|
|
|
|
|
|
|
topic.convert_to_private_message(admin)
|
|
|
|
|
|
|
|
expect(topic.reload.topic_allowed_users.pluck(:user_id))
|
|
|
|
.to contain_exactly(admin.id, post.user_id, post2.user_id)
|
|
|
|
end
|
|
|
|
|
2017-11-09 14:33:26 -06:00
|
|
|
it "changes user_action type" do
|
2019-01-03 11:03:01 -06:00
|
|
|
Jobs.run_immediately!
|
|
|
|
UserActionManager.enable
|
2017-11-09 14:33:26 -06:00
|
|
|
topic.convert_to_private_message(admin)
|
|
|
|
expect(author.user_actions.where(action_type: UserAction::NEW_TOPIC).count).to eq(0)
|
|
|
|
expect(author.user_actions.where(action_type: UserAction::NEW_PRIVATE_MESSAGE).count).to eq(1)
|
|
|
|
end
|
2019-07-22 12:02:21 -05:00
|
|
|
|
|
|
|
it "deletes notifications for users not allowed to see the message" do
|
|
|
|
user_notification = Fabricate(:mentioned_notification, post: post, user: Fabricate(:user))
|
|
|
|
admin_notification = Fabricate(:mentioned_notification, post: post, user: Fabricate(:admin))
|
|
|
|
|
|
|
|
Jobs.run_immediately!
|
|
|
|
topic.convert_to_private_message(admin)
|
|
|
|
|
|
|
|
expect(Notification.exists?(user_notification.id)).to eq(false)
|
|
|
|
expect(Notification.exists?(admin_notification.id)).to eq(true)
|
|
|
|
end
|
2020-04-03 10:42:01 -05:00
|
|
|
|
|
|
|
it "limits PM participants" do
|
|
|
|
SiteSetting.max_allowed_message_recipients = 2
|
|
|
|
Fabricate(:post, topic: topic)
|
|
|
|
Fabricate(:post, topic: topic)
|
|
|
|
|
|
|
|
private_message = topic.convert_to_private_message(post.user)
|
|
|
|
|
|
|
|
# Skips posters and just adds the acting user
|
|
|
|
expect(private_message.topic_allowed_users.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2022-03-15 04:51:10 -05:00
|
|
|
it "includes the poster of a single-post topic" do
|
|
|
|
moderator = Fabricate(:moderator)
|
|
|
|
private_message = topic.convert_to_private_message(moderator)
|
|
|
|
expect(private_message.allowed_users).to match_array([topic.user, moderator])
|
|
|
|
end
|
2016-05-01 06:48:43 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context 'when topic has replies' do
|
2016-05-01 06:48:43 -05:00
|
|
|
before do
|
|
|
|
@replied_user = Fabricate(:coding_horror)
|
|
|
|
create_post(topic: topic, user: @replied_user)
|
|
|
|
topic.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds users who replied to topic in Private Message' do
|
|
|
|
topic.convert_to_private_message(admin)
|
|
|
|
|
|
|
|
expect(topic.reload.topic_allowed_users.where(user_id: @replied_user.id).count).to eq(1)
|
|
|
|
expect(topic.reload.user.user_stat.post_count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
2018-03-08 06:29:04 -06:00
|
|
|
|
|
|
|
context 'when user already exists in topic_allowed_users table' do
|
|
|
|
before do
|
|
|
|
topic.topic_allowed_users.create!(user_id: admin.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works" do
|
2019-04-16 02:16:23 -05:00
|
|
|
topic.convert_to_private_message(admin)
|
|
|
|
|
|
|
|
expect(topic.reload.archetype).to eq("private_message")
|
2018-03-08 06:29:04 -06:00
|
|
|
end
|
|
|
|
end
|
2019-12-09 13:15:47 -06:00
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context 'with user_profiles with newly converted PM as featured topic' do
|
2019-12-09 13:15:47 -06:00
|
|
|
it "sets all matching user_profile featured topic ids to nil" do
|
|
|
|
author.user_profile.update(featured_topic: topic)
|
|
|
|
topic.convert_to_private_message(admin)
|
|
|
|
|
|
|
|
expect(author.user_profile.reload.featured_topic).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
2016-05-01 06:48:43 -05:00
|
|
|
end
|
|
|
|
end
|