Commit b067261e authored by Yamagishi Kazutoshi's avatar Yamagishi Kazutoshi Committed by Eugen Rochko

Introduce RuboCop

parent 8b229a11
......@@ -106,5 +106,8 @@ Style/RegexpLiteral:
Style/SymbolArray:
Enabled: false
Style/TrailingCommaInLiteral:
Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: 'comma'
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: 'comma'
......@@ -29,8 +29,9 @@ gem 'oj'
gem 'react_on_rails', '11.0.0'
group :development, :test do
gem 'pry-rails', '~> 0.3'
gem 'i18n-tasks', '~> 0.9', require: false
gem 'pry-rails', '~> 0.3'
gem 'rubocop', '~> 0.57', require: false
end
group :development do
......
......@@ -161,6 +161,7 @@ GEM
parser (>= 2.2.3.0)
rainbow (>= 2.2.2, < 4.0)
terminal-table (>= 1.5.1)
jaro_winkler (1.5.1)
jsonapi-renderer (0.2.0)
jwt (1.5.6)
libv8 (6.3.292.48.1)
......@@ -221,9 +222,11 @@ GEM
omniauth-oauth (~> 1.1)
rack
orm_adapter (0.5.0)
parallel (1.12.1)
parser (2.5.1.0)
ast (~> 2.4.0)
pg (0.21.0)
powerpack (0.1.2)
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
......@@ -301,6 +304,15 @@ GEM
responders (2.4.0)
actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3)
rubocop (0.57.2)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.5)
powerpack (~> 0.1)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.9.0)
sidekiq (5.1.3)
concurrent-ruby (~> 1.0)
connection_pool (~> 2.2, >= 2.2.0)
......@@ -378,6 +390,7 @@ DEPENDENCIES
react_on_rails (= 11.0.0)
redis (~> 3.3)
redis-rails
rubocop (~> 0.57)
sidekiq (~> 5.0)
sidekiq-status
spring
......
......@@ -23,14 +23,14 @@ class FriendsController < ApplicationController
end
def status
render json: Sidekiq::Status::get_all(job_id), serializer: StatusSerializer
render json: Sidekiq::Status.get_all(job_id), serializer: StatusSerializer
end
def follow_all
if Sidekiq::Status::complete?(job_id)
_friends = friends
if Sidekiq::Status.complete?(job_id)
friends = self.friends
_friends.each do |friend|
friends.each do |friend|
next if friend.relative_account_id.blank? || friend.following
begin
......@@ -41,7 +41,7 @@ class FriendsController < ApplicationController
end
end
Rails.cache.write("#{current_user.id}/friends", _friends.map { |f| [f.id, f.relative_account_id, f.following] })
Rails.cache.write("#{current_user.id}/friends", friends.map { |f| [f.id, f.relative_account_id, f.following] })
end
redirect_to friends_path
......@@ -54,11 +54,11 @@ class FriendsController < ApplicationController
end
def job_exists?
job_id.present? && Sidekiq::Status::get_all(job_id).key?('status')
job_id.present? && Sidekiq::Status.get_all(job_id).key?('status')
end
def friends
return unless Sidekiq::Status::complete?(job_id)
return unless Sidekiq::Status.complete?(job_id)
data_map = Rails.cache.fetch("#{current_user.id}/friends") { [] }.map { |d| [d.first, d] }.to_h
......@@ -79,8 +79,8 @@ class FriendsController < ApplicationController
Authorization.where(provider: 'mastodon', user_id: data.map(&:first))
.map(&:uid)
.map { |uid| uid.split('@').last }
.inject(Hash.new(0)) { |h, k| h[k] += 1; h }
.sort_by { |k, v| -v }
.each_with_object(Hash.new(0)) { |k, h| h[k] += 1 }
.sort_by { |_k, v| -v }
.take(MAX_INSTANCES)
.map { |k, _| fetch_instance_info(k) }
.compact
......
# frozen_string_literal: true
class HelloWorldController < ApplicationController
layout "hello_world"
def index
@hello_world_props = { name: "Stranger" }
end
end
# frozen_string_literal: true
module AccountsHelper
end
# frozen_string_literal: true
module AuthorizationsHelper
end
# frozen_string_literal: true
module FriendsHelper
def mastodon_profile_url(uid)
username, domain = uid.split('@')
......
# frozen_string_literal: true
module HomeHelper
end
# frozen_string_literal: true
module Users::OmniauthCallbacksHelper
end
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
layout 'mailer'
......
......@@ -13,14 +13,15 @@ class Authorization < ApplicationRecord
def info
return @info if defined?(@info)
if provider == 'mastodon'
if provider != 'mastodon'
@info = {}
return
end
@info = Rails.cache.fetch("mastodon-user:#{uid}", expires_in: 1.day) do
client = Mastodon::REST::Client.new(base_url: "https://#{domain}", bearer_token: token)
client.verify_credentials.attributes
end
else
@info = {}
end
rescue Mastodon::Error, HTTP::Error, OpenSSL::SSL::SSLError, Oj::ParseError, NoMethodError
@info = {}
end
......
......@@ -4,7 +4,7 @@ class MastodonClient < ApplicationRecord
class << self
def obtain!(domain, callback_url)
new_client = Mastodon::REST::Client.new(base_url: "https://#{domain}").create_app('Mastodon Bridge', callback_url, 'read follow')
client = self.new(domain: domain)
client = new(domain: domain)
client.client_id = new_client.client_id
client.client_secret = new_client.client_secret
......@@ -17,11 +17,13 @@ class MastodonClient < ApplicationRecord
def client_token
return attributes['client_token'] if attributes['client_token'].present?
res = http_client.post("https://#{domain}/oauth/token", params: {
params = {
grant_type: 'client_credentials',
client_id: client_id,
client_secret: client_secret,
})
}
res = http_client.post("https://#{domain}/oauth/token", params: params)
info = Oj.load(res.to_s, mode: :null)
......
......@@ -3,10 +3,10 @@
class User < ApplicationRecord
devise :omniauthable, omniauth_providers: [:twitter, :mastodon]
has_many :authorizations
has_many :authorizations, dependent: :destroy
has_one :twitter, -> { where(provider: 'twitter') }, class_name: 'Authorization'
has_one :mastodon, -> { where(provider: 'mastodon') }, class_name: 'Authorization'
has_one :twitter, -> { where(provider: 'twitter') }, class_name: 'Authorization', inverse_of: :user
has_one :mastodon, -> { where(provider: 'mastodon') }, class_name: 'Authorization', inverse_of: :user
attr_accessor :relative_account_id, :following
......
......@@ -24,6 +24,6 @@ class UserSerializer < ActiveModel::Serializer
end
def avatar_url
object.mastodon.info['avatar'] unless object.mastodon.info['avatar'].blank?
object.mastodon.info['avatar'].presence
end
end
......@@ -29,7 +29,7 @@ class FindFriendsWorker
.reject { |user| user.mastodon.nil? }
unless current_user.mastodon.nil?
total (all_friend_ids * 2) + 1
total all_friend_ids * 2 + 1
users.each do |user|
performed += 1
......@@ -37,7 +37,7 @@ class FindFriendsWorker
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)
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
......@@ -58,7 +58,7 @@ class FindFriendsWorker
def set_relationships!(current_user, users)
account_map = users.map { |user| [user.relative_account_id, user] }.to_h
account_ids = users.collect { |user| user.relative_account_id }.compact
account_ids = users.map(&:relative_account_id).compact
param_str = account_ids.map { |id| "id[]=#{id}" }.join('&')
current_user.mastodon_client.perform_request(:get, "/api/v1/accounts/relationships?#{param_str}").each do |relationship|
......
# frozen_string_literal: true
# This file is used by Rack-based servers to start the application.
require_relative 'config/environment'
......
......@@ -21,8 +21,6 @@ default: &default
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: postgres
development:
<<: *default
database: mastodon-bridge_development
......
# frozen_string_literal: true
Rails.application.routes.draw do
get 'hello_world', to: 'hello_world#index'
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
devise_scope :user do
......
# frozen_string_literal: true
require 'test_helper'
class AccountsControllerTest < ActionDispatch::IntegrationTest
......
# frozen_string_literal: true
require 'test_helper'
class AuthorizationsControllerTest < ActionDispatch::IntegrationTest
......
# frozen_string_literal: true
require 'test_helper'
class FriendsControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
test 'should get index' do
get friends_index_url
assert_response :success
end
end
# frozen_string_literal: true
require 'test_helper'
class HomeControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
test 'should get index' do
get home_index_url
assert_response :success
end
end
# frozen_string_literal: true
require 'test_helper'
class Users::OmniauthCallbacksControllerTest < ActionDispatch::IntegrationTest
......
# frozen_string_literal: true
require 'test_helper'
class AuthorizationTest < ActiveSupport::TestCase
......
# frozen_string_literal: true
require 'test_helper'
class MastodonClientTest < ActiveSupport::TestCase
......
# frozen_string_literal: true
require 'test_helper'
class UserTest < ActiveSupport::TestCase
......
# frozen_string_literal: true
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require File.expand_path('../config/environment', __dir__)
require 'rails/test_help'
class ActiveSupport::TestCase
......
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