authorization.rb 718 Bytes
Newer Older
Eugen Rochko's avatar
Eugen Rochko committed
1 2
# frozen_string_literal: true

Eugen Rochko's avatar
Eugen Rochko committed
3
class Authorization < ApplicationRecord
Eugen Rochko's avatar
Eugen Rochko committed
4
  belongs_to :user, inverse_of: :authorizations, required: true
5 6

  default_scope { order('id asc') }
Eugen Rochko's avatar
Eugen Rochko committed
7

8 9 10 11 12
  def domain
    return unless provider == 'mastodon'
    uid.split('@').last
  end

Eugen Rochko's avatar
Eugen Rochko committed
13 14 15 16 17
  def info
    return @info if defined?(@info)

    if provider == 'mastodon'
      @info  = Rails.cache.fetch("mastodon-user:#{uid}", expires_in: 1.day) do
18
        client = Mastodon::REST::Client.new(base_url: "https://#{domain}", bearer_token: token)
Eugen Rochko's avatar
Eugen Rochko committed
19 20 21
        client.verify_credentials.attributes
      end
    else
22
      @info = {}
Eugen Rochko's avatar
Eugen Rochko committed
23
    end
24
  rescue Mastodon::Error, HTTP::Error, OpenSSL::SSL::SSLError, Oj::ParseError, NoMethodError
25
    @info = {}
Eugen Rochko's avatar
Eugen Rochko committed
26
  end
Eugen Rochko's avatar
Eugen Rochko committed
27
end