about summary refs log tree commit diff
path: root/app/models/device.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2020-06-02 19:24:53 +0200
committerGitHub <noreply@github.com>2020-06-02 19:24:53 +0200
commit5d8398c8b8b51ee7363e7d45acc560f489783e34 (patch)
tree1e0b663049feafdc003ad3c01b25bf5d5d793402 /app/models/device.rb
parent9b7e3b4774d47c184aa759364d41f40e0cdfa210 (diff)
Add E2EE API (#13820)
Diffstat (limited to 'app/models/device.rb')
-rw-r--r--app/models/device.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/app/models/device.rb b/app/models/device.rb
new file mode 100644
index 000000000..97d0d2774
--- /dev/null
+++ b/app/models/device.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+# == Schema Information
+#
+# Table name: devices
+#
+#  id              :bigint(8)        not null, primary key
+#  access_token_id :bigint(8)
+#  account_id      :bigint(8)
+#  device_id       :string           default(""), not null
+#  name            :string           default(""), not null
+#  fingerprint_key :text             default(""), not null
+#  identity_key    :text             default(""), not null
+#  created_at      :datetime         not null
+#  updated_at      :datetime         not null
+#
+
+class Device < ApplicationRecord
+  belongs_to :access_token, class_name: 'Doorkeeper::AccessToken'
+  belongs_to :account
+
+  has_many :one_time_keys, dependent: :destroy, inverse_of: :device
+  has_many :encrypted_messages, dependent: :destroy, inverse_of: :device
+
+  validates :name, :fingerprint_key, :identity_key, presence: true
+  validates :fingerprint_key, :identity_key, ed25519_key: true
+
+  before_save :invalidate_associations, if: -> { device_id_changed? || fingerprint_key_changed? || identity_key_changed? }
+
+  private
+
+  def invalidate_associations
+    one_time_keys.destroy_all
+    encrypted_messages.destroy_all
+  end
+end