From 48b168be8544b1b082e44d7775de3efb6afe7065 Mon Sep 17 00:00:00 2001 From: juliajforesti Date: Thu, 20 Mar 2025 15:19:48 -0300 Subject: [PATCH 1/5] refactor: create `useNewMessageNotification` --- .../client/hooks/useNewMessageNotification.ts | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 apps/meteor/client/hooks/useNewMessageNotification.ts diff --git a/apps/meteor/client/hooks/useNewMessageNotification.ts b/apps/meteor/client/hooks/useNewMessageNotification.ts new file mode 100644 index 0000000000000..0bc812d3391a4 --- /dev/null +++ b/apps/meteor/client/hooks/useNewMessageNotification.ts @@ -0,0 +1,32 @@ +import type { AtLeast, ISubscription } from '@rocket.chat/core-typings'; +import { useUserPreference } from '@rocket.chat/ui-contexts'; +import { useCallback } from 'react'; + +import { useUserSoundPreferences } from './useUserSoundPreferences'; +import { CustomSounds } from '../../app/custom-sounds/client/lib/CustomSounds'; + +export const useNewMessageNotification = () => { + const newMessageNotification = useUserPreference('newMessageNotification'); + const { notificationsSoundVolume } = useUserSoundPreferences(); + + const notifyNewMessage = useCallback( + (sub: AtLeast) => { + if (!sub || sub.audioNotificationValue === 'none') { + return; + } + if (sub.audioNotificationValue && sub.audioNotificationValue !== '0') { + void CustomSounds.play(sub.audioNotificationValue, { + volume: Number((notificationsSoundVolume / 100).toPrecision(2)), + }); + } + + if (newMessageNotification && newMessageNotification !== 'none') { + void CustomSounds.play(newMessageNotification, { + volume: Number((notificationsSoundVolume / 100).toPrecision(2)), + }); + } + }, + [newMessageNotification, notificationsSoundVolume], + ); + return notifyNewMessage; +}; From 63966c38999d537e0eadc1f7c2ddb21c7482bfc5 Mon Sep 17 00:00:00 2001 From: juliajforesti Date: Thu, 20 Mar 2025 16:52:19 -0300 Subject: [PATCH 2/5] chore: reorg files into `hooks/notification` folder --- .../client/hooks/useNewMessageNotification.ts | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 apps/meteor/client/hooks/useNewMessageNotification.ts diff --git a/apps/meteor/client/hooks/useNewMessageNotification.ts b/apps/meteor/client/hooks/useNewMessageNotification.ts deleted file mode 100644 index 0bc812d3391a4..0000000000000 --- a/apps/meteor/client/hooks/useNewMessageNotification.ts +++ /dev/null @@ -1,32 +0,0 @@ -import type { AtLeast, ISubscription } from '@rocket.chat/core-typings'; -import { useUserPreference } from '@rocket.chat/ui-contexts'; -import { useCallback } from 'react'; - -import { useUserSoundPreferences } from './useUserSoundPreferences'; -import { CustomSounds } from '../../app/custom-sounds/client/lib/CustomSounds'; - -export const useNewMessageNotification = () => { - const newMessageNotification = useUserPreference('newMessageNotification'); - const { notificationsSoundVolume } = useUserSoundPreferences(); - - const notifyNewMessage = useCallback( - (sub: AtLeast) => { - if (!sub || sub.audioNotificationValue === 'none') { - return; - } - if (sub.audioNotificationValue && sub.audioNotificationValue !== '0') { - void CustomSounds.play(sub.audioNotificationValue, { - volume: Number((notificationsSoundVolume / 100).toPrecision(2)), - }); - } - - if (newMessageNotification && newMessageNotification !== 'none') { - void CustomSounds.play(newMessageNotification, { - volume: Number((notificationsSoundVolume / 100).toPrecision(2)), - }); - } - }, - [newMessageNotification, notificationsSoundVolume], - ); - return notifyNewMessage; -}; From 0eb2a42721213fbe1951d0e0332bc32b8456a6a1 Mon Sep 17 00:00:00 2001 From: juliajforesti Date: Thu, 20 Mar 2025 17:30:51 -0300 Subject: [PATCH 3/5] refactor: create `useDesktopNotification` --- .../notification/useDesktopNotification.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 apps/meteor/client/hooks/notification/useDesktopNotification.ts diff --git a/apps/meteor/client/hooks/notification/useDesktopNotification.ts b/apps/meteor/client/hooks/notification/useDesktopNotification.ts new file mode 100644 index 0000000000000..cd38506a62fda --- /dev/null +++ b/apps/meteor/client/hooks/notification/useDesktopNotification.ts @@ -0,0 +1,40 @@ +import type { INotificationDesktop } from '@rocket.chat/core-typings'; +import { useUser } from '@rocket.chat/ui-contexts'; +import { useCallback } from 'react'; + +import { e2e } from '../../../app/e2e/client'; +import { KonchatNotification } from '../../../app/ui/client/lib/KonchatNotification'; +import { RoomManager } from '../../lib/RoomManager'; +import { getAvatarAsPng } from '../../lib/utils/getAvatarAsPng'; + +export const useDesktopNotification = () => { + const user = useUser(); + const notifyDesktop = useCallback( + async (notification: INotificationDesktop) => { + if ( + notification.payload.rid === RoomManager.opened && + (typeof window.document.hasFocus === 'function' ? window.document.hasFocus() : undefined) + ) { + return; + } + if (user?.status === 'busy') { + return; + } + + if (notification.payload.message?.t === 'e2e') { + const e2eRoom = await e2e.getInstanceByRoomId(notification.payload.rid); + if (e2eRoom) { + notification.text = (await e2eRoom.decrypt(notification.payload.message.msg)).text; + } + } + + return getAvatarAsPng(notification.payload.sender?.username, (avatarAsPng) => { + notification.icon = avatarAsPng; + return KonchatNotification.notify(notification); + }); + }, + [user?.status], + ); + + return notifyDesktop; +}; From 7b781ca65be2ff92aacb9b36c82566458f21cea0 Mon Sep 17 00:00:00 2001 From: juliajforesti Date: Thu, 20 Mar 2025 17:32:32 -0300 Subject: [PATCH 4/5] refactor: replace `KonchatNotification.showDesktop` with `useDesktopNotification` --- apps/meteor/client/hooks/notification/useNotifyUser.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/meteor/client/hooks/notification/useNotifyUser.ts b/apps/meteor/client/hooks/notification/useNotifyUser.ts index a71dd6c25a0fa..7f2cfb918ab7e 100644 --- a/apps/meteor/client/hooks/notification/useNotifyUser.ts +++ b/apps/meteor/client/hooks/notification/useNotifyUser.ts @@ -4,10 +4,10 @@ import { useRouter, useStream, useUser, useUserPreference } from '@rocket.chat/u import { useEffect } from 'react'; import { useEmbeddedLayout } from '../useEmbeddedLayout'; +import { useDesktopNotification } from './useDesktopNotification'; import { useNewMessageNotification } from './useNewMessageNotification'; import { useNewRoomNotification } from './useNewRoomNotification'; import { CachedChatSubscription } from '../../../app/models/client'; -import { KonchatNotification } from '../../../app/ui/client/lib/KonchatNotification'; import { RoomManager } from '../../lib/RoomManager'; import { fireGlobalEvent } from '../../lib/utils/fireGlobalEvent'; @@ -19,6 +19,7 @@ export const useNotifyUser = () => { const muteFocusedConversations = useUserPreference('muteFocusedConversations'); const newRoomNotification = useNewRoomNotification(); const newMessageNotification = useNewMessageNotification(); + const showDesktopNotification = useDesktopNotification(); const notifyNewRoom = useEffectEvent(async (sub: AtLeast): Promise => { if (!user || user.status === 'busy') { @@ -48,12 +49,12 @@ export const useNotifyUser = () => { if (!hasFocus && messageIsInOpenedRoom) { // Play a notification sound newMessageNotification(notification.payload); - void KonchatNotification.showDesktop(notification); + showDesktopNotification(notification); } } else if (!hasFocus || !messageIsInOpenedRoom || !muteFocusedConversations) { // Play a notification sound newMessageNotification(notification.payload); - void KonchatNotification.showDesktop(notification); + showDesktopNotification(notification); } }); From a352d2f01e0dfa1b22be0ec5911fd2fadf26ae34 Mon Sep 17 00:00:00 2001 From: juliajforesti Date: Thu, 20 Mar 2025 17:33:00 -0300 Subject: [PATCH 5/5] chore: remove deprecated `showDesktop` method --- .../app/ui/client/lib/KonchatNotification.ts | 34 +------------------ 1 file changed, 1 insertion(+), 33 deletions(-) diff --git a/apps/meteor/app/ui/client/lib/KonchatNotification.ts b/apps/meteor/app/ui/client/lib/KonchatNotification.ts index 3b1e1f285f2aa..cebd0aa4324f1 100644 --- a/apps/meteor/app/ui/client/lib/KonchatNotification.ts +++ b/apps/meteor/app/ui/client/lib/KonchatNotification.ts @@ -1,14 +1,11 @@ -import type { INotificationDesktop, IUser } from '@rocket.chat/core-typings'; +import type { INotificationDesktop } from '@rocket.chat/core-typings'; import { Random } from '@rocket.chat/random'; import { Meteor } from 'meteor/meteor'; import { ReactiveVar } from 'meteor/reactive-var'; -import { RoomManager } from '../../../../client/lib/RoomManager'; import { onClientMessageReceived } from '../../../../client/lib/onClientMessageReceived'; -import { getAvatarAsPng } from '../../../../client/lib/utils/getAvatarAsPng'; import { router } from '../../../../client/providers/RouterProvider'; import { stripTags } from '../../../../lib/utils/stringUtils'; -import { e2e } from '../../../e2e/client'; import { getUserPreference } from '../../../utils/client'; import { getUserAvatarURL } from '../../../utils/client/getUserAvatarURL'; import { sdk } from '../../../utils/client/lib/SDKClient'; @@ -137,35 +134,6 @@ class KonchatNotification { } }; } - - public async showDesktop(notification: INotificationDesktop) { - if (!notification.payload.rid) { - return; - } - - if ( - notification.payload?.rid === RoomManager.opened && - (typeof window.document.hasFocus === 'function' ? window.document.hasFocus() : undefined) - ) { - return; - } - - if ((Meteor.user() as IUser | null)?.status === 'busy') { - return; - } - - if (notification.payload?.message?.t === 'e2e') { - const e2eRoom = await e2e.getInstanceByRoomId(notification.payload.rid); - if (e2eRoom) { - notification.text = (await e2eRoom.decrypt(notification.payload.message.msg)).text; - } - } - - return getAvatarAsPng(notification.payload?.sender?.username, (avatarAsPng) => { - notification.icon = avatarAsPng; - return this.notify(notification); - }); - } } const instance = new KonchatNotification();