Skip to content

A platform for community discussion. Free, open, simple.

UX: allow filter posts by user in user card with hidden profile (#35908)

Yuriy Kurantf54a32ee1efa
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 35f4859d636..45f707873dd 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -128,16 +128,11 @@ class UsersController < ApplicationController
user_serializer = InactiveUserSerializer.new(@user, scope: guardian, root: "user")
elsif !guardian.can_see_profile?(@user)
user_serializer = HiddenProfileSerializer.new(@user, scope: guardian, root: "user")
+ assign_topic_post_count(user_serializer) if for_card
else
serializer_class = for_card ? UserCardSerializer : UserSerializer
user_serializer = serializer_class.new(@user, scope: guardian, root: "user")
-
- topic_id = params[:include_post_count_for].to_i
- if topic_id != 0 && guardian.can_see?(Topic.find_by_id(topic_id))
- user_serializer.topic_post_count = {
- topic_id => Post.secured(guardian).where(topic_id: topic_id, user_id: @user.id).count,
- }
- end
+ assign_topic_post_count(user_serializer)
end
track_visit_to_user_profile if !params[:skip_track_visit] && (@user != current_user)
@@ -2078,6 +2073,15 @@ class UsersController < ApplicationController
private
+ def assign_topic_post_count(user_serializer)
+ topic_id = params[:include_post_count_for].to_i
+ if topic_id != 0 && guardian.can_see?(Topic.find_by_id(topic_id))
+ user_serializer.topic_post_count = {
+ topic_id => Post.secured(guardian).where(topic_id: topic_id, user_id: @user.id).count,
+ }
+ end
+ end
+
def clean_custom_field_values(field)
field_values = params[:user_fields][field.id.to_s]
diff --git a/app/serializers/hidden_profile_serializer.rb b/app/serializers/hidden_profile_serializer.rb
index 5755171bd7e..005e99c4ccf 100644
--- a/app/serializers/hidden_profile_serializer.rb
+++ b/app/serializers/hidden_profile_serializer.rb
@@ -1,7 +1,15 @@
# frozen_string_literal: true
class HiddenProfileSerializer < BasicUserSerializer
- attributes(:profile_hidden?, :title, :primary_group_name, :can_send_private_message_to_user)
+ attr_accessor :topic_post_count
+
+ attributes(
+ :profile_hidden?,
+ :title,
+ :topic_post_count,
+ :primary_group_name,
+ :can_send_private_message_to_user,
+ )
def profile_hidden?
true
@@ -14,4 +22,8 @@ class HiddenProfileSerializer < BasicUserSerializer
def primary_group_name
object.primary_group.try(:name)
end
+
+ def include_topic_post_count?
+ topic_post_count.present?
+ end
end
diff --git a/frontend/discourse/app/components/user-card-contents.gjs b/frontend/discourse/app/components/user-card-contents.gjs
index 3758b4827ca..0a6ed09431d 100644
--- a/frontend/discourse/app/components/user-card-contents.gjs
+++ b/frontend/discourse/app/components/user-card-contents.gjs
@@ -148,9 +148,9 @@ export default class UserCardContents extends CardContentsBase {
return username ? `user-card-${username}` : "";
}
- @discourseComputed("username", "topicPostCount")
- filterPostsLabel(username, count) {
- return i18n("topic.filter_to", { username, count });
+ @discourseComputed("topicPostCount")
+ filterPostsLabel(count) {
+ return i18n("topic.filter_to", { count });
}
@discourseComputed("[email protected]")
diff --git a/spec/system/page_objects/components/user_card.rb b/spec/system/page_objects/components/user_card.rb
index eed09830eea..e655f5a5829 100644
--- a/spec/system/page_objects/components/user_card.rb
+++ b/spec/system/page_objects/components/user_card.rb
@@ -10,6 +10,22 @@ module PageObjects
def showing_user?(username)
has_css?("#user-card.user-card-#{username}")
end
+
+ def has_filter_button?
+ has_css?("#user-card .usercard-controls .d-icon-filter")
+ end
+
+ def has_no_filter_button?
+ has_no_css?("#user-card .usercard-controls .d-icon-filter")
+ end
+
+ def filter_button_text
+ find("#user-card .usercard-controls .d-icon-filter + .d-button-label").text
+ end
+
+ def has_profile_hidden?
+ has_css?("#user-card .profile-hidden", visible: true)
+ end
end
end
end
diff --git a/spec/system/page_objects/pages/topic.rb b/spec/system/page_objects/pages/topic.rb
index e1d8452ab2b..6f855c1a981 100644
--- a/spec/system/page_objects/pages/topic.rb
+++ b/spec/system/page_objects/pages/topic.rb
@@ -96,6 +96,10 @@ module PageObjects
post_by_number(post).find(".show-more-actions").click
end
+ def click_post_author_avatar(post)
+ within_post(post) { find(".main-avatar[data-user-card='#{post.user.username}']").click }
+ end
+
def click_post_action_button(post, button)
find_post_action_button(post, button).click
end
diff --git a/spec/system/user_card_spec.rb b/spec/system/user_card_spec.rb
index ff0b1393bba..268a4bb9abe 100644
--- a/spec/system/user_card_spec.rb
+++ b/spec/system/user_card_spec.rb
@@ -31,4 +31,50 @@ describe "User Card", type: :system do
expect(user_card).to be_showing_user(user.username)
end
end
+
+ context "when filtering posts by user" do
+ fab!(:another_user, :user)
+ let!(:first_post_by_another_user) do
+ PostCreator.create!(another_user, topic_id: topic.id, raw: "First post by another user")
+ end
+ let!(:second_post_by_another_user) do
+ PostCreator.create!(another_user, topic_id: topic.id, raw: "Second post by another user")
+ end
+
+ it "shows filter button when user has 2+ posts in the topic" do
+ topic_page.visit_topic(topic)
+ topic_page.click_post_author_avatar(first_post_by_another_user)
+
+ expect(user_card).to be_visible
+ expect(user_card).to have_filter_button
+ expect(user_card.filter_button_text).to match(I18n.t("js.topic.filter_to", count: 2))
+ end
+
+ it "does not show filter button when user has less than 2 posts" do
+ topic_page.visit_topic(topic)
+ topic_page.click_post_author_avatar(topic.posts.first)
+
+ expect(user_card).to be_visible
+ expect(user_card).to have_no_filter_button
+ end
+
+ context "when user has hidden profile" do
+ before do
+ SiteSetting.allow_users_to_hide_profile = true
+ another_user.user_option.update!(hide_profile: true)
+ # Sign in as regular user (not admin) to see hidden profile behavior
+ sign_in(user)
+ end
+
+ it "shows filter button with post count for hidden profile" do
+ topic_page.visit_topic(topic)
+ topic_page.click_post_author_avatar(first_post_by_another_user)
+
+ expect(user_card).to be_showing_user(another_user.username)
+ expect(user_card).to have_profile_hidden
+ expect(user_card).to have_filter_button
+ expect(user_card.filter_button_text).to match(I18n.t("js.topic.filter_to", count: 2))
+ end
+ end
+ end
end