forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_simulator.rb
More file actions
59 lines (44 loc) · 1.4 KB
/
user_simulator.rb
File metadata and controls
59 lines (44 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# used during local testing, simulates a user active on the site.
#
# by default 1 new topic every 30 sec, 1 reply to last topic every 30 secs
require 'optparse'
require 'gabbler'
user_id = nil
def sentence
@gabbler ||= Gabbler.new.tap do |gabbler|
story = File.read(File.dirname(__FILE__) + "/alice.txt")
gabbler.learn(story)
end
sentence = ""
until sentence.length > 800 do
sentence << @gabbler.sentence
sentence << "\n"
end
sentence
end
OptionParser.new do |opts|
opts.banner = "Usage: ruby user_simulator.rb [options]"
opts.on("-u", "--user NUMBER", "user id") do |u|
user_id = u.to_i
end
end.parse!
unless user_id
puts "user must be specified"
exit
end
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
unless ["profile", "development"].include? Rails.env
puts "Bad idea to run a script that inserts random posts in any non development environment"
exit
end
user = User.find(user_id)
last_topics = Topic.order('id desc').limit(10).pluck(:id)
puts "Simulating activity for user id #{user.id}: #{user.name}"
while true
puts "Creating a random topic"
category = Category.where(read_restricted: false).order('random()').first
PostCreator.create(user, raw: sentence, title: sentence[0..50].strip, category: category.name)
puts "creating random reply"
PostCreator.create(user, raw: sentence, topic_id: last_topics.sample)
sleep 2
end