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: 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,

View File

@ -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({

View File

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

View File

@ -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<OwnProps & StateProps> = ({
@ -33,6 +36,7 @@ const LeftSearchResultChat: FC<OwnProps & StateProps> = ({
chat,
privateChatUser,
isPinned,
isMuted,
withUsername,
onClick,
}) => {
@ -42,6 +46,7 @@ const LeftSearchResultChat: FC<OwnProps & StateProps> = ({
chat,
privateChatUser,
isPinned,
isMuted,
handleDelete: openDeleteModal,
});
@ -82,11 +87,15 @@ export default memo(withGlobal<OwnProps>(
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));

View File

@ -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<OwnProps>(
return {
chat,
isMuted: chat.isMuted,
isMuted: selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)),
isPrivate: isChatPrivate(chat.id),
canDeleteChat: getCanDeleteChat(chat),
};

View File

@ -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<GlobalState, 'lastSyncTime'>;
type DispatchProps = Pick<GlobalActions, 'loadFullUser' | 'updateChatMutedState' | 'showNotification'>;
@ -39,6 +42,7 @@ const ChatExtra: FC<OwnProps & StateProps & DispatchProps> = ({
chat,
forceShowSelf,
canInviteUsers,
isMuted,
loadFullUser,
showNotification,
updateChatMutedState,
@ -52,7 +56,6 @@ const ChatExtra: FC<OwnProps & StateProps & DispatchProps> = ({
} = user || {};
const {
id: chatId,
isMuted: currentIsMuted,
username: chatUsername,
} = chat || {};
const lang = useLang();
@ -64,8 +67,8 @@ const ChatExtra: FC<OwnProps & StateProps & DispatchProps> = ({
}, [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<OwnProps & StateProps & DispatchProps> = ({
<Switcher
id="group-notifications"
label={userId ? 'Toggle User Notifications' : 'Toggle Chat Notifications'}
checked={!currentIsMuted}
checked={!isMuted}
inactive
/>
</ListItem>
@ -142,6 +145,7 @@ export default memo(withGlobal<OwnProps>(
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<OwnProps>(
);
return {
lastSyncTime, chat, user, canInviteUsers,
lastSyncTime, chat, user, canInviteUsers, isMuted,
};
},
(setGlobal, actions): DispatchProps => pick(actions, [

View File

@ -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<GlobalActions, (
@ -44,6 +48,7 @@ const ManageUser: FC<OwnProps & StateProps & DispatchProps> = ({
user,
chat,
progress,
isMuted,
updateContact,
deleteUser,
deleteHistory,
@ -57,15 +62,14 @@ const ManageUser: FC<OwnProps & StateProps & DispatchProps> = ({
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<OwnProps>(
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, [

View File

@ -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,
]);
};

View File

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