about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
Diffstat (limited to 'app/services')
-rw-r--r--app/services/activitypub/process_account_service.rb47
-rw-r--r--app/services/follow_service.rb2
-rw-r--r--app/services/post_status_service.rb3
-rw-r--r--app/services/unsubscribe_service.rb13
4 files changed, 50 insertions, 15 deletions
diff --git a/app/services/activitypub/process_account_service.rb b/app/services/activitypub/process_account_service.rb
index 29eb1c2e1..b54e447ad 100644
--- a/app/services/activitypub/process_account_service.rb
+++ b/app/services/activitypub/process_account_service.rb
@@ -8,11 +8,12 @@ class ActivityPub::ProcessAccountService < BaseService
   def call(username, domain, json)
     return if json['inbox'].blank?
 
-    @json     = json
-    @uri      = @json['id']
-    @username = username
-    @domain   = domain
-    @account  = Account.find_by(uri: @uri)
+    @json        = json
+    @uri         = @json['id']
+    @username    = username
+    @domain      = domain
+    @account     = Account.find_by(uri: @uri)
+    @collections = {}
 
     create_account  if @account.nil?
     upgrade_account if @account.ostatus?
@@ -47,11 +48,14 @@ class ActivityPub::ProcessAccountService < BaseService
     @account.url                 = url || @uri
     @account.display_name        = @json['name'] || ''
     @account.note                = @json['summary'] || ''
-    @account.avatar_remote_url   = image_url('icon')
-    @account.header_remote_url   = image_url('image')
+    @account.avatar_remote_url   = image_url('icon')  unless skip_download?
+    @account.header_remote_url   = image_url('image') unless skip_download?
     @account.public_key          = public_key || ''
     @account.locked              = @json['manuallyApprovesFollowers'] || false
-    @account.save!
+    @account.statuses_count      = outbox_total_items    if outbox_total_items.present?
+    @account.following_count     = following_total_items if following_total_items.present?
+    @account.followers_count     = followers_total_items if followers_total_items.present?
+    @account.save_with_optional_media!
   end
 
   def upgrade_account
@@ -88,6 +92,33 @@ class ActivityPub::ProcessAccountService < BaseService
     value['href']
   end
 
+  def outbox_total_items
+    collection_total_items('outbox')
+  end
+
+  def following_total_items
+    collection_total_items('following')
+  end
+
+  def followers_total_items
+    collection_total_items('followers')
+  end
+
+  def collection_total_items(type)
+    return if @json[type].blank?
+    return @collections[type] if @collections.key?(type)
+
+    collection = fetch_resource(@json[type])
+
+    @collections[type] = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
+  rescue HTTP::Error, OpenSSL::SSL::SSLError
+    @collections[type] = nil
+  end
+
+  def skip_download?
+    @account.suspended? || domain_block&.reject_media?
+  end
+
   def auto_suspend?
     domain_block && domain_block.suspend?
   end
diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb
index a92eb6b88..941556b60 100644
--- a/app/services/follow_service.rb
+++ b/app/services/follow_service.rb
@@ -12,7 +12,7 @@ class FollowService < BaseService
     raise ActiveRecord::RecordNotFound if target_account.nil? || target_account.id == source_account.id || target_account.suspended?
     raise Mastodon::NotPermittedError  if target_account.blocking?(source_account) || source_account.blocking?(target_account)
 
-    return if source_account.following?(target_account)
+    return if source_account.following?(target_account) || source_account.requested?(target_account)
 
     if target_account.locked? || target_account.activitypub?
       request_follow(source_account, target_account)
diff --git a/app/services/post_status_service.rb b/app/services/post_status_service.rb
index 56011a005..e5b0fe438 100644
--- a/app/services/post_status_service.rb
+++ b/app/services/post_status_service.rb
@@ -27,9 +27,10 @@ class PostStatusService < BaseService
                                         thread: in_reply_to,
                                         sensitive: options[:sensitive],
                                         spoiler_text: options[:spoiler_text] || '',
-                                        visibility: options[:visibility],
+                                        visibility: options[:visibility] || account.user&.setting_default_privacy,
                                         language: detect_language_for(text, account),
                                         application: options[:application])
+
       attach_media(status, media)
     end
 
diff --git a/app/services/unsubscribe_service.rb b/app/services/unsubscribe_service.rb
index 865f783bc..b99046712 100644
--- a/app/services/unsubscribe_service.rb
+++ b/app/services/unsubscribe_service.rb
@@ -4,16 +4,19 @@ class UnsubscribeService < BaseService
   def call(account)
     return if account.hub_url.blank?
 
-    @account  = account
-    @response = build_request.perform
+    @account = account
 
-    Rails.logger.debug "PuSH unsubscribe for #{@account.acct} failed: #{@response.status}" unless @response.status.success?
+    begin
+      @response = build_request.perform
+
+      Rails.logger.debug "PuSH unsubscribe for #{@account.acct} failed: #{@response.status}" unless @response.status.success?
+    rescue HTTP::Error, OpenSSL::SSL::SSLError => e
+      Rails.logger.debug "PuSH unsubscribe for #{@account.acct} failed: #{e}"
+    end
 
     @account.secret = ''
     @account.subscription_expires_at = nil
     @account.save!
-  rescue HTTP::Error, OpenSSL::SSL::SSLError
-    Rails.logger.debug "PuSH subscription request for #{@account.acct} could not be made due to HTTP or SSL error"
   end
 
   private