Skip to content

Commit

Permalink
Improve code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron committed Sep 29, 2016
1 parent e4aebad commit 927333f
Show file tree
Hide file tree
Showing 41 changed files with 126 additions and 122 deletions.
14 changes: 14 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Rails:
Enabled: true

Metrics/LineLength:
Enabled: false

Style/PerlBackrefs:
AutoCorrect: false

Style/ClassAndModuleChildren:
Enabled: false

Documentation:
Enabled: false
1 change: 0 additions & 1 deletion app/channels/application_cable/channel.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading.
module ApplicationCable
class Channel < ActionCable::Channel::Base
end
Expand Down
1 change: 0 additions & 1 deletion app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading.
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
Expand Down
1 change: 0 additions & 1 deletion app/channels/timeline_channel.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading.
class TimelineChannel < ApplicationCable::Channel
def subscribed
stream_from "timeline:#{current_user.account_id}"
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class AccountsController < ApplicationController
def show
respond_to do |format|
format.html do
@statuses = @account.statuses.order('id desc').with_includes.with_counters.paginate(page: params[:page], per_page: 10)
@statuses = @account.statuses.order('id desc').with_includes.with_counters.paginate(page: params[:page], per_page: 10)

if user_signed_in?
status_ids = @statuses.collect { |s| [s.id, s.reblog_of_id] }.flatten.uniq
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/salmon_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Api::SalmonController < ApiController
respond_to :txt

def update
ProcessInteractionService.new.(request.body.read, @account)
ProcessInteractionService.new.call(request.body.read, @account)
head 201
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Api::SubscriptionsController < ApiController

def show
if @account.subscription(api_subscription_url(@account.id)).valid?(params['hub.topic'])
@account.update(subscription_expires_at: Time.now + ((params['hub.lease_seconds'] || 86400).to_i).seconds)
@account.update(subscription_expires_at: Time.now.utc + (params['hub.lease_seconds'] || 86_400).to_i.seconds)
render plain: HTMLEntities.new.encode(params['hub.challenge']), status: 200
else
head 404
Expand All @@ -15,7 +15,7 @@ def update
body = request.body.read

if @account.subscription(api_subscription_url(@account.id)).verify(body, request.headers['HTTP_X_HUB_SIGNATURE'])
ProcessFeedService.new.(body, @account)
ProcessFeedService.new.call(body, @account)
head 201
else
head 202
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/api/v1/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ def statuses
end

def follow
@follow = FollowService.new.(current_user.account, @account.acct)
@follow = FollowService.new.call(current_user.account, @account.acct)
set_relationship
render action: :relationship
end

def unfollow
@unfollow = UnfollowService.new.(current_user.account, @account)
@unfollow = UnfollowService.new.call(current_user.account, @account)
set_relationship
render action: :relationship
end

def relationships
ids = params[:id].is_a?(Enumerable) ? params[:id].map { |id| id.to_i } : [params[:id].to_i]
ids = params[:id].is_a?(Enumerable) ? params[:id].map(&:to_i) : [params[:id].to_i]
@accounts = Account.find(ids)
@following = Account.following_map(ids, current_user.account_id)
@followed_by = Account.followed_by_map(ids, current_user.account_id)
Expand Down
6 changes: 2 additions & 4 deletions app/controllers/api/v1/follows_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ class Api::V1::FollowsController < ApiController
respond_to :json

def create
if params[:uri].blank?
raise ActiveRecord::RecordNotFound
end
raise ActiveRecord::RecordNotFound if params[:uri].blank?

@account = FollowService.new.(current_user.account, params[:uri]).try(:target_account)
@account = FollowService.new.call(current_user.account, params[:uri]).try(:target_account)
render action: :show
end
end
12 changes: 6 additions & 6 deletions app/controllers/api/v1/statuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@ def context
end

def create
@status = PostStatusService.new.(current_user.account, params[:status], params[:in_reply_to_id].blank? ? nil : Status.find(params[:in_reply_to_id]), params[:media_ids])
@status = PostStatusService.new.call(current_user.account, params[:status], params[:in_reply_to_id].blank? ? nil : Status.find(params[:in_reply_to_id]), params[:media_ids])
render action: :show
end

def destroy
@status = Status.where(account_id: current_user.account).find(params[:id])
RemoveStatusService.new.(@status)
RemoveStatusService.new.call(@status)
render_empty
end

def reblog
@status = ReblogService.new.(current_user.account, Status.find(params[:id])).reload
@status = ReblogService.new.call(current_user.account, Status.find(params[:id])).reload
render action: :show
end

def unreblog
RemoveStatusService.new.(Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!)
RemoveStatusService.new.call(Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!)
@status = Status.find(params[:id])
render action: :show
end

def favourite
@status = FavouriteService.new.(current_user.account, Status.find(params[:id])).status.reload
@status = FavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
render action: :show
end

def unfavourite
@status = UnfavouriteService.new.(current_user.account, Status.find(params[:id])).status.reload
@status = UnfavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
render action: :show
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :not_found

def raise_not_found
raise ActionController::RoutingError.new("No route matches #{params[:unmatched_route]}")
raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
end

protected
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/auth/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class Auth::RegistrationsController < Devise::RegistrationsController
layout 'auth'

before_filter :configure_sign_up_params, only: [:create]
before_action :configure_sign_up_params, only: [:create]

protected

def build_resource(hash = nil)
super(hash)
self.resource.build_account if self.resource.account.nil?
resource.build_account if resource.account.nil?
end

def configure_sign_up_params
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def index
@mentions = Feed.new(:mentions, current_user.account).get(20)
@token = find_or_create_access_token.token
end

private

def authenticate_user!
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class SettingsController < ApplicationController
layout 'auth'

before_action :authenticate_user!
before_action :set_account

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/stream_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def show
@descendants = @stream_entry.activity.descendants

if user_signed_in?
status_ids = [@stream_entry.activity_id] + @ancestors.map { |s| s.id } + @descendants.map { |s| s.id }
status_ids = [@stream_entry.activity_id] + @ancestors.map(&:id) + @descendants.map(&:id)
@favourited = Status.favourites_map(status_ids, current_user.account_id)
@reblogged = Status.reblogs_map(status_ids, current_user.account_id)
else
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/xrd_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ def username_from_resource

def pem_to_magic_key(public_key)
modulus, exponent = [public_key.n, public_key.e].map do |component|
result = ""
result = ''

until component == 0 do
until component.zero?
result << [component % 256].pack('C')
component >>= 8
end

result.reverse!
end

(["RSA"] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
(['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
end

def resource_param
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/atom_builder_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,6 @@ def root_tag(xml, tag, &block)
end

def single_link_avatar(xml, account, size, px)
xml.link('rel' => 'avatar', 'type' => account.avatar_content_type, 'media:width' => px, 'media:height' =>px, 'href' => full_asset_url(account.avatar.url(size, false)))
xml.link('rel' => 'avatar', 'type' => account.avatar_content_type, 'media:width' => px, 'media:height' => px, 'href' => full_asset_url(account.avatar.url(size, false)))
end
end
6 changes: 3 additions & 3 deletions app/helpers/stream_entries_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def entry_classes(status, is_predecessor, is_successor, include_threads)
end

def relative_time(date)
date < 5.days.ago ? date.strftime("%d.%m.%Y") : "#{time_ago_in_words(date)} ago"
date < 5.days.ago ? date.strftime('%d.%m.%Y') : "#{time_ago_in_words(date)} ago"
end

def reblogged_by_me_class(status)
user_signed_in? && @reblogged.has_key?(status.id) ? 'reblogged' : ''
user_signed_in? && @reblogged.key?(status.id) ? 'reblogged' : ''
end

def favourited_by_me_class(status)
user_signed_in? && @favourited.has_key?(status.id) ? 'favourited' : ''
user_signed_in? && @favourited.key?(status.id) ? 'favourited' : ''
end

def proper_status(status)
Expand Down
2 changes: 1 addition & 1 deletion app/lib/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def link_urls(html)
def link_mentions(html, mentions)
html.gsub(Account::MENTION_RE) do |match|
acct = Account::MENTION_RE.match(match)[1]
mention = mentions.find { |item| item.account.acct.eql?(acct) }
mention = mentions.find { |item| item.account.acct.casecmp(acct).zero? }

mention.nil? ? match : mention_html(match, mention.account)
end
Expand Down
32 changes: 14 additions & 18 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Account < ApplicationRecord
include Targetable

MENTION_RE = /(?:^|\s|\.|>)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif']
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze

# Local users
has_one :user, inverse_of: :account
Expand Down Expand Up @@ -45,11 +45,11 @@ class Account < ApplicationRecord
scope :expiring, -> (time) { where(subscription_expires_at: nil).or(where('subscription_expires_at < ?', time)).remote.with_followers }

def follow!(other_account)
self.active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
end

def unfollow!(other_account)
follow = self.active_relationships.find_by(target_account: other_account)
follow = active_relationships.find_by(target_account: other_account)
follow.destroy unless follow.nil?
end

Expand All @@ -58,15 +58,15 @@ def following?(other_account)
end

def local?
self.domain.nil?
domain.nil?
end

def acct
local? ? self.username : "#{self.username}@#{self.domain}"
local? ? username : "#{username}@#{domain}"
end

def subscribed?
!self.subscription_expires_at.nil?
!subscription_expires_at.nil?
end

def favourited?(status)
Expand All @@ -78,11 +78,11 @@ def reblogged?(status)
end

def keypair
self.private_key.nil? ? OpenSSL::PKey::RSA.new(self.public_key) : OpenSSL::PKey::RSA.new(self.private_key)
private_key.nil? ? OpenSSL::PKey::RSA.new(public_key) : OpenSSL::PKey::RSA.new(private_key)
end

def subscription(webhook_url)
OStatus2::Subscription.new(self.remote_url, secret: self.secret, lease_seconds: 86400 * 30, webhook: webhook_url, hub: self.hub_url)
OStatus2::Subscription.new(remote_url, secret: secret, lease_seconds: 86_400 * 30, webhook: webhook_url, hub: hub_url)
end

def ping!(atom_url, hubs)
Expand All @@ -91,10 +91,7 @@ def ping!(atom_url, hubs)
end

def avatar_remote_url=(url)
unless self[:avatar_remote_url] == url
self.avatar = URI.parse(url)
end

self.avatar = URI.parse(url) unless self[:avatar_remote_url] == url
self[:avatar_remote_url] = url
end

Expand All @@ -103,26 +100,25 @@ def object_type
end

def to_param
self.username
username
end

def self.find_local!(username)
self.find_remote!(username, nil)
find_remote!(username, nil)
end

def self.find_remote!(username, domain)
table = self.arel_table
self.where(table[:username].matches(username)).where(domain: domain).take!
where(arel_table[:username].matches(username)).where(domain: domain).take!
end

def self.find_local(username)
self.find_local!(username)
find_local!(username)
rescue ActiveRecord::RecordNotFound
nil
end

def self.find_remote(username, domain)
self.find_remote!(username, domain)
find_remote!(username, domain)
rescue ActiveRecord::RecordNotFound
nil
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/streamable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def thread
end

after_create do
self.account.stream_entries.create!(activity: self) if self.account.local?
account.stream_entries.create!(activity: self) if account.local?
end
end
end
4 changes: 2 additions & 2 deletions app/models/favourite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ def verb
end

def title
"#{self.account.acct} favourited a status by #{self.status.account.acct}"
"#{account.acct} favourited a status by #{status.account.acct}"
end

def object_type
target.object_type
end

def thread
self.status
status
end

def target
Expand Down
4 changes: 2 additions & 2 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def initialize(type, account)
def get(limit, max_id = nil)
max_id = '+inf' if max_id.nil?
unhydrated = redis.zrevrangebyscore(key, "(#{max_id}", '-inf', limit: [0, limit])
status_map = Hash.new
status_map = {}

# If we're after most recent items and none are there, we need to precompute the feed
if unhydrated.empty? && max_id == '+inf'
PrecomputeFeedService.new.(@type, @account, limit)
PrecomputeFeedService.new.call(@type, @account, limit)
else
Status.where(id: unhydrated).with_includes.with_counters.each { |status| status_map[status.id.to_s] = status }
unhydrated.map { |id| status_map[id] }.compact
Expand Down
Loading

0 comments on commit 927333f

Please sign in to comment.