From 94ceb06b02fc32be4f3f7b6072fe1a7449edf674 Mon Sep 17 00:00:00 2001
From: Gerhard Schlager <mail@gerhard-schlager.at>
Date: Thu, 16 Nov 2017 16:26:18 +0100
Subject: [PATCH] fix user lookup by email in disqus import script

---
 script/import_scripts/base.rb   | 6 +++++-
 script/import_scripts/disqus.rb | 4 ++--
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/script/import_scripts/base.rb b/script/import_scripts/base.rb
index f06d8cbe879..67c6ede4c73 100644
--- a/script/import_scripts/base.rb
+++ b/script/import_scripts/base.rb
@@ -269,7 +269,7 @@ class ImportScripts::Base
     merge = opts.delete(:merge)
     post_create_action = opts.delete(:post_create_action)
 
-    existing = User.joins(:user_emails).where("user_emails.email = ? OR username = ?", opts[:email].downcase, opts[:username]).first
+    existing = find_existing_user(opts[:email], opts[:username])
     return existing if existing && (merge || existing.custom_fields["import_id"].to_s == import_id.to_s)
 
     bio_raw = opts.delete(:bio_raw)
@@ -369,6 +369,10 @@ class ImportScripts::Base
     u # If there was an error creating the user, u.errors has the messages
   end
 
+  def find_existing_user(email, username)
+    User.joins(:user_emails).where("user_emails.email = ? OR username = ?", email.downcase, username).first
+  end
+
   def created_category(category)
     # override if needed
   end
diff --git a/script/import_scripts/disqus.rb b/script/import_scripts/disqus.rb
index a73febe4768..01bdb476291 100644
--- a/script/import_scripts/disqus.rb
+++ b/script/import_scripts/disqus.rb
@@ -67,7 +67,7 @@ class ImportScripts::Disqus < ImportScripts::Base
 
       puts "Creating #{title}... (#{t[:posts].size} posts)"
 
-      topic_user = User.where('email = ? OR username = ?', t[:author_email].downcase, t[:author_username]).first
+      topic_user = find_existing_user(t[:author_email], t[:author_username])
       begin
         post = TopicEmbed.import_remote(topic_user, t[:link], title: title)
         post.topic.update_column(:category_id, @category.id)
@@ -77,7 +77,7 @@ class ImportScripts::Disqus < ImportScripts::Base
 
       if post.present? && post.topic.posts_count <= 1
         (t[:posts] || []).each do |p|
-          post_user = User.where('email = ? OR username = ?', (p[:author_email] || '').downcase, p[:author_username]).first
+          post_user = find_existing_user(p[:author_email] || '', p[:author_username])
           next unless post_user.present?
 
           attrs = {