From a23773f83d713fa6ce6369adba12a5cb0a2ff16a Mon Sep 17 00:00:00 2001 From: Osama Sayegh Date: Wed, 4 Sep 2024 19:58:14 +0300 Subject: [PATCH] DEV: Deprecate `show_in_ui` argument of the `register_stat` API (#28727) We need to start printing deprecation notices when the `show_in_ui` argument is used because it works only for the old about page which will be removed soon. For the new about page, we've introduced a new API `addAboutPageActivity` which is more flexible than a true/false argument on the server side. Internal topic: t/136551. --- .../discourse/app/controllers/about.js | 20 ++++++++++ .../discourse/app/lib/plugin-api.gjs | 2 + .../discourse/app/templates/about.hbs | 5 +-- .../discourse/tests/helpers/qunit-helpers.js | 2 + lib/plugin/instance.rb | 21 ++++++++-- .../discourse/initializers/chat-setup.js | 38 +++++++++---------- plugins/chat/plugin.rb | 4 +- 7 files changed, 63 insertions(+), 29 deletions(-) diff --git a/app/assets/javascripts/discourse/app/controllers/about.js b/app/assets/javascripts/discourse/app/controllers/about.js index 6d211c17ed8..55206f821aa 100644 --- a/app/assets/javascripts/discourse/app/controllers/about.js +++ b/app/assets/javascripts/discourse/app/controllers/about.js @@ -3,6 +3,17 @@ import { alias, gt } from "@ember/object/computed"; import discourseComputed from "discourse-common/utils/decorators"; import I18n from "discourse-i18n"; +// TODO(osama): remove all of these methods when the legacy about page is +// removed +const customStats = []; +export function addLegacyStat(name) { + customStats.push(name); +} + +export function clearCustomStats() { + customStats.clear(); +} + export default class AboutController extends Controller { @gt("siteSettings.faq_url.length", 0) faqOverridden; @@ -34,4 +45,13 @@ export default class AboutController extends Controller { eu_visitors: eu, }); } + + @discourseComputed("site.displayed_about_plugin_stat_groups") + statGroups() { + const set = new Set(customStats); + for (const name of this.site.displayed_about_plugin_stat_groups || []) { + set.add(name); + } + return Array.from(set); + } } diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.gjs b/app/assets/javascripts/discourse/app/lib/plugin-api.gjs index 84c1e6cbc65..470fe28a53e 100644 --- a/app/assets/javascripts/discourse/app/lib/plugin-api.gjs +++ b/app/assets/javascripts/discourse/app/lib/plugin-api.gjs @@ -46,6 +46,7 @@ import { setDesktopScrollAreaHeight } from "discourse/components/topic-timeline/ import { addTopicTitleDecorator } from "discourse/components/topic-title"; import { setNotificationsLimit as setUserMenuNotificationsLimit } from "discourse/components/user-menu/notifications-list"; import { addUserMenuProfileTabItem } from "discourse/components/user-menu/profile-tab-content"; +import { addLegacyStat as addLegacyAboutPageStat } from "discourse/controllers/about"; import { addDiscoveryQueryParam } from "discourse/controllers/discovery/list"; import { registerFullPageSearchType } from "discourse/controllers/full-page-search"; import { registerCustomPostMessageCallback as registerCustomPostMessageCallback1 } from "discourse/controllers/topic"; @@ -3278,6 +3279,7 @@ class PluginApi { */ addAboutPageActivity(name, func) { addAboutPageActivity(name, func); + addLegacyAboutPageStat(name); } #deprecatedHeaderWidgetOverride(widgetName, override) { diff --git a/app/assets/javascripts/discourse/app/templates/about.hbs b/app/assets/javascripts/discourse/app/templates/about.hbs index 135aa123cc5..5b6fd6dba64 100644 --- a/app/assets/javascripts/discourse/app/templates/about.hbs +++ b/app/assets/javascripts/discourse/app/templates/about.hbs @@ -162,10 +162,7 @@ {{number this.model.stats.likes_30_days}} {{number this.model.stats.likes_count}} - {{#each - this.site.displayed_about_plugin_stat_groups - as |statGroupName| - }} + {{#each this.statGroups as |statGroupName|}} {{i18n (concat "about." statGroupName "_count")}} {{number diff --git a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js index 5c24b18ca61..c661d7eef65 100644 --- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js +++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js @@ -29,6 +29,7 @@ import { resetQuickSearchRandomTips } from "discourse/components/search-menu/res import { resetOnKeyUpCallbacks } from "discourse/components/search-menu/search-term"; import { resetTopicTitleDecorators } from "discourse/components/topic-title"; import { resetUserMenuProfileTabItems } from "discourse/components/user-menu/profile-tab-content"; +import { clearCustomStats as clearLegacyAboutPageStats } from "discourse/controllers/about"; import { resetCustomPostMessageCallbacks } from "discourse/controllers/topic"; import { clearHTMLCache } from "discourse/helpers/custom-html"; import { resetUsernameDecorators } from "discourse/helpers/decorate-username-selector"; @@ -253,6 +254,7 @@ export function testCleanup(container, app) { resetTransformers(); rollbackAllPrepends(); clearAboutPageActivities(); + clearLegacyAboutPageStats(); } function cleanupCssGeneratorTags() { diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index b41ac15f6c7..38199ed4d72 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -1122,9 +1122,24 @@ class Plugin::Instance # table. Some stats may be needed purely for reporting purposes and thus # do not need to be shown in the UI to admins/users. # - # TODO(osama): deprecate show_in_ui when experimental_redesigned_about_page_groups - # is removed - def register_stat(name, show_in_ui: false, expose_via_api: false, &block) + # TODO(osama): remove show_in_ui when experimental_redesigned_about_page_groups is removed + def register_stat( + name, + show_in_ui: ( + not_using_deprecated_arg = true + false + ), + expose_via_api: false, + &block + ) + if !not_using_deprecated_arg + Discourse.deprecate( + "`show_in_ui` argument of the `register_stat` API is deprecated. Please use the `addAboutPageActivity` JS API instead if you want your custom stat to be shown on the about page.", + since: "3.4.0.beta2", + drop_from: "3.5.0.beta1", + ) + end + # We do not want to register and display the same group multiple times. return if DiscoursePluginRegistry.stats.any? { |stat| stat.name == name } diff --git a/plugins/chat/assets/javascripts/discourse/initializers/chat-setup.js b/plugins/chat/assets/javascripts/discourse/initializers/chat-setup.js index a51aad398a1..b6372b88db9 100644 --- a/plugins/chat/assets/javascripts/discourse/initializers/chat-setup.js +++ b/plugins/chat/assets/javascripts/discourse/initializers/chat-setup.js @@ -29,11 +29,26 @@ class ChatSetupInit { setOwner(this, owner); this.appEvents.on("discourse:focus-changed", this, "_handleFocusChanged"); - if (!this.chatService.userCanChat) { - return; - } - withPluginApi("0.12.1", (api) => { + api.addAboutPageActivity("chat_messages", (periods) => { + const count = periods["7_days"]; + if (count) { + return { + icon: "comment-dots", + class: "chat-messages", + activityText: I18n.t("about.activities.chat_messages", { + count, + formatted_number: number(count), + }), + period: I18n.t("about.activities.periods.last_7_days"), + }; + } + }); + + if (!this.chatService.userCanChat) { + return; + } + api.onPageChange((path) => { const route = this.router.recognize(path); if (route.name.startsWith("chat.")) { @@ -157,21 +172,6 @@ class ChatSetupInit { category: "organisms", id: "chat", }); - - api.addAboutPageActivity("chat_messages", (periods) => { - const count = periods["7_days"]; - if (count) { - return { - icon: "comment-dots", - class: "chat-messages", - activityText: I18n.t("about.activities.chat_messages", { - count, - formatted_number: number(count), - }), - period: I18n.t("about.activities.periods.last_7_days"), - }; - } - }); }); } diff --git a/plugins/chat/plugin.rb b/plugins/chat/plugin.rb index c3e5ae096a9..6ec051470ee 100644 --- a/plugins/chat/plugin.rb +++ b/plugins/chat/plugin.rb @@ -500,9 +500,7 @@ after_initialize do register_email_unsubscriber("chat_summary", EmailControllerHelper::ChatSummaryUnsubscriber) - register_stat("chat_messages", show_in_ui: true, expose_via_api: true) do - Chat::Statistics.about_messages - end + register_stat("chat_messages", expose_via_api: true) { Chat::Statistics.about_messages } register_stat("chat_users", expose_via_api: true) { Chat::Statistics.about_users } register_stat("chat_channels", expose_via_api: true) { Chat::Statistics.about_channels }