1
0

device.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: devices
  5. #
  6. # id :bigint(8) not null, primary key
  7. # access_token_id :bigint(8)
  8. # account_id :bigint(8)
  9. # device_id :string default(""), not null
  10. # name :string default(""), not null
  11. # fingerprint_key :text default(""), not null
  12. # identity_key :text default(""), not null
  13. # created_at :datetime not null
  14. # updated_at :datetime not null
  15. #
  16. class Device < ApplicationRecord
  17. belongs_to :access_token, class_name: 'Doorkeeper::AccessToken'
  18. belongs_to :account
  19. has_many :one_time_keys, dependent: :destroy, inverse_of: :device
  20. has_many :encrypted_messages, dependent: :destroy, inverse_of: :device
  21. validates :name, :fingerprint_key, :identity_key, presence: true
  22. validates :fingerprint_key, :identity_key, ed25519_key: true
  23. before_save :invalidate_associations, if: -> { device_id_changed? || fingerprint_key_changed? || identity_key_changed? }
  24. private
  25. def invalidate_associations
  26. one_time_keys.destroy_all
  27. encrypted_messages.destroy_all
  28. end
  29. end