Skip to content
Extraits de code Groupes Projets
Valider fdc17bea rédigé par Eugen Rochko's avatar Eugen Rochko
Parcourir les fichiers

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

parent a91c3ef6
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de
avec 65 ajouts et 37 suppressions
# frozen_string_literal: true
class Tag < ApplicationRecord
has_and_belongs_to_many :statuses
......
# frozen_string_literal: true
class User < ApplicationRecord
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
......
# frozen_string_literal: true
class BaseService
include ActionView::Helpers::TextHelper
include ActionView::Helpers::SanitizeHelper
......
# frozen_string_literal: true
class BlockDomainService < BaseService
def call(domain)
DomainBlock.find_or_create_by!(domain: domain)
......
# 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
# frozen_string_literal: true
class FanOutOnWriteService < BaseService
# Push a status into home and mentions feeds
# @param [Status] status
......
# frozen_string_literal: true
class FavouriteService < BaseService
# Favourite a status and notify remote user
# @param [Account] account
......
# 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)
......
# 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
# 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
# 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
......
# 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
......
# 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
......
# 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
# 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}"
......
# 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)
......
# 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)
......
# 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
......
# frozen_string_literal: true
class ReblogService < BaseService
# Reblog a status and notify its remote author
# @param [Account] account Account to reblog from
......
# 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
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter