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

Introduce RuboCop

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