Commit 49433958 authored by Eugen Rochko's avatar Eugen Rochko

Catch Mastodon API/connection errors

parent 8b2885ea
......@@ -27,7 +27,8 @@ class FriendsController < ApplicationController
.map { |uid| uid.split('@').last }
.inject(Hash.new(0)) { |h, k| h[k] += 1; h }
.sort_by { |k, v| v }
.map { |k, _| Rails.cache.fetch("instance:#{k}", expires_in: 1.week) { Oj.load(HTTP.get("https://#{k}/api/v1/instance").to_s) } }
.map { |k, _| fetch_instance_info(k) }
.compact
end
def twitter_friend_ids
......@@ -50,4 +51,10 @@ class FriendsController < ApplicationController
_, domain = authorization.uid.split('@')
@mastodon_client ||= Mastodon::REST::Client.new(base_url: "https://#{domain}", bearer_token: authorization.token)
end
def fetch_instance_info(host)
Rails.cache.fetch("instance:#{host}", expires_in: 1.week) { Oj.load(HTTP.get("https://#{host}/api/v1/instance").to_s, mode: :strict) }
rescue HTTP::Error, OpenSSL::SSL::SSLError, Oj::ParseError
nil
end
end
......@@ -5,16 +5,23 @@ class Authorization < ApplicationRecord
default_scope { order('id asc') }
def domain
return unless provider == 'mastodon'
uid.split('@').last
end
def info
return @info if defined?(@info)
if provider == 'mastodon'
@info = Rails.cache.fetch("mastodon-user:#{uid}", expires_in: 1.day) do
client = Mastodon::REST::Client.new(base_url: "https://#{uid.split('@').last}", bearer_token: token)
client = Mastodon::REST::Client.new(base_url: "https://#{domain}", bearer_token: token)
client.verify_credentials.attributes
end
else
@info = nil
end
rescue Mastodon::Error, HTTP::Error, OpenSSL::SSL::SSLError
@info = nil
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment