friends_controller.rb 789 Bytes
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
  def index
    session[:job_id] = FindFriendsWorker.perform_async(current_user.id) unless job_id.present?
Eugen Rochko's avatar
Eugen Rochko committed
10 11
  end

Eugen Rochko's avatar
Eugen Rochko committed
12 13
  def results
    render json: Oj.dump(friends)
Eugen Rochko's avatar
Eugen Rochko committed
14 15
  end

Eugen Rochko's avatar
Eugen Rochko committed
16 17
  def status
    render json: Oj.dump(Sidekiq::Status::get_all(job_id))
Eugen Rochko's avatar
Eugen Rochko committed
18 19
  end

Eugen Rochko's avatar
Eugen Rochko committed
20
  private
21

Eugen Rochko's avatar
Eugen Rochko committed
22 23
  def job_id
    session[:job_id]
Eugen Rochko's avatar
Eugen Rochko committed
24 25
  end

Eugen Rochko's avatar
Eugen Rochko committed
26 27
  def friends
    return unless Sidekiq::Status::complete?(job_id)
28

Eugen Rochko's avatar
Eugen Rochko committed
29 30 31
    User.where(id: Authorization.where(provider: :twitter, uid: twitter_friend_ids).map(&:user_id))
        .includes(:twitter, :mastodon)
        .reject { |user| user.mastodon.nil? }
32 33
  end

Eugen Rochko's avatar
Eugen Rochko committed
34
  def twitter_friend_ids
Eugen Rochko's avatar
Eugen Rochko committed
35
    Rails.cache.read("#{current_user.id}/twitter-friends")
36
  end
Eugen Rochko's avatar
Eugen Rochko committed
37
end