65 lines
2.2 KiB
Ruby
65 lines
2.2 KiB
Ruby
module MastodonManager
|
|
class SyncAccountProfiles < MastodonManagerService
|
|
def initialize(direction: "down", overwrite: false, user: nil)
|
|
@direction = direction
|
|
@overwrite = overwrite
|
|
@user = user
|
|
|
|
if @direction != "down"
|
|
raise NotImplementedError
|
|
end
|
|
end
|
|
|
|
def call
|
|
if @user
|
|
Rails.logger.debug { "Syncing account profile for user #{@user.cn} (direction: #{@direction}, overwrite: #{@overwrite})"}
|
|
users = User.where(cn: @user.cn)
|
|
else
|
|
Rails.logger.debug { "Syncing account profiles (direction: #{@direction}, overwrite: #{@overwrite})"}
|
|
users = User
|
|
end
|
|
|
|
users.find_each do |user|
|
|
if user.mastodon_id.blank?
|
|
mastodon_user = MastodonManager::FindUser.call username: user.cn
|
|
if mastodon_user
|
|
Rails.logger.debug { "Setting mastodon_id for user #{user.cn}" }
|
|
user.update! mastodon_id: mastodon_user.dig(:account, :id).to_i
|
|
else
|
|
Rails.logger.debug { "No Mastodon user found for username #{user.cn}" }
|
|
next
|
|
end
|
|
end
|
|
|
|
next if user.avatar.attached? && user.display_name.present?
|
|
|
|
unless mastodon_user
|
|
Rails.logger.debug { "Fetching Mastodon account with ID #{user.mastodon_id} for #{user.cn}" }
|
|
mastodon_user = MastodonManager::FetchUser.call mastodon_id: user.mastodon_id
|
|
end
|
|
|
|
if user.display_name.blank?
|
|
if mastodon_display_name = mastodon_user.dig(:account, :display_name)
|
|
Rails.logger.debug { "Setting display name for user #{user.cn} from Mastodon" }
|
|
LdapManager::UpdateDisplayName.call(
|
|
dn: user.dn, display_name: mastodon_display_name
|
|
)
|
|
end
|
|
end
|
|
|
|
if !user.avatar.attached?
|
|
if avatar_url = mastodon_user.dig(:account, :avatar_static)
|
|
Rails.logger.debug { "Importing Mastodon avatar for user #{user.cn}" }
|
|
UserManager::ImportRemoteAvatar.call(
|
|
user: user, avatar_url: avatar_url
|
|
)
|
|
end
|
|
end
|
|
rescue => e
|
|
Sentry.capture_exception(e) if Setting.sentry_enabled?
|
|
Rails.logger.error e
|
|
end
|
|
end
|
|
end
|
|
end
|