Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Conversation

nattsw
Copy link
Contributor

@nattsw nattsw commented Jun 6, 2025

Special thanks to Moin for pointing this out.

Bug

If John created a post which is a solution in March, the user directory would show that John solution = 1.

In April, if John has not solved a topic, he would still have solution = 1 in the user directory.

Screenshot to user directory /u (does not show bug, just a reference):
Screenshot 2025-06-06 at 11 35 32 PM

Fix

The directory column query needs to include users with 0 solutions, so that the value would be updated.

@nattsw nattsw requested a review from Copilot June 6, 2025 15:41
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes the issue where users with zero solutions still showed a stale count by revising the directory query to include all users and adding a cross-date refresh test.

  • Added a new spec to verify that a user's solution count drops from 1 to 0 when the time window moves past their only solution.
  • Updated the SQL in plugin.rb to left-join users/posts/solutions so users with no recent solutions are included (counting zero).

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
spec/models/directory_item_spec.rb Added a when refreshing across dates context to test solution 0.
plugin.rb Switched to left joins and a COUNT(DISTINCT CASE …) to include zero solutions.

plugin.rb Outdated
Comment on lines 294 to 312
SELECT users.id AS user_id,
COUNT(DISTINCT st.topic_id) FILTER (WHERE
st.topic_id IS NOT NULL AND
t.id IS NOT NULL AND
t.archetype <> 'private_message' AND
t.deleted_at IS NULL AND
p.deleted_at IS NULL
) AS solutions
FROM users
LEFT JOIN posts p ON p.user_id = users.id
LEFT JOIN discourse_solved_solved_topics st
ON st.answer_post_id = p.id
AND st.created_at >= :since
LEFT JOIN topics t ON t.id = st.topic_id
WHERE users.id > 0
AND users.active
AND users.silenced_till IS NULL
AND users.suspended_till IS NULL
GROUP BY users.id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some performance concerns about the new query because it has to join the entire users table against the posts table. The previous query only looped through the discourse_solved_solved_topics which is going to have way less rows as compared to the users table.

To account for users with zero solutions, did you consider just setting the count for all users to zero first in an initial query before filling up the correct count using the original query?

@nattsw nattsw requested a review from tgxworld June 10, 2025 08:46
@nattsw nattsw force-pushed the directory-no-update branch from 65a3f60 to 90338c6 Compare June 10, 2025 14:23
@nattsw nattsw force-pushed the directory-no-update branch from 90338c6 to 7fa551a Compare June 11, 2025 02:28
@nattsw nattsw merged commit 4d20d83 into main Jun 11, 2025
6 checks passed
@nattsw nattsw deleted the directory-no-update branch June 11, 2025 02:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants