Commit 8087b7f8 authored by Eugen Rochko's avatar Eugen Rochko

Display following relation

parent 1c3266ce
......@@ -455,7 +455,7 @@ h4 {
max-width: 300px;
width: calc(50% - 10px);
margin-right: 10px;
&:nth-child(even) {
margin-right: 0;
}
......@@ -492,7 +492,16 @@ h4 {
align-items: center;
flex: 1 1 auto;
max-width: 300px;
width: calc(50% - 10px);
margin-bottom: 20px;
margin-right: 10px;
position: relative;
box-sizing: border-box;
padding: 10px;
&:nth-child(even) {
margin-right: 0;
}
.avatar {
width: 60px;
......@@ -510,6 +519,20 @@ h4 {
}
}
.following-indicator {
position: absolute;
width: 25px;
height: 25px;
background: $vibrant;
color: $white;
text-align: center;
border-radius: 50%;
font-size: 16px;
line-height: 25px;
bottom: 5px;
box-shadow: 3px -3px 6px rgba(#000, 0.3);
}
.name {
margin-left: 10px;
}
......
......@@ -6,6 +6,7 @@ class FriendsController < ApplicationController
before_action :authenticate_user!
before_action :set_friends, only: :index
before_action :set_top_instances, only: :index
before_action :set_relationships, if: -> { current_user&.mastodon }
rescue_from Twitter::Error do |e|
redirect_to root_path, alert: "Twitter error: #{e}"
......@@ -19,6 +20,7 @@ class FriendsController < ApplicationController
@friends = User.where(id: Authorization.where(provider: :twitter, uid: twitter_friend_ids).map(&:user_id))
.includes(:authorizations)
.reject { |user| user.mastodon.nil? }
.map { |user| fetch_account_id(user) }
end
def set_top_instances
......@@ -32,29 +34,38 @@ class FriendsController < ApplicationController
end
def twitter_friend_ids
Rails.cache.fetch("#{current_user.id}/twitter-friends", expires_in: 15.minutes) { twitter_client.friend_ids.to_a }
Rails.cache.fetch("#{current_user.id}/twitter-friends", expires_in: 15.minutes) { current_user.twitter_client.friend_ids.to_a }
end
def twitter_client
@twitter_client ||= Twitter::REST::Client.new do |config|
authorization = current_user.authorizations.find_by(provider: :twitter)
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
config.consumer_key = ENV['TWITTER_CLIENT_ID']
config.consumer_secret = ENV['TWITTER_CLIENT_SECRET']
config.access_token = authorization.try(:token)
config.access_token_secret = authorization.try(:secret)
def fetch_account_id(user)
user.tap do |user|
begin
user.relative_account_id = Rails.cache.fetch("#{current_user.id}/#{current_user.mastodon.domain}/#{user.mastodon.uid}", expires_in: 1.week) do
account, _ = current_user.mastodon_client.perform_request(:get, '/api/v1/accounts/search', q: user.mastodon.uid, resolve: 'true', limit: 1)
next if account.nil?
account['id']
end
rescue Mastodon::Error, HTTP::Error, OpenSSL::SSL::SSLError
user.relative_account_id = nil
end
end
end
def mastodon_client
authorization = current_user.authorizations.find_by(provider: :mastodon)
_, domain = authorization.uid.split('@')
@mastodon_client ||= Mastodon::REST::Client.new(base_url: "https://#{domain}", bearer_token: authorization.token)
end
def set_relationships
account_map = @friends.map { |user| [user.relative_account_id, user] }.to_h
account_ids = @friends.collect { |user| user.relative_account_id }.compact
param_str = account_ids.map { |id| "id[]=#{id}" }.join('&')
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
current_user.mastodon_client.perform_request(:get, "/api/v1/accounts/relationships?#{param_str}").each do |relationship|
account_map[relationship['id']].following = relationship['following']
end
rescue Mastodon::Error, HTTP::Error, OpenSSL::SSL::SSLError
nil
end
end
......@@ -5,14 +5,33 @@ class User < ApplicationRecord
has_many :authorizations
attr_accessor :relative_account_id, :following
def twitter
@twitter ||= authorizations.where(provider: :twitter).last
end
def twitter_client
return if twitter.nil?
@twitter_client ||= Twitter::REST::Client.new do |config|
config.consumer_key = ENV['TWITTER_CLIENT_ID']
config.consumer_secret = ENV['TWITTER_CLIENT_SECRET']
config.access_token = twitter.token
config.access_token_secret = twitter.secret
end
end
def mastodon
@mastodon ||= authorizations.where(provider: :mastodon).last
end
def mastodon_client
return if mastodon.nil?
@mastodon_client ||= Mastodon::REST::Client.new(base_url: "https://#{mastodon.domain}", bearer_token: mastodon.token)
end
class << self
def from_omniauth(auth, current_user)
authorization = Authorization.where(provider: auth.provider, uid: auth.uid.to_s).first_or_initialize(provider: auth.provider, uid: auth.uid.to_s)
......
......@@ -11,6 +11,8 @@
= link_to user.mastodon.info['url'] || user.mastodon.profile_url, class: 'user-card' do
.avatar
= image_tag user.mastodon.info['avatar'] unless user.mastodon.info['avatar'].blank?
- if user.following
.following-indicator= fa_icon('check')
.name
%span.display-name= user.mastodon.info['display_name'].presence || user.mastodon.info['username'] || user.mastodon.display_name
%span.username= "@#{user.mastodon.uid}"
......
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