The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

92 lines
2.6 KiB

  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. # id :bigint not null, primary key
  17. # user_id :bigint not null
  18. # session_id :string not null
  19. # created_at :datetime not null
  20. # updated_at :datetime not null
  21. # user_agent :string default(""), not null
  22. # ip :inet
  23. # access_token_id :bigint
  24. #
  25. class SessionActivation < ApplicationRecord
  26. belongs_to :user, inverse_of: :session_activations, required: true
  27. belongs_to :access_token, class_name: 'Doorkeeper::AccessToken', dependent: :destroy
  28. belongs_to :web_push_subscription, class_name: 'Web::PushSubscription', dependent: :destroy
  29. delegate :token,
  30. to: :access_token,
  31. allow_nil: true
  32. def detection
  33. @detection ||= Browser.new(user_agent)
  34. end
  35. def browser
  36. detection.id
  37. end
  38. def platform
  39. detection.platform.id
  40. end
  41. before_create :assign_access_token
  42. before_save :assign_user_agent
  43. class << self
  44. def active?(id)
  45. id && where(session_id: id).exists?
  46. end
  47. def activate(options = {})
  48. activation = create!(options)
  49. purge_old
  50. activation
  51. end
  52. def deactivate(id)
  53. return unless id
  54. where(session_id: id).destroy_all
  55. end
  56. def purge_old
  57. order('created_at desc').offset(Rails.configuration.x.max_session_activations).destroy_all
  58. end
  59. def exclusive(id)
  60. where('session_id != ?', id).destroy_all
  61. end
  62. end
  63. private
  64. def assign_user_agent
  65. self.user_agent = '' if user_agent.nil?
  66. end
  67. def assign_access_token
  68. superapp = Doorkeeper::Application.find_by(superapp: true)
  69. self.access_token = Doorkeeper::AccessToken.create!(application_id: superapp&.id,
  70. resource_owner_id: user_id,
  71. scopes: 'read write follow',
  72. expires_in: Doorkeeper.configuration.access_token_expires_in,
  73. use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?)
  74. end
  75. end