DEV: Add ability to populate private messages (#16705)

This commit is contained in:
Penar Musaraj 2022-05-12 14:29:49 -04:00 committed by GitHub
parent 88b34172af
commit 18ebe2fc28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 11 deletions

View File

@ -14,7 +14,7 @@ module DiscourseDev
category = topic.category category = topic.category
@max_likes_count = DiscourseDev.config.post[:max_likes_count] @max_likes_count = DiscourseDev.config.post[:max_likes_count]
unless category.groups.blank? if category&.groups.present?
group_ids = category.groups.pluck(:id) group_ids = category.groups.pluck(:id)
@user_ids = ::GroupUser.where(group_id: group_ids).pluck(:user_id) @user_ids = ::GroupUser.where(group_id: group_ids).pluck(:user_id)
@user_count = @user_ids.count @user_count = @user_ids.count
@ -54,7 +54,7 @@ module DiscourseDev
end end
def user 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? return Discourse.system_user if @user_ids.blank?
position = Faker::Number.between(from: 0, to: @user_count - 1) position = Faker::Number.between(from: 0, to: @user_count - 1)

View File

@ -69,8 +69,8 @@ module DiscourseDev
model.count model.count
end end
def self.populate! def self.populate!(*args)
self.new.populate! self.new(*args).populate!
end end
def self.random(model, use_existing_records: true) def self.random(model, use_existing_records: true)

View File

@ -6,8 +6,11 @@ require 'faker'
module DiscourseDev module DiscourseDev
class Topic < Record class Topic < Record
def initialize def initialize(private_messages: false, recipient: nil, ignore_current_count: false)
@settings = DiscourseDev.config.topic @settings = DiscourseDev.config.topic
@private_messages = private_messages
@recipient = recipient
@ignore_current_count = ignore_current_count
super(::Topic, @settings[:count]) super(::Topic, @settings[:count])
end end
@ -29,19 +32,29 @@ module DiscourseDev
max_views = SiteSetting.topic_views_heat_high + SiteSetting.topic_views_heat_medium max_views = SiteSetting.topic_views_heat_high + SiteSetting.topic_views_heat_medium
end 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], title: title[0, SiteSetting.max_topic_title_length],
raw: Faker::DiscourseMarkdown.sandwich(sentences: 5), raw: Faker::DiscourseMarkdown.sandwich(sentences: 5),
category: @category.id,
created_at: Faker::Time.between(from: DiscourseDev.config.start_date, to: DateTime.now), created_at: Faker::Time.between(from: DiscourseDev.config.start_date, to: DateTime.now),
tags: tags,
topic_opts: { topic_opts: {
import_mode: true, import_mode: true,
views: Faker::Number.between(from: 1, to: max_views), views: Faker::Number.between(from: 1, to: max_views),
custom_fields: { dev_sample: true } custom_fields: { dev_sample: true }
}, },
skip_validations: true skip_validations: true
} }.merge(merge_attributes)
end end
def title def title
@ -65,7 +78,13 @@ module DiscourseDev
end end
def create! 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 user = self.user
topic_data = Faker::DiscourseMarkdown.with_user(user.id) { data } topic_data = Faker::DiscourseMarkdown.with_user(user.id) { data }
post = PostCreator.new(user, topic_data).create! post = PostCreator.new(user, topic_data).create!
@ -82,12 +101,13 @@ module DiscourseDev
end end
def populate! def populate!
topics = super topics = super(ignore_current_count: @ignore_current_count)
delete_unwanted_sidekiq_jobs delete_unwanted_sidekiq_jobs
topics topics
end end
def user def user
return ::User.find_by_username(@recipient) if @private_messages
return User.random if @category.groups.blank? return User.random if @category.groups.blank?
group_ids = @category.groups.pluck(:id) group_ids = @category.groups.pluck(:id)

View File

@ -22,7 +22,23 @@ end
desc 'Creates sample topics' desc 'Creates sample topics'
task 'topics:populate' => ['db:load_config'] do |_, args| 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! 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 end
desc 'Create post revisions' desc 'Create post revisions'