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

View File

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

View File

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