DEV: Add rake task for generating avatars from SSO

This commit is contained in:
Gerhard Schlager 2023-08-07 12:23:58 +02:00 committed by Gerhard Schlager
parent df7dab9dce
commit b2fee68b3f
3 changed files with 71 additions and 0 deletions

View File

@ -118,6 +118,7 @@ class UserAvatar < ActiveRecord::Base
max_file_size: SiteSetting.max_image_size_kb.kilobytes,
tmp_file_name: "sso-avatar",
follow_redirect: true,
skip_rate_limit: !!options&.fetch(:skip_rate_limit, false),
)
return unless tempfile

View File

@ -117,6 +117,7 @@ class UserProfile < ActiveRecord::Base
max_file_size: SiteSetting.max_image_size_kb.kilobytes,
tmp_file_name: "sso-profile-background",
follow_redirect: true,
skip_rate_limit: true,
)
return unless tempfile

View File

@ -573,3 +573,72 @@ task "import:update_first_post_created_at" => :environment do
log "Done"
end
desc "Update avatars from external_avatar_url in SSO records"
task "import:update_avatars_from_sso" => :environment do
log "Updating avatars from SSO records"
sql = <<~SQL
SELECT user_id, external_avatar_url
FROM single_sign_on_records s
WHERE NOT EXISTS (
SELECT 1
FROM user_avatars a
WHERE a.user_id = s.user_id
)
SQL
queue = SizedQueue.new(1000)
threads = []
threads << Thread.new do ||
DB.query_each(sql) { |row| queue << { user_id: row.user_id, url: row.external_avatar_url } }
queue.close
end
max_count = DB.query_single(<<~SQL).first
SELECT COUNT(*)
FROM single_sign_on_records s
WHERE NOT EXISTS (
SELECT 1
FROM user_avatars a
WHERE a.user_id = s.user_id
)
SQL
status_queue = Queue.new
status_thread =
Thread.new do
error_count = 0
current_count = 0
while !(status = status_queue.pop).nil?
error_count += 1 if !status
current_count += 1
print "\r%7d / %7d (%d errors)" % [current_count, max_count, error_count]
end
end
20.times do
threads << Thread.new do
while row = queue.pop
begin
UserAvatar.import_url_for_user(
row[:url],
User.find(row[:user_id]),
override_gravatar: true,
skip_rate_limit: true,
)
status_queue << true
rescue StandardError
status_queue << false
end
end
end
end
threads.each(&:join)
status_queue.close
status_thread.join
end