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 class Tag < ApplicationRecord
has_and_belongs_to_many :statuses has_and_belongs_to_many :statuses
......
# frozen_string_literal: true
class User < ApplicationRecord class User < ApplicationRecord
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
......
# frozen_string_literal: true
class BaseService class BaseService
include ActionView::Helpers::TextHelper include ActionView::Helpers::TextHelper
include ActionView::Helpers::SanitizeHelper include ActionView::Helpers::SanitizeHelper
......
# frozen_string_literal: true
class BlockDomainService < BaseService class BlockDomainService < BaseService
def call(domain) def call(domain)
DomainBlock.find_or_create_by!(domain: domain) DomainBlock.find_or_create_by!(domain: domain)
......
# frozen_string_literal: true
class BlockService < BaseService class BlockService < BaseService
def call(account, target_account) def call(account, target_account)
return if account.id == target_account.id return if account.id == target_account.id
...@@ -20,6 +22,6 @@ class BlockService < BaseService ...@@ -20,6 +22,6 @@ class BlockService < BaseService
end end
def redis def redis
$redis Redis.current
end end
end end
# frozen_string_literal: true
class FanOutOnWriteService < BaseService class FanOutOnWriteService < BaseService
# Push a status into home and mentions feeds # Push a status into home and mentions feeds
# @param [Status] status # @param [Status] status
......
# frozen_string_literal: true
class FavouriteService < BaseService class FavouriteService < BaseService
# Favourite a status and notify remote user # Favourite a status and notify remote user
# @param [Account] account # @param [Account] account
......
# frozen_string_literal: true
class FetchAtomService < BaseService class FetchAtomService < BaseService
def call(url) def call(url)
response = http_client.head(url) response = http_client.head(url)
...@@ -9,15 +11,9 @@ class FetchAtomService < BaseService ...@@ -9,15 +11,9 @@ class FetchAtomService < BaseService
Rails.logger.debug "Remote status GET request returned code #{response.code}" Rails.logger.debug "Remote status GET request returned code #{response.code}"
return nil if response.code != 200 return nil if response.code != 200
return [url, fetch(url)] if response.mime_type == 'application/atom+xml'
if response.mime_type == 'application/atom+xml' return process_headers(url, response) unless response['Link'].blank?
return [url, fetch(url)] process_html(fetch(url))
elsif !response['Link'].blank?
return process_headers(url, response)
else
return process_html(fetch(url))
end
rescue OpenSSL::SSL::SSLError => e rescue OpenSSL::SSL::SSLError => e
Rails.logger.debug "SSL error: #{e}" Rails.logger.debug "SSL error: #{e}"
end end
...@@ -31,17 +27,17 @@ class FetchAtomService < BaseService ...@@ -31,17 +27,17 @@ class FetchAtomService < BaseService
alternate_link = page.xpath('//link[@rel="alternate"]').find { |link| link['type'] == 'application/atom+xml' } alternate_link = page.xpath('//link[@rel="alternate"]').find { |link| link['type'] == 'application/atom+xml' }
return nil if alternate_link.nil? return nil if alternate_link.nil?
return [alternate_link['href'], fetch(alternate_link['href'])] [alternate_link['href'], fetch(alternate_link['href'])]
end end
def process_headers(url, response) def process_headers(url, response)
Rails.logger.debug 'Processing link header' Rails.logger.debug 'Processing link header'
link_header = LinkHeader.parse(response['Link'].is_a?(Array) ? response['Link'].first : response['Link']) 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 process_html(fetch(url)) if alternate_link.nil?
return [alternate_link.href, fetch(alternate_link.href)] [alternate_link.href, fetch(alternate_link.href)]
end end
def fetch(url) def fetch(url)
......
# frozen_string_literal: true
class FetchRemoteAccountService < BaseService class FetchRemoteAccountService < BaseService
def call(url) def call(url)
atom_url, body = FetchAtomService.new.call(url) atom_url, body = FetchAtomService.new.call(url)
return nil if atom_url.nil? return nil if atom_url.nil?
return process_atom(atom_url, body) process_atom(atom_url, body)
end end
private private
...@@ -25,7 +27,7 @@ class FetchRemoteAccountService < BaseService ...@@ -25,7 +27,7 @@ class FetchRemoteAccountService < BaseService
Rails.logger.debug "Unparseable URL given: #{url}" Rails.logger.debug "Unparseable URL given: #{url}"
nil nil
rescue Nokogiri::XML::XPath::SyntaxError rescue Nokogiri::XML::XPath::SyntaxError
Rails.logger.debug "Invalid XML or missing namespace" Rails.logger.debug 'Invalid XML or missing namespace'
nil nil
end end
end end
# frozen_string_literal: true
class FetchRemoteStatusService < BaseService class FetchRemoteStatusService < BaseService
def call(url) def call(url)
atom_url, body = FetchAtomService.new.call(url) atom_url, body = FetchAtomService.new.call(url)
return nil if atom_url.nil? return nil if atom_url.nil?
return process_atom(atom_url, body) process_atom(atom_url, body)
end end
private private
...@@ -20,7 +22,7 @@ class FetchRemoteStatusService < BaseService ...@@ -20,7 +22,7 @@ class FetchRemoteStatusService < BaseService
statuses = ProcessFeedService.new.call(body, account) statuses = ProcessFeedService.new.call(body, account)
return statuses.first statuses.first
end end
def extract_author(url, xml) def extract_author(url, xml)
...@@ -34,7 +36,7 @@ class FetchRemoteStatusService < BaseService ...@@ -34,7 +36,7 @@ class FetchRemoteStatusService < BaseService
return FollowRemoteAccountService.new.call("#{username}@#{domain}") return FollowRemoteAccountService.new.call("#{username}@#{domain}")
rescue Nokogiri::XML::XPath::SyntaxError rescue Nokogiri::XML::XPath::SyntaxError
Rails.logger.debug "Invalid XML or missing namespace" Rails.logger.debug 'Invalid XML or missing namespace'
nil nil
end end
end end
# frozen_string_literal: true
class FollowRemoteAccountService < BaseService class FollowRemoteAccountService < BaseService
include OStatus2::MagicKey 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. # Find or create a local account for a remote user.
# When creating, look up the user's webfinger and fetch all # When creating, look up the user's webfinger and fetch all
...@@ -49,7 +51,7 @@ class FollowRemoteAccountService < BaseService ...@@ -49,7 +51,7 @@ class FollowRemoteAccountService < BaseService
get_profile(xml, account) get_profile(xml, account)
account.save! account.save!
return account account
end end
private private
......
# frozen_string_literal: true
class FollowService < BaseService class FollowService < BaseService
# Follow a remote user, notify remote user about the follow # Follow a remote user, notify remote user about the follow
# @param [Account] source_account From which to follow # @param [Account] source_account From which to follow
...@@ -35,7 +37,7 @@ class FollowService < BaseService ...@@ -35,7 +37,7 @@ class FollowService < BaseService
end end
def redis def redis
$redis Redis.current
end end
def follow_remote_account_service def follow_remote_account_service
......
# frozen_string_literal: true
class PostStatusService < BaseService class PostStatusService < BaseService
# Post a text status update, fetch and notify remote users mentioned # Post a text status update, fetch and notify remote users mentioned
# @param [Account] account Account from which to post # @param [Account] account Account from which to post
......
# frozen_string_literal: true
class PrecomputeFeedService < BaseService class PrecomputeFeedService < BaseService
# Fill up a user's home/mentions feed from DB and return a subset # Fill up a user's home/mentions feed from DB and return a subset
# @param [Symbol] type :home or :mentions # @param [Symbol] type :home or :mentions
# @param [Account] account # @param [Account] account
def call(type, account) def call(type, account)
instant_return = []
Status.send("as_#{type}_timeline", account).limit(FeedManager::MAX_ITEMS).each do |status| Status.send("as_#{type}_timeline", account).limit(FeedManager::MAX_ITEMS).each do |status|
next if FeedManager.instance.filter?(type, status, account) 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) 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 ...@@ -14,6 +14,6 @@ class PrecomputeFeedService < BaseService
private private
def redis def redis
$redis Redis.current
end end
end end
# frozen_string_literal: true
class ProcessFeedService < BaseService class ProcessFeedService < BaseService
ACTIVITY_NS = 'http://activitystrea.ms/spec/1.0/'.freeze ACTIVITY_NS = 'http://activitystrea.ms/spec/1.0/'
THREAD_NS = 'http://purl.org/syndication/thread/1.0'.freeze THREAD_NS = 'http://purl.org/syndication/thread/1.0'
def call(body, account) def call(body, account)
xml = Nokogiri::XML(body) xml = Nokogiri::XML(body)
...@@ -89,13 +91,13 @@ class ProcessFeedService < BaseService ...@@ -89,13 +91,13 @@ class ProcessFeedService < BaseService
account = @account account = @account
end end
status = Status.create!({ status = Status.create!(
uri: id(entry), uri: id(entry),
url: url(entry), url: url(entry),
account: account, account: account,
text: content(entry), text: content(entry),
created_at: published(entry), created_at: published(entry)
}) )
if thread?(entry) if thread?(entry)
Rails.logger.debug "Trying to attach #{status.id} (#{id(entry)}) to #{thread(entry).first}" Rails.logger.debug "Trying to attach #{status.id} (#{id(entry)}) to #{thread(entry).first}"
......
# frozen_string_literal: true
class ProcessHashtagsService < BaseService class ProcessHashtagsService < BaseService
def call(status, tags = []) def call(status, tags = [])
if status.local? tags = status.text.scan(Tag::HASHTAG_RE).map(&:first) if status.local?
tags = status.text.scan(Tag::HASHTAG_RE).map(&:first)
end
tags.map(&:downcase).uniq.each do |tag| tags.map(&:downcase).uniq.each do |tag|
status.tags << Tag.where(name: tag).first_or_initialize(name: tag) status.tags << Tag.where(name: tag).first_or_initialize(name: tag)
......
# frozen_string_literal: true
class ProcessInteractionService < BaseService 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 # Record locally the remote interaction with our user
# @param [String] envelope Salmon envelope # @param [String] envelope Salmon envelope
...@@ -76,9 +78,7 @@ class ProcessInteractionService < BaseService ...@@ -76,9 +78,7 @@ class ProcessInteractionService < BaseService
return if status.nil? return if status.nil?
if account.id == status.account_id remove_status_service.call(status) if account.id == status.account_id
remove_status_service.call(status)
end
end end
def favourite!(xml, from_account) def favourite!(xml, from_account)
......
# frozen_string_literal: true
class ProcessMentionsService < BaseService class ProcessMentionsService < BaseService
# Scan status for mentions and fetch remote mentioned users, create # Scan status for mentions and fetch remote mentioned users, create
# local mention pointers, send Salmon notifications to mentioned # local mention pointers, send Salmon notifications to mentioned
......
# frozen_string_literal: true
class ReblogService < BaseService class ReblogService < BaseService
# Reblog a status and notify its remote author # Reblog a status and notify its remote author
# @param [Account] account Account to reblog from # @param [Account] account Account to reblog from
......
# frozen_string_literal: true
class RemoveStatusService < BaseService class RemoveStatusService < BaseService
def call(status) def call(status)
remove_from_self(status) if status.account.local? remove_from_self(status) if status.account.local?
...@@ -62,6 +64,6 @@ class RemoveStatusService < BaseService ...@@ -62,6 +64,6 @@ class RemoveStatusService < BaseService
end end
def redis def redis
$redis Redis.current
end end
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