Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 1 addition & 33 deletions apps/meteor/app/ui/client/lib/KonchatNotification.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
Expand Down
40 changes: 40 additions & 0 deletions apps/meteor/client/hooks/notification/useDesktopNotification.ts
Original file line number Diff line number Diff line change
@@ -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;
};
7 changes: 4 additions & 3 deletions apps/meteor/client/hooks/notification/useNotifyUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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<ISubscription, 'rid'>): Promise<void> => {
if (!user || user.status === 'busy') {
Expand Down Expand Up @@ -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);
}
});

Expand Down
Loading