about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2018-08-22 20:56:32 +0200
committerThibaut Girka <thib@sitedethib.com>2018-08-22 20:56:32 +0200
commite70fc059a9511d43b42c2502514f6220b416cdd5 (patch)
treeabc0ea9862c55c2e114c855b20eb4a35f1141709 /app/services
parent628fca50e20bcf41f206877083fc5ee8789c1088 (diff)
parent56f882aed6fc81bbe4fb8821f11ba196795c99a8 (diff)
Merge branch 'master' into glitch-soc/merge-upstream
Diffstat (limited to 'app/services')
-rw-r--r--app/services/activitypub/fetch_remote_account_service.rb2
-rw-r--r--app/services/activitypub/fetch_remote_key_service.rb2
-rw-r--r--app/services/activitypub/fetch_remote_status_service.rb2
-rw-r--r--app/services/backup_service.rb24
-rw-r--r--app/services/fetch_remote_account_service.rb7
5 files changed, 30 insertions, 7 deletions
diff --git a/app/services/activitypub/fetch_remote_account_service.rb b/app/services/activitypub/fetch_remote_account_service.rb
index 41fec9170..1ec9ee5dd 100644
--- a/app/services/activitypub/fetch_remote_account_service.rb
+++ b/app/services/activitypub/fetch_remote_account_service.rb
@@ -11,7 +11,7 @@ class ActivityPub::FetchRemoteAccountService < BaseService
     @json = if prefetched_body.nil?
               fetch_resource(uri, id)
             else
-              body_to_json(prefetched_body)
+              body_to_json(prefetched_body, compare_id: id ? uri : nil)
             end
 
     return if !supported_context? || !expected_type? || (break_on_redirect && @json['movedTo'].present?)
diff --git a/app/services/activitypub/fetch_remote_key_service.rb b/app/services/activitypub/fetch_remote_key_service.rb
index 505baccd4..df17d9079 100644
--- a/app/services/activitypub/fetch_remote_key_service.rb
+++ b/app/services/activitypub/fetch_remote_key_service.rb
@@ -17,7 +17,7 @@ class ActivityPub::FetchRemoteKeyService < BaseService
         @json = fetch_resource(uri, id)
       end
     else
-      @json = body_to_json(prefetched_body)
+      @json = body_to_json(prefetched_body, compare_id: id ? uri : nil)
     end
 
     return unless supported_context?(@json) && expected_type?
diff --git a/app/services/activitypub/fetch_remote_status_service.rb b/app/services/activitypub/fetch_remote_status_service.rb
index 2b447abb3..469821032 100644
--- a/app/services/activitypub/fetch_remote_status_service.rb
+++ b/app/services/activitypub/fetch_remote_status_service.rb
@@ -8,7 +8,7 @@ class ActivityPub::FetchRemoteStatusService < BaseService
     @json = if prefetched_body.nil?
               fetch_resource(uri, id, on_behalf_of)
             else
-              body_to_json(prefetched_body)
+              body_to_json(prefetched_body, compare_id: id ? uri : nil)
             end
 
     return unless supported_context? && expected_type?
diff --git a/app/services/backup_service.rb b/app/services/backup_service.rb
index da7db6462..4cfa22ab8 100644
--- a/app/services/backup_service.rb
+++ b/app/services/backup_service.rb
@@ -44,6 +44,7 @@ class BackupService < BaseService
         Gem::Package::TarWriter.new(gz) do |tar|
           dump_media_attachments!(tar)
           dump_outbox!(tar)
+          dump_likes!(tar)
           dump_actor!(tar)
         end
       end
@@ -82,6 +83,8 @@ class BackupService < BaseService
 
     actor[:icon][:url]  = 'avatar' + File.extname(actor[:icon][:url])  if actor[:icon]
     actor[:image][:url] = 'header' + File.extname(actor[:image][:url]) if actor[:image]
+    actor[:outbox]      = 'outbox.json'
+    actor[:likes]       = 'likes.json'
 
     download_to_tar(tar, account.avatar, 'avatar' + File.extname(account.avatar.path)) if account.avatar.exists?
     download_to_tar(tar, account.header, 'header' + File.extname(account.header.path)) if account.header.exists?
@@ -91,15 +94,30 @@ class BackupService < BaseService
     tar.add_file_simple('actor.json', 0o444, json.bytesize) do |io|
       io.write(json)
     end
+  end
+
+  def dump_likes!(tar)
+    collection = serialize(ActivityPub::CollectionPresenter.new(id: 'likes.json', type: :ordered, size: 0, items: []), ActivityPub::CollectionSerializer)
+
+    Status.reorder(nil).joins(:favourites).includes(:account).merge(account.favourites).find_in_batches do |statuses|
+      statuses.each do |status|
+        collection[:totalItems] += 1
+        collection[:orderedItems] << ActivityPub::TagManager.instance.uri_for(status)
+      end
 
-    tar.add_file_simple('key.pem', 0o444, account.private_key.bytesize) do |io|
-      io.write(account.private_key)
+      GC.start
+    end
+
+    json = Oj.dump(collection)
+
+    tar.add_file_simple('likes.json', 0o444, json.bytesize) do |io|
+      io.write(json)
     end
   end
 
   def collection_presenter
     ActivityPub::CollectionPresenter.new(
-      id: account_outbox_url(account),
+      id: 'outbox.json',
       type: :ordered,
       size: account.statuses_count,
       items: []
diff --git a/app/services/fetch_remote_account_service.rb b/app/services/fetch_remote_account_service.rb
index a0f031a44..cfc560022 100644
--- a/app/services/fetch_remote_account_service.rb
+++ b/app/services/fetch_remote_account_service.rb
@@ -27,7 +27,7 @@ class FetchRemoteAccountService < BaseService
 
     account = author_from_xml(xml.at_xpath('/xmlns:feed', xmlns: OStatus::TagManager::XMLNS), false)
 
-    UpdateRemoteProfileService.new.call(xml, account) unless account.nil?
+    UpdateRemoteProfileService.new.call(xml, account) if account.present? && trusted_domain?(url, account)
 
     account
   rescue TypeError
@@ -37,4 +37,9 @@ class FetchRemoteAccountService < BaseService
     Rails.logger.debug 'Invalid XML or missing namespace'
     nil
   end
+
+  def trusted_domain?(url, account)
+    domain = Addressable::URI.parse(url).normalized_host
+    domain.casecmp(account.domain).zero? || domain.casecmp(Addressable::URI.parse(account.remote_url.presence || account.uri).normalized_host).zero?
+  end
 end