Management: Respect global settings in notifications toggle (#1199)

This commit is contained in:
Alexander Zinchuk 2021-06-23 11:22:18 +03:00
parent 322ce9f94a
commit d2ca29e961
9 changed files with 52 additions and 22 deletions

View File

@ -409,9 +409,15 @@ export async function updateChatMutedState({
peer: new GramJs.InputNotifyPeer({ peer: new GramJs.InputNotifyPeer({
peer: buildInputPeer(chat.id, chat.accessHash), 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({ void requestChatUpdate({
chat, chat,
serverTimeOffset, serverTimeOffset,

View File

@ -242,7 +242,7 @@ export function updateNotificationSettings(peerType: 'contact' | 'group' | 'broa
const settings = { const settings = {
showPreviews: shouldShowPreviews, showPreviews: shouldShowPreviews,
silent: isSilent, silent: isSilent,
muteUntil: isSilent ? MAX_INT_32 : undefined, muteUntil: isSilent ? MAX_INT_32 : 0,
}; };
return invokeRequest(new GramJs.account.UpdateNotifySettings({ return invokeRequest(new GramJs.account.UpdateNotifySettings({

View File

@ -177,6 +177,7 @@ const Chat: FC<OwnProps & StateProps & DispatchProps> = ({
handleDelete: openDeleteModal, handleDelete: openDeleteModal,
folderId, folderId,
isPinned, isPinned,
isMuted,
}); });
const lang = useLang(); const lang = useLang();

View File

@ -7,8 +7,10 @@ import { ApiChat, ApiUser } from '../../../api/types';
import useChatContextActions from '../../../hooks/useChatContextActions'; import useChatContextActions from '../../../hooks/useChatContextActions';
import useFlag from '../../../hooks/useFlag'; import useFlag from '../../../hooks/useFlag';
import { isChatPrivate, getPrivateChatUserId } from '../../../modules/helpers'; import { isChatPrivate, getPrivateChatUserId, selectIsChatMuted } from '../../../modules/helpers';
import { selectChat, selectUser, selectIsChatPinned } from '../../../modules/selectors'; import {
selectChat, selectUser, selectIsChatPinned, selectNotifySettings, selectNotifyExceptions,
} from '../../../modules/selectors';
import useSelectWithEnter from '../../../hooks/useSelectWithEnter'; import useSelectWithEnter from '../../../hooks/useSelectWithEnter';
import PrivateChatInfo from '../../common/PrivateChatInfo'; import PrivateChatInfo from '../../common/PrivateChatInfo';
@ -26,6 +28,7 @@ type StateProps = {
chat?: ApiChat; chat?: ApiChat;
privateChatUser?: ApiUser; privateChatUser?: ApiUser;
isPinned?: boolean; isPinned?: boolean;
isMuted?: boolean;
}; };
const LeftSearchResultChat: FC<OwnProps & StateProps> = ({ const LeftSearchResultChat: FC<OwnProps & StateProps> = ({
@ -33,6 +36,7 @@ const LeftSearchResultChat: FC<OwnProps & StateProps> = ({
chat, chat,
privateChatUser, privateChatUser,
isPinned, isPinned,
isMuted,
withUsername, withUsername,
onClick, onClick,
}) => { }) => {
@ -42,6 +46,7 @@ const LeftSearchResultChat: FC<OwnProps & StateProps> = ({
chat, chat,
privateChatUser, privateChatUser,
isPinned, isPinned,
isMuted,
handleDelete: openDeleteModal, handleDelete: openDeleteModal,
}); });
@ -82,11 +87,15 @@ export default memo(withGlobal<OwnProps>(
const privateChatUserId = chat && getPrivateChatUserId(chat); const privateChatUserId = chat && getPrivateChatUserId(chat);
const privateChatUser = privateChatUserId ? selectUser(global, privateChatUserId) : undefined; const privateChatUser = privateChatUserId ? selectUser(global, privateChatUserId) : undefined;
const isPinned = selectIsChatPinned(global, chatId); const isPinned = selectIsChatPinned(global, chatId);
const isMuted = chat
? selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global))
: undefined;
return { return {
chat, chat,
privateChatUser, privateChatUser,
isPinned, isPinned,
isMuted,
}; };
}, },
)(LeftSearchResultChat)); )(LeftSearchResultChat));

View File

@ -9,9 +9,9 @@ import { IAnchorPosition } from '../../types';
import { IS_SINGLE_COLUMN_LAYOUT } from '../../util/environment'; import { IS_SINGLE_COLUMN_LAYOUT } from '../../util/environment';
import { disableScrolling, enableScrolling } from '../../util/scrollLock'; import { disableScrolling, enableScrolling } from '../../util/scrollLock';
import { selectChat } from '../../modules/selectors'; import { selectChat, selectNotifySettings, selectNotifyExceptions } from '../../modules/selectors';
import { pick } from '../../util/iteratees'; import { pick } from '../../util/iteratees';
import { isChatPrivate, getCanDeleteChat } from '../../modules/helpers'; import { isChatPrivate, getCanDeleteChat, selectIsChatMuted } from '../../modules/helpers';
import useShowTransition from '../../hooks/useShowTransition'; import useShowTransition from '../../hooks/useShowTransition';
import useLang from '../../hooks/useLang'; import useLang from '../../hooks/useLang';
@ -192,7 +192,7 @@ export default memo(withGlobal<OwnProps>(
return { return {
chat, chat,
isMuted: chat.isMuted, isMuted: selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)),
isPrivate: isChatPrivate(chat.id), isPrivate: isChatPrivate(chat.id),
canDeleteChat: getCanDeleteChat(chat), canDeleteChat: getCanDeleteChat(chat),
}; };

View File

@ -6,9 +6,11 @@ import { withGlobal } from '../../lib/teact/teactn';
import { GlobalActions, GlobalState } from '../../global/types'; import { GlobalActions, GlobalState } from '../../global/types';
import { ApiChat, ApiUser } from '../../api/types'; import { ApiChat, ApiUser } from '../../api/types';
import { selectChat, selectUser } from '../../modules/selectors';
import { 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'; } from '../../modules/helpers';
import renderText from '../common/helpers/renderText'; import renderText from '../common/helpers/renderText';
import { pick } from '../../util/iteratees'; import { pick } from '../../util/iteratees';
@ -29,6 +31,7 @@ type StateProps = {
user?: ApiUser; user?: ApiUser;
chat?: ApiChat; chat?: ApiChat;
canInviteUsers?: boolean; canInviteUsers?: boolean;
isMuted?: boolean;
} & Pick<GlobalState, 'lastSyncTime'>; } & Pick<GlobalState, 'lastSyncTime'>;
type DispatchProps = Pick<GlobalActions, 'loadFullUser' | 'updateChatMutedState' | 'showNotification'>; type DispatchProps = Pick<GlobalActions, 'loadFullUser' | 'updateChatMutedState' | 'showNotification'>;
@ -39,6 +42,7 @@ const ChatExtra: FC<OwnProps & StateProps & DispatchProps> = ({
chat, chat,
forceShowSelf, forceShowSelf,
canInviteUsers, canInviteUsers,
isMuted,
loadFullUser, loadFullUser,
showNotification, showNotification,
updateChatMutedState, updateChatMutedState,
@ -52,7 +56,6 @@ const ChatExtra: FC<OwnProps & StateProps & DispatchProps> = ({
} = user || {}; } = user || {};
const { const {
id: chatId, id: chatId,
isMuted: currentIsMuted,
username: chatUsername, username: chatUsername,
} = chat || {}; } = chat || {};
const lang = useLang(); const lang = useLang();
@ -64,8 +67,8 @@ const ChatExtra: FC<OwnProps & StateProps & DispatchProps> = ({
}, [loadFullUser, userId, lastSyncTime]); }, [loadFullUser, userId, lastSyncTime]);
const handleNotificationChange = useCallback(() => { const handleNotificationChange = useCallback(() => {
updateChatMutedState({ chatId, isMuted: !currentIsMuted }); updateChatMutedState({ chatId, isMuted: !isMuted });
}, [chatId, currentIsMuted, updateChatMutedState]); }, [chatId, isMuted, updateChatMutedState]);
if (!chat || chat.isRestricted || (isSelf && !forceShowSelf)) { if (!chat || chat.isRestricted || (isSelf && !forceShowSelf)) {
return undefined; return undefined;
@ -128,7 +131,7 @@ const ChatExtra: FC<OwnProps & StateProps & DispatchProps> = ({
<Switcher <Switcher
id="group-notifications" id="group-notifications"
label={userId ? 'Toggle User Notifications' : 'Toggle Chat Notifications'} label={userId ? 'Toggle User Notifications' : 'Toggle Chat Notifications'}
checked={!currentIsMuted} checked={!isMuted}
inactive inactive
/> />
</ListItem> </ListItem>
@ -142,6 +145,7 @@ export default memo(withGlobal<OwnProps>(
const chat = chatOrUserId ? selectChat(global, chatOrUserId) : undefined; const chat = chatOrUserId ? selectChat(global, chatOrUserId) : undefined;
const user = isChatPrivate(chatOrUserId) ? selectUser(global, chatOrUserId) : undefined; const user = isChatPrivate(chatOrUserId) ? selectUser(global, chatOrUserId) : undefined;
const isMuted = chat && selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global));
const canInviteUsers = chat && ( const canInviteUsers = chat && (
(!isChatChannel(chat) && !isUserRightBanned(chat, 'inviteUsers')) (!isChatChannel(chat) && !isUserRightBanned(chat, 'inviteUsers'))
@ -149,7 +153,7 @@ export default memo(withGlobal<OwnProps>(
); );
return { return {
lastSyncTime, chat, user, canInviteUsers, lastSyncTime, chat, user, canInviteUsers, isMuted,
}; };
}, },
(setGlobal, actions): DispatchProps => pick(actions, [ (setGlobal, actions): DispatchProps => pick(actions, [

View File

@ -9,7 +9,10 @@ import { ApiChat, ApiUser } from '../../../api/types';
import { ManagementProgress } from '../../../types'; import { ManagementProgress } from '../../../types';
import { pick } from '../../../util/iteratees'; 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 useFlag from '../../../hooks/useFlag';
import useLang from '../../../hooks/useLang'; import useLang from '../../../hooks/useLang';
@ -31,6 +34,7 @@ type StateProps = {
user?: ApiUser; user?: ApiUser;
chat: ApiChat; chat: ApiChat;
progress?: ManagementProgress; progress?: ManagementProgress;
isMuted?: boolean;
}; };
type DispatchProps = Pick<GlobalActions, ( type DispatchProps = Pick<GlobalActions, (
@ -44,6 +48,7 @@ const ManageUser: FC<OwnProps & StateProps & DispatchProps> = ({
user, user,
chat, chat,
progress, progress,
isMuted,
updateContact, updateContact,
deleteUser, deleteUser,
deleteHistory, deleteHistory,
@ -57,15 +62,14 @@ const ManageUser: FC<OwnProps & StateProps & DispatchProps> = ({
const currentFirstName = user ? (user.firstName || '') : ''; const currentFirstName = user ? (user.firstName || '') : '';
const currentLastName = user ? (user.lastName || '') : ''; const currentLastName = user ? (user.lastName || '') : '';
const currentIsMuted = chat ? chat.isMuted : undefined;
const [firstName, setFirstName] = useState(currentFirstName); const [firstName, setFirstName] = useState(currentFirstName);
const [lastName, setLastName] = useState(currentLastName); const [lastName, setLastName] = useState(currentLastName);
const [isNotificationsEnabled, setIsNotificationsEnabled] = useState(!currentIsMuted); const [isNotificationsEnabled, setIsNotificationsEnabled] = useState(!isMuted);
useEffect(() => { useEffect(() => {
setIsNotificationsEnabled(!currentIsMuted); setIsNotificationsEnabled(!isMuted);
}, [currentIsMuted]); }, [isMuted]);
useEffect(() => { useEffect(() => {
setIsProfileFieldsTouched(false); setIsProfileFieldsTouched(false);
@ -202,9 +206,10 @@ export default memo(withGlobal<OwnProps>(
const user = selectUser(global, userId); const user = selectUser(global, userId);
const chat = selectChat(global, userId)!; const chat = selectChat(global, userId)!;
const { progress } = global.management; const { progress } = global.management;
const isMuted = selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global));
return { return {
user, chat, progress, user, chat, progress, isMuted,
}; };
}, },
(global, actions): DispatchProps => pick(actions, [ (global, actions): DispatchProps => pick(actions, [

View File

@ -1,6 +1,8 @@
import { useMemo } from '../lib/teact/teact'; import { useMemo } from '../lib/teact/teact';
import { getDispatch } from '../lib/teact/teactn'; import { getDispatch } from '../lib/teact/teactn';
import { ApiChat, ApiUser } from '../api/types'; import { ApiChat, ApiUser } from '../api/types';
import { import {
isChatArchived, getCanDeleteChat, isChatPrivate, isChatChannel, isChatArchived, getCanDeleteChat, isChatPrivate, isChatChannel,
} from '../modules/helpers'; } from '../modules/helpers';
@ -12,12 +14,14 @@ export default ({
handleDelete, handleDelete,
folderId, folderId,
isPinned, isPinned,
isMuted,
}: { }: {
chat: ApiChat | undefined; chat: ApiChat | undefined;
privateChatUser: ApiUser | undefined; privateChatUser: ApiUser | undefined;
handleDelete: () => void; handleDelete: () => void;
folderId?: number; folderId?: number;
isPinned?: boolean; isPinned?: boolean;
isMuted?: boolean;
}) => { }) => {
const lang = useLang(); const lang = useLang();
@ -47,7 +51,7 @@ export default ({
} }
: { title: lang('PinToTop'), icon: 'pin', handler: () => toggleChatPinned({ id: chat.id, folderId }) }; : { title: lang('PinToTop'), icon: 'pin', handler: () => toggleChatPinned({ id: chat.id, folderId }) };
const actionMute = chat.isMuted const actionMute = isMuted
? { ? {
title: lang('ChatList.Unmute'), title: lang('ChatList.Unmute'),
icon: 'unmute', icon: 'unmute',
@ -85,6 +89,6 @@ export default ({
]; ];
}, [ }, [
chat, privateChatUser, lang, isPinned, handleDelete, toggleChatUnread, toggleChatPinned, folderId, chat, privateChatUser, lang, isPinned, handleDelete, toggleChatUnread, toggleChatPinned, folderId,
updateChatMutedState, toggleChatArchived, updateChatMutedState, toggleChatArchived, isMuted,
]); ]);
}; };

View File

@ -198,6 +198,7 @@ addReducer('updateChatMutedState', (global, actions, payload) => {
return; return;
} }
setGlobal(updateChat(global, chatId, { isMuted }));
void callApi('updateChatMutedState', { chat, isMuted, serverTimeOffset }); void callApi('updateChatMutedState', { chat, isMuted, serverTimeOffset });
}); });