mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 06:41:25 +08:00
DEV: move discourse_dev
gem to the core. (#13360)
And get avatar images from `discourse_dev_assets` gem.
This commit is contained in:
60
lib/discourse_dev/category.rb
Normal file
60
lib/discourse_dev/category.rb
Normal file
@ -0,0 +1,60 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'discourse_dev/record'
|
||||
require 'rails'
|
||||
require 'faker'
|
||||
|
||||
module DiscourseDev
|
||||
class Category < Record
|
||||
|
||||
def initialize
|
||||
super(::Category, DiscourseDev.config.category[:count])
|
||||
@parent_category_ids = ::Category.where(parent_category_id: nil).pluck(:id)
|
||||
end
|
||||
|
||||
def data
|
||||
name = Faker::Discourse.unique.category
|
||||
parent_category_id = nil
|
||||
|
||||
if Faker::Boolean.boolean(true_ratio: 0.6)
|
||||
offset = Faker::Number.between(from: 0, to: @parent_category_ids.count - 1)
|
||||
parent_category_id = @parent_category_ids[offset]
|
||||
@permissions = ::Category.find(parent_category_id).permissions_params.presence
|
||||
else
|
||||
@permissions = nil
|
||||
end
|
||||
|
||||
{
|
||||
name: name,
|
||||
description: Faker::Lorem.paragraph,
|
||||
user_id: ::Discourse::SYSTEM_USER_ID,
|
||||
color: Faker::Color.hex_color.last(6),
|
||||
parent_category_id: parent_category_id
|
||||
}
|
||||
end
|
||||
|
||||
def permissions
|
||||
return @permissions if @permissions.present?
|
||||
return { everyone: :full } if Faker::Boolean.boolean(true_ratio: 0.75)
|
||||
|
||||
permission = {}
|
||||
group = Group.random
|
||||
permission[group.id] = Faker::Number.between(from: 1, to: 3)
|
||||
|
||||
permission
|
||||
end
|
||||
|
||||
def create!
|
||||
super do |category|
|
||||
category.set_permissions(permissions)
|
||||
category.save!
|
||||
|
||||
@parent_category_ids << category.id if category.parent_category_id.blank?
|
||||
end
|
||||
end
|
||||
|
||||
def self.random
|
||||
super(::Category)
|
||||
end
|
||||
end
|
||||
end
|
149
lib/discourse_dev/config.rb
Normal file
149
lib/discourse_dev/config.rb
Normal file
@ -0,0 +1,149 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails'
|
||||
require 'highline/import'
|
||||
|
||||
module DiscourseDev
|
||||
class Config
|
||||
attr_reader :config, :file_path
|
||||
|
||||
def initialize
|
||||
default_file_path = File.join(Rails.root, "config", "dev_defaults.yml")
|
||||
@file_path = File.join(Rails.root, "config", "dev.yml")
|
||||
default_config = YAML.load_file(default_file_path)
|
||||
|
||||
if File.exists?(file_path)
|
||||
user_config = YAML.load_file(file_path)
|
||||
else
|
||||
puts "I did no detect a custom `config/dev.yml` file, creating one for you where you can amend defaults."
|
||||
FileUtils.cp(default_file_path, file_path)
|
||||
user_config = {}
|
||||
end
|
||||
|
||||
@config = default_config.deep_merge(user_config).deep_symbolize_keys
|
||||
end
|
||||
|
||||
def update!
|
||||
update_site_settings
|
||||
create_admin_user
|
||||
create_new_user
|
||||
set_seed
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_site_settings
|
||||
puts "Updating site settings..."
|
||||
|
||||
site_settings = config[:site_settings] || {}
|
||||
|
||||
site_settings.each do |key, value|
|
||||
puts "#{key} = #{value}"
|
||||
SiteSetting.set(key, value)
|
||||
end
|
||||
|
||||
SiteSetting.refresh!
|
||||
end
|
||||
|
||||
def create_admin_user
|
||||
puts "Creating default admin user account..."
|
||||
|
||||
settings = config[:admin]
|
||||
|
||||
if settings.present?
|
||||
create_admin_user_from_settings(settings)
|
||||
else
|
||||
create_admin_user_from_input
|
||||
end
|
||||
end
|
||||
|
||||
def create_new_user
|
||||
settings = config[:new_user]
|
||||
|
||||
if settings.present?
|
||||
email = settings[:email] || "new_user@example.com"
|
||||
|
||||
new_user = ::User.create!(
|
||||
email: email,
|
||||
username: settings[:username] || UserNameSuggester.suggest(email)
|
||||
)
|
||||
new_user.email_tokens.update_all confirmed: true
|
||||
new_user.activate
|
||||
end
|
||||
end
|
||||
|
||||
def set_seed
|
||||
seed = self.seed || 1
|
||||
Faker::Config.random = Random.new(seed)
|
||||
end
|
||||
|
||||
def start_date
|
||||
DateTime.parse(config[:start_date])
|
||||
end
|
||||
|
||||
def method_missing(name)
|
||||
config[name.to_sym]
|
||||
end
|
||||
|
||||
def create_admin_user_from_settings(settings)
|
||||
email = settings[:email]
|
||||
|
||||
admin = ::User.with_email(email).first_or_create!(
|
||||
email: email,
|
||||
username: settings[:username] || UserNameSuggester.suggest(email),
|
||||
password: settings[:password]
|
||||
)
|
||||
admin.grant_admin!
|
||||
if admin.trust_level < 1
|
||||
admin.change_trust_level!(1)
|
||||
end
|
||||
admin.email_tokens.update_all confirmed: true
|
||||
admin.activate
|
||||
end
|
||||
|
||||
def create_admin_user_from_input
|
||||
begin
|
||||
email = ask("Email: ")
|
||||
password = ask("Password (optional, press ENTER to skip): ")
|
||||
username = UserNameSuggester.suggest(email)
|
||||
|
||||
admin = ::User.new(
|
||||
email: email,
|
||||
username: username
|
||||
)
|
||||
|
||||
if password.present?
|
||||
admin.password = password
|
||||
else
|
||||
puts "Once site is running use https://localhost:9292/user/#{username}/become to access the account in development"
|
||||
end
|
||||
|
||||
admin.name = ask("Full name: ") if SiteSetting.full_name_required
|
||||
saved = admin.save
|
||||
|
||||
if saved
|
||||
File.open(file_path, 'a') do | file|
|
||||
file.puts("admin:")
|
||||
file.puts(" username: #{admin.username}")
|
||||
file.puts(" email: #{admin.email}")
|
||||
file.puts(" password: #{password}") if password.present?
|
||||
end
|
||||
else
|
||||
say(admin.errors.full_messages.join("\n"))
|
||||
end
|
||||
end while !saved
|
||||
|
||||
admin.active = true
|
||||
admin.save
|
||||
|
||||
admin.grant_admin!
|
||||
if admin.trust_level < 1
|
||||
admin.change_trust_level!(1)
|
||||
end
|
||||
admin.email_tokens.update_all confirmed: true
|
||||
admin.activate
|
||||
|
||||
say("\nAdmin account created successfully with username #{admin.username}")
|
||||
end
|
||||
end
|
||||
end
|
38
lib/discourse_dev/group.rb
Normal file
38
lib/discourse_dev/group.rb
Normal file
@ -0,0 +1,38 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'discourse_dev/record'
|
||||
require 'rails'
|
||||
require 'faker'
|
||||
|
||||
module DiscourseDev
|
||||
class Group < Record
|
||||
|
||||
def initialize
|
||||
super(::Group, DiscourseDev.config.group[:count])
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
name: Faker::Discourse.unique.group,
|
||||
public_exit: Faker::Boolean.boolean,
|
||||
public_admission: Faker::Boolean.boolean,
|
||||
primary_group: Faker::Boolean.boolean,
|
||||
created_at: Faker::Time.between(from: DiscourseDev.config.start_date, to: DateTime.now),
|
||||
}
|
||||
end
|
||||
|
||||
def create!
|
||||
super do |group|
|
||||
if Faker::Boolean.boolean
|
||||
group.add_owner(::Discourse.system_user)
|
||||
group.allow_membership_requests = true
|
||||
group.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.random
|
||||
super(::Group)
|
||||
end
|
||||
end
|
||||
end
|
107
lib/discourse_dev/post.rb
Normal file
107
lib/discourse_dev/post.rb
Normal file
@ -0,0 +1,107 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'discourse_dev/record'
|
||||
require 'faker'
|
||||
|
||||
module DiscourseDev
|
||||
class Post < Record
|
||||
|
||||
attr_reader :topic
|
||||
|
||||
def initialize(topic, count)
|
||||
super(::Post, count)
|
||||
@topic = topic
|
||||
|
||||
category = topic.category
|
||||
@max_likes_count = DiscourseDev.config.post[:max_likes_count]
|
||||
unless 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
|
||||
@max_likes_count = @user_count - 1
|
||||
end
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
topic_id: topic.id,
|
||||
raw: Faker::DiscourseMarkdown.sandwich(sentences: 5),
|
||||
created_at: Faker::Time.between(from: topic.last_posted_at, to: DateTime.now),
|
||||
skip_validations: true,
|
||||
skip_guardian: true
|
||||
}
|
||||
end
|
||||
|
||||
def create!
|
||||
user = self.user
|
||||
data = Faker::DiscourseMarkdown.with_user(user.id) { self.data }
|
||||
post = PostCreator.new(user, data).create!
|
||||
topic.reload
|
||||
generate_likes(post)
|
||||
end
|
||||
|
||||
def generate_likes(post)
|
||||
user_ids = [post.user_id]
|
||||
|
||||
Faker::Number.between(from: 0, to: @max_likes_count).times do
|
||||
user = self.user
|
||||
next if user_ids.include?(user.id)
|
||||
|
||||
PostActionCreator.new(user, post, PostActionType.types[:like], created_at: Faker::Time.between(from: post.created_at, to: DateTime.now)).perform
|
||||
user_ids << user.id
|
||||
end
|
||||
end
|
||||
|
||||
def user
|
||||
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)
|
||||
::User.find(@user_ids[position])
|
||||
end
|
||||
|
||||
def populate!
|
||||
generate_likes(topic.first_post)
|
||||
|
||||
@count.times do
|
||||
create!
|
||||
end
|
||||
end
|
||||
|
||||
def self.add_replies!(args)
|
||||
if !args[:topic_id]
|
||||
puts "Topic ID is required. Aborting."
|
||||
return
|
||||
end
|
||||
|
||||
if !::Topic.find_by_id(args[:topic_id])
|
||||
puts "Topic ID does not match topic in DB, aborting."
|
||||
return
|
||||
end
|
||||
|
||||
topic = ::Topic.find_by_id(args[:topic_id])
|
||||
count = args[:count] ? args[:count].to_i : 50
|
||||
|
||||
puts "Creating #{count} replies in '#{topic.title}'"
|
||||
|
||||
count.times do |i|
|
||||
begin
|
||||
user = User.random
|
||||
reply = Faker::DiscourseMarkdown.with_user(user.id) do
|
||||
{
|
||||
topic_id: topic.id,
|
||||
raw: Faker::DiscourseMarkdown.sandwich(sentences: 5),
|
||||
skip_validations: true
|
||||
}
|
||||
end
|
||||
PostCreator.new(user, reply).create!
|
||||
rescue ActiveRecord::RecordNotSaved => e
|
||||
puts e
|
||||
end
|
||||
end
|
||||
|
||||
puts "Done!"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
68
lib/discourse_dev/record.rb
Normal file
68
lib/discourse_dev/record.rb
Normal file
@ -0,0 +1,68 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'discourse_dev'
|
||||
require 'rails'
|
||||
require 'faker'
|
||||
|
||||
module DiscourseDev
|
||||
class Record
|
||||
DEFAULT_COUNT = 30.freeze
|
||||
|
||||
attr_reader :model, :type
|
||||
|
||||
def initialize(model, count = DEFAULT_COUNT)
|
||||
@@initialized ||= begin
|
||||
Faker::Discourse.unique.clear
|
||||
RateLimiter.disable
|
||||
true
|
||||
end
|
||||
|
||||
@model = model
|
||||
@type = model.to_s
|
||||
@count = count
|
||||
end
|
||||
|
||||
def create!
|
||||
record = model.create!(data)
|
||||
yield(record) if block_given?
|
||||
end
|
||||
|
||||
def populate!
|
||||
if current_count >= @count
|
||||
puts "Already have #{current_count} #{type.downcase} records"
|
||||
|
||||
Rake.application.top_level_tasks.each do |task_name|
|
||||
Rake::Task[task_name].reenable
|
||||
end
|
||||
|
||||
Rake::Task['dev:repopulate'].invoke
|
||||
return
|
||||
elsif current_count > 0
|
||||
@count -= current_count
|
||||
puts "There are #{current_count} #{type.downcase} records. Creating #{@count} more."
|
||||
else
|
||||
puts "Creating #{@count} sample #{type.downcase} records"
|
||||
end
|
||||
|
||||
@count.times do
|
||||
create!
|
||||
putc "."
|
||||
end
|
||||
|
||||
puts
|
||||
end
|
||||
|
||||
def current_count
|
||||
model.count
|
||||
end
|
||||
|
||||
def self.populate!
|
||||
self.new.populate!
|
||||
end
|
||||
|
||||
def self.random(model)
|
||||
offset = Faker::Number.between(from: 0, to: model.count - 1)
|
||||
model.offset(offset).first
|
||||
end
|
||||
end
|
||||
end
|
32
lib/discourse_dev/tag.rb
Normal file
32
lib/discourse_dev/tag.rb
Normal file
@ -0,0 +1,32 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'discourse_dev/record'
|
||||
require 'rails'
|
||||
require 'faker'
|
||||
|
||||
module DiscourseDev
|
||||
class Tag < Record
|
||||
|
||||
def initialize
|
||||
super(::Tag, DiscourseDev.config.tag[:count])
|
||||
end
|
||||
|
||||
def create!
|
||||
super
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
# If the name is taken, try again
|
||||
retry
|
||||
end
|
||||
|
||||
def populate!
|
||||
return unless SiteSetting.tagging_enabled
|
||||
super
|
||||
end
|
||||
|
||||
def data
|
||||
{
|
||||
name: Faker::Discourse.unique.tag,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
108
lib/discourse_dev/topic.rb
Normal file
108
lib/discourse_dev/topic.rb
Normal file
@ -0,0 +1,108 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'discourse_dev/record'
|
||||
require 'faker'
|
||||
|
||||
module DiscourseDev
|
||||
class Topic < Record
|
||||
|
||||
def initialize
|
||||
@settings = DiscourseDev.config.topic
|
||||
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
|
||||
|
||||
{
|
||||
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
|
||||
}
|
||||
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 do
|
||||
@tags << Faker::Discourse.tag
|
||||
end
|
||||
|
||||
@tags.uniq
|
||||
end
|
||||
|
||||
def create!
|
||||
@category = Category.random
|
||||
user = self.user
|
||||
topic = Faker::DiscourseMarkdown.with_user(user.id) { data }
|
||||
post = PostCreator.new(user, topic).create!
|
||||
|
||||
if override = @settings.dig(:replies, :overrides).find { |o| o[:title] == topic[:title] }
|
||||
reply_count = override[:count]
|
||||
else
|
||||
reply_count = Faker::Number.between(from: @settings.dig(:replies, :min), to: @settings.dig(:replies, :max))
|
||||
end
|
||||
|
||||
Post.new(post.topic, reply_count).populate!
|
||||
end
|
||||
|
||||
def populate!
|
||||
super
|
||||
delete_unwanted_sidekiq_jobs
|
||||
end
|
||||
|
||||
def user
|
||||
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 do |job|
|
||||
job.delete if job.item["class"] == "Jobs::UserEmail"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
93
lib/discourse_dev/user.rb
Normal file
93
lib/discourse_dev/user.rb
Normal file
@ -0,0 +1,93 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'discourse_dev/record'
|
||||
require 'faker'
|
||||
|
||||
module DiscourseDev
|
||||
class User < Record
|
||||
attr_reader :images
|
||||
|
||||
def initialize
|
||||
super(::User, DiscourseDev.config.user[:count])
|
||||
|
||||
@images = DiscourseDevAssets.avatars
|
||||
end
|
||||
|
||||
def data
|
||||
name = Faker::Name.unique.name
|
||||
email = Faker::Internet.unique.email(name: name, domain: "faker.invalid")
|
||||
username = Faker::Internet.unique.username(specifier: ::User.username_length)
|
||||
username = UserNameSuggester.suggest(username)
|
||||
username_lower = ::User.normalize_username(username)
|
||||
|
||||
{
|
||||
name: name,
|
||||
email: email,
|
||||
username: username,
|
||||
username_lower: username_lower,
|
||||
moderator: Faker::Boolean.boolean(true_ratio: 0.1),
|
||||
trust_level: Faker::Number.between(from: 1, to: 4),
|
||||
created_at: Faker::Time.between(from: DiscourseDev.config.start_date, to: DateTime.now),
|
||||
}
|
||||
end
|
||||
|
||||
def create!
|
||||
super do |user|
|
||||
user.activate
|
||||
set_random_avatar(user)
|
||||
Faker::Number.between(from: 0, to: 2).times do
|
||||
group = Group.random
|
||||
|
||||
group.add(user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.random
|
||||
super(::User)
|
||||
end
|
||||
|
||||
def set_random_avatar(user)
|
||||
return if images.blank?
|
||||
return unless Faker::Boolean.boolean
|
||||
|
||||
avatar_index = Faker::Number.between(from: 0, to: images.count - 1)
|
||||
avatar_path = images[avatar_index]
|
||||
create_avatar(user, avatar_path)
|
||||
@images.delete_at(avatar_index)
|
||||
end
|
||||
|
||||
def create_avatar(user, avatar_path)
|
||||
tempfile = copy_to_tempfile(avatar_path)
|
||||
filename = "avatar#{File.extname(avatar_path)}"
|
||||
upload = UploadCreator.new(tempfile, filename, type: "avatar").create_for(user.id)
|
||||
|
||||
if upload.present? && upload.persisted?
|
||||
user.create_user_avatar
|
||||
user.user_avatar.update(custom_upload_id: upload.id)
|
||||
user.update(uploaded_avatar_id: upload.id)
|
||||
else
|
||||
STDERR.puts "Failed to upload avatar for user #{user.username}: #{avatar_path}"
|
||||
STDERR.puts upload.errors.inspect if upload
|
||||
end
|
||||
rescue
|
||||
STDERR.puts "Failed to create avatar for user #{user.username}: #{avatar_path}"
|
||||
ensure
|
||||
tempfile.close! if tempfile
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def copy_to_tempfile(source_path)
|
||||
extension = File.extname(source_path)
|
||||
tmp = Tempfile.new(['discourse-upload', extension])
|
||||
|
||||
File.open(source_path) do |source_stream|
|
||||
IO.copy_stream(source_stream, tmp)
|
||||
end
|
||||
|
||||
tmp.rewind
|
||||
tmp
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user