discourse/app/models/concerns/reports/top_ignored_users.rb
Martin Brennan 15838aa756
DEV: Convert AdminReport component to gjs (#31011)
This commit converts the `AdminReport` component, which is quite
high complexity, to gjs. After this initial round, ideally this
component would be broken up into smaller components because it is
getting quite big now.

Also in this commit:

* Add an option to display the report description in a tooltip, which
was
   the main way the description was shown until recently. We want to use
   this on the dashboard view mostly.
* Move admin report "mode" definitions to the server-side Report model,
inside a `Report::MODES` constant, collecting the modes defined in
various
   places in the UI into one place
* Refactor report code to refer to mode definitions
* Add a `REPORT_MODES` constant in JS via javascript.rake and refactor
  JS to refer to the modes
* Delete old admin report components that are no longer used
  (trust-level-counts, counts, per-day-counts) which were replaced
  by admin-report-counters a while ago
* Add a new `registerReportModeComponent` plugin API, some plugins
   introduce their own modes (like AI's `emotion`) and components and
   we need a way to render them
2025-01-29 10:33:43 +10:00

82 lines
2.5 KiB
Ruby
Vendored

# frozen_string_literal: true
module Reports::TopIgnoredUsers
extend ActiveSupport::Concern
class_methods do
def report_top_ignored_users(report)
report.modes = [Report::MODES[:table]]
report.labels = [
{
type: :user,
properties: {
id: :ignored_user_id,
username: :ignored_username,
avatar: :ignored_user_avatar_template,
},
title: I18n.t("reports.top_ignored_users.labels.ignored_user"),
},
{
type: :number,
properties: [: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
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, 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
end
end