access_token_extension.rb 874 B

1234567891011121314151617181920212223242526272829
  1. # frozen_string_literal: true
  2. module AccessTokenExtension
  3. extend ActiveSupport::Concern
  4. included do
  5. include Redisable
  6. has_many :web_push_subscriptions, class_name: 'Web::PushSubscription', inverse_of: :access_token
  7. after_commit :push_to_streaming_api
  8. scope :expired, -> { where.not(expires_in: nil).where('created_at + MAKE_INTERVAL(secs => expires_in) < NOW()') }
  9. scope :not_revoked, -> { where(revoked_at: nil) }
  10. scope :revoked, -> { where.not(revoked_at: nil).where(revoked_at: ...Time.now.utc) }
  11. end
  12. def revoke(clock = Time)
  13. update(revoked_at: clock.now.utc)
  14. end
  15. def update_last_used(request, clock = Time)
  16. update(last_used_at: clock.now.utc, last_used_ip: request.remote_ip)
  17. end
  18. def push_to_streaming_api
  19. redis.publish("timeline:access_token:#{id}", Oj.dump(event: :kill)) if revoked? || destroyed?
  20. end
  21. end