Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Dec 17, 2024
1 parent 7edc3db commit 6f78421
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 79 deletions.
73 changes: 55 additions & 18 deletions apps/meteor/app/ui/client/lib/KonchatNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -207,30 +207,67 @@ 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<string>(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)),
});
});
}
}

const instance = new KonchatNotification();

export { instance as KonchatNotification };

// 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<string>(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);
// }
// });
// });
2 changes: 1 addition & 1 deletion apps/meteor/client/hooks/useNotifyUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T>(queue: T[]) => {
const userSubscriptions = useUserSubscriptions(query);

const playNewRoomSoundContinuously = useSetting('Livechat_continuous_sound_notification_new_livechat_room');
Expand All @@ -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) {
Expand All @@ -39,7 +41,7 @@ export const useContinuousSoundNotification = () => {
return;
}

if (userSubscriptions.length === 0) {
if (!hasUnreadRoom) {
CustomSounds.pause(continuousCustomSoundId);
return;
}
Expand All @@ -48,5 +50,5 @@ export const useContinuousSoundNotification = () => {
volume: notificationsSoundVolume,
loop: true,
});
}, [continuousCustomSoundId, playNewRoomSoundContinuously, userSubscriptions, notificationsSoundVolume]);
}, [continuousCustomSoundId, playNewRoomSoundContinuously, userSubscriptions, notificationsSoundVolume, hasUnreadRoom]);
};
3 changes: 0 additions & 3 deletions apps/meteor/client/lib/chats/flows/sendMessage.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,8 +11,6 @@ import { processSlashCommand } from './processSlashCommand';
import { processTooLongMessage } from './processTooLongMessage';

const process = async (chat: ChatAPI, message: IMessage, previewUrls?: string[], isSlashCommandAllowed?: boolean): Promise<void> => {
KonchatNotification.removeRoomNotification(message.rid);

if (await processSetReaction(chat, message)) {
return;
}
Expand Down
3 changes: 0 additions & 3 deletions apps/meteor/client/providers/CustomSoundProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,8 +17,6 @@ const CustomSoundProvider = ({ children }: CustomSoundProviderProps) => {
void CustomSounds.fetchCustomSoundList();
}, [userId]);

useContinuousSoundNotification();

const streamAll = useStream('notify-all');

useEffect(() => {
Expand Down
16 changes: 9 additions & 7 deletions apps/meteor/client/providers/OmnichannelProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { OmnichannelContext } from '../contexts/OmnichannelContext';
import { useHasLicenseModule } from '../hooks/useHasLicenseModule';
import { useReactiveValue } from '../hooks/useReactiveValue';
import { useShouldPreventAction } from '../hooks/useShouldPreventAction';
import { useOmnichannelContinuousSoundNotification } from '../hooks/useOmnichannelContinuousSoundNotification';

Check failure on line 24 in apps/meteor/client/providers/OmnichannelProvider.tsx

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

`../hooks/useOmnichannelContinuousSoundNotification` import should occur before import of `../hooks/useReactiveValue`

const emptyContextValue: OmnichannelContextValue = {
inquiries: { enabled: false },
Expand Down Expand Up @@ -150,13 +151,14 @@ 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);
});
useOmnichannelContinuousSoundNotification(queue);
// queue?.map(({ rid }) => {
// if (queueNotification.has(rid)) {
// return;
// }
// setQueueNotification((prev) => new Set([...prev, rid]));
// return KonchatNotification.newRoom();
// });

const contextValue = useMemo<OmnichannelContextValue>(() => {
if (!enabled) {
Expand Down
1 change: 0 additions & 1 deletion apps/meteor/client/startup/notifications/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
import './konchatNotifications';
import './notification';
43 changes: 0 additions & 43 deletions apps/meteor/client/startup/notifications/notification.ts

This file was deleted.

0 comments on commit 6f78421

Please sign in to comment.