Use `add_directory_column` API to add solutions count to user directory (#137)

This commit is contained in:
Mark VanLandingham 2021-06-17 09:31:11 -05:00 committed by GitHub
parent fe4d0c0bd8
commit 58c594fdda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 0 deletions

View File

@ -4,6 +4,7 @@ en:
alt: alt:
solved: solved:
accepted_notification: "accepted" accepted_notification: "accepted"
solutions: "Solutions"
solved: solved:
title: "Solved" title: "Solved"

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddSolutionsToDirectoryItems < ActiveRecord::Migration[6.1]
def change
add_column :directory_items, :solutions, :integer, default: 0
end
end

View File

@ -613,6 +613,26 @@ SQL
options[:refresh_stream] = true if old_category_allows != new_category_allows options[:refresh_stream] = true if old_category_allows != new_category_allows
end end
query = "
WITH x AS (SELECT
u.id user_id,
COUNT(DISTINCT ua.id) AS solutions
FROM users AS u
INNER JOIN user_actions AS ua ON ua.user_id = u.id AND ua.action_type = #{UserAction::SOLVED} AND COALESCE(ua.created_at, :since) > :since
WHERE u.active
AND u.silenced_till IS NULL
AND u.id > 0
GROUP BY u.id
)
UPDATE directory_items di SET
solutions = x.solutions
FROM x
WHERE x.user_id = di.user_id
AND di.period_type = :period_type
AND di.solutions <> x.solutions
"
add_directory_column("solutions", query: query)
add_to_class(:composer_messages_finder, :check_topic_is_solved) do add_to_class(:composer_messages_finder, :check_topic_is_solved) do
return if !SiteSetting.solved_enabled || SiteSetting.disable_solved_education_message return if !SiteSetting.solved_enabled || SiteSetting.disable_solved_education_message
return if !replying? || @topic.blank? || @topic.private_message? return if !replying? || @topic.blank? || @topic.private_message?