A platform for community discussion. Free, open, simple.
DEV: Add ability to populate private messages (#16705)
Penar Musaraj18ebe2fc28a5
diff --git a/lib/discourse_dev/post.rb b/lib/discourse_dev/post.rbindex ed2e2f65dbb..ef0e072daa4 100644--- a/lib/discourse_dev/post.rb+++ b/lib/discourse_dev/post.rb@@ -14,7 +14,7 @@ module DiscourseDevcategory = 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 DiscourseDevenddef 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)diff --git a/lib/discourse_dev/record.rb b/lib/discourse_dev/record.rbindex 71ff5af9cda..3e2767ef62c 100644--- a/lib/discourse_dev/record.rb+++ b/lib/discourse_dev/record.rb@@ -69,8 +69,8 @@ module DiscourseDevmodel.countend- def self.populate!- self.new.populate!+ def self.populate!(*args)+ self.new(*args).populate!enddef self.random(model, use_existing_records: true)diff --git a/lib/discourse_dev/topic.rb b/lib/discourse_dev/topic.rbindex b15d7fc85f5..c08af628ebd 100644--- a/lib/discourse_dev/topic.rb+++ b/lib/discourse_dev/topic.rb@@ -6,8 +6,11 @@ require 'faker'module DiscourseDevclass 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_countsuper(::Topic, @settings[:count])end@@ -29,19 +32,29 @@ module DiscourseDevmax_views = SiteSetting.topic_views_heat_high + SiteSetting.topic_views_heat_mediumend+ 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)enddef title@@ -65,7 +78,13 @@ module DiscourseDevenddef 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.usertopic_data = Faker::DiscourseMarkdown.with_user(user.id) { data }post = PostCreator.new(user, topic_data).create!@@ -82,12 +101,13 @@ module DiscourseDevenddef populate!- topics = super+ topics = super(ignore_current_count: @ignore_current_count)delete_unwanted_sidekiq_jobstopicsenddef user+ return ::User.find_by_username(@recipient) if @private_messagesreturn User.random if @category.groups.blank?group_ids = @category.groups.pluck(:id)diff --git a/lib/tasks/populate.rake b/lib/tasks/populate.rakeindex 6197bb684b8..1ebd4d8940e 100644--- a/lib/tasks/populate.rake+++ b/lib/tasks/populate.rake@@ -22,7 +22,23 @@ enddesc 'Creates sample topics'task 'topics:populate' => ['db:load_config'] do |_, args|- DiscourseDev::Topic.populate!+ 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)enddesc 'Create post revisions'