Skip to content

Commit

Permalink
Revert "Add admin-warning feature for critical deprecations"
Browse files Browse the repository at this point in the history
Moved into core via discourse/discourse@07caa5b

This reverts commit 49e1f6a.
  • Loading branch information
davidtaylorhq committed Jan 3, 2024
1 parent d95706b commit 49a327b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 110 deletions.
65 changes: 1 addition & 64 deletions assets/javascripts/discourse/services/deprecation-collector.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { registerDeprecationHandler } from "@ember/debug";
import { cancel } from "@ember/runloop";
import Service, { inject as service } from "@ember/service";
import { withPluginApi } from "discourse/lib/plugin-api";
import identifySource from "discourse/lib/source-identifier";
import { escapeExpression } from "discourse/lib/utilities";
import discourseDebounce from "discourse-common/lib/debounce";
import { registerDeprecationHandler as registerDiscourseDeprecationHandler } from "discourse-common/lib/deprecated";
import getURL from "discourse-common/lib/get-url";
import { bind } from "discourse-common/utils/decorators";
import I18n from "discourse-i18n";

// Deprecation handling APIs don't have any way to unregister handlers, so we set up permenant
// handlers and link them up to the application lifecycle using module-local state.
Expand All @@ -21,20 +18,12 @@ registerDiscourseDeprecationHandler((message, opts) =>
handler?.(message, opts)
);

const CRITICAL_DEPRECATIONS = [
/^discourse.modal-controllers$/,
/^(?!discourse\.)/,
];

export default class DeprecationCollector extends Service {
@service router;
@service currentUser;
@service siteSettings;

#configById = new Map();
#counts = new Map();
#reportDebounce;
#adminWarned = false;

constructor() {
super(...arguments);
Expand Down Expand Up @@ -74,13 +63,10 @@ export default class DeprecationCollector extends Service {
return;
}

const source = identifySource();
if (source?.type === "browser-extension") {
if (identifySource()?.type === "browser-extension") {
return;
}

this.maybeNotifyAdmin(options.id, source);

let count = this.#counts.get(options.id) || 0;
count += 1;
this.#counts.set(options.id, count);
Expand All @@ -107,53 +93,4 @@ export default class DeprecationCollector extends Service {

navigator.sendBeacon(getURL("/deprecation-collector/log"), body);
}

maybeNotifyAdmin(id, source) {
if (this.#adminWarned) {
return;
}

if (!this.currentUser?.admin) {
return;
}

if (!this.siteSettings?.deprecation_collector_warn_critical_deprecations) {
return;
}

if (CRITICAL_DEPRECATIONS.some((pattern) => pattern.test(id))) {
this.notifyAdmin(id, source);
}
}

notifyAdmin(id, source) {
this.#adminWarned = true;

let notice = I18n.t("deprecation_collector.critical_deprecations");

if (
this.siteSettings?.deprecation_collector_critical_deprecations_message
) {
notice +=
" " +
this.siteSettings.deprecation_collector_critical_deprecations_message;
}

if (source?.type === "theme") {
notice +=
" " +
I18n.t("deprecation_collector.theme_source", {
name: escapeExpression(source.name),
path: source.path,
});
}

withPluginApi("0.1", (api) => {
api.addGlobalNotice(notice, "critical-deprecation", {
dismissable: true,
dismissDuration: moment.duration(1, "day"),
level: "warn",
});
});
}
}
4 changes: 0 additions & 4 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,3 @@ en:
site_settings:
categories:
deprecation_collector: "Deprecation Collector"
js:
deprecation_collector:
critical_deprecations: "<b>[Admin Notice]</b> One of your themes or plugins needs updating for compatibility with upcoming Discourse core changes (<a target='_blank' href='https://meta.discourse.org/t/287211'>more info</a>)."
theme_source: "Identified theme: <a target='_blank' href='%{path}'>'%{name}'</a>."
8 changes: 0 additions & 8 deletions config/settings.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
deprecation_collector:
deprecation_collector_enabled:
default: false
deprecation_collector_warn_critical_deprecations:
default: false
hidden: true
client: true
deprecation_collector_critical_deprecations_message:
default: ""
hidden: true
client: true
48 changes: 14 additions & 34 deletions spec/system/deprecation_collector_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
# frozen_string_literal: true

describe "Deprecation collector", type: :system do
before { SiteSetting.deprecation_collector_enabled = true }

let(:test_deprecation_id) { DeprecationCollector::List.first }
let(:logged_deprecations) { [] }
let!(:stub) do
DeprecationCollector
.stubs(:add_to_counter)
.with do |value|
logged_deprecations << value
true
end
end

it "successfully reports deprecations to the server" do
SiteSetting.deprecation_collector_enabled = true

visit("/latest")
expect(find("#main-outlet-wrapper")).to be_visible

logged_deprecations = []

test_deprecation_id = DeprecationCollector::List.first

stub =
DeprecationCollector
.stubs(:add_to_counter)
.with do |value|
logged_deprecations << value
true
end

# Trigger some fake deprecations
page.execute_script <<~JS
const deprecated = require("discourse-common/lib/deprecated").default;
Expand All @@ -32,25 +33,4 @@
expect(logged_deprecations).to include(test_deprecation_id, "_other_discourse")
end
end

it "warns admins about deprecations when enabled" do
sign_in Fabricate(:admin)

SiteSetting.deprecation_collector_warn_critical_deprecations = true
SiteSetting.deprecation_collector_critical_deprecations_message =
"Discourse core changes will be applied to your site on Jan 15."

visit("/latest")

page.execute_script <<~JS
const deprecated = require("discourse-common/lib/deprecated").default;
deprecated("Fake deprecation message", { id: #{test_deprecation_id.to_json} })
JS

message = find("#global-notice-critical-deprecation")
expect(message).to have_text(
"One of your themes or plugins needs updating for compatibility with upcoming Discourse core changes",
)
expect(message).to have_text(SiteSetting.deprecation_collector_critical_deprecations_message)
end
end

0 comments on commit 49a327b

Please sign in to comment.