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

This commit is contained in:
Eugen Rochko
2016-11-15 16:56:29 +01:00
parent a91c3ef6ce
commit fdc17bea58
96 changed files with 329 additions and 126 deletions

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class BaseService
include ActionView::Helpers::TextHelper
include ActionView::Helpers::SanitizeHelper

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class BlockDomainService < BaseService
def call(domain)
DomainBlock.find_or_create_by!(domain: domain)

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class BlockService < BaseService
def call(account, target_account)
return if account.id == target_account.id
@ -20,6 +22,6 @@ class BlockService < BaseService
end
def redis
$redis
Redis.current
end
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class FanOutOnWriteService < BaseService
# Push a status into home and mentions feeds
# @param [Status] status

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class FavouriteService < BaseService
# Favourite a status and notify remote user
# @param [Account] account

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class FetchAtomService < BaseService
def call(url)
response = http_client.head(url)
@ -9,15 +11,9 @@ class FetchAtomService < BaseService
Rails.logger.debug "Remote status GET request returned code #{response.code}"
return nil if response.code != 200
if response.mime_type == 'application/atom+xml'
return [url, fetch(url)]
elsif !response['Link'].blank?
return process_headers(url, response)
else
return process_html(fetch(url))
end
return [url, fetch(url)] if response.mime_type == 'application/atom+xml'
return process_headers(url, response) unless response['Link'].blank?
process_html(fetch(url))
rescue OpenSSL::SSL::SSLError => e
Rails.logger.debug "SSL error: #{e}"
end
@ -31,17 +27,17 @@ class FetchAtomService < BaseService
alternate_link = page.xpath('//link[@rel="alternate"]').find { |link| link['type'] == 'application/atom+xml' }
return nil if alternate_link.nil?
return [alternate_link['href'], fetch(alternate_link['href'])]
[alternate_link['href'], fetch(alternate_link['href'])]
end
def process_headers(url, response)
Rails.logger.debug 'Processing link header'
link_header = LinkHeader.parse(response['Link'].is_a?(Array) ? response['Link'].first : response['Link'])
alternate_link = link_header.find_link(['rel', 'alternate'], ['type', 'application/atom+xml'])
alternate_link = link_header.find_link(%w(rel alternate), %w(type application/atom+xml))
return process_html(fetch(url)) if alternate_link.nil?
return [alternate_link.href, fetch(alternate_link.href)]
[alternate_link.href, fetch(alternate_link.href)]
end
def fetch(url)

View File

@ -1,9 +1,11 @@
# frozen_string_literal: true
class FetchRemoteAccountService < BaseService
def call(url)
atom_url, body = FetchAtomService.new.call(url)
return nil if atom_url.nil?
return process_atom(atom_url, body)
process_atom(atom_url, body)
end
private
@ -25,7 +27,7 @@ class FetchRemoteAccountService < BaseService
Rails.logger.debug "Unparseable URL given: #{url}"
nil
rescue Nokogiri::XML::XPath::SyntaxError
Rails.logger.debug "Invalid XML or missing namespace"
Rails.logger.debug 'Invalid XML or missing namespace'
nil
end
end

View File

@ -1,9 +1,11 @@
# frozen_string_literal: true
class FetchRemoteStatusService < BaseService
def call(url)
atom_url, body = FetchAtomService.new.call(url)
return nil if atom_url.nil?
return process_atom(atom_url, body)
process_atom(atom_url, body)
end
private
@ -20,7 +22,7 @@ class FetchRemoteStatusService < BaseService
statuses = ProcessFeedService.new.call(body, account)
return statuses.first
statuses.first
end
def extract_author(url, xml)
@ -34,7 +36,7 @@ class FetchRemoteStatusService < BaseService
return FollowRemoteAccountService.new.call("#{username}@#{domain}")
rescue Nokogiri::XML::XPath::SyntaxError
Rails.logger.debug "Invalid XML or missing namespace"
Rails.logger.debug 'Invalid XML or missing namespace'
nil
end
end

View File

@ -1,7 +1,9 @@
# frozen_string_literal: true
class FollowRemoteAccountService < BaseService
include OStatus2::MagicKey
DFRN_NS = 'http://purl.org/macgirvin/dfrn/1.0'.freeze
DFRN_NS = 'http://purl.org/macgirvin/dfrn/1.0'
# Find or create a local account for a remote user.
# When creating, look up the user's webfinger and fetch all
@ -49,7 +51,7 @@ class FollowRemoteAccountService < BaseService
get_profile(xml, account)
account.save!
return account
account
end
private

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class FollowService < BaseService
# Follow a remote user, notify remote user about the follow
# @param [Account] source_account From which to follow
@ -35,7 +37,7 @@ class FollowService < BaseService
end
def redis
$redis
Redis.current
end
def follow_remote_account_service

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class PostStatusService < BaseService
# Post a text status update, fetch and notify remote users mentioned
# @param [Account] account Account from which to post

View File

@ -1,10 +1,10 @@
# frozen_string_literal: true
class PrecomputeFeedService < BaseService
# Fill up a user's home/mentions feed from DB and return a subset
# @param [Symbol] type :home or :mentions
# @param [Account] account
def call(type, account)
instant_return = []
Status.send("as_#{type}_timeline", account).limit(FeedManager::MAX_ITEMS).each do |status|
next if FeedManager.instance.filter?(type, status, account)
redis.zadd(FeedManager.instance.key(type, account.id), status.id, status.reblog? ? status.reblog_of_id : status.id)
@ -14,6 +14,6 @@ class PrecomputeFeedService < BaseService
private
def redis
$redis
Redis.current
end
end

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true
class ProcessFeedService < BaseService
ACTIVITY_NS = 'http://activitystrea.ms/spec/1.0/'.freeze
THREAD_NS = 'http://purl.org/syndication/thread/1.0'.freeze
ACTIVITY_NS = 'http://activitystrea.ms/spec/1.0/'
THREAD_NS = 'http://purl.org/syndication/thread/1.0'
def call(body, account)
xml = Nokogiri::XML(body)
@ -89,13 +91,13 @@ class ProcessFeedService < BaseService
account = @account
end
status = Status.create!({
status = Status.create!(
uri: id(entry),
url: url(entry),
account: account,
text: content(entry),
created_at: published(entry),
})
created_at: published(entry)
)
if thread?(entry)
Rails.logger.debug "Trying to attach #{status.id} (#{id(entry)}) to #{thread(entry).first}"

View File

@ -1,8 +1,8 @@
# frozen_string_literal: true
class ProcessHashtagsService < BaseService
def call(status, tags = [])
if status.local?
tags = status.text.scan(Tag::HASHTAG_RE).map(&:first)
end
tags = status.text.scan(Tag::HASHTAG_RE).map(&:first) if status.local?
tags.map(&:downcase).uniq.each do |tag|
status.tags << Tag.where(name: tag).first_or_initialize(name: tag)

View File

@ -1,5 +1,7 @@
# frozen_string_literal: true
class ProcessInteractionService < BaseService
ACTIVITY_NS = 'http://activitystrea.ms/spec/1.0/'.freeze
ACTIVITY_NS = 'http://activitystrea.ms/spec/1.0/'
# Record locally the remote interaction with our user
# @param [String] envelope Salmon envelope
@ -76,9 +78,7 @@ class ProcessInteractionService < BaseService
return if status.nil?
if account.id == status.account_id
remove_status_service.call(status)
end
remove_status_service.call(status) if account.id == status.account_id
end
def favourite!(xml, from_account)

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class ProcessMentionsService < BaseService
# Scan status for mentions and fetch remote mentioned users, create
# local mention pointers, send Salmon notifications to mentioned

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class ReblogService < BaseService
# Reblog a status and notify its remote author
# @param [Account] account Account to reblog from

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class RemoveStatusService < BaseService
def call(status)
remove_from_self(status) if status.account.local?
@ -62,6 +64,6 @@ class RemoveStatusService < BaseService
end
def redis
$redis
Redis.current
end
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class SearchService < BaseService
def call(query, limit, resolve = false)
return if query.blank?
@ -5,10 +7,10 @@ class SearchService < BaseService
username, domain = query.split('@')
results = if domain.nil?
Account.search_for(username)
else
Account.search_for("#{username} #{domain}")
end
Account.search_for(username)
else
Account.search_for("#{username} #{domain}")
end
results = results.limit(limit).with_counters

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class SendInteractionService < BaseService
# Send an Atom representation of an interaction to a remote Salmon endpoint
# @param [StreamEntry] stream_entry

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class SubscribeService < BaseService
def call(account)
account.secret = SecureRandom.hex

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class UnblockService < BaseService
def call(account, target_account)
account.unblock!(target_account) if account.blocking?(target_account)

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class UnfavouriteService < BaseService
def call(account, status)
favourite = Favourite.find_by!(account: account, status: status)

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class UnfollowService < BaseService
# Unfollow and notify the remote user
# @param [Account] source_account Where to unfollow from
@ -21,6 +23,6 @@ class UnfollowService < BaseService
end
def redis
$redis
Redis.current
end
end

View File

@ -1,14 +1,16 @@
# frozen_string_literal: true
class UpdateRemoteProfileService < BaseService
POCO_NS = 'http://portablecontacts.net/spec/1.0'
def call(author_xml, account)
return if author_xml.nil?
if author_xml.at_xpath('./poco:displayName', poco: POCO_NS).nil?
account.display_name = account.username
else
account.display_name = author_xml.at_xpath('./poco:displayName', poco: POCO_NS).content
end
account.display_name = if author_xml.at_xpath('./poco:displayName', poco: POCO_NS).nil?
account.username
else
author_xml.at_xpath('./poco:displayName', poco: POCO_NS).content
end
unless author_xml.at_xpath('./poco:note').nil?
account.note = author_xml.at_xpath('./poco:note', poco: POCO_NS).content