DEV: Allow plugins to hide user stats by new guardian method (#9772)

DEV: Allow plugins to hide user stats by new guardian method (#9772)

diff --git a/app/assets/javascripts/discourse/app/templates/user/summary.hbs b/app/assets/javascripts/discourse/app/templates/user/summary.hbs
index ddd8959..de025db 100644
--- a/app/assets/javascripts/discourse/app/templates/user/summary.hbs
+++ b/app/assets/javascripts/discourse/app/templates/user/summary.hbs
@@ -1,53 +1,55 @@
 {{#d-section pageClass="user-summary" tagName=""}}
   <div class="user-content">
-    <div class="top-section stats-section">
-      <h3 class="stats-title">{{i18n "user.summary.stats"}}</h3>
-      <ul>
-        <li>
-          {{user-stat value=model.days_visited label="user.summary.days_visited"}}
-        </li>
-        <li>
-          {{user-stat value=timeRead label="user.summary.time_read" type="string"}}
-        </li>
-        {{#if showRecentTimeRead}}
+    {{#if model.can_see_summary_stats}}
+      <div class="top-section stats-section">
+        <h3 class="stats-title">{{i18n "user.summary.stats"}}</h3>
+        <ul>
           <li>
-            {{user-stat value=recentTimeRead label="user.summary.recent_time_read" type="string"}}
+            {{user-stat value=model.days_visited label="user.summary.days_visited"}}
+          </li>
+          <li>
+            {{user-stat value=timeRead label="user.summary.time_read" type="string"}}
+          </li>
+          {{#if showRecentTimeRead}}
+            <li>
+              {{user-stat value=recentTimeRead label="user.summary.recent_time_read" type="string"}}
+            </li>
+          {{/if}}
+          <li>
+            {{user-stat value=model.topics_entered label="user.summary.topics_entered"}}
+          </li>
+          <li>
+            {{user-stat value=model.posts_read_count label="user.summary.posts_read"}}
           </li>
-        {{/if}}
-        <li>
-          {{user-stat value=model.topics_entered label="user.summary.topics_entered"}}
-        </li>
-        <li>
-          {{user-stat value=model.posts_read_count label="user.summary.posts_read"}}
-        </li>
-        <li class="linked-stat">
-          {{#link-to "userActivity.likesGiven"}}
-            {{user-stat value=model.likes_given icon="heart" label="user.summary.likes_given"}}
-          {{/link-to}}
-        </li>
-        {{#if model.bookmark_count}}
           <li class="linked-stat">
-            {{#link-to "userActivity.bookmarks"}}
-              {{user-stat value=model.bookmark_count label="user.summary.bookmark_count"}}
+            {{#link-to "userActivity.likesGiven"}}
+              {{user-stat value=model.likes_given icon="heart" label="user.summary.likes_given"}}
             {{/link-to}}
           </li>
-        {{/if}}
-        <li class="linked-stat">
-          {{#link-to "userActivity.topics"}}
-            {{user-stat value=model.topic_count label="user.summary.topic_count"}}
-          {{/link-to}}
-        </li>
-        <li class="linked-stat">
-          {{#link-to "userActivity.replies"}}
-            {{user-stat value=model.post_count label="user.summary.post_count"}}
-          {{/link-to}}
-        </li>
-        <li>
-          {{user-stat value=model.likes_received icon="heart" label="user.summary.likes_received"}}
-        </li>
-        {{plugin-outlet name="user-summary-stat" tagName="" connectorTagName="li" args=(hash model=model)}}
-      </ul>
-    </div>
+          {{#if model.bookmark_count}}
+            <li class="linked-stat">
+              {{#link-to "userActivity.bookmarks"}}
+                {{user-stat value=model.bookmark_count label="user.summary.bookmark_count"}}
+              {{/link-to}}
+            </li>
+          {{/if}}
+          <li class="linked-stat">
+            {{#link-to "userActivity.topics"}}
+              {{user-stat value=model.topic_count label="user.summary.topic_count"}}
+            {{/link-to}}
+          </li>
+          <li class="linked-stat">
+            {{#link-to "userActivity.replies"}}
+              {{user-stat value=model.post_count label="user.summary.post_count"}}
+            {{/link-to}}
+          </li>
+          <li>
+            {{user-stat value=model.likes_received icon="heart" label="user.summary.likes_received"}}
+          </li>
+          {{plugin-outlet name="user-summary-stat" tagName="" connectorTagName="li" args=(hash model=model)}}
+        </ul>
+      </div>
+    {{/if}}
 
     <div class="top-section">
       {{#user-summary-section title="top_replies" class="replies-section pull-left"}}
diff --git a/app/models/user_summary.rb b/app/models/user_summary.rb
index 435702d..09c025b 100644
--- a/app/models/user_summary.rb
+++ b/app/models/user_summary.rb
@@ -112,6 +112,10 @@ class UserSummary
     @user.id
   end
 
+  def user
+    @user
+  end
+
   def user_stat
     @user.user_stat
   end
diff --git a/app/serializers/user_summary_serializer.rb b/app/serializers/user_summary_serializer.rb
index d05ef81..3a7c9d8 100644
--- a/app/serializers/user_summary_serializer.rb
+++ b/app/serializers/user_summary_serializer.rb
@@ -56,7 +56,12 @@ class UserSummarySerializer < ApplicationSerializer
              :post_count,
              :time_read,
              :recent_time_read,
-             :bookmark_count
+             :bookmark_count,
+             :can_see_summary_stats
+
+  def can_see_summary_stats
+    scope.can_see_summary_stats?(object.user)
+  end
 
   def include_badges?
     SiteSetting.enable_badges
@@ -73,4 +78,40 @@ class UserSummarySerializer < ApplicationSerializer
   def recent_time_read
     object.recent_time_read
   end
+
+  def include_likes_given?
+    can_see_summary_stats
+  end
+
+  def include_likes_received?
+    can_see_summary_stats
+  end
+
+  def include_topics_entered?
+    can_see_summary_stats
+  end
+
+  def include_posts_read_count?
+    can_see_summary_stats
+  end
+
+  def include_days_visited?
+    can_see_summary_stats
+  end
+
+  def include_topic_count?
+    can_see_summary_stats
+  end
+
+  def include_post_count?
+    can_see_summary_stats
+  end
+
+  def include_time_read?
+    can_see_summary_stats
+  end
+
+  def include_recent_time_read?
+    can_see_summary_stats
+  end
 end
diff --git a/lib/guardian/user_guardian.rb b/lib/guardian/user_guardian.rb
index 4ee12fe..517c924 100644
--- a/lib/guardian/user_guardian.rb
+++ b/lib/guardian/user_guardian.rb
@@ -154,4 +154,8 @@ module UserGuardian
         .exists?
     )
   end
+
+  def can_see_summary_stats?(target_user)
+    true
+  end
 end

GitHub sha: bec6bace

1 Like

This commit appears in #9772 which was approved by eviltrout. It was merged by markvanlan.