authorization.rb 687 B

12345678910111213141516171819202122232425262728
  1. # frozen_string_literal: true
  2. class Authorization < ApplicationRecord
  3. belongs_to :user, inverse_of: :authorizations, required: true
  4. default_scope { order('id asc') }
  5. def domain
  6. return unless provider == 'mastodon'
  7. uid.split('@').last
  8. end
  9. def info
  10. return @info if defined?(@info)
  11. if provider == 'mastodon'
  12. @info = Rails.cache.fetch("mastodon-user:#{uid}", expires_in: 1.day) do
  13. client = Mastodon::REST::Client.new(base_url: "https://#{domain}", bearer_token: token)
  14. client.verify_credentials.attributes
  15. end
  16. else
  17. @info = {}
  18. end
  19. rescue Mastodon::Error, HTTP::Error, OpenSSL::SSL::SSLError
  20. @info = {}
  21. end
  22. end