2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-29 04:11:04 -04:00
|
|
|
# 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")
|
|
|
|
|
2013-08-18 21:33:24 -04:00
|
|
|
unless ["profile", "development"].include? Rails.env
|
2013-05-29 04:11:04 -04:00
|
|
|
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
|
2013-05-30 02:49:11 -04:00
|
|
|
puts "Creating a random topic"
|
2013-08-27 20:43:26 -04:00
|
|
|
category = Category.where(read_restricted: false).order('random()').first
|
2013-05-30 02:49:11 -04:00
|
|
|
PostCreator.create(user, raw: sentence, title: sentence[0..50].strip, category: category.name)
|
2013-05-29 04:11:04 -04:00
|
|
|
|
|
|
|
puts "creating random reply"
|
|
|
|
PostCreator.create(user, raw: sentence, topic_id: last_topics.sample)
|
|
|
|
|
2013-08-27 20:43:26 -04:00
|
|
|
sleep 2
|
2013-05-29 04:11:04 -04:00
|
|
|
end
|