Commit b95738a3 authored by Eugen Rochko's avatar Eugen Rochko

Fix scopes, friends list, cache twitter friend ids

parent 59917919
......@@ -8,10 +8,19 @@ class FriendsController < ApplicationController
fetch_related_mastodons
end
def follow
user = User.find(params[:id])
mastodon_uid = user.authorizations.find_by(provider: :mastodon).uid
mastodon_client.follow_by_uri(mastodon_uid)
redirect_to friends_path
end
private
def fetch_twitter_followees
@twitter_friend_ids = twitter_client.friend_ids
@twitter_friend_ids = Rails.cache.fetch("#{current_user.id}/twitter-friends", expires_in: 1.minute) do
twitter_client.friend_ids
end
end
def fetch_related_mastodons
......@@ -28,4 +37,10 @@ class FriendsController < ApplicationController
config.access_token_secret = authorization.try(:secret)
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
end
......@@ -3,14 +3,16 @@
<tr>
<th>Twitter user</th>
<th>Mastodon user</th>
<th></th>
</tr>
</thead>
<tbody>
<% @friends.each do |user| %>
<tr>
<td><%= @twitter_map[user.authorizations.find_by(provider: :twitter).uid].screen_name %></td>
<td><%= user.authorizations.find_by(provider: :twitter).uid %></td>
<td><%= user.authorizations.find_by(provider: :mastodon).try(:uid) %></td>
<td><%= link_to "Follow", follow_friend_path(user) %></td>
</tr>
<% end %>
</tbody>
......
......@@ -249,7 +249,7 @@ Devise.setup do |config|
# up on your models and hooks.
config.omniauth :twitter, ENV['TWITTER_CLIENT_ID'], ENV['TWITTER_CLIENT_SECRET']
config.omniauth :mastodon, scopes: 'read follow', credentials: lambda { |domain, callback_url|
config.omniauth :mastodon, authorize_options: { scope: 'read follow' }, credentials: lambda { |domain, callback_url|
client = MastodonClient.first_or_initialize(domain: domain)
return [client.client_id, client.client_secret] unless client.new_record?
......
Rails.application.routes.draw do
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
resources :friends, only: :index
resources :friends, only: :index do
member do
get :follow
end
end
root to: 'home#index'
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