about summary refs log tree commit diff
path: root/app/models/session_activation.rb
blob: 7eb16af8f47dcb13a627e4f4dda7bad8328f26f0 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true
# == Schema Information
#
# Table name: session_activations
#
#  id                       :integer          not null, primary key
#  user_id                  :integer          not null
#  session_id               :string           not null
#  created_at               :datetime         not null
#  updated_at               :datetime         not null
#  user_agent               :string           default(""), not null
#  ip                       :inet
#  access_token_id          :integer
#  web_push_subscription_id :integer
#

#  id              :integer          not null, primary key
#  user_id         :integer          not null
#  session_id      :string           not null
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#  user_agent      :string           default(""), not null
#  ip              :inet
#  access_token_id :integer
#

class SessionActivation < ApplicationRecord
  belongs_to :access_token, class_name: 'Doorkeeper::AccessToken', dependent: :destroy
  belongs_to :web_push_subscription, class_name: 'Web::PushSubscription', dependent: :destroy

  delegate :token,
           to: :access_token,
           allow_nil: true

  def detection
    @detection ||= Browser.new(user_agent)
  end

  def browser
    detection.id
  end

  def platform
    detection.platform.id
  end

  before_create :assign_access_token
  before_save   :assign_user_agent

  class << self
    def active?(id)
      id && where(session_id: id).exists?
    end

    def activate(options = {})
      activation = create!(options)
      purge_old
      activation
    end

    def deactivate(id)
      return unless id
      where(session_id: id).destroy_all
    end

    def purge_old
      order('created_at desc').offset(Rails.configuration.x.max_session_activations).destroy_all
    end

    def exclusive(id)
      where('session_id != ?', id).destroy_all
    end
  end

  private

  def assign_user_agent
    self.user_agent = '' if user_agent.nil?
  end

  def assign_access_token
    superapp = Doorkeeper::Application.find_by(superapp: true)

    self.access_token = Doorkeeper::AccessToken.create!(application_id: superapp&.id,
                                                        resource_owner_id: user_id,
                                                        scopes: 'read write follow',
                                                        expires_in: Doorkeeper.configuration.access_token_expires_in,
                                                        use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?)
  end
end