diff --git a/apps/meteor/app/ui/client/lib/KonchatNotification.ts b/apps/meteor/app/ui/client/lib/KonchatNotification.ts index ff67250c000b..2b463886fdac 100644 --- a/apps/meteor/app/ui/client/lib/KonchatNotification.ts +++ b/apps/meteor/app/ui/client/lib/KonchatNotification.ts @@ -10,7 +10,7 @@ import { router } from '../../../../client/providers/RouterProvider'; import { stripTags } from '../../../../lib/utils/stringUtils'; import { CustomSounds } from '../../../custom-sounds/client/lib/CustomSounds'; import { e2e } from '../../../e2e/client'; -import { Subscriptions } from '../../../models/client'; +import { Subscriptions, Users } from '../../../models/client'; import { getUserPreference } from '../../../utils/client'; import { getUserAvatarURL } from '../../../utils/client/getUserAvatarURL'; import { getUserNotificationsSoundVolume } from '../../../utils/client/getUserNotificationsSoundVolume'; @@ -207,27 +207,29 @@ class KonchatNotification { } } - public newRoom(rid: IRoom['_id']) { + public newRoom() { Tracker.nonreactive(() => { - let newRoomSound = Session.get('newRoomSound') as IRoom['_id'][] | undefined; - if (newRoomSound) { - newRoomSound = [...newRoomSound, rid]; - } else { - newRoomSound = [rid]; + const uid = Meteor.userId(); + if (!uid) { + return; } + const user = Users.findOne(uid, { + fields: { + 'settings.preferences.newRoomNotification': 1, + 'settings.preferences.notificationsSoundVolume': 1, + }, + }); + const newRoomNotification = getUserPreference(user, 'newRoomNotification'); + const audioVolume = getUserNotificationsSoundVolume(user?._id); - return Session.set('newRoomSound', newRoomSound); - }); - } - - public removeRoomNotification(rid: IRoom['_id']) { - let newRoomSound = (Session.get('newRoomSound') as IRoom['_id'][] | undefined) ?? []; - newRoomSound = newRoomSound.filter((_rid) => _rid !== rid); - Tracker.nonreactive(() => Session.set('newRoomSound', newRoomSound)); - - const link = document.querySelector(`.link-room-${rid}`); + if (!newRoomNotification) { + return; + } - link?.classList.remove('new-room-highlight'); + void CustomSounds.play(newRoomNotification, { + volume: Number((audioVolume / 100).toPrecision(2)), + }); + }); } } diff --git a/apps/meteor/client/hooks/useNotifyUser.ts b/apps/meteor/client/hooks/useNotifyUser.ts index baa804908e8a..440c979fed11 100644 --- a/apps/meteor/client/hooks/useNotifyUser.ts +++ b/apps/meteor/client/hooks/useNotifyUser.ts @@ -22,7 +22,7 @@ export const useNotifyUser = () => { } if ((!router.getRouteParameters().name || router.getRouteParameters().name !== sub.name) && !sub.ls && sub.alert === true) { - KonchatNotification.newRoom(sub.rid); + KonchatNotification.newRoom(); } }); diff --git a/apps/meteor/client/hooks/useContinuousSoundNotification.ts b/apps/meteor/client/hooks/useOmnichannelContinuousSoundNotification.ts similarity index 87% rename from apps/meteor/client/hooks/useContinuousSoundNotification.ts rename to apps/meteor/client/hooks/useOmnichannelContinuousSoundNotification.ts index 80abb492363b..bcfdf40ae0b8 100644 --- a/apps/meteor/client/hooks/useContinuousSoundNotification.ts +++ b/apps/meteor/client/hooks/useOmnichannelContinuousSoundNotification.ts @@ -6,7 +6,7 @@ import { useUserSoundPreferences } from './useUserSoundPreferences'; import { CustomSounds } from '../../app/custom-sounds/client/lib/CustomSounds'; const query = { t: 'l', ls: { $exists: false }, open: true }; -export const useContinuousSoundNotification = () => { +export const useOmnichannelContinuousSoundNotification = (queue: T[]) => { const userSubscriptions = useUserSubscriptions(query); const playNewRoomSoundContinuously = useSetting('Livechat_continuous_sound_notification_new_livechat_room'); @@ -16,6 +16,8 @@ export const useContinuousSoundNotification = () => { const continuousCustomSoundId = newRoomNotification && `${newRoomNotification}-continuous`; + const hasUnreadRoom = userSubscriptions.length > 0 || queue.length > 0; + useEffect(() => { let audio: ICustomSound; if (playNewRoomSoundContinuously && continuousCustomSoundId) { @@ -39,7 +41,7 @@ export const useContinuousSoundNotification = () => { return; } - if (userSubscriptions.length === 0) { + if (!hasUnreadRoom) { CustomSounds.pause(continuousCustomSoundId); return; } @@ -48,5 +50,5 @@ export const useContinuousSoundNotification = () => { volume: notificationsSoundVolume, loop: true, }); - }, [continuousCustomSoundId, playNewRoomSoundContinuously, userSubscriptions, notificationsSoundVolume]); + }, [continuousCustomSoundId, playNewRoomSoundContinuously, userSubscriptions, notificationsSoundVolume, hasUnreadRoom]); }; diff --git a/apps/meteor/client/lib/chats/flows/sendMessage.ts b/apps/meteor/client/lib/chats/flows/sendMessage.ts index e025730682d5..3d372900fb29 100644 --- a/apps/meteor/client/lib/chats/flows/sendMessage.ts +++ b/apps/meteor/client/lib/chats/flows/sendMessage.ts @@ -1,6 +1,5 @@ import type { IMessage } from '@rocket.chat/core-typings'; -import { KonchatNotification } from '../../../../app/ui/client/lib/KonchatNotification'; import { sdk } from '../../../../app/utils/client/lib/SDKClient'; import { t } from '../../../../app/utils/lib/i18n'; import { onClientBeforeSendMessage } from '../../onClientBeforeSendMessage'; @@ -12,8 +11,6 @@ import { processSlashCommand } from './processSlashCommand'; import { processTooLongMessage } from './processTooLongMessage'; const process = async (chat: ChatAPI, message: IMessage, previewUrls?: string[], isSlashCommandAllowed?: boolean): Promise => { - KonchatNotification.removeRoomNotification(message.rid); - if (await processSetReaction(chat, message)) { return; } diff --git a/apps/meteor/client/providers/CustomSoundProvider.tsx b/apps/meteor/client/providers/CustomSoundProvider.tsx index 5404d37cf918..49100f8e4754 100644 --- a/apps/meteor/client/providers/CustomSoundProvider.tsx +++ b/apps/meteor/client/providers/CustomSoundProvider.tsx @@ -3,7 +3,6 @@ import type { ReactNode } from 'react'; import React, { useEffect } from 'react'; import { CustomSounds } from '../../app/custom-sounds/client/lib/CustomSounds'; -import { useContinuousSoundNotification } from '../hooks/useContinuousSoundNotification'; type CustomSoundProviderProps = { children?: ReactNode; @@ -18,8 +17,6 @@ const CustomSoundProvider = ({ children }: CustomSoundProviderProps) => { void CustomSounds.fetchCustomSoundList(); }, [userId]); - useContinuousSoundNotification(); - const streamAll = useStream('notify-all'); useEffect(() => { diff --git a/apps/meteor/client/providers/OmnichannelProvider.tsx b/apps/meteor/client/providers/OmnichannelProvider.tsx index 9ed860c5abff..aeb83530e059 100644 --- a/apps/meteor/client/providers/OmnichannelProvider.tsx +++ b/apps/meteor/client/providers/OmnichannelProvider.tsx @@ -19,6 +19,7 @@ import { ClientLogger } from '../../lib/ClientLogger'; import type { OmnichannelContextValue } from '../contexts/OmnichannelContext'; import { OmnichannelContext } from '../contexts/OmnichannelContext'; import { useHasLicenseModule } from '../hooks/useHasLicenseModule'; +import { useOmnichannelContinuousSoundNotification } from '../hooks/useOmnichannelContinuousSoundNotification'; import { useReactiveValue } from '../hooks/useReactiveValue'; import { useShouldPreventAction } from '../hooks/useShouldPreventAction'; @@ -62,7 +63,6 @@ const OmnichannelProvider = ({ children }: OmnichannelProviderProps) => { const getRoutingConfig = useMethod('livechat:getRoutingConfig'); const [routeConfig, setRouteConfig] = useSafely(useState(undefined)); - const [queueNotification, setQueueNotification] = useState(new Set()); const accessible = hasAccess && omniChannelEnabled; const iceServersSetting: any = useSetting('WebRTC_Servers'); @@ -150,13 +150,23 @@ const OmnichannelProvider = ({ children }: OmnichannelProviderProps) => { }, [manuallySelected, omnichannelPoolMaxIncoming, omnichannelSortingMechanism]), ); - queue?.map(({ rid }) => { - if (queueNotification.has(rid)) { - return; - } - setQueueNotification((prev) => new Set([...prev, rid])); - return KonchatNotification.newRoom(rid); - }); + useEffect(() => { + const observer = LivechatInquiry.find( + { status: LivechatInquiryStatus.QUEUED }, + { + sort: getOmniChatSortQuery(omnichannelSortingMechanism), + limit: omnichannelPoolMaxIncoming, + }, + ).observe({ + added: (_inquiry) => { + KonchatNotification.newRoom(); + }, + }); + + return () => observer.stop(); + }, [omnichannelPoolMaxIncoming, omnichannelSortingMechanism]); + + useOmnichannelContinuousSoundNotification(queue ?? []); const contextValue = useMemo(() => { if (!enabled) { diff --git a/apps/meteor/client/startup/notifications/index.ts b/apps/meteor/client/startup/notifications/index.ts index 866dae4d52f3..a3d13d46c5fb 100644 --- a/apps/meteor/client/startup/notifications/index.ts +++ b/apps/meteor/client/startup/notifications/index.ts @@ -1,2 +1 @@ import './konchatNotifications'; -import './notification'; diff --git a/apps/meteor/client/startup/notifications/notification.ts b/apps/meteor/client/startup/notifications/notification.ts deleted file mode 100644 index 31ba20da0060..000000000000 --- a/apps/meteor/client/startup/notifications/notification.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { Meteor } from 'meteor/meteor'; -import { Session } from 'meteor/session'; -import { Tracker } from 'meteor/tracker'; - -import { CustomSounds } from '../../../app/custom-sounds/client/lib/CustomSounds'; -import { Users } from '../../../app/models/client'; -import { getUserPreference } from '../../../app/utils/client'; -import { getUserNotificationsSoundVolume } from '../../../app/utils/client/getUserNotificationsSoundVolume'; - -Meteor.startup(() => { - Tracker.autorun(() => { - const uid = Meteor.userId(); - - if (!uid) { - return; - } - - const user = Users.findOne(uid, { - fields: { - 'settings.preferences.newRoomNotification': 1, - 'settings.preferences.notificationsSoundVolume': 1, - }, - }); - const newRoomNotification = getUserPreference(user, 'newRoomNotification'); - const audioVolume = getUserNotificationsSoundVolume(user?._id); - - if (!newRoomNotification) { - return; - } - - if ((Session.get('newRoomSound') || []).length > 0) { - setTimeout(() => { - if (newRoomNotification !== 'none') { - CustomSounds.play(newRoomNotification, { - volume: Number((audioVolume / 100).toPrecision(2)), - }); - } - }, 0); - } else { - CustomSounds.pause(newRoomNotification); - } - }); -});