friends_controller.rb 2.28 KB
Newer Older
Eugen Rochko's avatar
Eugen Rochko committed
1 2
# frozen_string_literal: true

Eugen Rochko's avatar
Eugen Rochko committed
3 4 5 6 7
require 'twitter'

class FriendsController < ApplicationController
  before_action :authenticate_user!

Eugen Rochko's avatar
Eugen Rochko committed
8 9 10 11
  rescue_from Twitter::Error do |e|
    redirect_to root_path, alert: "Twitter error: #{e}"
  end

Eugen Rochko's avatar
Eugen Rochko committed
12
  def index
13 14
    @tweet_text = URI.encode("I am #{current_user.mastodon.try(:uid)} on Mastodon! Find your Twitter friends in the fediverse")

Eugen Rochko's avatar
Eugen Rochko committed
15
    fetch_twitter_followees
16
    fetch_twitter_followers
Eugen Rochko's avatar
Eugen Rochko committed
17 18 19
    fetch_related_mastodons
  end

20 21 22 23
  def follow
    user = User.find(params[:id])
    mastodon_uid = user.authorizations.find_by(provider: :mastodon).uid
    mastodon_client.follow_by_uri(mastodon_uid)
Eugen Rochko's avatar
Eugen Rochko committed
24
    redirect_to friends_path, notice: "Successfully followed #{mastodon_uid} from your Mastodon account"
25
  rescue Mastodon::Error::Unauthorized
Eugen Rochko's avatar
Eugen Rochko committed
26
    redirect_to friends_path, alert: 'The access token for your Mastodon account has expired or was revoked'
27 28
  end

Eugen Rochko's avatar
Eugen Rochko committed
29 30 31
  private

  def fetch_twitter_followees
Eugen Rochko's avatar
Eugen Rochko committed
32
    @twitter_friend_ids = Rails.cache.fetch("#{current_user.id}/twitter-friends", expires_in: 15.minutes) { twitter_client.friend_ids }
Eugen Rochko's avatar
Eugen Rochko committed
33 34
  end

35
  def fetch_twitter_followers
Eugen Rochko's avatar
Eugen Rochko committed
36
    @twitter_follower_ids = Rails.cache.fetch("#{current_user.id}/twitter-followers", expires_in: 15.minutes) { twitter_client.follower_ids }
37 38
  end

Eugen Rochko's avatar
Eugen Rochko committed
39
  def fetch_related_mastodons
40 41
    found_ids1 = Authorization.where(provider: :twitter, uid: @twitter_friend_ids.to_a)
    found_ids2 = Authorization.where(provider: :twitter, uid: @twitter_follower_ids.to_a)
42

43 44
    @friends   = User.where(id: found_ids1.map(&:user_id)).includes(:authorizations)
    @followers = User.where(id: found_ids2.map(&:user_id)).includes(:authorizations)
Eugen Rochko's avatar
Eugen Rochko committed
45 46 47 48 49 50 51 52 53 54 55 56
  end

  def twitter_client
    @twitter_client ||= Twitter::REST::Client.new do |config|
      authorization = current_user.authorizations.find_by(provider: :twitter)

      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)
    end
  end
57 58 59 60 61 62

  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
Eugen Rochko's avatar
Eugen Rochko committed
63
end