Clean up Rakefile

This commit is contained in:
Karina Kwiatek 2023-01-02 11:33:30 +01:00
parent e35354a352
commit 756ce39eea
1 changed files with 102 additions and 97 deletions

199
Rakefile
View File

@ -1,36 +1,36 @@
# frozen_string_literal: true
# Add your own tasks in files placed in lib/tasks ending in .rake, # Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__) require File.expand_path("config/application", __dir__)
Rails.application.load_tasks Rails.application.load_tasks
namespace :justask do namespace :justask do
desc "Upload to AWS" desc "Upload to AWS"
task paperclaws: :environment do task paperclaws: :environment do
if APP_CONFIG["fog"]["credentials"].nil? or APP_CONFIG["fog"]["credentials"]["provider"] != "AWS" throw "Needs fog (AWS) to be defined in justask.yml" if APP_CONFIG["fog"]["credentials"].nil? || (APP_CONFIG["fog"]["credentials"]["provider"] != "AWS")
throw "Needs fog (AWS) to be defined in justask.yml"
end
format = '%t (%c/%C) [%b>%i] %e' format = "%t (%c/%C) [%b>%i] %e"
root = "#{Rails.root}/public/system" root = Rails.root.join("/public/system")
files = Dir["#{root}/**/*.*"] files = Dir["#{root}/**/*.*"]
progress = ProgressBar.create title: 'Processing files', format: format, starting_at: 0, total: files.length progress = ProgressBar.create(title: "Processing files", format:, starting_at: 0, total: files.length)
# weird voodoo, something is causing just using "APP_CONFIG["fog"]["credentials"]" as Fog::Storage.new to cause an exception # weird voodoo, something is causing just using "APP_CONFIG["fog"]["credentials"]" as Fog::Storage.new to cause an exception
# TODO: Programmatically copy? # TODO: Programmatically copy?
credentials = { credentials = {
provider: "AWS", provider: "AWS",
aws_access_key_id: APP_CONFIG["fog"]["credentials"]["aws_access_key_id"], aws_access_key_id: APP_CONFIG["fog"]["credentials"]["aws_access_key_id"],
aws_secret_access_key: APP_CONFIG["fog"]["credentials"]["aws_secret_access_key"], aws_secret_access_key: APP_CONFIG["fog"]["credentials"]["aws_secret_access_key"],
region: APP_CONFIG["fog"]["credentials"]["region"] region: APP_CONFIG["fog"]["credentials"]["region"]
} }
fog = Fog::Storage.new credentials fog = Fog::Storage.new credentials
bucket = fog.directories.get APP_CONFIG["fog"]["directory"] bucket = fog.directories.get APP_CONFIG["fog"]["directory"]
files.each do |file| files.each do |file|
bucket.files.create key: file[root.length + 1 ... file.length], body: File.open(file), public: true bucket.files.create key: file[root.length + 1...file.length], body: File.open(file), public: true
progress.increment progress.increment
end end
@ -39,29 +39,25 @@ namespace :justask do
desc "Recount everything!" desc "Recount everything!"
task recount: :environment do task recount: :environment do
format = '%t (%c/%C) [%b>%i] %e' format = "%t (%c/%C) [%b>%i] %e"
total = User.count total = User.count
progress = ProgressBar.create title: 'Processing users', format: format, starting_at: 0, total: total progress = ProgressBar.create(title: "Processing users", format:, starting_at: 0, total:)
User.all.each do |user| User.all.each do |user|
begin begin
answered = Answer.where(user: user).count user.answered_count = Answer.where(user:).count
asked = Question.where(user: user).where(author_is_anonymous: false).count user.asked_count = Question.where(user:).where(author_is_anonymous: false).count
commented = Comment.where(user: user).count user.commented_count = Comment.where(user:).count
smiled = Smile.where(user: user).count user.smiled_count = Appendable::Reaction.where(user:).count
user.answered_count = answered
user.asked_count = asked
user.commented_count = commented
user.smiled_count = smiled
user.save! user.save!
end end
progress.increment progress.increment
end end
total = Question.count total = Question.count
progress = ProgressBar.create title: 'Processing questions', format: format, starting_at: 0, total: total progress = ProgressBar.create(title: "Processing questions", format:, starting_at: 0, total:)
Question.all.each do |question| Question.all.each do |question|
begin begin
answers = Answer.where(question: question).count answers = Answer.where(question:).count
question.answer_count = answers question.answer_count = answers
question.save! question.save!
end end
@ -69,13 +65,11 @@ namespace :justask do
end end
total = Answer.count total = Answer.count
progress = ProgressBar.create title: 'Processing answers', format: format, starting_at: 0, total: total progress = ProgressBar.create(title: "Processing answers", format:, starting_at: 0, total:)
Answer.all.each do |answer| Answer.all.each do |answer|
begin begin
smiles = Smile.where(answer: answer).count answer.comment_count = Comment.where(answer:).count
comments = Comment.where(answer: answer).count answer.smile_count = Appendable::Reaction.where(answer:).count
answer.comment_count = comments
answer.smile_count = smiles
answer.save! answer.save!
end end
progress.increment progress.increment
@ -83,10 +77,10 @@ namespace :justask do
end end
desc "Gives admin status to a user." desc "Gives admin status to a user."
task :admin, [:screen_name] => :environment do |t, args| task :admin, [:screen_name] => :environment do |_t, args|
abort "screen name required" if args[:screen_name].nil? abort "screen name required" if args[:screen_name].nil?
user = User.find_by_screen_name(args[:screen_name]) user = User.find_by(screen_name: args[:screen_name])
abort "user #{args[:screen_name]} not found" if user.nil? abort "user #{args[:screen_name]} not found" if user.nil?
user.add_role :administrator user.add_role :administrator
@ -94,10 +88,10 @@ namespace :justask do
end end
desc "Removes admin status from a user." desc "Removes admin status from a user."
task :deadmin, [:screen_name] => :environment do |t, args| task :deadmin, [:screen_name] => :environment do |_t, args|
abort "screen name required" if args[:screen_name].nil? abort "screen name required" if args[:screen_name].nil?
user = User.find_by_screen_name(args[:screen_name]) user = User.find_by(screen_name: args[:screen_name])
abort "user #{args[:screen_name]} not found" if user.nil? abort "user #{args[:screen_name]} not found" if user.nil?
user.remove_role :administrator user.remove_role :administrator
@ -105,10 +99,10 @@ namespace :justask do
end end
desc "Gives moderator status to a user." desc "Gives moderator status to a user."
task :mod, [:screen_name] => :environment do |t, args| task :mod, [:screen_name] => :environment do |_t, args|
abort "screen name required" if args[:screen_name].nil? abort "screen name required" if args[:screen_name].nil?
user = User.find_by_screen_name(args[:screen_name]) user = User.find_by(screen_name: args[:screen_name])
abort "user #{args[:screen_name]} not found" if user.nil? abort "user #{args[:screen_name]} not found" if user.nil?
user.add_role :moderator user.add_role :moderator
@ -116,10 +110,10 @@ namespace :justask do
end end
desc "Removes moderator status from a user." desc "Removes moderator status from a user."
task :demod, [:screen_name] => :environment do |t, args| task :demod, [:screen_name] => :environment do |_t, args|
abort "screen name required" if args[:screen_name].nil? abort "screen name required" if args[:screen_name].nil?
user = User.find_by_screen_name(args[:screen_name]) user = User.find_by(screen_name: args[:screen_name])
abort "user #{args[:screen_name]} not found" if user.nil? abort "user #{args[:screen_name]} not found" if user.nil?
user.remove_role :moderator user.remove_role :moderator
@ -127,83 +121,92 @@ namespace :justask do
end end
desc "Hits an user with the banhammer." desc "Hits an user with the banhammer."
task :permanently_ban, [:screen_name, :reason] => :environment do |t, args| task :permanently_ban, %i[screen_name reason] => :environment do |_t, args|
fail "screen name required" if args[:screen_name].nil? raise "screen name required" if args[:screen_name].nil?
user = User.find_by_screen_name(args[:screen_name])
fail "user #{args[:screen_name]} not found" if user.nil? user = User.find_by(screen_name: args[:screen_name])
raise "user #{args[:screen_name]} not found" if user.nil?
UseCase::User::Ban.call( UseCase::User::Ban.call(
target_user_id: user.id, target_user_id: user.id,
expiry: nil, expiry: nil,
reason: args[:reason], reason: args[:reason]
) )
puts "#{user.screen_name} got hit by\033[5m YE OLDE BANHAMMER\033[0m!!1!" puts "#{user.screen_name} got hit by\033[5m YE OLDE BANHAMMER\033[0m!!1!"
end end
desc "Hits an user with the banhammer for one day." desc "Hits an user with the banhammer for one day."
task :ban, [:screen_name, :reason] => :environment do |t, args| task :ban, %i[screen_name reason] => :environment do |_t, args|
fail "screen name required" if args[:screen_name].nil? raise "screen name required" if args[:screen_name].nil?
user = User.find_by_screen_name(args[:screen_name])
user = User.find_by(screen_name: args[:screen_name])
UseCase::User::Ban.call( UseCase::User::Ban.call(
target_user_id: user.id, target_user_id: user.id,
expiry: DateTime.current + 1, expiry: DateTime.current + 1,
reason: args[:reason], reason: args[:reason]
) )
puts "#{user.screen_name} got hit by\033[5m YE OLDE BANHAMMER\033[0m!!1!" puts "#{user.screen_name} got hit by\033[5m YE OLDE BANHAMMER\033[0m!!1!"
end end
desc "Hits an user with the banhammer for one week." desc "Hits an user with the banhammer for one week."
task :week_ban, [:screen_name, :reason] => :environment do |t, args| task :week_ban, %i[screen_name reason] => :environment do |_t, args|
fail "screen name required" if args[:screen_name].nil? raise "screen name required" if args[:screen_name].nil?
user = User.find_by_screen_name(args[:screen_name])
user = User.find_by(screen_name: args[:screen_name])
UseCase::User::Ban.call( UseCase::User::Ban.call(
target_user_id: user.id, target_user_id: user.id,
expiry: DateTime.current + 7, expiry: DateTime.current + 7,
reason: args[:reason], reason: args[:reason]
) )
puts "#{user.screen_name} got hit by\033[5m YE OLDE BANHAMMER\033[0m!!1!" puts "#{user.screen_name} got hit by\033[5m YE OLDE BANHAMMER\033[0m!!1!"
end end
desc "Hits an user with the banhammer for one month." desc "Hits an user with the banhammer for one month."
task :month_ban, [:screen_name, :reason] => :environment do |t, args| task :month_ban, %i[screen_name reason] => :environment do |_t, args|
fail "screen name required" if args[:screen_name].nil? raise "screen name required" if args[:screen_name].nil?
user = User.find_by_screen_name(args[:screen_name])
user = User.find_by(screen_name: args[:screen_name])
UseCase::User::Ban.call( UseCase::User::Ban.call(
target_user_id: user.id, target_user_id: user.id,
expiry: DateTime.current + 30, expiry: DateTime.current + 30,
reason: args[:reason], reason: args[:reason]
) )
puts "#{user.screen_name} got hit by\033[5m YE OLDE BANHAMMER\033[0m!!1!" puts "#{user.screen_name} got hit by\033[5m YE OLDE BANHAMMER\033[0m!!1!"
end end
desc "Hits an user with the banhammer for one year." desc "Hits an user with the banhammer for one year."
task :year_ban, [:screen_name, :reason] => :environment do |t, args| task :year_ban, %i[screen_name reason] => :environment do |_t, args|
fail "screen name required" if args[:screen_name].nil? raise "screen name required" if args[:screen_name].nil?
user = User.find_by_screen_name(args[:screen_name])
user = User.find_by(screen_name: args[:screen_name])
UseCase::User::Ban.call( UseCase::User::Ban.call(
target_user_id: user.id, target_user_id: user.id,
expiry: DateTime.current + 365, expiry: DateTime.current + 365,
reason: args[:reason], reason: args[:reason]
) )
puts "#{user.screen_name} got hit by\033[5m YE OLDE BANHAMMER\033[0m!!1!" puts "#{user.screen_name} got hit by\033[5m YE OLDE BANHAMMER\033[0m!!1!"
end end
desc "Hits an user with the banhammer for one aeon." desc "Hits an user with the banhammer for one aeon."
task :aeon_ban, [:screen_name, :reason] => :environment do |t, args| task :aeon_ban, %i[screen_name reason] => :environment do |_t, args|
fail "screen name required" if args[:screen_name].nil? raise "screen name required" if args[:screen_name].nil?
user = User.find_by_screen_name(args[:screen_name])
user = User.find_by(screen_name: args[:screen_name])
UseCase::User::Ban.call( UseCase::User::Ban.call(
target_user_id: user.id, target_user_id: user.id,
expiry: DateTime.current + 365_000_000_000, expiry: DateTime.current + 365_000_000_000,
reason: args[:reason], reason: args[:reason]
) )
puts "#{user.screen_name} got hit by\033[5m YE OLDE BANHAMMER\033[0m!!1!" puts "#{user.screen_name} got hit by\033[5m YE OLDE BANHAMMER\033[0m!!1!"
end end
desc "Removes banned status from an user." desc "Removes banned status from an user."
task :unban, [:screen_name] => :environment do |t, args| task :unban, [:screen_name] => :environment do |_t, args|
fail "screen name required" if args[:screen_name].nil? raise "screen name required" if args[:screen_name].nil?
user = User.find_by_screen_name(args[:screen_name])
fail "user #{args[:screen_name]} not found" if user.nil? user = User.find_by(screen_name: args[:screen_name])
raise "user #{args[:screen_name]} not found" if user.nil?
UseCase::User::Unban.call(user.id) UseCase::User::Unban.call(user.id)
puts "#{user.screen_name} is no longer banned." puts "#{user.screen_name} is no longer banned."
end end
@ -211,7 +214,7 @@ namespace :justask do
desc "Lists all users." desc "Lists all users."
task lusers: :environment do task lusers: :environment do
User.all.each do |u| User.all.each do |u|
puts "#{sprintf "%3d", u.id}. #{u.screen_name}" puts "#{format '%3d', u.id}. #{u.screen_name}"
end end
end end
@ -219,8 +222,8 @@ namespace :justask do
task remove_stale: :environment do task remove_stale: :environment do
puts "Removing stale users…" puts "Removing stale users…"
removed = User.where(confirmed_at: nil) removed = User.where(confirmed_at: nil)
.where("confirmation_sent_at < ?", DateTime.now.utc - 3.months) .where("confirmation_sent_at < ?", DateTime.now.utc - 3.months)
.destroy_all.count .destroy_all.count
puts "Removed #{removed} users" puts "Removed #{removed} users"
end end
@ -228,22 +231,24 @@ namespace :justask do
task loners: :environment do task loners: :environment do
people = {} people = {}
Question.all.each do |q| Question.all.each do |q|
if q.author_is_anonymous && !%w[justask retrospring_exporter].include?(q.author_identifier) next unless q.author_is_anonymous && %w[justask retrospring_exporter].exclude?(q.author_identifier)
q.answers.each do |a|
if q.user == a.user q.answers.each do |a|
people[q.user.screen_name] ||= 0 next unless q.user == a.user
people[q.user.screen_name] += 1
puts "#{q.user.screen_name} -- answer id #{a.id}" people[q.user.screen_name] ||= 0
end people[q.user.screen_name] += 1
end puts "#{q.user.screen_name} -- answer id #{a.id}"
end end
end end
max = 0 max = 0
res = [] res = []
people.each { |k, v| max = v if v > max } people.each do |k, v|
people.each { |k, v| res << k if v == max } max = v if v > max
if res.length == 0 res << k if v == max
end
if res.length.zero?
puts "No one? I hope you're just on the development session." puts "No one? I hope you're just on the development session."
else else
puts res.length == 1 ? "And the winner is..." : "And the winners are..." puts res.length == 1 ? "And the winner is..." : "And the winners are..."
@ -256,33 +261,33 @@ end
namespace :db do namespace :db do
namespace :schema do namespace :schema do
task :create_timestampid_function do task create_timestampid_function: :environment do
conn = ActiveRecord::Base.connection conn = ActiveRecord::Base.connection
have_func = conn.execute("SELECT EXISTS(SELECT * FROM pg_proc WHERE proname = 'gen_timestamp_id');").values.first.first have_func = conn.execute("SELECT EXISTS(SELECT * FROM pg_proc WHERE proname = 'gen_timestamp_id');").values.first.first
next if have_func next if have_func
statement = File.read(File.join(__dir__, 'db/migrate/20200704163504_use_timestamped_ids.rb')).match(/<~SQL(?<stmt>.+)SQL$/m)[:stmt] statement = File.read(File.join(__dir__, "db/migrate/20200704163504_use_timestamped_ids.rb")).match(/<~SQL(?<stmt>.+)SQL$/m)[:stmt]
conn.execute(statement) conn.execute(statement)
end end
task :create_id_sequences do task create_id_sequences: :environment do
conn = ActiveRecord::Base.connection conn = ActiveRecord::Base.connection
# required for the timestampid function to work properly # required for the timestampid function to work properly
%i[questions answers comments smiles comment_smiles users mute_rules].each do |tbl| %i[questions answers comments users mute_rules].each do |tbl|
conn.execute("CREATE SEQUENCE IF NOT EXISTS #{tbl}_id_seq") conn.execute("CREATE SEQUENCE IF NOT EXISTS #{tbl}_id_seq")
end end
end end
end end
# create timestampid before loading schema # create timestampid before loading schema
Rake::Task['db:schema:load'].enhance ['db:schema:create_timestampid_function'] Rake::Task["db:schema:load"].enhance ["db:schema:create_timestampid_function"]
Rake::Task['db:test:load_schema'].enhance ['db:schema:create_timestampid_function'] Rake::Task["db:test:load_schema"].enhance ["db:schema:create_timestampid_function"]
# create id_sequences after loading schema # create id_sequences after loading schema
Rake::Task['db:schema:load'].enhance do Rake::Task["db:schema:load"].enhance do
Rake::Task['db:schema:create_id_sequences'].invoke Rake::Task["db:schema:create_id_sequences"].invoke
end end
Rake::Task['db:test:load_schema'].enhance do Rake::Task["db:test:load_schema"].enhance do
Rake::Task['db:schema:create_id_sequences'].invoke Rake::Task["db:schema:create_id_sequences"].invoke
end end
end end