2021-06-14 11:04:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "discourse_dev/record"
|
|
|
|
require "faker"
|
|
|
|
|
|
|
|
module DiscourseDev
|
|
|
|
class Topic < Record
|
2022-05-12 14:29:49 -04:00
|
|
|
def initialize(private_messages: false, recipient: nil, ignore_current_count: false)
|
2021-06-14 11:04:44 -04:00
|
|
|
@settings = DiscourseDev.config.topic
|
2022-05-12 14:29:49 -04:00
|
|
|
@private_messages = private_messages
|
|
|
|
@recipient = recipient
|
|
|
|
@ignore_current_count = ignore_current_count
|
2021-06-14 11:04:44 -04:00
|
|
|
super(::Topic, @settings[:count])
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
max_views = 0
|
|
|
|
|
|
|
|
case Faker::Number.between(from: 0, to: 5)
|
|
|
|
when 0
|
|
|
|
max_views = 10
|
|
|
|
when 1
|
|
|
|
max_views = 100
|
|
|
|
when 2
|
|
|
|
max_views = SiteSetting.topic_views_heat_low
|
|
|
|
when 3
|
|
|
|
max_views = SiteSetting.topic_views_heat_medium
|
|
|
|
when 4
|
|
|
|
max_views = SiteSetting.topic_views_heat_high
|
|
|
|
when 5
|
|
|
|
max_views = SiteSetting.topic_views_heat_high + SiteSetting.topic_views_heat_medium
|
|
|
|
end
|
|
|
|
|
2022-05-12 14:29:49 -04:00
|
|
|
if @category
|
|
|
|
merge_attributes = { category: @category.id, tags: tags }
|
|
|
|
else
|
|
|
|
merge_attributes = { archetype: "private_message", target_usernames: [@recipient] }
|
|
|
|
end
|
|
|
|
|
2021-06-14 11:04:44 -04:00
|
|
|
{
|
|
|
|
title: title[0, SiteSetting.max_topic_title_length],
|
|
|
|
raw: Faker::DiscourseMarkdown.sandwich(sentences: 5),
|
|
|
|
created_at: Faker::Time.between(from: DiscourseDev.config.start_date, to: DateTime.now),
|
|
|
|
topic_opts: {
|
|
|
|
import_mode: true,
|
|
|
|
views: Faker::Number.between(from: 1, to: max_views),
|
|
|
|
custom_fields: {
|
|
|
|
dev_sample: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
skip_validations: true,
|
2022-05-12 14:29:49 -04:00
|
|
|
}.merge(merge_attributes)
|
2021-06-14 11:04:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def title
|
|
|
|
if current_count < I18n.t("faker.discourse.topics").count
|
|
|
|
Faker::Discourse.unique.topic
|
|
|
|
else
|
|
|
|
Faker::Lorem
|
|
|
|
.unique
|
|
|
|
.sentence(word_count: 5, supplemental: true, random_words_to_add: 4)
|
|
|
|
.chomp(".")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tags
|
|
|
|
return unless SiteSetting.tagging_enabled
|
|
|
|
|
|
|
|
@tags = []
|
|
|
|
|
|
|
|
Faker::Number
|
|
|
|
.between(from: @settings.dig(:tags, :min), to: @settings.dig(:tags, :max))
|
|
|
|
.times { @tags << Faker::Discourse.tag }
|
|
|
|
|
|
|
|
@tags.uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
def create!
|
2022-05-12 14:29:49 -04:00
|
|
|
if @private_messages && !::User.find_by_username(@recipient)
|
|
|
|
puts "Cannot create PMs for missing user with username: #{@recipient}"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
@category = @private_messages ? nil : Category.random
|
|
|
|
|
2021-06-14 11:04:44 -04:00
|
|
|
user = self.user
|
2021-07-15 08:15:32 -04:00
|
|
|
topic_data = Faker::DiscourseMarkdown.with_user(user.id) { data }
|
|
|
|
post = PostCreator.new(user, topic_data).create!
|
2021-06-14 11:04:44 -04:00
|
|
|
|
2021-07-15 08:15:32 -04:00
|
|
|
if override = @settings.dig(:replies, :overrides).find { |o| o[:title] == topic_data[:title] }
|
2021-06-14 11:04:44 -04:00
|
|
|
reply_count = override[:count]
|
|
|
|
else
|
|
|
|
reply_count =
|
|
|
|
Faker::Number.between(
|
|
|
|
from: @settings.dig(:replies, :min),
|
|
|
|
to: @settings.dig(:replies, :max),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-07-15 08:15:32 -04:00
|
|
|
topic = post.topic
|
|
|
|
Post.new(topic, reply_count).populate!
|
|
|
|
topic
|
2021-06-14 11:04:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def populate!
|
2022-05-12 14:29:49 -04:00
|
|
|
topics = super(ignore_current_count: @ignore_current_count)
|
2021-06-14 11:04:44 -04:00
|
|
|
delete_unwanted_sidekiq_jobs
|
2021-07-15 08:15:32 -04:00
|
|
|
topics
|
2021-06-14 11:04:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def user
|
2022-05-12 14:29:49 -04:00
|
|
|
return ::User.find_by_username(@recipient) if @private_messages
|
2021-06-14 11:04:44 -04:00
|
|
|
return User.random if @category.groups.blank?
|
|
|
|
|
|
|
|
group_ids = @category.groups.pluck(:id)
|
|
|
|
user_ids = ::GroupUser.where(group_id: group_ids).pluck(:user_id)
|
|
|
|
user_count = user_ids.count
|
|
|
|
position = Faker::Number.between(from: 0, to: user_count - 1)
|
|
|
|
::User.find(user_ids[position] || Discourse::SYSTEM_USER_ID)
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_count
|
|
|
|
category_definition_topic_ids = ::Category.pluck(:topic_id)
|
|
|
|
::Topic.where(archetype: :regular).where.not(id: category_definition_topic_ids).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_unwanted_sidekiq_jobs
|
|
|
|
Sidekiq::ScheduledSet.new.each { |job| job.delete if job.item["class"] == "Jobs::UserEmail" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|