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; return;
} }
if (update.chatId === currentChatId) { const isActiveChat = update.chatId === currentChatId;
if (isActiveChat) {
setTimeout(() => { setTimeout(() => {
actions.requestChatUpdate({ chatId: update.chatId }); actions.requestChatUpdate({ chatId: update.chatId });
}, CURRENT_CHAT_UNREAD_DELAY); }, CURRENT_CHAT_UNREAD_DELAY);
} else { } else {
showNewMessageNotification({ chat, message });
setGlobal(updateChat(global, update.chatId, { setGlobal(updateChat(global, update.chatId, {
unreadCount: chat.unreadCount ? chat.unreadCount + 1 : 1, unreadCount: chat.unreadCount ? chat.unreadCount + 1 : 1,
...(update.message.hasUnreadMention && { ...(update.message.hasUnreadMention && {
@ -120,6 +121,8 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => {
})); }));
} }
showNewMessageNotification({ chat, message, isActiveChat });
break; 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; if (chat.isMuted) return false;
// Dont show notification for active chat if client has focus
if (isActive && document.hasFocus()) return false;
await getDispatch().loadNotificationsSettings(); await getDispatch().loadNotificationsSettings();
const global = getGlobal(); const global = getGlobal();
switch (chat.type) { switch (chat.type) {
@ -225,11 +229,12 @@ function getNotificationContent(chat: ApiChat, message: ApiMessage) {
export async function showNewMessageNotification({ export async function showNewMessageNotification({
chat, chat,
message, message,
}: { chat: ApiChat; message: Partial<ApiMessage> }) { isActiveChat,
}: { chat: ApiChat; message: Partial<ApiMessage>; isActiveChat: boolean}) {
if (!checkIfNotificationsSupported()) return; if (!checkIfNotificationsSupported()) return;
if (!message.id) return; if (!message.id) return;
const shouldNotify = await checkIfShouldNotify(chat); const shouldNotify = await checkIfShouldNotify(chat, isActiveChat);
if (!shouldNotify) return; if (!shouldNotify) return;
const { const {
@ -260,6 +265,9 @@ export async function showNewMessageNotification({
chatId: chat.id, chatId: chat.id,
messageId: message.id, messageId: message.id,
}); });
if (window.focus) {
window.focus();
}
}; };
} }