Refactor account.rb (#2881)
* Call simple query * Refactor Account#keypair * Replace escaped single quote * Extract similar behavior to method * Gather the same condition to one block * Remove not needed receiverlolsob-rspec
parent
31d5a6da08
commit
2745fc5268
|
@ -48,12 +48,16 @@ class Account < ApplicationRecord
|
||||||
|
|
||||||
# Local users
|
# Local users
|
||||||
has_one :user, inverse_of: :account
|
has_one :user, inverse_of: :account
|
||||||
validates :username, presence: true, format: { with: /\A[a-z0-9_]+\z/i }, uniqueness: { scope: :domain, case_sensitive: false }, length: { maximum: 30 }, if: 'local?'
|
|
||||||
validates :username, presence: true, uniqueness: { scope: :domain, case_sensitive: true }, unless: 'local?'
|
|
||||||
|
|
||||||
# Local user profile validations
|
validates :username, presence: true
|
||||||
validates :display_name, length: { maximum: 30 }, if: 'local?'
|
validates :username, uniqueness: { scope: :domain, case_sensitive: true }, unless: 'local?'
|
||||||
validates :note, length: { maximum: 160 }, if: 'local?'
|
|
||||||
|
# Local user validations
|
||||||
|
with_options if: 'local?' do
|
||||||
|
validates :username, format: { with: /\A[a-z0-9_]+\z/i }, uniqueness: { scope: :domain, case_sensitive: false }, length: { maximum: 30 }
|
||||||
|
validates :display_name, length: { maximum: 30 }
|
||||||
|
validates :note, length: { maximum: 160 }
|
||||||
|
end
|
||||||
|
|
||||||
# Timelines
|
# Timelines
|
||||||
has_many :stream_entries, inverse_of: :account, dependent: :destroy
|
has_many :stream_entries, inverse_of: :account, dependent: :destroy
|
||||||
|
@ -114,15 +118,15 @@ class Account < ApplicationRecord
|
||||||
delegate :allowed_languages, to: :user, prefix: false, allow_nil: true
|
delegate :allowed_languages, to: :user, prefix: false, allow_nil: true
|
||||||
|
|
||||||
def follow!(other_account)
|
def follow!(other_account)
|
||||||
active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
|
active_relationships.find_or_create_by!(target_account: other_account)
|
||||||
end
|
end
|
||||||
|
|
||||||
def block!(other_account)
|
def block!(other_account)
|
||||||
block_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
|
block_relationships.find_or_create_by!(target_account: other_account)
|
||||||
end
|
end
|
||||||
|
|
||||||
def mute!(other_account)
|
def mute!(other_account)
|
||||||
mute_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
|
mute_relationships.find_or_create_by!(target_account: other_account)
|
||||||
end
|
end
|
||||||
|
|
||||||
def unfollow!(other_account)
|
def unfollow!(other_account)
|
||||||
|
@ -181,15 +185,15 @@ class Account < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def favourited?(status)
|
def favourited?(status)
|
||||||
status.proper.favourites.where(account: self).count.positive?
|
status.proper.favourites.where(account: self).exists?
|
||||||
end
|
end
|
||||||
|
|
||||||
def reblogged?(status)
|
def reblogged?(status)
|
||||||
status.proper.reblogs.where(account: self).count.positive?
|
status.proper.reblogs.where(account: self).exists?
|
||||||
end
|
end
|
||||||
|
|
||||||
def keypair
|
def keypair
|
||||||
private_key.nil? ? OpenSSL::PKey::RSA.new(public_key) : OpenSSL::PKey::RSA.new(private_key)
|
OpenSSL::PKey::RSA.new(private_key || public_key)
|
||||||
end
|
end
|
||||||
|
|
||||||
def subscription(webhook_url)
|
def subscription(webhook_url)
|
||||||
|
@ -262,9 +266,7 @@ class Account < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_for(terms, limit = 10)
|
def search_for(terms, limit = 10)
|
||||||
terms = Arel.sql(connection.quote(terms.gsub(/['?\\:]/, ' ')))
|
textsearch, query = generate_query_for_search(terms)
|
||||||
textsearch = '(setweight(to_tsvector(\'simple\', accounts.display_name), \'A\') || setweight(to_tsvector(\'simple\', accounts.username), \'B\') || setweight(to_tsvector(\'simple\', coalesce(accounts.domain, \'\')), \'C\'))'
|
|
||||||
query = 'to_tsquery(\'simple\', \'\'\' \' || ' + terms + ' || \' \'\'\' || \':*\')'
|
|
||||||
|
|
||||||
sql = <<-SQL.squish
|
sql = <<-SQL.squish
|
||||||
SELECT
|
SELECT
|
||||||
|
@ -276,13 +278,11 @@ class Account < ApplicationRecord
|
||||||
LIMIT ?
|
LIMIT ?
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
Account.find_by_sql([sql, limit])
|
find_by_sql([sql, limit])
|
||||||
end
|
end
|
||||||
|
|
||||||
def advanced_search_for(terms, account, limit = 10)
|
def advanced_search_for(terms, account, limit = 10)
|
||||||
terms = Arel.sql(connection.quote(terms.gsub(/['?\\:]/, ' ')))
|
textsearch, query = generate_query_for_search(terms)
|
||||||
textsearch = '(setweight(to_tsvector(\'simple\', accounts.display_name), \'A\') || setweight(to_tsvector(\'simple\', accounts.username), \'B\') || setweight(to_tsvector(\'simple\', coalesce(accounts.domain, \'\')), \'C\'))'
|
|
||||||
query = 'to_tsquery(\'simple\', \'\'\' \' || ' + terms + ' || \' \'\'\' || \':*\')'
|
|
||||||
|
|
||||||
sql = <<-SQL.squish
|
sql = <<-SQL.squish
|
||||||
SELECT
|
SELECT
|
||||||
|
@ -296,7 +296,7 @@ class Account < ApplicationRecord
|
||||||
LIMIT ?
|
LIMIT ?
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
Account.find_by_sql([sql, account.id, account.id, limit])
|
find_by_sql([sql, account.id, account.id, limit])
|
||||||
end
|
end
|
||||||
|
|
||||||
def following_map(target_account_ids, account_id)
|
def following_map(target_account_ids, account_id)
|
||||||
|
@ -321,6 +321,14 @@ class Account < ApplicationRecord
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def generate_query_for_search(terms)
|
||||||
|
terms = Arel.sql(connection.quote(terms.gsub(/['?\\:]/, ' ')))
|
||||||
|
textsearch = "(setweight(to_tsvector('simple', accounts.display_name), 'A') || setweight(to_tsvector('simple', accounts.username), 'B') || setweight(to_tsvector('simple', coalesce(accounts.domain, '')), 'C'))"
|
||||||
|
query = "to_tsquery('simple', ''' ' || #{terms} || ' ''' || ':*')"
|
||||||
|
|
||||||
|
[textsearch, query]
|
||||||
|
end
|
||||||
|
|
||||||
def follow_mapping(query, field)
|
def follow_mapping(query, field)
|
||||||
query.pluck(field).each_with_object({}) { |id, mapping| mapping[id] = true }
|
query.pluck(field).each_with_object({}) { |id, mapping| mapping[id] = true }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue