WIP Sync Mastodon IDs/profiles to local accounts
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			This commit is contained in:
		
							parent
							
								
									c291765777
								
							
						
					
					
						commit
						c53d9d3c60
					
				
							
								
								
									
										12
									
								
								app/services/mastodon_manager/fetch_user.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								app/services/mastodon_manager/fetch_user.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | |||||||
|  | module MastodonManager | ||||||
|  |   class FetchUser < MastodonManagerService | ||||||
|  |     def initialize(mastodon_id:) | ||||||
|  |       @mastodon_id = mastodon_id | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     def call | ||||||
|  |       user = get "v1/admin/accounts/#{@mastodon_id}" | ||||||
|  |       user.with_indifferent_access | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
							
								
								
									
										14
									
								
								app/services/mastodon_manager/find_user.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								app/services/mastodon_manager/find_user.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | |||||||
|  | module MastodonManager | ||||||
|  |   class FindUser < MastodonManagerService | ||||||
|  |     def initialize(username:) | ||||||
|  |       @username = username | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     def call | ||||||
|  |       users = get "v2/admin/accounts?username=#{@username}&origin=local" | ||||||
|  |       users = users.map { |u| u.with_indifferent_access } | ||||||
|  |       # Results may contain partial matches | ||||||
|  |       users.find { |u| u.dig(:username) == @username } | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
							
								
								
									
										57
									
								
								app/services/mastodon_manager/sync_account_profiles.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								app/services/mastodon_manager/sync_account_profiles.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,57 @@ | |||||||
|  | module MastodonManager | ||||||
|  |   class SyncAccountProfiles < MastodonManagerService | ||||||
|  |     def initialize(direction: "down", overwrite: false) | ||||||
|  |       @direction = direction | ||||||
|  |       @overwrite = overwrite | ||||||
|  | 
 | ||||||
|  |       if @direction != "down" | ||||||
|  |         raise NotImplementedError | ||||||
|  |       end | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     def call | ||||||
|  |       Rails.logger.debug { "Syncing account profiles (direction: #{@direction}, overwrite: #{@overwrite})"} | ||||||
|  | 
 | ||||||
|  |       User.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: @user[:id] | ||||||
|  |           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 | ||||||
							
								
								
									
										12
									
								
								app/services/user_manager/import_remote_avatar.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								app/services/user_manager/import_remote_avatar.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | |||||||
|  | module UserManager | ||||||
|  |   class ImportRemoteAvatar < UserManagerService | ||||||
|  |     def initialize(user:, avatar_url:) | ||||||
|  |       @user = user | ||||||
|  |       @avatar_url = avatar_url | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     def call | ||||||
|  | 
 | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user