discourse_dev 0.0.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -0
  3. data/auth/app/views/fake_discourse_connect/form.html.erb +120 -0
  4. data/auth/plugin.rb +219 -0
  5. data/avatars/03F55412-DE8A-4F83-AAA6-D67EE5CE48DA-200w.jpeg +0 -0
  6. data/avatars/1C4EEDC2-FE9C-40B3-A2C9-A038873EE692-200w.jpeg +0 -0
  7. data/avatars/26CFEFB3-21C8-49FC-8C19-8E6A62B6D2E0-200w.jpeg +0 -0
  8. data/avatars/282A12CA-E0D7-4011-8BDD-1FAFAAB035F7-200w.jpeg +0 -0
  9. data/avatars/2DDDE973-40EC-4004-ABC0-73FD4CD6D042-200w.jpeg +0 -0
  10. data/avatars/344CFC24-61FB-426C-B3D1-CAD5BCBD3209-200w.jpeg +0 -0
  11. data/avatars/852EC6E1-347C-4187-9D42-DF264CCF17BF-200w.jpeg +0 -0
  12. data/avatars/A7299C8E-CEFC-47D9-939A-3C8CA0EA4D13-200w.jpeg +0 -0
  13. data/avatars/AEF44435-B547-4B84-A2AE-887DFAEE6DDF-200w.jpeg +0 -0
  14. data/avatars/B3CF5288-34B0-4A5E-9877-5965522529D6-200w.jpeg +0 -0
  15. data/avatars/BA0CB1F2-8C79-4376-B13B-DD5FB8772537-200w.jpeg +0 -0
  16. data/avatars/E0B4CAB3-F491-4322-BEF2-208B46748D4A-200w.jpeg +0 -0
  17. data/avatars/FBEBF655-4886-455A-A4A4-D62B77DD419B-200w.jpeg +0 -0
  18. data/config/dev.yml +38 -0
  19. data/config/locales/client.en.yml +6 -0
  20. data/{lib/faker/locales/en.yml → config/locales/faker.en.yml} +1 -1
  21. data/discourse_dev.gemspec +1 -0
  22. data/lib/discourse_dev.rb +49 -4
  23. data/lib/discourse_dev/category.rb +3 -3
  24. data/lib/discourse_dev/config.rb +101 -29
  25. data/lib/discourse_dev/group.rb +4 -5
  26. data/lib/discourse_dev/post.rb +64 -5
  27. data/lib/discourse_dev/record.rb +6 -1
  28. data/lib/discourse_dev/tag.rb +2 -2
  29. data/lib/discourse_dev/tasks/dev.rake +5 -1
  30. data/lib/discourse_dev/tasks/populate.rake +5 -0
  31. data/lib/discourse_dev/topic.rb +52 -10
  32. data/lib/discourse_dev/user.rb +58 -7
  33. data/lib/discourse_dev/version.rb +1 -1
  34. data/lib/faker/discourse_markdown.rb +96 -0
  35. metadata +36 -4
  36. data/lib/discourse_dev/config.yml +0 -4
data/lib/discourse_dev.rb CHANGED
@@ -2,12 +2,57 @@
2
2
 
3
3
  require 'i18n'
4
4
 
5
- Dir[File.dirname(__FILE__) + '/**/*.rb'].each {|file| require file }
6
-
7
- I18n.load_path += Dir[File.join(__dir__, 'faker', 'locales', '**/*.yml')]
8
- I18n.reload! if I18n.backend.initialized?
5
+ Dir[File.dirname(__FILE__) + '/**/*.rb'].each { |file| require file }
9
6
 
10
7
  module DiscourseDev
11
8
  require 'discourse_dev/railtie'
12
9
  require 'discourse_dev/engine'
10
+
11
+ def self.auth_plugin_enabled?
12
+ config.auth_plugin_enabled
13
+ end
14
+
15
+ def self.config
16
+ @config ||= Config.new
17
+ end
18
+
19
+ def self.auth_plugin
20
+ return unless auth_plugin_enabled?
21
+
22
+ @auth_plugin ||= begin
23
+ path = File.join(root, 'auth', 'plugin.rb')
24
+ source = File.read(path)
25
+ metadata = Plugin::Metadata.parse(source)
26
+ Plugin::Instance.new(metadata, path)
27
+ end
28
+ end
29
+
30
+ def self.settings_file
31
+ File.join(root, "config", "settings.yml")
32
+ end
33
+
34
+ def self.client_locale_files(locale_str)
35
+ Dir[File.join(root, "config", "locales", "client*.#{locale_str}.yml")]
36
+ end
37
+
38
+ def self.root
39
+ File.expand_path("..", __dir__)
40
+ end
41
+ end
42
+
43
+ require "active_record/database_configurations"
44
+
45
+ ActiveRecord::Tasks::DatabaseTasks.module_eval do
46
+ alias_method :rails_each_current_configuration, :each_current_configuration
47
+
48
+ private
49
+ def each_current_configuration(environment, name = nil)
50
+ rails_each_current_configuration(environment, name) { |db_config|
51
+ next if environment == "development" &&
52
+ ENV["SKIP_TEST_DATABASE"] == "1" &&
53
+ db_config.configuration_hash[:database] != "discourse_development"
54
+
55
+ yield db_config
56
+ }
57
+ end
13
58
  end
@@ -7,8 +7,8 @@ require 'faker'
7
7
  module DiscourseDev
8
8
  class Category < Record
9
9
 
10
- def initialize(count = DEFAULT_COUNT)
11
- super(::Category, count)
10
+ def initialize
11
+ super(::Category, DiscourseDev.config.category[:count])
12
12
  @parent_category_ids = ::Category.where(parent_category_id: nil).pluck(:id)
13
13
  end
14
14
 
@@ -36,7 +36,7 @@ module DiscourseDev
36
36
  def permissions
37
37
  return @permissions if @permissions.present?
38
38
  return { everyone: :full } if Faker::Boolean.boolean(true_ratio: 0.75)
39
-
39
+
40
40
  permission = {}
41
41
  group = Group.random
42
42
  permission[group.id] = Faker::Number.between(from: 1, to: 3)
@@ -1,77 +1,149 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rails'
4
+ require 'highline/import'
4
5
 
5
6
  module DiscourseDev
6
7
  class Config
7
- attr_reader :config, :default_config
8
+ attr_reader :config, :file_path
8
9
 
9
10
  def initialize
10
- @default_config = YAML.load_file(File.join(File.expand_path(__dir__), "config.yml"))
11
- file_path = File.join(Rails.root, "config", "dev.yml")
11
+ default_file_path = File.join(DiscourseDev.root, "config", "dev.yml")
12
+ @file_path = File.join(Rails.root, "config", "dev.yml")
13
+ default_config = YAML.load_file(default_file_path)
12
14
 
13
15
  if File.exists?(file_path)
14
- @config = YAML.load_file(file_path)
16
+ user_config = YAML.load_file(file_path)
15
17
  else
16
- @config = {}
18
+ puts "I did no detect a custom `config/dev.yml` file, creating one for you where you can amend defaults."
19
+ FileUtils.cp(default_file_path, file_path)
20
+ user_config = {}
17
21
  end
22
+
23
+ @config = default_config.deep_merge(user_config).deep_symbolize_keys
18
24
  end
19
25
 
20
26
  def update!
21
27
  update_site_settings
22
28
  create_admin_user
29
+ create_new_user
23
30
  set_seed
24
31
  end
25
32
 
33
+ private
34
+
26
35
  def update_site_settings
27
36
  puts "Updating site settings..."
28
37
 
29
- site_settings = config["site_settings"] || {}
38
+ site_settings = config[:site_settings] || {}
30
39
 
31
40
  site_settings.each do |key, value|
32
41
  puts "#{key} = #{value}"
33
42
  SiteSetting.set(key, value)
34
43
  end
35
44
 
36
- keys = site_settings.keys
37
-
38
- default_config["site_settings"].each do |key, value|
39
- next if keys.include?(key)
40
-
41
- puts "#{key} = #{value}"
42
- SiteSetting.set(key, value)
43
- end
44
-
45
45
  SiteSetting.refresh!
46
46
  end
47
47
 
48
48
  def create_admin_user
49
49
  puts "Creating default admin user account..."
50
50
 
51
- settings = config["admin"]
51
+ settings = config[:admin]
52
+
53
+ if settings.present?
54
+ create_admin_user_from_settings(settings)
55
+ else
56
+ create_admin_user_from_input
57
+ end
58
+ end
59
+
60
+ def create_new_user
61
+ settings = config[:new_user]
52
62
 
53
63
  if settings.present?
54
- email = settings["email"]
64
+ email = settings[:email] || "new_user@example.com"
55
65
 
56
- admin = ::User.create!(
66
+ new_user = ::User.create!(
57
67
  email: email,
58
- username: settings["username"] || UserNameSuggester.suggest(email),
59
- password: settings["password"]
68
+ username: settings[:username] || UserNameSuggester.suggest(email)
60
69
  )
61
- admin.grant_admin!
62
- if admin.trust_level < 1
63
- admin.change_trust_level!(1)
64
- end
65
- admin.email_tokens.update_all confirmed: true
66
- admin.activate
67
- else
68
- Rake::Task['admin:create'].invoke
70
+ new_user.email_tokens.update_all confirmed: true
71
+ new_user.activate
69
72
  end
70
73
  end
71
74
 
72
75
  def set_seed
73
- seed = config["seed"] || default_config["seed"] || 1
76
+ seed = self.seed || 1
74
77
  Faker::Config.random = Random.new(seed)
75
78
  end
79
+
80
+ def start_date
81
+ DateTime.parse(config[:start_date])
82
+ end
83
+
84
+ def method_missing(name)
85
+ config[name.to_sym]
86
+ end
87
+
88
+ def create_admin_user_from_settings(settings)
89
+ email = settings[:email]
90
+
91
+ admin = ::User.create!(
92
+ email: email,
93
+ username: settings[:username] || UserNameSuggester.suggest(email),
94
+ password: settings[:password]
95
+ )
96
+ admin.grant_admin!
97
+ if admin.trust_level < 1
98
+ admin.change_trust_level!(1)
99
+ end
100
+ admin.email_tokens.update_all confirmed: true
101
+ admin.activate
102
+ end
103
+
104
+ def create_admin_user_from_input
105
+ begin
106
+ email = ask("Email: ")
107
+ password = ask("Password (optional, press ENTER to skip): ")
108
+ username = UserNameSuggester.suggest(email)
109
+
110
+ admin = ::User.new(
111
+ email: email,
112
+ username: username
113
+ )
114
+
115
+ if password.present?
116
+ admin.password = password
117
+ else
118
+ puts "Once site is running use https://localhost:9292/user/#{username}/become to access the account in development"
119
+ end
120
+
121
+ admin.name = ask("Full name: ") if SiteSetting.full_name_required
122
+ saved = admin.save
123
+
124
+ if saved
125
+ File.open(file_path, 'a') do | file|
126
+ file.puts("admin:")
127
+ file.puts(" username: #{admin.username}")
128
+ file.puts(" email: #{admin.email}")
129
+ file.puts(" password: #{password}") if password.present?
130
+ end
131
+ else
132
+ say(admin.errors.full_messages.join("\n"))
133
+ end
134
+ end while !saved
135
+
136
+ admin.active = true
137
+ admin.save
138
+
139
+ admin.grant_admin!
140
+ if admin.trust_level < 1
141
+ admin.change_trust_level!(1)
142
+ end
143
+ admin.email_tokens.update_all confirmed: true
144
+ admin.activate
145
+
146
+ say("\nAdmin account created successfully with username #{admin.username}")
147
+ end
76
148
  end
77
149
  end
@@ -7,10 +7,8 @@ require 'faker'
7
7
  module DiscourseDev
8
8
  class Group < Record
9
9
 
10
- DEFAULT_COUNT = 15.freeze
11
-
12
- def initialize(count = DEFAULT_COUNT)
13
- super(::Group, count)
10
+ def initialize
11
+ super(::Group, DiscourseDev.config.group[:count])
14
12
  end
15
13
 
16
14
  def data
@@ -18,7 +16,8 @@ module DiscourseDev
18
16
  name: Faker::Discourse.unique.group,
19
17
  public_exit: Faker::Boolean.boolean,
20
18
  public_admission: Faker::Boolean.boolean,
21
- primary_group: Faker::Boolean.boolean
19
+ primary_group: Faker::Boolean.boolean,
20
+ created_at: Faker::Time.between(from: DiscourseDev.config.start_date, to: DateTime.now),
22
21
  }
23
22
  end
24
23
 
@@ -8,43 +8,102 @@ module DiscourseDev
8
8
 
9
9
  attr_reader :topic
10
10
 
11
- def initialize(topic, count = DEFAULT_COUNT)
11
+ def initialize(topic, count)
12
12
  super(::Post, count)
13
13
  @topic = topic
14
14
 
15
15
  category = topic.category
16
+ @max_likes_count = DiscourseDev.config.post[:max_likes_count]
16
17
  unless category.groups.blank?
17
18
  group_ids = category.groups.pluck(:id)
18
19
  @user_ids = ::GroupUser.where(group_id: group_ids).pluck(:user_id)
19
20
  @user_count = @user_ids.count
21
+ @max_likes_count = @user_count - 1
20
22
  end
21
23
  end
22
24
 
23
25
  def data
24
26
  {
25
27
  topic_id: topic.id,
26
- raw: Faker::Markdown.sandwich(sentences: 5),
27
- skip_validations: true
28
+ raw: Faker::DiscourseMarkdown.sandwich(sentences: 5),
29
+ created_at: Faker::Time.between(from: topic.last_posted_at, to: DateTime.now),
30
+ skip_validations: true,
31
+ skip_guardian: true
28
32
  }
29
33
  end
30
34
 
31
35
  def create!
32
- PostCreator.new(user, data).create!
36
+ user = self.user
37
+ data = Faker::DiscourseMarkdown.with_user(user.id) { self.data }
38
+ post = PostCreator.new(user, data).create!
39
+ topic.reload
40
+ generate_likes(post)
41
+ end
42
+
43
+ def generate_likes(post)
44
+ user_ids = [post.user_id]
45
+
46
+ Faker::Number.between(from: 0, to: @max_likes_count).times do
47
+ user = self.user
48
+ next if user_ids.include?(user.id)
49
+
50
+ PostActionCreator.new(user, post, PostActionType.types[:like], created_at: Faker::Time.between(from: post.created_at, to: DateTime.now)).perform
51
+ user_ids << user.id
52
+ end
33
53
  end
34
54
 
35
55
  def user
36
56
  return User.random if topic.category.groups.blank?
37
57
  return Discourse.system_user if @user_ids.blank?
38
-
58
+
39
59
  position = Faker::Number.between(from: 0, to: @user_count - 1)
40
60
  ::User.find(@user_ids[position])
41
61
  end
42
62
 
43
63
  def populate!
64
+ generate_likes(topic.first_post)
65
+
44
66
  @count.times do |i|
45
67
  @index = i
46
68
  create!
47
69
  end
48
70
  end
71
+
72
+ def self.add_replies!(args)
73
+ if !args[:topic_id]
74
+ puts "Topic ID is required. Aborting."
75
+ return
76
+ end
77
+
78
+ if !::Topic.find_by_id(args[:topic_id])
79
+ puts "Topic ID does not match topic in DB, aborting."
80
+ return
81
+ end
82
+
83
+ topic = ::Topic.find_by_id(args[:topic_id])
84
+ count = args[:count] ? args[:count].to_i : 50
85
+
86
+ puts "Creating #{count} replies in '#{topic.title}'"
87
+
88
+ count.times do |i|
89
+ @index = i
90
+ begin
91
+ user = User.random
92
+ reply = Faker::DiscourseMarkdown.with_user(user.id) do
93
+ {
94
+ topic_id: topic.id,
95
+ raw: Faker::DiscourseMarkdown.sandwich(sentences: 5),
96
+ skip_validations: true
97
+ }
98
+ end
99
+ PostCreator.new(user, reply).create!
100
+ rescue ActiveRecord::RecordNotSaved => e
101
+ puts e
102
+ end
103
+ end
104
+
105
+ puts "Done!"
106
+ end
107
+
49
108
  end
50
109
  end
@@ -11,7 +11,12 @@ module DiscourseDev
11
11
  attr_reader :model, :type
12
12
 
13
13
  def initialize(model, count = DEFAULT_COUNT)
14
- Faker::Discourse.unique.clear
14
+ @@initialized ||= begin
15
+ Faker::Discourse.unique.clear
16
+ RateLimiter.disable
17
+ true
18
+ end
19
+
15
20
  @model = model
16
21
  @type = model.to_s
17
22
  @count = count
@@ -7,8 +7,8 @@ require 'faker'
7
7
  module DiscourseDev
8
8
  class Tag < Record
9
9
 
10
- def initialize(count = DEFAULT_COUNT)
11
- super(::Tag, count)
10
+ def initialize
11
+ super(::Tag, DiscourseDev.config.tag[:count])
12
12
  end
13
13
 
14
14
  def data
@@ -4,6 +4,9 @@ def check_environment!
4
4
  if !Rails.env.development?
5
5
  raise "Database commands are only supported in development environment"
6
6
  end
7
+
8
+ ENV['SKIP_TEST_DATABASE'] = "1"
9
+ ENV['SKIP_MULTISITE'] = "1"
7
10
  end
8
11
 
9
12
  desc 'Run db:migrate:reset task and populate sample content for development environment'
@@ -17,11 +20,12 @@ end
17
20
 
18
21
  desc 'Initialize development environment'
19
22
  task 'dev:config' => ['db:load_config'] do |_, args|
20
- DiscourseDev::Config.new.update!
23
+ DiscourseDev.config.update!
21
24
  end
22
25
 
23
26
  desc 'Populate sample content for development environment'
24
27
  task 'dev:populate' => ['db:load_config'] do |_, args|
28
+ system("redis-cli flushall")
25
29
  Rake::Task['groups:populate'].invoke
26
30
  Rake::Task['users:populate'].invoke
27
31
  Rake::Task['categories:populate'].invoke
@@ -24,3 +24,8 @@ desc 'Creates sample topics'
24
24
  task 'topics:populate' => ['db:load_config'] do |_, args|
25
25
  DiscourseDev::Topic.populate!
26
26
  end
27
+
28
+ desc 'Add replies to a topic'
29
+ task 'replies:populate', [:topic_id, :count] => ['db:load_config'] do |_, args|
30
+ DiscourseDev::Post.add_replies!(args)
31
+ end