about summary refs log tree commit diff
path: root/app/models/account_alias.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2019-09-19 20:58:19 +0200
committerGitHub <noreply@github.com>2019-09-19 20:58:19 +0200
commit3ed94dcc1acf73f1d0d1ab43567b88ee953f57c9 (patch)
tree3bec67e9d52943779f1b4070556ccab8d64bf6a4 /app/models/account_alias.rb
parentb6df9c10671cd7bf48de3dbd7a94a92fb0a148ec (diff)
Add account migration UI (#11846)
Fix #10736

- Change data export to be available for non-functional accounts
- Change non-functional accounts to include redirecting accounts
Diffstat (limited to 'app/models/account_alias.rb')
-rw-r--r--app/models/account_alias.rb41
1 files changed, 41 insertions, 0 deletions
diff --git a/app/models/account_alias.rb b/app/models/account_alias.rb
new file mode 100644
index 000000000..e9a0dd79e
--- /dev/null
+++ b/app/models/account_alias.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+# == Schema Information
+#
+# Table name: account_aliases
+#
+#  id         :bigint(8)        not null, primary key
+#  account_id :bigint(8)
+#  acct       :string           default(""), not null
+#  uri        :string           default(""), not null
+#  created_at :datetime         not null
+#  updated_at :datetime         not null
+#
+
+class AccountAlias < ApplicationRecord
+  belongs_to :account
+
+  validates :acct, presence: true, domain: { acct: true }
+  validates :uri, presence: true
+
+  before_validation :set_uri
+  after_create :add_to_account
+  after_destroy :remove_from_account
+
+  private
+
+  def set_uri
+    target_account = ResolveAccountService.new.call(acct)
+    self.uri       = ActivityPub::TagManager.instance.uri_for(target_account) unless target_account.nil?
+  rescue Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError, Mastodon::Error
+    # Validation will take care of it
+  end
+
+  def add_to_account
+    account.update(also_known_as: account.also_known_as + [uri])
+  end
+
+  def remove_from_account
+    account.update(also_known_as: account.also_known_as.reject { |x| x == uri })
+  end
+end