Skip to content

Instantly share code, notes, and snippets.

@unarist
Last active July 31, 2017 00:44
Show Gist options
  • Save unarist/961ec6af5243bcaa8bf2608bd97ad3f0 to your computer and use it in GitHub Desktop.
Save unarist/961ec6af5243bcaa8bf2608bd97ad3f0 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
namespace :dev do
desc "Seed some accounts"
task seed: :environment do
domain = ENV['LOCAL_DOMAIN'] || Rails.configuration.x.local_domain
admin = Account.find_local('admin')
accounts = 20.times.map { |i| Account.find_or_create_by(username: "test#{i}") }
accounts.each do |account|
User.where(email: "#{account.username}@#{domain}").first_or_create_by(email: "#{account.username}@#{domain}", password: 'mastodonadmin', password_confirmation: 'mastodonadmin', confirmed_at: Time.now.utc, admin: false, account: account)
end
if admin.statuses.empty? then
FollowService.new.call(admin, accounts[1].acct)
statuses = 50.times.map { |i|
PostStatusService.new.call(accounts[1], "user: #{i}")
PostStatusService.new.call(admin, i.to_s)
}
statuses.take(10).each { |s| FavouriteService.new.call(accounts[2], s)}
statuses.drop(10).take(2).each { |s| ReblogService.new.call(accounts[2], s)}
prev = PostStatusService.new.call(accounts[3], 'hello')
prev = PostStatusService.new.call(accounts[4], 'reply', prev)
PostStatusService.new.call(accounts[3], 'again', prev)
PostStatusService.new.call(accounts[5], '@admin mention')
PostStatusService.new.call(accounts[5], '@admin direct', nil, visibility: 'direct')
end
end
desc "Simulate many actions"
task simulate: :environment do
1000.times do |i|
Rnd.reset if (i % 100) == 0
n = rand(100)
if n < 5 then
count = (Account.count * 0.5).floor
targets = Account.order('random()').limit(count).to_a
acc = Account.find_or_create_by(username: "acc#{Time.now.to_i}")
targets.each do |target|
#FollowService.new.call(acc, target.acc)
acc.follow!(target)
end
elsif n < 20
ReblogService.new.call(Rnd.account, Rnd.public_status)
elsif n < 30
FavouriteService.new.call(Rnd.account, Rnd.public_status)
elsif n < 60
target = Rnd.public_status
PostStatusService.new.call(Rnd.account, "@#{target.account.username} hoge", target, visibility: Rnd.public_visibility)
elsif n < 70
targets = rand(4).times.map { "@" + Rnd.account.username }.join(' ')
PostStatusService.new.call(Rnd.account, "#{targets} hoge")
else
PostStatusService.new.call(Rnd.account, "hoge", nil, visibility: Rnd.visibility)
end
end
end
end
class Rnd
class << self
def cached(query)
key = query.to_sql
@cache ||= {}
@cache[key] ||= query.size
query.offset(rand(@cache[key])).first
end
def reset
@cache = {}
end
def account
cached(Account.all)
end
def public_status
cached(Status.where(visibility: [:public, :unlisted]))
end
def visibility
[:public, :unlisted, :private, :direct].sample
end
def public_visibility
[:public, :unlisted].sample
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment