FEATURE: Add post edits count to user activity (#13495)

FEATURE: Add post edits count to user activity (#13495)

diff --git a/app/assets/javascripts/admin/addon/controllers/admin-user-index.js b/app/assets/javascripts/admin/addon/controllers/admin-user-index.js
index 517f0c0..b9492e3 100644
--- a/app/assets/javascripts/admin/addon/controllers/admin-user-index.js
+++ b/app/assets/javascripts/admin/addon/controllers/admin-user-index.js
@@ -121,6 +121,11 @@ export default Controller.extend(CanCheckEmails, {
     }
   },
 
+  @discourseComputed("model.username")
+  postEditsByEditorFilter(username) {
+    return { editor: username };
+  },
+
   groupAdded(added) {
     this.model
       .groupAdded(added)
diff --git a/app/assets/javascripts/admin/addon/templates/user-index.hbs b/app/assets/javascripts/admin/addon/templates/user-index.hbs
index 41da0bf..17350dc 100644
--- a/app/assets/javascripts/admin/addon/templates/user-index.hbs
+++ b/app/assets/javascripts/admin/addon/templates/user-index.hbs
@@ -641,6 +641,20 @@
     <div class="field">{{i18n "user.invited.days_visited"}}</div>
     <div class="value">{{html-safe model.days_visited}}</div>
   </div>
+  <div class="display-row post-edits-count">
+    <div class="field">{{i18n "admin.user.post_edits_count" }}</div>
+    <div class="value">
+      {{if (gt model.post_edits_count 0) model.post_edits_count "0"}}
+    </div>
+    <div class="controls">
+      {{#if (gt model.post_edits_count 0) }}
+        {{#link-to "adminReports.show" "post_edits" (query-params filters=postEditsByEditorFilter) class="btn btn-icon"}}
+          {{d-icon "far-eye"}}
+          {{i18n "admin.user.view_edits"}}
+        {{/link-to}}
+      {{/if}}
+    </div>
+  </div>
 </section>
 
 {{#if model.single_sign_on_record}}
diff --git a/app/assets/javascripts/discourse/tests/acceptance/admin-user-index-test.js b/app/assets/javascripts/discourse/tests/acceptance/admin-user-index-test.js
index 296b876..3c399d2 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/admin-user-index-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/admin-user-index-test.js
@@ -3,7 +3,7 @@ import {
   exists,
   queryAll,
 } from "discourse/tests/helpers/qunit-helpers";
-import { click, fillIn, visit } from "@ember/test-helpers";
+import { click, currentURL, fillIn, visit } from "@ember/test-helpers";
 import selectKit from "discourse/tests/helpers/select-kit-helper";
 import { test } from "qunit";
 
@@ -63,6 +63,40 @@ acceptance("Admin - User Index", function (needs) {
     );
   });
 
+  test("shows the number of post edits", async function (assert) {
+    await visit("/admin/users/1/eviltrout");
+
+    assert.equal(queryAll(".post-edits-count .value").text().trim(), "6");
+
+    assert.ok(
+      exists(".post-edits-count .controls .btn.btn-icon"),
+      "View edits button exists"
+    );
+  });
+
+  test("a link to view post edits report exists", async function (assert) {
+    await visit("/admin/users/1/eviltrout");
+
+    let filter = encodeURIComponent('{"editor":"eviltrout"}');
+
+    await click(".post-edits-count .controls .btn.btn-icon");
+
+    assert.equal(
+      currentURL(),
+      `/admin/reports/post_edits?filters=${filter}`,
+      "it redirects to the right admin report"
+    );
+  });
+
+  test("hides the 'view Edits' button if the count is zero", async function (assert) {
+    await visit("/admin/users/2/sam");
+
+    assert.ok(
+      !exists(".post-edits-count .controls .btn.btn-icon"),
+      "View Edits button not present"
+    );
+  });
+
   test("will clear unsaved groups when switching user", async function (assert) {
     await visit("/admin/users/2/sam");
 
diff --git a/app/assets/javascripts/discourse/tests/helpers/create-pretender.js b/app/assets/javascripts/discourse/tests/helpers/create-pretender.js
index 123eaa9..be32ddf 100644
--- a/app/assets/javascripts/discourse/tests/helpers/create-pretender.js
+++ b/app/assets/javascripts/discourse/tests/helpers/create-pretender.js
@@ -746,6 +746,7 @@ export function applyDefaultHandlers(pretender) {
       username: "eviltrout",
       email: "eviltrout@example.com",
       admin: true,
+      post_edits_count: 6,
     });
   });
 
diff --git a/app/assets/javascripts/discourse/tests/integration/components/admin-report-test.js b/app/assets/javascripts/discourse/tests/integration/components/admin-report-test.js
index df9a581..b88b216 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/admin-report-test.js
+++ b/app/assets/javascripts/discourse/tests/integration/components/admin-report-test.js
@@ -166,6 +166,17 @@ discourseModule("Integration | Component | admin-report", function (hooks) {
     },
   });
 
+  componentTest("post edits", {
+    template: hbs`{{admin-report dataSourceName='post_edits'}}`,
+
+    test(assert) {
+      assert.ok(
+        exists(".admin-report.post-edits"),
+        "it displays the post edits report"
+      );
+    },
+  });
+
   componentTest("not found", {
     template: hbs`{{admin-report dataSourceName='not_found'}}`,
 
diff --git a/app/assets/stylesheets/common/admin/customize.scss b/app/assets/stylesheets/common/admin/customize.scss
index 4eeb081..45dac3e 100644
--- a/app/assets/stylesheets/common/admin/customize.scss
+++ b/app/assets/stylesheets/common/admin/customize.scss
@@ -1,4 +1,4 @@
-// Customise area
+// Customize area
 
 .email-template {
   input {
diff --git a/app/models/concerns/reports/post_edits.rb b/app/models/concerns/reports/post_edits.rb
index 21ba866..554fea5 100644
--- a/app/models/concerns/reports/post_edits.rb
+++ b/app/models/concerns/reports/post_edits.rb
@@ -6,6 +6,7 @@ module Reports::PostEdits
   class_methods do
     def report_post_edits(report)
       category_id, include_subcategories = report.add_category_filter
+      editor_username = report.filters['editor']
 
       report.modes = [:table]
 
@@ -89,7 +90,12 @@ module Reports::PostEdits
         end
       end
 
-      builder.where("editor.id > 0 AND editor.id != author.id")
+      if editor_username
+        builder.where("editor.username = ?", editor_username)
+      else
+        builder.where("editor.id > 0 AND editor.id != author.id")
+      end
+
       builder.where("pr.created_at >= :start_date", start_date: report.start_date)
       builder.where("pr.created_at <= :end_date", end_date: report.end_date)
 
diff --git a/app/models/post_revision.rb b/app/models/post_revision.rb
index 4557f0f..9d9d48c 100644
--- a/app/models/post_revision.rb
+++ b/app/models/post_revision.rb
@@ -59,4 +59,4 @@ end
 #
 #  index_post_revisions_on_post_id             (post_id)
 #  index_post_revisions_on_post_id_and_number  (post_id,number)
-#
+#  index_post_revisions_on_user_id             (user_id)
diff --git a/app/models/user.rb b/app/models/user.rb
index 4f80745..5204aff 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -433,7 +433,6 @@ class User < ActiveRecord::Base
   end
 
   def created_topic_count
-    stat = user_stat || create_user_stat
     stat.topic_count
   end
 
@@ -894,10 +893,17 @@ class User < ActiveRecord::Base
   end
 
   def post_count
-    stat = user_stat || create_user_stat
     stat.post_count
   end
 
+  def post_edits_count
+    stat.post_edits_count
+  end
+
+  def increment_post_edits_count
+    stat.increment!(:post_edits_count)
+  end
+
   def flags_given_count
     PostAction.where(user_id: id, post_action_type_id: PostActionType.flag_types_without_custom.values).count
   end
@@ -1468,9 +1474,7 @@ class User < ActiveRecord::Base
   end
 
   def create_user_stat
-    stat = UserStat.new(new_since: Time.now)
-    stat.user_id = id
-    stat.save!
+    UserStat.create!(new_since: Time.zone.now, user_id: id)
   end
 
   def create_user_option
@@ -1659,6 +1663,10 @@ class User < ActiveRecord::Base
 
   private
 
+  def stat
+    user_stat || create_user_stat
+  end
+
   def trigger_user_automatic_group_refresh
     if !staged
       Group.user_trust_level_change!(id, trust_level)
diff --git a/app/models/user_stat.rb b/app/models/user_stat.rb
index 045dcac..33e0118 100644

[... diff too long, it was truncated ...]

GitHub sha: e7b8e755833c56aafd8b33be5487c3228d872a43

This commit appears in #13495 which was approved by pmusaraj, eviltrout, and tgxworld. It was merged by jmperez127.