FEATURE: Include muted users count within the ignored users report (#7230)

FEATURE: Include muted users count within the ignored users report (#7230)

diff --git a/app/models/report.rb b/app/models/report.rb
index c217844..78c0975 100644
--- a/app/models/report.rb
+++ b/app/models/report.rb
@@ -1591,31 +1591,59 @@ class Report
           :ignores_count,
         ],
         title: I18n.t("reports.top_ignored_users.labels.ignores_count")
+      },
+      {
+        type: :number,
+        properties: [
+          :mutes_count,
+        ],
+        title: I18n.t("reports.top_ignored_users.labels.mutes_count")
       }
     ]
 
     report.data = []
 
     sql = <<~SQL
-      SELECT
-      u.id AS user_id,
-      u.username,
-      u.uploaded_avatar_id,
-      COUNT(*) AS ignores_count
-      FROM users AS u
-      INNER JOIN ignored_users AS ig ON ig.ignored_user_id = u.id
-      WHERE ig.created_at >= '#{report.start_date}' AND ig.created_at <= '#{report.end_date}'
-      GROUP BY u.id
-      ORDER BY COUNT(*) DESC
-      LIMIT #{report.limit || 250}
+        WITH ignored_users AS (
+          SELECT
+          ignored_user_id as user_id,
+          COUNT(*) AS ignores_count
+          FROM ignored_users
+          WHERE created_at >= '#{report.start_date}' AND created_at <= '#{report.end_date}'
+          GROUP BY ignored_user_id
+          ORDER BY COUNT(*) DESC
+          LIMIT :limit
+        ),
+        muted_users AS (
+          SELECT
+          muted_user_id as user_id,
+          COUNT(*) AS mutes_count
+          FROM muted_users
+          WHERE created_at >= '#{report.start_date}' AND created_at <= '#{report.end_date}'
+          GROUP BY muted_user_id
+          ORDER BY COUNT(*) DESC
+          LIMIT :limit
+        )
+
+        SELECT u.id as user_id,
+               u.username as username,
+               u.uploaded_avatar_id as uploaded_avatar_id,
+               ig.ignores_count as ignores_count,
+               COALESCE(mu.mutes_count, 0) as mutes_count,
+               ig.ignores_count + COALESCE(mu.mutes_count, 0) as total
+        FROM users as u
+        JOIN ignored_users as ig ON ig.user_id = u.id
+        LEFT OUTER JOIN muted_users as mu ON mu.user_id = u.id
+        ORDER BY total DESC
     SQL
 
-    DB.query(sql).each do |row|
+    DB.query(sql, limit: report.limit || 250).each do |row|
       report.data << {
         ignored_user_id: row.user_id,
         ignored_username: row.username,
         ignored_user_avatar_template: User.avatar_template(row.username, row.uploaded_avatar_id),
         ignores_count: row.ignores_count,
+        mutes_count: row.mutes_count,
       }
     end
   end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 78c1fa4..14d3d78 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1234,6 +1234,7 @@ en:
       labels:
         ignored_user: Ignored User
         ignores_count: Ignores count
+        mutes_count: Mutes count
       description: "List top ignored users."
 
   dashboard:
diff --git a/spec/fabricators/muted_user.rb b/spec/fabricators/muted_user.rb
new file mode 100644
index 0000000..9c486d6
--- /dev/null
+++ b/spec/fabricators/muted_user.rb
@@ -0,0 +1,3 @@
+Fabricator(:muted_user) do
+  user
+end
diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb
index 42e252e..d9ad101 100644
--- a/spec/models/report_spec.rb
+++ b/spec/models/report_spec.rb
@@ -1108,15 +1108,29 @@ describe Report do
 
       it "works" do
         expect(report.data.length).to eq(2)
-        expect_row_to_be_equal(report.data[0], john)
-        expect_row_to_be_equal(report.data[1], matt)
+        expect_row_to_be_equal(report.data[0], john, 1, 0)
+        expect_row_to_be_equal(report.data[1], matt, 1, 0)
       end
 
-      def expect_row_to_be_equal(row, user)
+      def expect_row_to_be_equal(row, user, ignores, mutes)
         expect(row[:ignored_user_id]).to eq(user.id)
         expect(row[:ignored_username]).to eq(user.username)
         expect(row[:ignored_user_avatar_template]).to eq(User.avatar_template(user.username, user.uploaded_avatar_id))
-        expect(row[:ignores_count]).to eq(1)
+        expect(row[:ignores_count]).to eq(ignores)
+        expect(row[:mutes_count]).to eq(mutes)
+      end
+
+      context "when muted users exist" do
+        before do
+          Fabricate(:muted_user, user: tarek, muted_user: john)
+          Fabricate(:muted_user, user: tarek, muted_user: matt)
+        end
+
+        it "works" do
+          expect(report.data.length).to eq(2)
+          expect_row_to_be_equal(report.data[0], john, 1, 1)
+          expect_row_to_be_equal(report.data[1], matt, 1, 1)
+        end
       end
     end

GitHub sha: 605530a7

1 Like