From d2ca29e961ebd9be96e555f67544e8f105f838cb Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 23 Jun 2021 11:22:18 +0300 Subject: [PATCH] Management: Respect global settings in notifications toggle (#1199) --- src/api/gramjs/methods/chats.ts | 8 +++++++- src/api/gramjs/methods/settings.ts | 2 +- src/components/left/main/Chat.tsx | 1 + .../left/search/LeftSearchResultChat.tsx | 13 +++++++++++-- src/components/middle/HeaderMenuContainer.tsx | 6 +++--- src/components/right/ChatExtra.tsx | 18 +++++++++++------- src/components/right/management/ManageUser.tsx | 17 +++++++++++------ src/hooks/useChatContextActions.ts | 8 ++++++-- src/modules/actions/api/chats.ts | 1 + 9 files changed, 52 insertions(+), 22 deletions(-) diff --git a/src/api/gramjs/methods/chats.ts b/src/api/gramjs/methods/chats.ts index 215f21236..6e713ec6a 100644 --- a/src/api/gramjs/methods/chats.ts +++ b/src/api/gramjs/methods/chats.ts @@ -409,9 +409,15 @@ export async function updateChatMutedState({ peer: new GramJs.InputNotifyPeer({ peer: buildInputPeer(chat.id, chat.accessHash), }), - settings: new GramJs.InputPeerNotifySettings({ muteUntil: isMuted ? MAX_INT_32 : undefined }), + settings: new GramJs.InputPeerNotifySettings({ muteUntil: isMuted ? MAX_INT_32 : 0 }), })); + onUpdate({ + '@type': 'updateNotifyExceptions', + id: chat.id, + isMuted, + }); + void requestChatUpdate({ chat, serverTimeOffset, diff --git a/src/api/gramjs/methods/settings.ts b/src/api/gramjs/methods/settings.ts index e18f0d287..c1c2c2049 100644 --- a/src/api/gramjs/methods/settings.ts +++ b/src/api/gramjs/methods/settings.ts @@ -242,7 +242,7 @@ export function updateNotificationSettings(peerType: 'contact' | 'group' | 'broa const settings = { showPreviews: shouldShowPreviews, silent: isSilent, - muteUntil: isSilent ? MAX_INT_32 : undefined, + muteUntil: isSilent ? MAX_INT_32 : 0, }; return invokeRequest(new GramJs.account.UpdateNotifySettings({ diff --git a/src/components/left/main/Chat.tsx b/src/components/left/main/Chat.tsx index 2be279a44..1e8f7b07f 100644 --- a/src/components/left/main/Chat.tsx +++ b/src/components/left/main/Chat.tsx @@ -177,6 +177,7 @@ const Chat: FC = ({ handleDelete: openDeleteModal, folderId, isPinned, + isMuted, }); const lang = useLang(); diff --git a/src/components/left/search/LeftSearchResultChat.tsx b/src/components/left/search/LeftSearchResultChat.tsx index b204e9eb9..bfdc85209 100644 --- a/src/components/left/search/LeftSearchResultChat.tsx +++ b/src/components/left/search/LeftSearchResultChat.tsx @@ -7,8 +7,10 @@ import { ApiChat, ApiUser } from '../../../api/types'; import useChatContextActions from '../../../hooks/useChatContextActions'; import useFlag from '../../../hooks/useFlag'; -import { isChatPrivate, getPrivateChatUserId } from '../../../modules/helpers'; -import { selectChat, selectUser, selectIsChatPinned } from '../../../modules/selectors'; +import { isChatPrivate, getPrivateChatUserId, selectIsChatMuted } from '../../../modules/helpers'; +import { + selectChat, selectUser, selectIsChatPinned, selectNotifySettings, selectNotifyExceptions, +} from '../../../modules/selectors'; import useSelectWithEnter from '../../../hooks/useSelectWithEnter'; import PrivateChatInfo from '../../common/PrivateChatInfo'; @@ -26,6 +28,7 @@ type StateProps = { chat?: ApiChat; privateChatUser?: ApiUser; isPinned?: boolean; + isMuted?: boolean; }; const LeftSearchResultChat: FC = ({ @@ -33,6 +36,7 @@ const LeftSearchResultChat: FC = ({ chat, privateChatUser, isPinned, + isMuted, withUsername, onClick, }) => { @@ -42,6 +46,7 @@ const LeftSearchResultChat: FC = ({ chat, privateChatUser, isPinned, + isMuted, handleDelete: openDeleteModal, }); @@ -82,11 +87,15 @@ export default memo(withGlobal( const privateChatUserId = chat && getPrivateChatUserId(chat); const privateChatUser = privateChatUserId ? selectUser(global, privateChatUserId) : undefined; const isPinned = selectIsChatPinned(global, chatId); + const isMuted = chat + ? selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)) + : undefined; return { chat, privateChatUser, isPinned, + isMuted, }; }, )(LeftSearchResultChat)); diff --git a/src/components/middle/HeaderMenuContainer.tsx b/src/components/middle/HeaderMenuContainer.tsx index 57d7f7c03..a4062d698 100644 --- a/src/components/middle/HeaderMenuContainer.tsx +++ b/src/components/middle/HeaderMenuContainer.tsx @@ -9,9 +9,9 @@ import { IAnchorPosition } from '../../types'; import { IS_SINGLE_COLUMN_LAYOUT } from '../../util/environment'; import { disableScrolling, enableScrolling } from '../../util/scrollLock'; -import { selectChat } from '../../modules/selectors'; +import { selectChat, selectNotifySettings, selectNotifyExceptions } from '../../modules/selectors'; import { pick } from '../../util/iteratees'; -import { isChatPrivate, getCanDeleteChat } from '../../modules/helpers'; +import { isChatPrivate, getCanDeleteChat, selectIsChatMuted } from '../../modules/helpers'; import useShowTransition from '../../hooks/useShowTransition'; import useLang from '../../hooks/useLang'; @@ -192,7 +192,7 @@ export default memo(withGlobal( return { chat, - isMuted: chat.isMuted, + isMuted: selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)), isPrivate: isChatPrivate(chat.id), canDeleteChat: getCanDeleteChat(chat), }; diff --git a/src/components/right/ChatExtra.tsx b/src/components/right/ChatExtra.tsx index f525d249f..13dba7c0b 100644 --- a/src/components/right/ChatExtra.tsx +++ b/src/components/right/ChatExtra.tsx @@ -6,9 +6,11 @@ import { withGlobal } from '../../lib/teact/teactn'; import { GlobalActions, GlobalState } from '../../global/types'; import { ApiChat, ApiUser } from '../../api/types'; -import { selectChat, selectUser } from '../../modules/selectors'; import { - getChatDescription, getChatLink, getHasAdminRight, isChatChannel, isChatPrivate, isUserRightBanned, + selectChat, selectNotifyExceptions, selectNotifySettings, selectUser, +} from '../../modules/selectors'; +import { + getChatDescription, getChatLink, getHasAdminRight, isChatChannel, isChatPrivate, isUserRightBanned, selectIsChatMuted, } from '../../modules/helpers'; import renderText from '../common/helpers/renderText'; import { pick } from '../../util/iteratees'; @@ -29,6 +31,7 @@ type StateProps = { user?: ApiUser; chat?: ApiChat; canInviteUsers?: boolean; + isMuted?: boolean; } & Pick; type DispatchProps = Pick; @@ -39,6 +42,7 @@ const ChatExtra: FC = ({ chat, forceShowSelf, canInviteUsers, + isMuted, loadFullUser, showNotification, updateChatMutedState, @@ -52,7 +56,6 @@ const ChatExtra: FC = ({ } = user || {}; const { id: chatId, - isMuted: currentIsMuted, username: chatUsername, } = chat || {}; const lang = useLang(); @@ -64,8 +67,8 @@ const ChatExtra: FC = ({ }, [loadFullUser, userId, lastSyncTime]); const handleNotificationChange = useCallback(() => { - updateChatMutedState({ chatId, isMuted: !currentIsMuted }); - }, [chatId, currentIsMuted, updateChatMutedState]); + updateChatMutedState({ chatId, isMuted: !isMuted }); + }, [chatId, isMuted, updateChatMutedState]); if (!chat || chat.isRestricted || (isSelf && !forceShowSelf)) { return undefined; @@ -128,7 +131,7 @@ const ChatExtra: FC = ({ @@ -142,6 +145,7 @@ export default memo(withGlobal( const chat = chatOrUserId ? selectChat(global, chatOrUserId) : undefined; const user = isChatPrivate(chatOrUserId) ? selectUser(global, chatOrUserId) : undefined; + const isMuted = chat && selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)); const canInviteUsers = chat && ( (!isChatChannel(chat) && !isUserRightBanned(chat, 'inviteUsers')) @@ -149,7 +153,7 @@ export default memo(withGlobal( ); return { - lastSyncTime, chat, user, canInviteUsers, + lastSyncTime, chat, user, canInviteUsers, isMuted, }; }, (setGlobal, actions): DispatchProps => pick(actions, [ diff --git a/src/components/right/management/ManageUser.tsx b/src/components/right/management/ManageUser.tsx index 68b934424..8397d6c57 100644 --- a/src/components/right/management/ManageUser.tsx +++ b/src/components/right/management/ManageUser.tsx @@ -9,7 +9,10 @@ import { ApiChat, ApiUser } from '../../../api/types'; import { ManagementProgress } from '../../../types'; import { pick } from '../../../util/iteratees'; -import { selectChat, selectUser } from '../../../modules/selectors'; +import { + selectChat, selectNotifyExceptions, selectNotifySettings, selectUser, +} from '../../../modules/selectors'; +import { selectIsChatMuted } from '../../../modules/helpers'; import useFlag from '../../../hooks/useFlag'; import useLang from '../../../hooks/useLang'; @@ -31,6 +34,7 @@ type StateProps = { user?: ApiUser; chat: ApiChat; progress?: ManagementProgress; + isMuted?: boolean; }; type DispatchProps = Pick = ({ user, chat, progress, + isMuted, updateContact, deleteUser, deleteHistory, @@ -57,15 +62,14 @@ const ManageUser: FC = ({ const currentFirstName = user ? (user.firstName || '') : ''; const currentLastName = user ? (user.lastName || '') : ''; - const currentIsMuted = chat ? chat.isMuted : undefined; const [firstName, setFirstName] = useState(currentFirstName); const [lastName, setLastName] = useState(currentLastName); - const [isNotificationsEnabled, setIsNotificationsEnabled] = useState(!currentIsMuted); + const [isNotificationsEnabled, setIsNotificationsEnabled] = useState(!isMuted); useEffect(() => { - setIsNotificationsEnabled(!currentIsMuted); - }, [currentIsMuted]); + setIsNotificationsEnabled(!isMuted); + }, [isMuted]); useEffect(() => { setIsProfileFieldsTouched(false); @@ -202,9 +206,10 @@ export default memo(withGlobal( const user = selectUser(global, userId); const chat = selectChat(global, userId)!; const { progress } = global.management; + const isMuted = selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)); return { - user, chat, progress, + user, chat, progress, isMuted, }; }, (global, actions): DispatchProps => pick(actions, [ diff --git a/src/hooks/useChatContextActions.ts b/src/hooks/useChatContextActions.ts index a09169b26..104e23406 100644 --- a/src/hooks/useChatContextActions.ts +++ b/src/hooks/useChatContextActions.ts @@ -1,6 +1,8 @@ import { useMemo } from '../lib/teact/teact'; import { getDispatch } from '../lib/teact/teactn'; + import { ApiChat, ApiUser } from '../api/types'; + import { isChatArchived, getCanDeleteChat, isChatPrivate, isChatChannel, } from '../modules/helpers'; @@ -12,12 +14,14 @@ export default ({ handleDelete, folderId, isPinned, + isMuted, }: { chat: ApiChat | undefined; privateChatUser: ApiUser | undefined; handleDelete: () => void; folderId?: number; isPinned?: boolean; + isMuted?: boolean; }) => { const lang = useLang(); @@ -47,7 +51,7 @@ export default ({ } : { title: lang('PinToTop'), icon: 'pin', handler: () => toggleChatPinned({ id: chat.id, folderId }) }; - const actionMute = chat.isMuted + const actionMute = isMuted ? { title: lang('ChatList.Unmute'), icon: 'unmute', @@ -85,6 +89,6 @@ export default ({ ]; }, [ chat, privateChatUser, lang, isPinned, handleDelete, toggleChatUnread, toggleChatPinned, folderId, - updateChatMutedState, toggleChatArchived, + updateChatMutedState, toggleChatArchived, isMuted, ]); }; diff --git a/src/modules/actions/api/chats.ts b/src/modules/actions/api/chats.ts index 1d2ccc765..9e8ac84e5 100644 --- a/src/modules/actions/api/chats.ts +++ b/src/modules/actions/api/chats.ts @@ -198,6 +198,7 @@ addReducer('updateChatMutedState', (global, actions, payload) => { return; } + setGlobal(updateChat(global, chatId, { isMuted })); void callApi('updateChatMutedState', { chat, isMuted, serverTimeOffset }); });