From 70911cbd4356ba57c3b9a9733857c5c91dfbe27e Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 29 Aug 2017 12:49:31 -0400 Subject: [PATCH] preload email correctly (not backwards compat with stable) --- plugin.rb | 21 ++++++++++----------- spec/integration/assign_spec.rb | 11 +++++++++++ 2 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 spec/integration/assign_spec.rb diff --git a/plugin.rb b/plugin.rb index 4532b5c..3ac198f 100644 --- a/plugin.rb +++ b/plugin.rb @@ -20,9 +20,9 @@ after_initialize do def self.backfill_auto_assign staff_mention = User.where('moderator OR admin') - .pluck('username') - .map{|name| "p.cooked ILIKE '%mention%@#{name}%'"} - .join(' OR ') + .pluck('username') + .map { |name| "p.cooked ILIKE '%mention%@#{name}%'" } + .join(' OR ') sql = < 0 - users = User.where("id in ( + users = User.where("users.id in ( SELECT value::int FROM topic_custom_fields WHERE name = 'assigned_to_id' AND topic_id IN (?) )", topics.map(&:id)) - .select(:id, :email, :username, :uploaded_avatar_id) + .joins('join user_emails on user_emails.user_id = users.id AND user_emails.primary') + .select(:id, 'user_emails.email', :username, :uploaded_avatar_id) map = {} - users.each{|u| map[u.id] = u} + users.each { |u| map[u.id] = u } topics.each do |t| if id = t.custom_fields['assigned_to_id'] @@ -300,7 +301,7 @@ SQL results = results.joins("LEFT JOIN topic_custom_fields tc_assign ON topics.id = tc_assign.topic_id AND tc_assign.name = 'assigned_to_id'") - .where("tc_assign.name IS NULL") + .where("tc_assign.name IS NULL") else if username == "*" @@ -352,7 +353,7 @@ SQL end def include_assigned_to_user? - if SiteSetting.assigns_public || scope.is_staff? + if SiteSetting.assigns_public || scope.is_staff? # subtle but need to catch cases where stuff is not assigned object.topic.custom_fields.keys.include?("assigned_to_id") end @@ -389,12 +390,10 @@ SQL Discourse::Application.routes.append do mount ::DiscourseAssign::Engine, at: "/assign" - get "topics/private-messages-assigned/:username" => "list#private_messages_assigned", - as: "topics_private_messages_assigned", constraints: {username: /[\w.\-]+?/} + get "topics/private-messages-assigned/:username" => "list#private_messages_assigned", as: "topics_private_messages_assigned", constraints: { username: /[\w.\-]+?/ } end end - on(:post_created) do |post| ::TopicAssigner.auto_assign(post, force: true) end diff --git a/spec/integration/assign_spec.rb b/spec/integration/assign_spec.rb new file mode 100644 index 0000000..0eb6d18 --- /dev/null +++ b/spec/integration/assign_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +describe 'integration tests' do + it 'preloads data in topic list' do + admin = Fabricate(:admin) + post = create_post + list = TopicList.new("latest", admin, [post.topic]) + TopicList.preload([post.topic], list) + # should not explode for now + end +end