From db6eff7be9ffff6c7d111fd0e344eaea80ed482e Mon Sep 17 00:00:00 2001 From: Osama Sayegh Date: Tue, 20 Aug 2024 16:16:05 +0300 Subject: [PATCH] DEV: Allow custom site activity items in the new /about page (#28400) This commit introduces a new frontend API to add custom items to the "Site activity" section in the new /about page. The new API is called `addAboutPageActivity` and it works along side the `register_stat` serve-side API which serializes the data that the frontend API consumes. More details of how the two APIs work together is in the JSDoc comment above the API function definition. Internal topic: t/128545/9. --- .../discourse/app/components/about-page.gjs | 41 +++++++++++- .../discourse/app/lib/plugin-api.gjs | 42 +++++++++++- .../discourse/tests/helpers/qunit-helpers.js | 2 + .../components/about-page-test.gjs | 64 +++++++++++++++++++ docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md | 4 ++ lib/plugin/instance.rb | 3 + .../discourse/initializers/chat-setup.js | 16 +++++ plugins/chat/config/locales/client.en.yml | 4 ++ plugins/chat/plugin.rb | 1 + .../system/about_page_site_acitivity_spec.rb | 30 +++++++++ .../components/about_page_site_activity.rb | 8 +++ .../about_page_site_activity_item.rb | 10 +++ 12 files changed, 223 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/discourse/tests/integration/components/about-page-test.gjs create mode 100644 plugins/chat/spec/system/about_page_site_acitivity_spec.rb diff --git a/app/assets/javascripts/discourse/app/components/about-page.gjs b/app/assets/javascripts/discourse/app/components/about-page.gjs index 9406dd14188..5d929aaa44c 100644 --- a/app/assets/javascripts/discourse/app/components/about-page.gjs +++ b/app/assets/javascripts/discourse/app/components/about-page.gjs @@ -9,6 +9,16 @@ import i18n from "discourse-common/helpers/i18n"; import escape from "discourse-common/lib/escape"; import I18n from "discourse-i18n"; +const pluginActivitiesFuncs = []; + +export function addAboutPageActivity(name, func) { + pluginActivitiesFuncs.push({ name, func }); +} + +export function clearAboutPageActivities() { + pluginActivitiesFuncs.clear(); +} + export default class AboutPage extends Component { get moderatorsCount() { return this.args.model.moderators.length; @@ -57,7 +67,7 @@ export default class AboutPage extends Component { } get siteActivities() { - return [ + const list = [ { icon: "scroll", class: "topics", @@ -104,6 +114,8 @@ export default class AboutPage extends Component { period: I18n.t("about.activities.periods.all_time"), }, ]; + + return list.concat(this.siteActivitiesFromPlugins()); } get contactInfo() { @@ -139,6 +151,33 @@ export default class AboutPage extends Component { } } + siteActivitiesFromPlugins() { + const stats = this.args.model.stats; + const statKeys = Object.keys(stats); + + const configs = []; + for (const { name, func } of pluginActivitiesFuncs) { + let present = false; + const periods = {}; + for (const stat of statKeys) { + const prefix = `${name}_`; + if (stat.startsWith(prefix)) { + present = true; + const period = stat.replace(prefix, ""); + periods[period] = stats[stat]; + } + } + if (!present) { + continue; + } + const config = func(periods); + if (config) { + configs.push(config); + } + } + return configs; + } +