Skip to content

A platform for community discussion. Free, open, simple.

FIX: Update do-not-disturb icon in real-time on glimmer header (#28200)

David Taylor7977ae61f7f3
diff --git a/app/assets/javascripts/discourse/app/models/user.js b/app/assets/javascripts/discourse/app/models/user.js
index 000a0e5d8bd..b7264695f8a 100644
--- a/app/assets/javascripts/discourse/app/models/user.js
+++ b/app/assets/javascripts/discourse/app/models/user.js
@@ -1215,6 +1215,7 @@ export default class User extends RestModel.extend(Evented) {
updateDoNotDisturbStatus(ends_at) {
this.set("do_not_disturb_until", ends_at);
this.appEvents.trigger("do-not-disturb:changed", this.do_not_disturb_until);
+ getOwner(this).lookup("service:notifications")._checkDoNotDisturb();
}
updateDraftProperties(properties) {
@@ -1228,10 +1229,11 @@ export default class User extends RestModel.extend(Evented) {
}
isInDoNotDisturb() {
- return (
- this.do_not_disturb_until &&
- new Date(this.do_not_disturb_until) >= new Date()
- );
+ if (this !== getOwner(this).lookup("service:current-user")) {
+ throw "isInDoNotDisturb is only supported for currentUser";
+ }
+
+ return getOwner(this).lookup("service:notifications").isInDoNotDisturb;
}
@discourseComputed(
diff --git a/app/assets/javascripts/discourse/app/services/notifications.js b/app/assets/javascripts/discourse/app/services/notifications.js
new file mode 100644
index 00000000000..269c7ebbd34
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/services/notifications.js
@@ -0,0 +1,44 @@
+import { tracked } from "@glimmer/tracking";
+import Service, { service } from "@ember/service";
+import { disableImplicitInjections } from "discourse/lib/implicit-injections";
+
+@disableImplicitInjections
+export default class NotificationsService extends Service {
+ @service currentUser;
+
+ @tracked isInDoNotDisturb;
+
+ #dndTimer;
+
+ constructor() {
+ super(...arguments);
+
+ this._checkDoNotDisturb();
+ }
+
+ _checkDoNotDisturb() {
+ clearTimeout(this.#dndTimer);
+
+ if (this.currentUser?.do_not_disturb_until) {
+ const remainingMs = this.currentUser.do_not_disturb_until - Date.now();
+
+ if (remainingMs <= 0) {
+ this.isInDoNotDisturb = false;
+ return;
+ }
+
+ this.isInDoNotDisturb = true;
+
+ this.#dndTimer = setTimeout(
+ () => this._checkDoNotDisturb(),
+ Math.min(remainingMs, 60000)
+ );
+ } else {
+ this.isInDoNotDisturb = false;
+ }
+ }
+
+ willDestroy() {
+ clearTimeout(this.#dndTimer);
+ }
+}