Sync Mastodon IDs/profiles to local accounts
Add a new service to import some data from Mastodon accounts: * Find users by username, store Mastodon account ID in local db when found * Import display name (don't overwrite existing) * Import avatar (don't overwrite existing)
This commit is contained in:
43
app/services/user_manager/import_remote_avatar.rb
Normal file
43
app/services/user_manager/import_remote_avatar.rb
Normal file
@@ -0,0 +1,43 @@
|
||||
module UserManager
|
||||
class ImportRemoteAvatar < UserManagerService
|
||||
def initialize(user:, avatar_url:)
|
||||
@user = user
|
||||
@avatar_url = avatar_url
|
||||
end
|
||||
|
||||
def call
|
||||
if import_remote_avatar
|
||||
LdapManager::UpdateAvatar.call(user: @user)
|
||||
XmppSetAvatarJob.perform_later(user: @user) if Setting.ejabberd_enabled?
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def import_remote_avatar
|
||||
tempfile = Down.download(@avatar_url)
|
||||
content_type = tempfile.content_type
|
||||
unless %w[image/jpeg image/png].include?(content_type)
|
||||
Rails.logger.warn { "Wrong content type of remote avatar for user #{user.cn}: '#{content_type}'" }
|
||||
return false
|
||||
end
|
||||
|
||||
img_data = UserManager::ProcessAvatar.call(io: tempfile)
|
||||
tempfile = Tempfile.create
|
||||
tempfile.binmode
|
||||
tempfile.write(img_data)
|
||||
tempfile.rewind
|
||||
|
||||
hash = Digest::SHA256.hexdigest(img_data)
|
||||
ext = content_type == "image/png" ? "png" : "jpg"
|
||||
filename = "#{hash}.#{ext}"
|
||||
key = "users/#{@user.cn}/avatars/#{filename}"
|
||||
|
||||
@user.avatar.attach io: tempfile, key: key, filename: filename
|
||||
rescue => e
|
||||
Sentry.capture_exception(e) if Setting.sentry_enabled?
|
||||
Rails.logger.warn "Importing remote avatar failed: \"#{e.message}\""
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
21
app/services/user_manager/process_avatar.rb
Normal file
21
app/services/user_manager/process_avatar.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
module UserManager
|
||||
class ProcessAvatar < UserManagerService
|
||||
def initialize(io:)
|
||||
@io = io
|
||||
end
|
||||
|
||||
def call
|
||||
processed = ImageProcessing::Vips
|
||||
.source(@io)
|
||||
.resize_to_fill(400, 400)
|
||||
.saver(strip: true)
|
||||
.call
|
||||
@io.rewind
|
||||
processed.read
|
||||
rescue Vips::Error => e
|
||||
Sentry.capture_exception(e) if Setting.sentry_enabled?
|
||||
Rails.logger.warn { "Image processing failed for avatar: #{e.message}" }
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user