about summary refs log tree commit diff
path: root/app/models/device.rb
blob: 5dc6cf1e66f6fb45f91bcc456d5c27f009ea65e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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