Notification: Fix focusing window on click (#1067)

This commit is contained in:
Alexander Zinchuk 2021-05-09 13:30:44 +03:00
parent 278b94ca49
commit 2c1197592a
2 changed files with 16 additions and 5 deletions

View File

@ -106,12 +106,13 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => {
return;
}
if (update.chatId === currentChatId) {
const isActiveChat = update.chatId === currentChatId;
if (isActiveChat) {
setTimeout(() => {
actions.requestChatUpdate({ chatId: update.chatId });
}, CURRENT_CHAT_UNREAD_DELAY);
} else {
showNewMessageNotification({ chat, message });
setGlobal(updateChat(global, update.chatId, {
unreadCount: chat.unreadCount ? chat.unreadCount + 1 : 1,
...(update.message.hasUnreadMention && {
@ -120,6 +121,8 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => {
}));
}
showNewMessageNotification({ chat, message, isActiveChat });
break;
}

View File

@ -161,8 +161,12 @@ export async function subscribe() {
}
}
async function checkIfShouldNotify(chat: ApiChat) {
async function checkIfShouldNotify(chat: ApiChat, isActive: boolean) {
if (chat.isMuted) return false;
// Dont show notification for active chat if client has focus
if (isActive && document.hasFocus()) return false;
await getDispatch().loadNotificationsSettings();
const global = getGlobal();
switch (chat.type) {
@ -225,11 +229,12 @@ function getNotificationContent(chat: ApiChat, message: ApiMessage) {
export async function showNewMessageNotification({
chat,
message,
}: { chat: ApiChat; message: Partial<ApiMessage> }) {
isActiveChat,
}: { chat: ApiChat; message: Partial<ApiMessage>; isActiveChat: boolean}) {
if (!checkIfNotificationsSupported()) return;
if (!message.id) return;
const shouldNotify = await checkIfShouldNotify(chat);
const shouldNotify = await checkIfShouldNotify(chat, isActiveChat);
if (!shouldNotify) return;
const {
@ -260,6 +265,9 @@ export async function showNewMessageNotification({
chatId: chat.id,
messageId: message.id,
});
if (window.focus) {
window.focus();
}
};
}