diff --git a/lib/discourse_dev/post.rb b/lib/discourse_dev/post.rb index ed2e2f65dbb..ef0e072daa4 100644 --- a/lib/discourse_dev/post.rb +++ b/lib/discourse_dev/post.rb @@ -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) diff --git a/lib/discourse_dev/record.rb b/lib/discourse_dev/record.rb index 71ff5af9cda..3e2767ef62c 100644 --- a/lib/discourse_dev/record.rb +++ b/lib/discourse_dev/record.rb @@ -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) diff --git a/lib/discourse_dev/topic.rb b/lib/discourse_dev/topic.rb index b15d7fc85f5..c08af628ebd 100644 --- a/lib/discourse_dev/topic.rb +++ b/lib/discourse_dev/topic.rb @@ -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) diff --git a/lib/tasks/populate.rake b/lib/tasks/populate.rake index 6197bb684b8..1ebd4d8940e 100644 --- a/lib/tasks/populate.rake +++ b/lib/tasks/populate.rake @@ -22,7 +22,23 @@ end desc '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) end desc 'Create post revisions'