From 54ea7f5dfeb2ecbad6362b050e234da366906666 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 4 Sep 2016 21:06:04 +0200 Subject: [PATCH] Case-insensitive search by usernames --- app/controllers/accounts_controller.rb | 2 +- app/controllers/stream_entries_controller.rb | 2 +- app/models/account.rb | 12 +++++++++++- app/services/process_mentions_service.rb | 2 +- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index f4073d0936..162e85dec0 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -29,7 +29,7 @@ class AccountsController < ApplicationController private def set_account - @account = Account.find_by!(username: params[:username], domain: nil) + @account = Account.find_local!(params[:username]) end def set_webfinger_header diff --git a/app/controllers/stream_entries_controller.rb b/app/controllers/stream_entries_controller.rb index cbf7bfdff5..1e50d3b60a 100644 --- a/app/controllers/stream_entries_controller.rb +++ b/app/controllers/stream_entries_controller.rb @@ -16,7 +16,7 @@ class StreamEntriesController < ApplicationController private def set_account - @account = Account.find_by!(username: params[:account_username], domain: nil) + @account = Account.find_local!(params[:account_username]) end def set_stream_entry diff --git a/app/models/account.rb b/app/models/account.rb index af8b6cc3d4..bcd6c1eba6 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -96,8 +96,12 @@ class Account < ApplicationRecord end def self.find_local!(username) + self.find_remote!(username, nil) + end + + def self.find_remote!(username, domain) table = self.arel_table - self.where(table[:username].matches(username)).where(domain: nil).take! + self.where(table[:username].matches(username)).where(domain: domain).take! end def self.find_local(username) @@ -106,6 +110,12 @@ class Account < ApplicationRecord nil end + def self.find_remote(username, domain) + self.find_remote!(username, domain) + rescue ActiveRecord::RecordNotFound + nil + end + before_create do if local? keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048) diff --git a/app/services/process_mentions_service.rb b/app/services/process_mentions_service.rb index aec150a948..288f56be40 100644 --- a/app/services/process_mentions_service.rb +++ b/app/services/process_mentions_service.rb @@ -8,7 +8,7 @@ class ProcessMentionsService < BaseService status.text.scan(Account::MENTION_RE).each do |match| username, domain = match.first.split('@') - mentioned_account = Account.find_by(username: username, domain: domain) + mentioned_account = Account.find_remote(username, domain) if mentioned_account.nil? && !domain.nil? mentioned_account = follow_remote_account_service.("#{match.first}")