DEV: Add ability to populate private messages (#16705)
This commit is contained in:
parent
88b34172af
commit
18ebe2fc28
|
@ -14,7 +14,7 @@ module DiscourseDev
|
|||
|
||||
category = topic.category
|
||||
@max_likes_count = DiscourseDev.config.post[:max_likes_count]
|
||||
unless category.groups.blank?
|
||||
if category&.groups.present?
|
||||
group_ids = category.groups.pluck(:id)
|
||||
@user_ids = ::GroupUser.where(group_id: group_ids).pluck(:user_id)
|
||||
@user_count = @user_ids.count
|
||||
|
@ -54,7 +54,7 @@ module DiscourseDev
|
|||
end
|
||||
|
||||
def user
|
||||
return User.random if topic.category.groups.blank?
|
||||
return User.random if topic.category&.groups.blank?
|
||||
return Discourse.system_user if @user_ids.blank?
|
||||
|
||||
position = Faker::Number.between(from: 0, to: @user_count - 1)
|
||||
|
|
|
@ -69,8 +69,8 @@ module DiscourseDev
|
|||
model.count
|
||||
end
|
||||
|
||||
def self.populate!
|
||||
self.new.populate!
|
||||
def self.populate!(*args)
|
||||
self.new(*args).populate!
|
||||
end
|
||||
|
||||
def self.random(model, use_existing_records: true)
|
||||
|
|
|
@ -6,8 +6,11 @@ require 'faker'
|
|||
module DiscourseDev
|
||||
class Topic < Record
|
||||
|
||||
def initialize
|
||||
def initialize(private_messages: false, recipient: nil, ignore_current_count: false)
|
||||
@settings = DiscourseDev.config.topic
|
||||
@private_messages = private_messages
|
||||
@recipient = recipient
|
||||
@ignore_current_count = ignore_current_count
|
||||
super(::Topic, @settings[:count])
|
||||
end
|
||||
|
||||
|
@ -29,19 +32,29 @@ module DiscourseDev
|
|||
max_views = SiteSetting.topic_views_heat_high + SiteSetting.topic_views_heat_medium
|
||||
end
|
||||
|
||||
if @category
|
||||
merge_attributes = {
|
||||
category: @category.id,
|
||||
tags: tags
|
||||
}
|
||||
else
|
||||
merge_attributes = {
|
||||
archetype: "private_message",
|
||||
target_usernames: [@recipient]
|
||||
}
|
||||
end
|
||||
|
||||
{
|
||||
title: title[0, SiteSetting.max_topic_title_length],
|
||||
raw: Faker::DiscourseMarkdown.sandwich(sentences: 5),
|
||||
category: @category.id,
|
||||
created_at: Faker::Time.between(from: DiscourseDev.config.start_date, to: DateTime.now),
|
||||
tags: tags,
|
||||
topic_opts: {
|
||||
import_mode: true,
|
||||
views: Faker::Number.between(from: 1, to: max_views),
|
||||
custom_fields: { dev_sample: true }
|
||||
},
|
||||
skip_validations: true
|
||||
}
|
||||
}.merge(merge_attributes)
|
||||
end
|
||||
|
||||
def title
|
||||
|
@ -65,7 +78,13 @@ module DiscourseDev
|
|||
end
|
||||
|
||||
def create!
|
||||
@category = Category.random
|
||||
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
|
||||
|
||||
user = self.user
|
||||
topic_data = Faker::DiscourseMarkdown.with_user(user.id) { data }
|
||||
post = PostCreator.new(user, topic_data).create!
|
||||
|
@ -82,12 +101,13 @@ module DiscourseDev
|
|||
end
|
||||
|
||||
def populate!
|
||||
topics = super
|
||||
topics = super(ignore_current_count: @ignore_current_count)
|
||||
delete_unwanted_sidekiq_jobs
|
||||
topics
|
||||
end
|
||||
|
||||
def user
|
||||
return ::User.find_by_username(@recipient) if @private_messages
|
||||
return User.random if @category.groups.blank?
|
||||
|
||||
group_ids = @category.groups.pluck(:id)
|
||||
|
|
|
@ -22,8 +22,24 @@ end
|
|||
|
||||
desc 'Creates sample topics'
|
||||
task 'topics:populate' => ['db:load_config'] do |_, args|
|
||||
if ENV['IGNORE_CURRENT_COUNT'] == "true"
|
||||
DiscourseDev::Topic.populate!(ignore_current_count: true)
|
||||
else
|
||||
DiscourseDev::Topic.populate!
|
||||
end
|
||||
end
|
||||
|
||||
desc 'Creates sample private messages'
|
||||
task 'private_messages:populate', [:recipient] => ['db:load_config'] do |_, args|
|
||||
args.with_defaults(type: 'string')
|
||||
|
||||
if !args[:recipient]
|
||||
puts "ERROR: Expecting rake private_messages:populate[recipient]"
|
||||
exit 1
|
||||
end
|
||||
|
||||
DiscourseDev::Topic.populate!(private_messages: true, recipient: args[:recipient], ignore_current_count: true)
|
||||
end
|
||||
|
||||
desc 'Create post revisions'
|
||||
task 'post_revisions:populate' => ['db:load_config'] do |_, args|
|
||||
|
|
Loading…
Reference in New Issue