about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
authorFire Demon <firedemon@creature.cafe>2020-08-01 20:20:29 -0500
committerFire Demon <firedemon@creature.cafe>2020-08-30 05:45:17 -0500
commit3e57d03573d267e54da66effdd502b3372f4bbd9 (patch)
treec881cb16c098788aff4fe5dd505ba1d5660bdc50 /app/lib
parentb69e14dc76cd3d4623253f3b4dc424c48f59c135 (diff)
[Privacy, Command Tags] Add advanced deletes
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/command_tag/command/account_tools.rb44
1 files changed, 44 insertions, 0 deletions
diff --git a/app/lib/command_tag/command/account_tools.rb b/app/lib/command_tag/command/account_tools.rb
index a2f24789f..b433c32d4 100644
--- a/app/lib/command_tag/command/account_tools.rb
+++ b/app/lib/command_tag/command/account_tools.rb
@@ -11,6 +11,37 @@ module CommandTag::Command::AccountTools
 
   alias handle_acct_at_start handle_account_at_start
 
+  def handle_delete_before_save(args)
+    unless args
+      RemovalWorker.perform_async(@parent.id, immediate: true) if author_of_parent? && status_text_blank?
+      return
+    end
+
+    args.flat_map(&:split).uniq.each do |id|
+      if id.match?(/\A\d+\z/)
+        object = @account.statuses.find_by(id: id)
+      elsif id.start_with?('https://')
+        begin
+          object = ActivityPub::TagManager.instance.uri_to_resource(id, Status)
+          if object.blank? && ActivityPub::TagManager.instance.local_uri?(id)
+            id = Addressable::URI.parse(id)&.normalized_path&.sub(/\A.*\/([^\/]*)\/*/, '\1')
+            next unless id.present? && id.match?(/\A\d+\z/)
+
+            object = find_status_or_create_stub(id)
+          end
+        rescue Addressable::URI::InvalidURIError
+          next
+        end
+      end
+
+      next if object.blank? || object.account_id != @account.id
+
+      RemovalWorker.perform_async(object.id, immediate: true, unpublished: true)
+    end
+  end
+
+  alias handle_destroy_before_save handle_delete_before_save
+
   private
 
   def handle_account_set(args)
@@ -34,4 +65,17 @@ module CommandTag::Command::AccountTools
       end
     end
   end
+
+  def find_status_or_create_stub(id)
+    status_params = {
+      id: id,
+      account: @account,
+      text: '(Deleted)',
+      local: true,
+      visibility: :public,
+      local_only: false,
+      published: false,
+    }
+    Status.where(id: id).first_or_create(status_params)
+  end
 end