DEV: Refactor `logs-notice`/`global-notice` (#15000)

This commit is contained in:
Jarek Radosz 2021-11-18 21:49:58 +01:00 committed by GitHub
parent 2c045c6368
commit e6670393df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 156 additions and 163 deletions

View File

@ -1,11 +1,11 @@
import EmberObject, { computed } from "@ember/object"; import EmberObject, { action } from "@ember/object";
import cookie, { removeCookie } from "discourse/lib/cookie"; import cookie, { removeCookie } from "discourse/lib/cookie";
import Component from "@ember/component"; import Component from "@ember/component";
import I18n from "I18n"; import I18n from "I18n";
import LogsNotice from "discourse/services/logs-notice"; import discourseComputed, { bind } from "discourse-common/utils/decorators";
import { bind } from "discourse-common/utils/decorators";
import getURL from "discourse-common/lib/get-url"; import getURL from "discourse-common/lib/get-url";
import { htmlSafe } from "@ember/template"; import { htmlSafe } from "@ember/template";
import { inject as service } from "@ember/service";
const _pluginNotices = []; const _pluginNotices = [];
@ -48,32 +48,55 @@ const Notice = EmberObject.extend({
}); });
export default Component.extend({ export default Component.extend({
logsNoticeService: service("logsNotice"),
logNotice: null, logNotice: null,
init() { init() {
this._super(...arguments); this._super(...arguments);
this._setupObservers(); this.logsNoticeService.addObserver("hidden", this._handleLogsNoticeUpdate);
this.logsNoticeService.addObserver("text", this._handleLogsNoticeUpdate);
}, },
willDestroyElement() { willDestroyElement() {
this._super(...arguments); this._super(...arguments);
this._tearDownObservers(); this.logsNoticeService.removeObserver("text", this._handleLogsNoticeUpdate);
this.logsNoticeService.removeObserver(
"hidden",
this._handleLogsNoticeUpdate
);
}, },
notices: computed( @discourseComputed(
"site.isReadOnly", "site.isReadOnly",
"site.wizard_required",
"siteSettings.login_required",
"siteSettings.disable_emails", "siteSettings.disable_emails",
"logNotice.{id,text,hidden}", "siteSettings.global_notice",
function () { "siteSettings.bootstrap_mode_enabled",
"siteSettings.bootstrap_mode_min_users",
"session.safe_mode",
"logNotice.{id,text,hidden}"
)
notices(
isReadOnly,
wizardRequired,
loginRequired,
disableEmails,
globalNotice,
bootstrapModeEnabled,
bootstrapModeMinUsers,
safeMode,
logNotice
) {
let notices = []; let notices = [];
if (cookie("dosp") === "1") { if (cookie("dosp") === "1") {
removeCookie("dosp", { path: "/" }); removeCookie("dosp", { path: "/" });
notices.push( notices.push(
Notice.create({ Notice.create({
text: this.siteSettings.login_required text: loginRequired
? I18n.t("forced_anonymous_login_required") ? I18n.t("forced_anonymous_login_required")
: I18n.t("forced_anonymous"), : I18n.t("forced_anonymous"),
id: "forced-anonymous", id: "forced-anonymous",
@ -81,13 +104,13 @@ export default Component.extend({
); );
} }
if (this.session && this.session.safe_mode) { if (safeMode) {
notices.push( notices.push(
Notice.create({ text: I18n.t("safe_mode.enabled"), id: "safe-mode" }) Notice.create({ text: I18n.t("safe_mode.enabled"), id: "safe-mode" })
); );
} }
if (this.site.isReadOnly) { if (isReadOnly) {
notices.push( notices.push(
Notice.create({ Notice.create({
text: I18n.t("read_only_mode.enabled"), text: I18n.t("read_only_mode.enabled"),
@ -96,10 +119,7 @@ export default Component.extend({
); );
} }
if ( if (disableEmails === "yes" || disableEmails === "non-staff") {
this.siteSettings.disable_emails === "yes" ||
this.siteSettings.disable_emails === "non-staff"
) {
notices.push( notices.push(
Notice.create({ Notice.create({
text: I18n.t("emails_are_disabled"), text: I18n.t("emails_are_disabled"),
@ -108,7 +128,7 @@ export default Component.extend({
); );
} }
if (this.site.wizard_required) { if (wizardRequired) {
const requiredText = I18n.t("wizard_required", { const requiredText = I18n.t("wizard_required", {
url: getURL("/wizard"), url: getURL("/wizard"),
}); });
@ -117,15 +137,12 @@ export default Component.extend({
); );
} }
if ( if (this.currentUser?.staff && bootstrapModeEnabled) {
this.get("currentUser.staff") && if (bootstrapModeMinUsers > 0) {
this.siteSettings.bootstrap_mode_enabled
) {
if (this.siteSettings.bootstrap_mode_min_users > 0) {
notices.push( notices.push(
Notice.create({ Notice.create({
text: I18n.t("bootstrap_mode_enabled", { text: I18n.t("bootstrap_mode_enabled", {
count: this.siteSettings.bootstrap_mode_min_users, count: bootstrapModeMinUsers,
}), }),
id: "alert-bootstrap-mode", id: "alert-bootstrap-mode",
}) })
@ -140,26 +157,24 @@ export default Component.extend({
} }
} }
if ( if (globalNotice?.length > 0) {
this.siteSettings.global_notice &&
this.siteSettings.global_notice.length
) {
notices.push( notices.push(
Notice.create({ Notice.create({
text: this.siteSettings.global_notice, text: globalNotice,
id: "alert-global-notice", id: "alert-global-notice",
}) })
); );
} }
if (this.logNotice) { if (logNotice) {
notices.push(this.logNotice); notices.push(logNotice);
} }
return notices.concat(_pluginNotices).filter((notice) => { return notices.concat(_pluginNotices).filter((notice) => {
if (notice.options.visibility) { if (notice.options.visibility) {
return notice.options.visibility(notice); return notice.options.visibility(notice);
} else { }
const key = `${GLOBAL_NOTICE_DISMISSED_PROMPT_KEY}-${notice.id}`; const key = `${GLOBAL_NOTICE_DISMISSED_PROMPT_KEY}-${notice.id}`;
const value = this.keyValueStore.get(key); const value = this.keyValueStore.get(key);
@ -179,12 +194,10 @@ export default Component.extend({
} else { } else {
return false; return false;
} }
}
}); });
} },
),
actions: { @action
dismissNotice(notice) { dismissNotice(notice) {
if (notice.options.onDismiss) { if (notice.options.onDismiss) {
notice.options.onDismiss(notice); notice.options.onDismiss(notice);
@ -202,32 +215,23 @@ export default Component.extend({
alert.style.display = "none"; alert.style.display = "none";
} }
}, },
},
_setupObservers() {
LogsNotice.current().addObserver("hidden", this._handleLogsNoticeUpdate);
LogsNotice.current().addObserver("text", this._handleLogsNoticeUpdate);
},
_tearDownObservers() {
LogsNotice.current().removeObserver("text", this._handleLogsNoticeUpdate);
LogsNotice.current().removeObserver("hidden", this._handleLogsNoticeUpdate);
},
@bind @bind
_handleLogsNoticeUpdate() { _handleLogsNoticeUpdate() {
const logNotice = Notice.create({ const logNotice = Notice.create({
text: htmlSafe(LogsNotice.currentProp("message")), text: htmlSafe(this.logsNoticeService.message),
id: "alert-logs-notice", id: "alert-logs-notice",
options: { options: {
dismissable: true, dismissable: true,
persistentDismiss: false, persistentDismiss: false,
visibility() { visibility() {
return !LogsNotice.currentProp("hidden"); return !this.logsNoticeService.hidden;
}, },
onDismiss() { onDismiss() {
LogsNotice.currentProp("hidden", true); this.logsNoticeService.setProperties({
LogsNotice.currentProp("text", ""); hidden: true,
text: "",
});
}, },
}, },
}); });

View File

@ -98,7 +98,6 @@ export default {
app.inject(t, "store", "service:store"); app.inject(t, "store", "service:store");
app.inject(t, "site", "site:main"); app.inject(t, "site", "site:main");
app.inject(t, "searchService", "service:search"); app.inject(t, "searchService", "service:search");
app.inject(t, "keyValueStore", "key-value-store:main");
}); });
ALL_TARGETS.concat("service").forEach((t) => { ALL_TARGETS.concat("service").forEach((t) => {
@ -106,6 +105,7 @@ export default {
app.inject(t, "messageBus", "message-bus:main"); app.inject(t, "messageBus", "message-bus:main");
app.inject(t, "siteSettings", "site-settings:main"); app.inject(t, "siteSettings", "site-settings:main");
app.inject(t, "topicTrackingState", "topic-tracking-state:main"); app.inject(t, "topicTrackingState", "topic-tracking-state:main");
app.inject(t, "keyValueStore", "key-value-store:main");
}); });
if (currentUser) { if (currentUser) {

View File

@ -1,22 +1,26 @@
import discourseComputed, { import discourseComputed, { observes } from "discourse-common/utils/decorators";
observes, import Service from "@ember/service";
on,
} from "discourse-common/utils/decorators";
import EmberObject from "@ember/object";
import I18n from "I18n"; import I18n from "I18n";
import { autoUpdatingRelativeAge } from "discourse/lib/formatter"; import { autoUpdatingRelativeAge } from "discourse/lib/formatter";
import getURL from "discourse-common/lib/get-url"; import getURL from "discourse-common/lib/get-url";
import { htmlSafe } from "@ember/template"; import { htmlSafe } from "@ember/template";
import { isEmpty } from "@ember/utils"; import { isEmpty } from "@ember/utils";
import { readOnly } from "@ember/object/computed";
const LOGS_NOTICE_KEY = "logs-notice-text"; const LOGS_NOTICE_KEY = "logs-notice-text";
const LogsNotice = EmberObject.extend({ export default Service.extend({
text: "", text: "",
@on("init") isAdmin: readOnly("currentUser.admin"),
_setup() {
if (!this.isActivated) { init() {
this._super(...arguments);
if (
this.siteSettings.alert_admins_if_errors_per_hour === 0 &&
this.siteSettings.alert_admins_if_errors_per_minute === 0
) {
return; return;
} }
@ -63,11 +67,6 @@ const LogsNotice = EmberObject.extend({
return htmlSafe(text); return htmlSafe(text);
}, },
@discourseComputed("currentUser")
isAdmin(currentUser) {
return currentUser && currentUser.admin;
},
@discourseComputed("isEmpty", "isAdmin") @discourseComputed("isEmpty", "isAdmin")
hidden(thisIsEmpty, isAdmin) { hidden(thisIsEmpty, isAdmin) {
return !isAdmin || thisIsEmpty; return !isAdmin || thisIsEmpty;
@ -77,14 +76,4 @@ const LogsNotice = EmberObject.extend({
_updateKeyValueStore() { _updateKeyValueStore() {
this.keyValueStore.setItem(LOGS_NOTICE_KEY, this.text); this.keyValueStore.setItem(LOGS_NOTICE_KEY, this.text);
}, },
@discourseComputed(
"siteSettings.alert_admins_if_errors_per_hour",
"siteSettings.alert_admins_if_errors_per_minute"
)
isActivated(errorsPerHour, errorsPerMinute) {
return errorsPerHour > 0 || errorsPerMinute > 0;
},
}); });
export default LogsNotice;