Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

prototypical changes to the notification center and its behaviour when new notifications are polled

Jonas Jabaria5a17baf3233
diff --git a/frontend/src/app/core/state/in-app-notifications/in-app-notifications.actions.ts b/frontend/src/app/core/state/in-app-notifications/in-app-notifications.actions.ts
index 16498830cbe..017600bc266 100644
--- a/frontend/src/app/core/state/in-app-notifications/in-app-notifications.actions.ts
+++ b/frontend/src/app/core/state/in-app-notifications/in-app-notifications.actions.ts
@@ -54,6 +54,11 @@ export const notificationCountIncreased = action(
props<{ origin:string, count:number }>(),
);
+export const notificationCountChanged = action(
+ '[IAN] The backend sent a notification count that was different to the last known',
+ props<{ origin:string, count:number }>(),
+);
+
export const centerUpdatedInPlace = action(
'[IAN] The notification center updated the notification list without a full page refresh',
props<{ origin:string }>(),
diff --git a/frontend/src/app/features/in-app-notifications/bell/state/ian-bell.query.ts b/frontend/src/app/features/in-app-notifications/bell/state/ian-bell.query.ts
index 6748f4f7be9..b763d40caa5 100644
--- a/frontend/src/app/features/in-app-notifications/bell/state/ian-bell.query.ts
+++ b/frontend/src/app/features/in-app-notifications/bell/state/ian-bell.query.ts
@@ -8,6 +8,12 @@ import {
export class IanBellQuery extends Query<IanBellState> {
unread$ = this.select('totalUnread');
+ unreadCountChanged$ = this.unread$.pipe(
+ pairwise(),
+ filter(([last, curr]) => curr !== last),
+ map(([, curr]) => curr),
+ );
+
unreadCountIncreased$ = this.unread$.pipe(
pairwise(),
filter(([last, curr]) => curr > last),
diff --git a/frontend/src/app/features/in-app-notifications/bell/state/ian-bell.service.ts b/frontend/src/app/features/in-app-notifications/bell/state/ian-bell.service.ts
index 671ed2e9599..9a511306d10 100644
--- a/frontend/src/app/features/in-app-notifications/bell/state/ian-bell.service.ts
+++ b/frontend/src/app/features/in-app-notifications/bell/state/ian-bell.service.ts
@@ -43,6 +43,7 @@ import { IanBellQuery } from 'core-app/features/in-app-notifications/bell/state/
import { EffectCallback, EffectHandler } from 'core-app/core/state/effects/effect-handler.decorator';
import {
notificationCountIncreased,
+ notificationCountChanged,
notificationsMarkedRead,
} from 'core-app/core/state/in-app-notifications/in-app-notifications.actions';
import { ActionsService } from 'core-app/core/state/actions/actions.service';
@@ -68,6 +69,9 @@ export class IanBellService {
readonly actions$:ActionsService,
readonly resourceService:InAppNotificationsResourceService,
) {
+ this.query.unreadCountChanged$.subscribe((count) => {
+ this.actions$.dispatch(notificationCountChanged({ origin: this.id, count }));
+ });
this.query.unreadCountIncreased$.pipe(skip(1)).subscribe((count) => {
this.actions$.dispatch(notificationCountIncreased({ origin: this.id, count }));
});
diff --git a/frontend/src/app/features/in-app-notifications/center/in-app-notification-center.component.ts b/frontend/src/app/features/in-app-notifications/center/in-app-notification-center.component.ts
index 641983075d1..8507250baf2 100644
--- a/frontend/src/app/features/in-app-notifications/center/in-app-notification-center.component.ts
+++ b/frontend/src/app/features/in-app-notifications/center/in-app-notification-center.component.ts
@@ -26,7 +26,7 @@
// See COPYRIGHT and LICENSE files for more details.
//++
-import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, OnInit } from '@angular/core';
+import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, OnInit, OnDestroy } from '@angular/core';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { filter, map } from 'rxjs/operators';
import { StateService } from '@uirouter/angular';
@@ -50,7 +50,7 @@ import {
styleUrls: ['./in-app-notification-center.component.sass'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
-export class InAppNotificationCenterComponent implements OnInit {
+export class InAppNotificationCenterComponent implements OnInit, OnDestroy {
maxSize = NOTIFICATIONS_MAX_SIZE;
hasMoreThanPageSize$ = this.storeService.hasMoreThanPageSize$;
@@ -76,6 +76,8 @@ export class InAppNotificationCenterComponent implements OnInit {
selectedWorkPackage$ = this.storeService.selectedWorkPackage$;
+ handleVisibilityChangeBound:EventListener;
+
reasonMenuItems = [
{
key: 'mentioned',
@@ -158,6 +160,29 @@ export class InAppNotificationCenterComponent implements OnInit {
filter: this.urlParams.get('filter'),
name: this.urlParams.get('name'),
});
+ this.checkNotificationPermissions();
+ this.setupEventListeners();
+ }
+
+ ngOnDestroy():void {
+ this.removeEventListeners();
+ }
+
+ setupEventListeners():void {
+ this.handleVisibilityChangeBound = () => { void this.handleVisibilityChange(); };
+ document.addEventListener('visibilitychange', this.handleVisibilityChangeBound);
+ }
+
+ removeEventListeners():void {
+ document.removeEventListener('visibilitychange', this.handleVisibilityChangeBound);
+ }
+
+ handleVisibilityChange() {
+ if (document.hidden) {
+ this.storeService.setCenterHidden(true);
+ } else {
+ this.storeService.setCenterHidden(false);
+ }
}
noNotificationText(hasNotifications:boolean):string {
@@ -171,4 +196,17 @@ export class InAppNotificationCenterComponent implements OnInit {
return this.text.no_notification_for_filter;
}
+
+ checkNotificationPermissions():void {
+ // Check if the browser supports notifications
+ if (!('Notification' in window)) {
+ return;
+ }
+ // if so, ask for permission if not already denied
+ if (Notification.permission !== 'denied') {
+ Notification.requestPermission().catch((error) => {
+ console.error(error);
+ });
+ }
+ }
}
diff --git a/frontend/src/app/features/in-app-notifications/center/state/ian-center.service.ts b/frontend/src/app/features/in-app-notifications/center/state/ian-center.service.ts
index 4c215a07cbc..054ac6a32c3 100644
--- a/frontend/src/app/features/in-app-notifications/center/state/ian-center.service.ts
+++ b/frontend/src/app/features/in-app-notifications/center/state/ian-center.service.ts
@@ -39,6 +39,7 @@ import {
markNotificationsAsRead,
notificationCountIncreased,
notificationsMarkedRead,
+ notificationCountChanged,
} from 'core-app/core/state/in-app-notifications/in-app-notifications.actions';
import { INotification } from 'core-app/core/state/in-app-notifications/in-app-notification.model';
import { EffectCallback, EffectHandler } from 'core-app/core/state/effects/effect-handler.decorator';
@@ -223,13 +224,25 @@ export class IanCenterService extends UntilDestroyedMixin {
this.reload.next(true);
}
+ setCenterHidden(hidden:boolean):void {
+ this.store.update({ centerHidden: hidden });
+
+ if (!hidden) {
+ this.removeNotificationIndicatorInIcon();
+ }
+ }
+
markAsRead(notifications:ID[]):void {
+ this.hideNewNotifcationToast();
+
this.actions$.dispatch(
markNotificationsAsRead({ origin: this.id, notifications }),
);
}
openSplitScreen(workPackageId:string, tabIdentifier:string = 'activity'):void {
+ this.hideNewNotifcationToast();
+
const link = this.pathHelper.notificationsDetailsPath(workPackageId, tabIdentifier) + window.location.search;
Turbo.visit(link, { frame: 'content-bodyRight', action: 'advance' });
}
@@ -244,6 +257,8 @@ export class IanCenterService extends UntilDestroyedMixin {
}
showNextNotification():void {
+ this.hideNewNotifcationToast();
+
void this
.notifications$
.pipe(take(1))
@@ -259,44 +274,220 @@ export class IanCenterService extends UntilDestroyedMixin {
});
}
+ // /**
+ // * Check for updates after bell count increased
+ // */
+ // @EffectCallback(notificationCountIncreased)
+ // private checkForNewNotifications() {
+ // this.onReload.pipe(take(1)).subscribe((collection) => {
+ // const { activeCollection } = this.query.getValue();
+ // const hasNewNotifications = !collection.ids.reduce(
+ // (allInOldCollection, id) => allInOldCollection && activeCollection.ids.includes(id),
+ // true,
+ // );
+
+ // if (!hasNewNotifications) {
+ // return;
+ // }
+
+ // if (this.activeReloadToast) {
+ // this.toastService.remove(this.activeReloadToast);
+ // this.activeReloadToast = null;
+ // }
+
+ // this.activeReloadToast = this.toastService.add({
+ // type: 'info',
+ // icon: 'bell',
+ // message: this.I18n.t('js.notifications.center.new_notifications.message'),
+ // link: {
+ // text: this.I18n.t('js.notifications.center.new_notifications.link_text'),
+ // target: () => {
+ // this.store.update({ activeCollection: collection });
+ // this.actions$.dispatch(centerUpdatedInPlace({ origin: this.id }));
+ // this.activeReloadToast = null;
+ // },
+ // },
+ // });
+ // });
+ // this.reload.next(false);
+ // }
+
/**
- * Check for updates after bell count increased
+ * Handle updates after bell count changed
*/
- @EffectCallback(notificationCountIncreased)
- private checkForNewNotifications() {
- this.onReload.pipe(take(1)).subscribe((collection) => {
+ @EffectCallback(notificationCountChanged)
+ private handleChangedNotificationCount() {
+ // update the UI state for increased AND decreased notifications
+ // decreasing the notification count could happen when the user
+ // itself marks notifications as read in the split view or on another tab
+ this.onReload.pipe(
+ take(1),
+ switchMap((collection) => {
+ this.store.update({ activeCollection: collection });
+ this.actions$.dispatch(centerUpdatedInPlace({ origin: this.id }));
+ return this.selectNotifications$.pipe(
+ map((notifications) => ({ notifications, collection })),
+ );
+ }),
+ switchMap(({ notifications, collection }) => {
+ return this.selectedWorkPackage$.pipe(
+ take(1),
+ map((selectedWpId) => ({ notifications, collection, selectedWpId })),
+ );
+ }),
+ ).subscribe(({ notifications, collection, selectedWpId }) => {
const { activeCollection } = this.query.getValue();
- const hasNewNotifications = !collection.ids.reduce(
- (allInOldCollection, id) => allInOldCollection && activeCollection.ids.includes(id),
- true,
+ const newNotificationIds = activeCollection.ids.filter(
+ (id) => !collection.ids.includes(id),
);
+ const hasNewNotifications = newNotificationIds.length > 0;
if (!hasNewNotifications) {
return;
}
+ const newNotifications = notifications.filter((n) => newNotificationIds.includes(n.id));
+ const newNotificationsForOtherWPs = newNotifications.filter((notification) => {
+ const wpId = this.getWorkPackageIdFromNotification(notification);
+ return wpId && wpId !== selectedWpId;
+ });
- if (this.activeReloadToast) {
- this.toastService.remove(this.activeReloadToast);
- this.activeReloadToast = null;
- }
+ const hasNewNotificationsForOtherThanSelectedWp = newNotificationsForOtherWPs.length > 0;
- this.activeReloadToast = this.toastService.add({
- type: 'info',
- icon: 'bell',
- message: this.I18n.t('js.notifications.center.new_notifications.message'),
- link: {
- text: this.I18n.t('js.notifications.center.new_notifications.link_text'),
- target: () => {
- this.store.update({ activeCollection: collection });
- this.actions$.dispatch(centerUpdatedInPlace({ origin: this.id }));
- this.activeReloadToast = null;
- },
- },
- });
+ this.informAboutUnreadNotification(hasNewNotificationsForOtherThanSelectedWp);
});
+
this.reload.next(false);
}
+ private getWorkPackageIdFromNotification(notification:INotification):string | null {
+ if (notification._links.resource && notification._links.resource.href) {
+ return idFromLink(notification._links.resource.href);
+ }
+ return null;
+ }
+
+ private informAboutUnreadNotification(hasNewNotificationsForOtherThanSelectedWp:boolean):void {
+ const state = this.store.getValue();
+
+ if (state.centerHidden) {
+ // notification center is not visible (as far as it can be determined - e.g. another browser tab is selected)
+ // try to notify user even when notification is associated with selected workpackage
+ if (this.browserNotificationsEnabled()) {
+ this.createBrowserNotification();
+ }
+
+ this.showNotificationIndicatorInIcon();
+ this.showNewNotificationToast();
+ } else if (hasNewNotificationsForOtherThanSelectedWp) {
+ // notification center is visible (as far as it can be determined)
+ // then do not trigger the browser notification or such
+ // just render the toast in case the notifications are associated with a workpackage other than the selected
+ this.showNewNotificationToast();
+ }
+ }
+
+ private showNewNotificationToast():void {
+ this.hideNewNotifcationToast();
+ this.activeReloadToast = this.toastService.add({
+ type: 'info',
+ icon: 'bell',
+ message: this.I18n.t('js.notifications.center.new_notifications.message'),
+ });
+ }
+
+ private hideNewNotifcationToast():void {
+ if (this.activeReloadToast) {
+ this.toastService.remove(this.activeReloadToast);
+ this.activeReloadToast = null;
+ }
+ }
+
+ private browserNotificationsEnabled():boolean {
+ let disabled = false;
+ // Check if the browser supports notifications
+ if (!('Notification' in window)) {
+ disabled = true;
+ }
+
+ if (Notification.permission === 'denied') {
+ disabled = true;
+ }
+
+ return !disabled;
+ }
+
+ private createBrowserNotification():void {
+ const notification = new Notification('OpenProject', {
+ body: this.I18n.t('js.notifications.center.new_notifications.message'),
+ tag: 'opNewNotification', // important: without a tag the notifications would sum up which would be highly annoying
+ });
+
+ notification.onclick = () => {
+ window.focus();
+ notification.close();
+ this.removeNotificationIndicatorInIcon();
+ };
+ }
+
+ private showNotificationIndicatorInIcon():void {
+ const iconLink = window.document.querySelector("link[rel*='icon']");
+ if (iconLink) {
+ (iconLink as HTMLLinkElement).href = 'favicon_notification.ico';
+ }
+ }
+
+ private removeNotificationIndicatorInIcon():void {
+ const iconLink = window.document.querySelector("link[rel*='icon']");
+ if (iconLink) {
+ (iconLink as HTMLLinkElement).href = 'favicon.ico';
+ }
+ }
+
+ private playNotificationSound():void {
+ const audioContext = new window.AudioContext();
+
+ const createOscillator = (freq:number, type:OscillatorType = 'sine') => {
+ const osc = audioContext.createOscillator();
+ osc.type = type;
+ osc.frequency.setValueAtTime(freq, audioContext.currentTime);
+ return osc;
+ };
+
+ const createEnvelope = () => {
+ const gain = audioContext.createGain();
+ gain.gain.setValueAtTime(0, audioContext.currentTime);
+ gain.gain.linearRampToValueAtTime(0.2, audioContext.currentTime + 0.005);
+ gain.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.5);
+ return gain;
+ };
+
+ const oscillators = [
+ createOscillator(880), // A5
+ createOscillator(1108.73), // C#6
+ createOscillator(1318.51), // E6
+ createOscillator(1760, 'triangle'), // A6 (overtone)
+ ];
+
+ const masterGain = audioContext.createGain();
+ masterGain.gain.setValueAtTime(0.7, audioContext.currentTime);
+
+ oscillators.forEach((osc) => {
+ const envelope = createEnvelope();
+ osc.connect(envelope);
+ envelope.connect(masterGain);
+ osc.start();
+ osc.stop(audioContext.currentTime + 0.5);
+ });
+
+ masterGain.connect(audioContext.destination);
+
+ // Clean up
+ setTimeout(() => {
+ masterGain.disconnect();
+ void audioContext.close();
+ }, 1000);
+ }
+
/**
* Reload after notifications were successfully marked as read
*/
diff --git a/frontend/src/app/features/in-app-notifications/center/state/ian-center.store.ts b/frontend/src/app/features/in-app-notifications/center/state/ian-center.store.ts
index 00c3c04ef1e..5adc94a6bee 100644
--- a/frontend/src/app/features/in-app-notifications/center/state/ian-center.store.ts
+++ b/frontend/src/app/features/in-app-notifications/center/state/ian-center.store.ts
@@ -20,6 +20,8 @@ export interface IanCenterState {
/** Number of elements not showing after max values loaded */
notLoaded:number;
+
+ centerHidden:boolean;
}
export const IAN_FACET_FILTERS:Record<InAppNotificationFacet, ApiV3ListFilter[]> = {
@@ -37,6 +39,7 @@ export function createInitialState():IanCenterState {
activeCollection: { ids: [] },
activeFacet: 'unread',
notLoaded: 0,
+ centerHidden: false,
};
}
diff --git a/public/favicon_notification.ico b/public/favicon_notification.ico
new file mode 100644
index 00000000000..f6a1bb6f203
Binary files /dev/null and b/public/favicon_notification.ico differ