]> cat aescling's git repositories - mastodon.git/blob - app/models/session_activation.rb
Change belongs_to_required_by_default to true (#5888)
[mastodon.git] / app / models / session_activation.rb
1 # frozen_string_literal: true
2 # == Schema Information
3 #
4 # Table name: session_activations
5 #
6 # id :integer not null, primary key
7 # session_id :string not null
8 # created_at :datetime not null
9 # updated_at :datetime not null
10 # user_agent :string default(""), not null
11 # ip :inet
12 # access_token_id :integer
13 # user_id :integer not null
14 # web_push_subscription_id :integer
15 #
16
17 class SessionActivation < ApplicationRecord
18 belongs_to :user, inverse_of: :session_activations
19 belongs_to :access_token, class_name: 'Doorkeeper::AccessToken', dependent: :destroy, optional: true
20 belongs_to :web_push_subscription, class_name: 'Web::PushSubscription', dependent: :destroy, optional: true
21
22 delegate :token,
23 to: :access_token,
24 allow_nil: true
25
26 def detection
27 @detection ||= Browser.new(user_agent)
28 end
29
30 def browser
31 detection.id
32 end
33
34 def platform
35 detection.platform.id
36 end
37
38 before_create :assign_access_token
39 before_save :assign_user_agent
40
41 class << self
42 def active?(id)
43 id && where(session_id: id).exists?
44 end
45
46 def activate(**options)
47 activation = create!(options)
48 purge_old
49 activation
50 end
51
52 def deactivate(id)
53 return unless id
54 where(session_id: id).destroy_all
55 end
56
57 def purge_old
58 order('created_at desc').offset(Rails.configuration.x.max_session_activations).destroy_all
59 end
60
61 def exclusive(id)
62 where('session_id != ?', id).destroy_all
63 end
64 end
65
66 private
67
68 def assign_user_agent
69 self.user_agent = '' if user_agent.nil?
70 end
71
72 def assign_access_token
73 superapp = Doorkeeper::Application.find_by(superapp: true)
74
75 self.access_token = Doorkeeper::AccessToken.create!(application_id: superapp&.id,
76 resource_owner_id: user_id,
77 scopes: 'read write follow',
78 expires_in: Doorkeeper.configuration.access_token_expires_in,
79 use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?)
80 end
81 end
This page took 0.082553 seconds and 4 git commands to generate.