PERF: Avoid additional database query when viewing own user. (#14239)

PERF: Avoid additional database query when viewing own user. (#14239)

diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index acfa7e1..bdb33fe 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -534,11 +534,16 @@ class ApplicationController < ActionController::Base
     opts ||= {}
     user = if params[:username]
       username_lower = params[:username].downcase.chomp('.json')
-      find_opts = { username_lower: username_lower }
-      find_opts[:active] = true unless opts[:include_inactive] || current_user.try(:staff?)
-      result = User
-      (result = result.includes(*eager_load)) if !eager_load.empty?
-      result.find_by(find_opts)
+
+      if current_user && current_user.username_lower == username_lower
+        current_user
+      else
+        find_opts = { username_lower: username_lower }
+        find_opts[:active] = true unless opts[:include_inactive] || current_user.try(:staff?)
+        result = User
+        (result = result.includes(*eager_load)) if !eager_load.empty?
+        result.find_by(find_opts)
+      end
     elsif params[:external_id]
       external_id = params[:external_id].chomp('.json')
       if provider_name = params[:external_provider]

GitHub sha: 1937474e849b78b07d4f002bae3acec709a31f13

This commit appears in #14239 which was approved by eviltrout. It was merged by tgxworld.