Commit fdc17bea authored by Eugen Rochko's avatar Eugen Rochko

Fix rubocop issues, introduce usage of frozen literal to improve performance

parent a91c3ef6
...@@ -18,9 +18,29 @@ Metrics/MethodLength: ...@@ -18,9 +18,29 @@ Metrics/MethodLength:
CountComments: false CountComments: false
Max: 10 Max: 10
Metrics/ModuleLength: Metrics/AbcSize:
Max: 100 Max: 100
Metrics/BlockNesting:
Max: 3
Metrics/ClassLength:
CountComments: false
Max: 200
Metrics/CyclomaticComplexity:
Max: 15
Metrics/MethodLength:
Max: 55
Metrics/ModuleLength:
CountComments: false
Max: 200
Metrics/PerceivedComplexity:
Max: 10
Metrics/ParameterLists: Metrics/ParameterLists:
Max: 4 Max: 4
CountKeywordArgs: true CountKeywordArgs: true
...@@ -37,10 +57,10 @@ Style/Documentation: ...@@ -37,10 +57,10 @@ Style/Documentation:
Enabled: false Enabled: false
Style/DoubleNegation: Style/DoubleNegation:
Enabled: false Enabled: true
Style/FrozenStringLiteralComment: Style/FrozenStringLiteralComment:
Enabled: false Enabled: true
Style/SpaceInsideHashLiteralBraces: Style/SpaceInsideHashLiteralBraces:
EnforcedStyle: space EnforcedStyle: space
...@@ -51,10 +71,18 @@ Style/TrailingCommaInLiteral: ...@@ -51,10 +71,18 @@ Style/TrailingCommaInLiteral:
Style/RegexpLiteral: Style/RegexpLiteral:
Enabled: false Enabled: false
Style/Lambda:
Enabled: false
Rails/HasAndBelongsToMany:
Enabled: false
AllCops: AllCops:
TargetRubyVersion: 2.2 TargetRubyVersion: 2.3
Exclude: Exclude:
- 'spec/**/*' - 'spec/**/*'
- 'db/**/*' - 'db/**/*'
- 'app/views/**/*' - 'app/views/**/*'
- 'config/**/*' - 'config/**/*'
- 'bin/*'
- 'Rakefile'
# frozen_string_literal: true
source 'https://rubygems.org' source 'https://rubygems.org'
gem 'rails', '5.0.0.1' gem 'rails', '5.0.0.1'
......
# frozen_string_literal: true
module ApplicationCable module ApplicationCable
class Channel < ActionCable::Channel::Base class Channel < ActionCable::Channel::Base
protected protected
......
# frozen_string_literal: true
module ApplicationCable module ApplicationCable
class Connection < ActionCable::Connection::Base class Connection < ActionCable::Connection::Base
identified_by :current_user identified_by :current_user
......
# frozen_string_literal: true
class HashtagChannel < ApplicationCable::Channel class HashtagChannel < ApplicationCable::Channel
def subscribed def subscribed
tag = params[:tag].downcase tag = params[:tag].downcase
......
# frozen_string_literal: true
class PublicChannel < ApplicationCable::Channel class PublicChannel < ApplicationCable::Channel
def subscribed def subscribed
stream_from 'timeline:public', lambda { |encoded_message| stream_from 'timeline:public', lambda { |encoded_message|
......
# frozen_string_literal: true
class TimelineChannel < ApplicationCable::Channel class TimelineChannel < ApplicationCable::Channel
def subscribed def subscribed
stream_from "timeline:#{current_user.account_id}" stream_from "timeline:#{current_user.account_id}"
......
# frozen_string_literal: true
class AboutController < ApplicationController class AboutController < ApplicationController
before_action :set_body_classes before_action :set_body_classes
......
# frozen_string_literal: true
class AccountsController < ApplicationController class AccountsController < ApplicationController
layout 'public' layout 'public'
...@@ -41,10 +43,7 @@ class AccountsController < ApplicationController ...@@ -41,10 +43,7 @@ class AccountsController < ApplicationController
end end
def set_link_headers def set_link_headers
response.headers['Link'] = LinkHeader.new([ response.headers['Link'] = LinkHeader.new([[webfinger_account_url, [%w(rel lrdd), %w(type application/xrd+xml)]], [account_url(@account, format: 'atom'), [%w(rel alternate), %w(type application/atom+xml)]]])
[webfinger_account_url, [['rel', 'lrdd'], ['type', 'application/xrd+xml']]],
[account_url(@account, format: 'atom'), [['rel', 'alternate'], ['type', 'application/atom+xml']]]
])
end end
def webfinger_account_url def webfinger_account_url
......
# frozen_string_literal: true
class Api::SalmonController < ApiController class Api::SalmonController < ApiController
before_action :set_account before_action :set_account
respond_to :txt respond_to :txt
......
# frozen_string_literal: true
class Api::SubscriptionsController < ApiController class Api::SubscriptionsController < ApiController
before_action :set_account before_action :set_account
respond_to :txt respond_to :txt
......
# frozen_string_literal: true
class Api::V1::AccountsController < ApiController class Api::V1::AccountsController < ApiController
before_action -> { doorkeeper_authorize! :read }, except: [:follow, :unfollow, :block, :unblock] before_action -> { doorkeeper_authorize! :read }, except: [:follow, :unfollow, :block, :unblock]
before_action -> { doorkeeper_authorize! :follow }, only: [:follow, :unfollow, :block, :unblock] before_action -> { doorkeeper_authorize! :follow }, only: [:follow, :unfollow, :block, :unblock]
...@@ -20,7 +22,7 @@ class Api::V1::AccountsController < ApiController ...@@ -20,7 +22,7 @@ class Api::V1::AccountsController < ApiController
@accounts = results.map { |f| accounts[f.target_account_id] } @accounts = results.map { |f| accounts[f.target_account_id] }
next_path = following_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT next_path = following_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
prev_path = following_api_v1_account_url(since_id: results.first.id) if results.size > 0 prev_path = following_api_v1_account_url(since_id: results.first.id) unless results.empty?
set_pagination_headers(next_path, prev_path) set_pagination_headers(next_path, prev_path)
...@@ -33,7 +35,7 @@ class Api::V1::AccountsController < ApiController ...@@ -33,7 +35,7 @@ class Api::V1::AccountsController < ApiController
@accounts = results.map { |f| accounts[f.account_id] } @accounts = results.map { |f| accounts[f.account_id] }
next_path = followers_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT next_path = followers_api_v1_account_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
prev_path = followers_api_v1_account_url(since_id: results.first.id) if results.size > 0 prev_path = followers_api_v1_account_url(since_id: results.first.id) unless results.empty?
set_pagination_headers(next_path, prev_path) set_pagination_headers(next_path, prev_path)
...@@ -56,7 +58,7 @@ class Api::V1::AccountsController < ApiController ...@@ -56,7 +58,7 @@ class Api::V1::AccountsController < ApiController
set_maps(@statuses) set_maps(@statuses)
next_path = statuses_api_v1_account_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT next_path = statuses_api_v1_account_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
prev_path = statuses_api_v1_account_url(since_id: @statuses.first.id) if @statuses.size > 0 prev_path = statuses_api_v1_account_url(since_id: @statuses.first.id) unless @statuses.empty?
set_pagination_headers(next_path, prev_path) set_pagination_headers(next_path, prev_path)
end end
......
# frozen_string_literal: true
class Api::V1::AppsController < ApiController class Api::V1::AppsController < ApiController
respond_to :json respond_to :json
......
# frozen_string_literal: true
class Api::V1::FollowsController < ApiController class Api::V1::FollowsController < ApiController
before_action -> { doorkeeper_authorize! :follow } before_action -> { doorkeeper_authorize! :follow }
before_action :require_user! before_action :require_user!
......
# frozen_string_literal: true
class Api::V1::MediaController < ApiController class Api::V1::MediaController < ApiController
before_action -> { doorkeeper_authorize! :write } before_action -> { doorkeeper_authorize! :write }
before_action :require_user! before_action :require_user!
......
# frozen_string_literal: true
class Api::V1::StatusesController < ApiController class Api::V1::StatusesController < ApiController
before_action -> { doorkeeper_authorize! :read }, except: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite] before_action -> { doorkeeper_authorize! :read }, except: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite]
before_action -> { doorkeeper_authorize! :write }, only: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite] before_action -> { doorkeeper_authorize! :write }, only: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite]
...@@ -10,7 +12,7 @@ class Api::V1::StatusesController < ApiController ...@@ -10,7 +12,7 @@ class Api::V1::StatusesController < ApiController
end end
def context def context
@context = OpenStruct.new({ ancestors: @status.ancestors, descendants: @status.descendants }) @context = OpenStruct.new(ancestors: @status.ancestors, descendants: @status.descendants)
set_maps([@status] + @context[:ancestors] + @context[:descendants]) set_maps([@status] + @context[:ancestors] + @context[:descendants])
end end
...@@ -20,7 +22,7 @@ class Api::V1::StatusesController < ApiController ...@@ -20,7 +22,7 @@ class Api::V1::StatusesController < ApiController
@accounts = results.map { |r| accounts[r.account_id] } @accounts = results.map { |r| accounts[r.account_id] }
next_path = reblogged_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT next_path = reblogged_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
prev_path = reblogged_by_api_v1_status_url(since_id: results.first.id) if results.size > 0 prev_path = reblogged_by_api_v1_status_url(since_id: results.first.id) unless results.empty?
set_pagination_headers(next_path, prev_path) set_pagination_headers(next_path, prev_path)
...@@ -33,7 +35,7 @@ class Api::V1::StatusesController < ApiController ...@@ -33,7 +35,7 @@ class Api::V1::StatusesController < ApiController
@accounts = results.map { |f| accounts[f.account_id] } @accounts = results.map { |f| accounts[f.account_id] }
next_path = favourited_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT next_path = favourited_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
prev_path = favourited_by_api_v1_status_url(since_id: results.first.id) if results.size > 0 prev_path = favourited_by_api_v1_status_url(since_id: results.first.id) unless results.empty?
set_pagination_headers(next_path, prev_path) set_pagination_headers(next_path, prev_path)
......
# frozen_string_literal: true
class Api::V1::TimelinesController < ApiController class Api::V1::TimelinesController < ApiController
before_action -> { doorkeeper_authorize! :read } before_action -> { doorkeeper_authorize! :read }
before_action :require_user!, only: [:home, :mentions] before_action :require_user!, only: [:home, :mentions]
...@@ -10,7 +12,7 @@ class Api::V1::TimelinesController < ApiController ...@@ -10,7 +12,7 @@ class Api::V1::TimelinesController < ApiController
set_maps(@statuses) set_maps(@statuses)
next_path = api_v1_home_timeline_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT next_path = api_v1_home_timeline_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
prev_path = api_v1_home_timeline_url(since_id: @statuses.first.id) if @statuses.size > 0 prev_path = api_v1_home_timeline_url(since_id: @statuses.first.id) unless @statuses.empty?
set_pagination_headers(next_path, prev_path) set_pagination_headers(next_path, prev_path)
...@@ -23,7 +25,7 @@ class Api::V1::TimelinesController < ApiController ...@@ -23,7 +25,7 @@ class Api::V1::TimelinesController < ApiController
set_maps(@statuses) set_maps(@statuses)
next_path = api_v1_mentions_timeline_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT next_path = api_v1_mentions_timeline_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
prev_path = api_v1_mentions_timeline_url(since_id: @statuses.first.id) if @statuses.size > 0 prev_path = api_v1_mentions_timeline_url(since_id: @statuses.first.id) unless @statuses.empty?
set_pagination_headers(next_path, prev_path) set_pagination_headers(next_path, prev_path)
...@@ -36,7 +38,7 @@ class Api::V1::TimelinesController < ApiController ...@@ -36,7 +38,7 @@ class Api::V1::TimelinesController < ApiController
set_maps(@statuses) set_maps(@statuses)
next_path = api_v1_public_timeline_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT next_path = api_v1_public_timeline_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
prev_path = api_v1_public_timeline_url(since_id: @statuses.first.id) if @statuses.size > 0 prev_path = api_v1_public_timeline_url(since_id: @statuses.first.id) unless @statuses.empty?
set_pagination_headers(next_path, prev_path) set_pagination_headers(next_path, prev_path)
...@@ -50,7 +52,7 @@ class Api::V1::TimelinesController < ApiController ...@@ -50,7 +52,7 @@ class Api::V1::TimelinesController < ApiController
set_maps(@statuses) set_maps(@statuses)
next_path = api_v1_hashtag_timeline_url(params[:id], max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT next_path = api_v1_hashtag_timeline_url(params[:id], max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
prev_path = api_v1_hashtag_timeline_url(params[:id], since_id: @statuses.first.id) if @statuses.size > 0 prev_path = api_v1_hashtag_timeline_url(params[:id], since_id: @statuses.first.id) unless @statuses.empty?
set_pagination_headers(next_path, prev_path) set_pagination_headers(next_path, prev_path)
......
# frozen_string_literal: true
class ApiController < ApplicationController class ApiController < ApplicationController
DEFAULT_STATUSES_LIMIT = 20 DEFAULT_STATUSES_LIMIT = 20
DEFAULT_ACCOUNTS_LIMIT = 40 DEFAULT_ACCOUNTS_LIMIT = 40
...@@ -51,8 +53,8 @@ class ApiController < ApplicationController ...@@ -51,8 +53,8 @@ class ApiController < ApplicationController
def set_pagination_headers(next_path = nil, prev_path = nil) def set_pagination_headers(next_path = nil, prev_path = nil)
links = [] links = []
links << [next_path, [['rel', 'next']]] if next_path links << [next_path, [%w(rel next)]] if next_path
links << [prev_path, [['rel', 'prev']]] if prev_path links << [prev_path, [%w(rel prev)]] if prev_path
response.headers['Link'] = LinkHeader.new(links) response.headers['Link'] = LinkHeader.new(links)
end end
...@@ -76,7 +78,7 @@ class ApiController < ApplicationController ...@@ -76,7 +78,7 @@ class ApiController < ApplicationController
render json: {}, status: 200 render json: {}, status: 200
end end
def set_maps(statuses) def set_maps(statuses) # rubocop:disable Style/AccessorMethodName
if current_account.nil? if current_account.nil?
@reblogs_map = {} @reblogs_map = {}
@favourites_map = {} @favourites_map = {}
......
# frozen_string_literal: true
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception. # Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead. # For APIs, you may want to use :null_session instead.
......
# frozen_string_literal: true
class Auth::ConfirmationsController < Devise::ConfirmationsController class Auth::ConfirmationsController < Devise::ConfirmationsController
layout 'auth' layout 'auth'
end end
# frozen_string_literal: true
class Auth::PasswordsController < Devise::PasswordsController class Auth::PasswordsController < Devise::PasswordsController
layout 'auth' layout 'auth'
end end
# frozen_string_literal: true
class Auth::RegistrationsController < Devise::RegistrationsController class Auth::RegistrationsController < Devise::RegistrationsController
layout 'auth' layout 'auth'
......
# frozen_string_literal: true
class Auth::SessionsController < Devise::SessionsController class Auth::SessionsController < Devise::SessionsController
include Devise::Controllers::Rememberable include Devise::Controllers::Rememberable
......
# frozen_string_literal: true
class HomeController < ApplicationController class HomeController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
......
# frozen_string_literal: true
class MediaController < ApplicationController class MediaController < ApplicationController
before_action :set_media_attachment before_action :set_media_attachment
......
# frozen_string_literal: true
class Oauth::AuthorizationsController < Doorkeeper::AuthorizationsController class Oauth::AuthorizationsController < Doorkeeper::AuthorizationsController
skip_before_action :authenticate_resource_owner! skip_before_action :authenticate_resource_owner!
......
# frozen_string_literal: true
class Settings::PreferencesController < ApplicationController class Settings::PreferencesController < ApplicationController
layout 'auth' layout 'auth'
......
# frozen_string_literal: true
class Settings::ProfilesController < ApplicationController class Settings::ProfilesController < ApplicationController
layout 'auth' layout 'auth'
......
# frozen_string_literal: true
class StreamEntriesController < ApplicationController class StreamEntriesController < ApplicationController
layout 'public' layout 'public'
...@@ -29,9 +31,7 @@ class StreamEntriesController < ApplicationController ...@@ -29,9 +31,7 @@ class StreamEntriesController < ApplicationController
end end
def set_link_headers def set_link_headers
response.headers['Link'] = LinkHeader.new([ response.headers['Link'] = LinkHeader.new([[account_stream_entry_url(@account, @stream_entry, format: 'atom'), [%w(rel alternate), %w(type application/atom+xml)]]])
[account_stream_entry_url(@account, @stream_entry, format: 'atom'), [['rel', 'alternate'], ['type', 'application/atom+xml']]]
])
end end
def set_stream_entry def set_stream_entry
......
# frozen_string_literal: true
class TagsController < ApplicationController class TagsController < ApplicationController
layout 'public' layout 'public'
......
# frozen_string_literal: true
class XrdController < ApplicationController class XrdController < ApplicationController
before_action :set_default_format_json, only: :webfinger before_action :set_default_format_json, only: :webfinger
before_action :set_default_format_xml, only: :host_meta before_action :set_default_format_xml, only: :host_meta
...@@ -26,11 +28,11 @@ class XrdController < ApplicationController ...@@ -26,11 +28,11 @@ class XrdController < ApplicationController
private private
def set_default_format_xml def set_default_format_xml
request.format = 'xml' if request.headers["HTTP_ACCEPT"].nil? && params[:format].nil? request.format = 'xml' if request.headers['HTTP_ACCEPT'].nil? && params[:format].nil?
end end
def set_default_format_json def set_default_format_json
request.format = 'json' if request.headers["HTTP_ACCEPT"].nil? && params[:format].nil? request.format = 'json' if request.headers['HTTP_ACCEPT'].nil? && params[:format].nil?
end end
def username_from_resource def username_from_resource
...@@ -44,14 +46,14 @@ class XrdController < ApplicationController ...@@ -44,14 +46,14 @@ class XrdController < ApplicationController
def pem_to_magic_key(public_key) def pem_to_magic_key(public_key)
modulus, exponent = [public_key.n, public_key.e].map do |component| modulus, exponent = [public_key.n, public_key.e].map do |component|
result = '' result = []
until component.zero? until component.zero?
result << [component % 256].pack('C') result << [component % 256].pack('C')
component >>= 8 component >>= 8
end end
result.reverse! result.reverse.join
end end
(['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.') (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
......
# frozen_string_literal: true
module AboutHelper module AboutHelper
end end
# frozen_string_literal: true
module AccountsHelper module AccountsHelper
def pagination_options def pagination_options
{ {
previous_label: "#{fa_icon('chevron-left')} Prev".html_safe, previous_label: safe_join([fa_icon('chevron-left'), 'Prev'], ' '),
next_label: "Next #{fa_icon('chevron-right')}".html_safe, next_label: safe_join(['Next', fa_icon('chevron-right')], ' '),
inner_window: 1, inner_window: 1,
outer_window: 0 outer_window: 0,
} }
end end
end end
# frozen_string_literal: true
module ApplicationHelper module ApplicationHelper
def active_nav_class(path) def active_nav_class(path)
current_page?(path) ? 'active' : '' current_page?(path) ? 'active' : ''
......
# frozen_string_literal: true
module AtomBuilderHelper module AtomBuilderHelper
def stream_updated_at def stream_updated_at
@account.stream_entries.last ? (@account.updated_at > @account.stream_entries.last.created_at ? @account.updated_at : @account.stream_entries.last.created_at) : @account.updated_at if @account.stream_entries.last
(@account.updated_at > @account.stream_entries.last.created_at ? @account.updated_at : @account.stream_entries.last.created_at)
else
@account.updated_at
end
end end
def entry(xml, is_root = false, &block) def entry(xml, is_root = false, &block)
...@@ -98,7 +104,7 @@ module AtomBuilderHelper ...@@ -98,7 +104,7 @@ module AtomBuilderHelper
end end
def in_reply_to(xml, uri, url) def in_reply_to(xml, uri, url)
xml['thr'].send('in-reply-to', { ref: uri, href: url, type: 'text/html' }) xml['thr'].send('in-reply-to', ref: uri, href: url, type: 'text/html')
end end
def link_mention(xml, account) def link_mention(xml, account)
......
# frozen_string_literal: true
module HomeHelper module HomeHelper
def default_props def default_props
{ {
token: @token, token: @token,
account: render(file: 'api/v1/accounts/show', locals: { account: current_user.account }, formats: :json) account: render(file: 'api/v1/accounts/show', locals: { account: current_user.account }, formats: :json),
} }
end end
end end
# frozen_string_literal: true
module RoutingHelper module RoutingHelper