Gab-Social/app/lib/tag_manager.rb
rubic0n a623252c59 Convert to boolean in C code
casecmp?(str) is 1.5x faster than casecmp(str).zero?
It's 1 less method call in Ruby, and the C code can convert
to boolean, rather than returning a number and determining
if it's zero.
2021-02-17 23:25:10 -06:00

60 lines
1.4 KiB
Ruby

# frozen_string_literal: true
require 'singleton'
class TagManager
include Singleton
include RoutingHelper
def web_domain?(domain)
domain.nil? || domain.gsub(/[\/]/, '').casecmp?(Rails.configuration.x.web_domain)
end
def local_domain?(domain)
domain.nil? || domain.gsub(/[\/]/, '').casecmp?(Rails.configuration.x.local_domain)
end
def normalize_domain(domain)
return if domain.nil?
uri = Addressable::URI.new
uri.host = domain.gsub(/[\/]/, '')
uri.normalized_host
end
def normalize_link(link)
return if link.nil?
uri = Addressable::URI.parse(link)
return "#{uri.domain}#{uri.normalized_path}".strip
end
def normalize_link_domain(link)
return if link.nil?
uri = Addressable::URI.parse(link)
return "#{uri.domain}".strip
end
def same_acct?(canonical, needle)
return true if canonical.casecmp?(needle)
username, domain = needle.split('@')
local_domain?(domain) && canonical.casecmp?(username)
end
def local_url?(url)
uri = Addressable::URI.parse(url).normalize
domain = uri.host + (uri.port ? ":#{uri.port}" : '')
TagManager.instance.web_domain?(domain)
end
def url_for(target)
return target.url if target.respond_to?(:local?) && !target.local?
case target.object_type
when :person
short_account_with_replies_url(target)
when :note, :comment, :activity
short_account_status_url(target.account, target)
end
end
end