From 9251b468a5868e114006d10222f46ed080015080 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Thu, 13 Jan 2022 01:11:41 +0100 Subject: [PATCH] Management: Fix chat history being deleted along with deleted contact (#1632) --- src/api/gramjs/methods/index.ts | 2 +- src/api/gramjs/methods/users.ts | 4 ++-- src/api/gramjs/updater.ts | 2 +- src/api/types/updates.ts | 6 +++--- .../right/management/ManageUser.tsx | 19 +++++-------------- src/global/types.ts | 2 +- src/modules/actions/api/users.ts | 8 ++++---- src/modules/actions/apiUpdaters/users.ts | 6 +++--- src/modules/reducers/users.ts | 11 ++++++++--- 9 files changed, 28 insertions(+), 32 deletions(-) diff --git a/src/api/gramjs/methods/index.ts b/src/api/gramjs/methods/index.ts index e65c49b1..42df839a 100644 --- a/src/api/gramjs/methods/index.ts +++ b/src/api/gramjs/methods/index.ts @@ -28,7 +28,7 @@ export { export { fetchFullUser, fetchNearestCountry, fetchTopUsers, fetchContactList, fetchUsers, - addContact, updateContact, deleteUser, fetchProfilePhotos, fetchCommonChats, + addContact, updateContact, deleteContact, fetchProfilePhotos, fetchCommonChats, } from './users'; export { diff --git a/src/api/gramjs/methods/users.ts b/src/api/gramjs/methods/users.ts index b3a1450a..89073c0f 100644 --- a/src/api/gramjs/methods/users.ts +++ b/src/api/gramjs/methods/users.ts @@ -181,7 +181,7 @@ export function addContact({ }), true); } -export async function deleteUser({ +export async function deleteContact({ id, accessHash, }: { @@ -200,7 +200,7 @@ export async function deleteUser({ } onUpdate({ - '@type': 'deleteUser', + '@type': 'deleteContact', id, }); } diff --git a/src/api/gramjs/updater.ts b/src/api/gramjs/updater.ts index e652e5b9..7220a167 100644 --- a/src/api/gramjs/updater.ts +++ b/src/api/gramjs/updater.ts @@ -758,7 +758,7 @@ export function updater(update: Update, originRequest?: GramJs.AnyRequest) { .filter((e) => e instanceof GramJs.User && !e.contact) .forEach((user) => { onUpdate({ - '@type': 'deleteUser', + '@type': 'deleteContact', id: buildApiPeerId(user.id, 'user'), }); }); diff --git a/src/api/types/updates.ts b/src/api/types/updates.ts index b051d3bf..4bc3b0d0 100644 --- a/src/api/types/updates.ts +++ b/src/api/types/updates.ts @@ -284,8 +284,8 @@ export type ApiUpdateDraftMessage = { replyingToId?: number; }; -export type ApiDeleteUser = { - '@type': 'deleteUser'; +export type ApiDeleteContact = { + '@type': 'deleteContact'; id: string; }; @@ -433,7 +433,7 @@ export type ApiUpdate = ( ApiUpdateNewMessage | ApiUpdateMessage | ApiUpdateThreadInfo | ApiUpdateCommonBoxMessages | ApiUpdateChannelMessages | ApiUpdateDeleteMessages | ApiUpdateMessagePoll | ApiUpdateMessagePollVote | ApiUpdateDeleteHistory | ApiUpdateMessageSendSucceeded | ApiUpdateMessageSendFailed | ApiUpdateServiceNotification | - ApiDeleteUser | ApiUpdateUser | ApiUpdateUserStatus | ApiUpdateUserFullInfo | ApiUpdateDeleteProfilePhotos | + ApiDeleteContact | ApiUpdateUser | ApiUpdateUserStatus | ApiUpdateUserFullInfo | ApiUpdateDeleteProfilePhotos | ApiUpdateAvatar | ApiUpdateMessageImage | ApiUpdateDraftMessage | ApiUpdateError | ApiUpdateResetContacts | ApiUpdateFavoriteStickers | ApiUpdateStickerSet | diff --git a/src/components/right/management/ManageUser.tsx b/src/components/right/management/ManageUser.tsx index 6e44ad73..b920ac4c 100644 --- a/src/components/right/management/ManageUser.tsx +++ b/src/components/right/management/ManageUser.tsx @@ -4,7 +4,7 @@ import React, { } from '../../../lib/teact/teact'; import { getDispatch, withGlobal } from '../../../lib/teact/teactn'; -import { ApiChat, ApiUser } from '../../../api/types'; +import { ApiUser } from '../../../api/types'; import { ManagementProgress } from '../../../types'; import { @@ -33,7 +33,6 @@ type OwnProps = { type StateProps = { user?: ApiUser; - chat: ApiChat; progress?: ManagementProgress; isMuted?: boolean; }; @@ -43,7 +42,6 @@ const ERROR_FIRST_NAME_MISSING = 'Please provide first name'; const ManageUser: FC = ({ userId, user, - chat, progress, isMuted, onClose, @@ -51,10 +49,8 @@ const ManageUser: FC = ({ }) => { const { updateContact, - deleteUser, - deleteHistory, + deleteContact, closeManagement, - openChat, } = getDispatch(); const [isDeleteDialogOpen, openDeleteDialog, closeDeleteDialog] = useFlag(); @@ -125,15 +121,10 @@ const ManageUser: FC = ({ }, [firstName, lastName, updateContact, userId, isNotificationsEnabled]); const handleDeleteContact = useCallback(() => { - deleteHistory({ - chatId: chat.id, - shouldDeleteForAll: false, - }); - deleteUser({ userId }); + deleteContact({ userId }); closeDeleteDialog(); closeManagement(); - openChat({ id: undefined }); - }, [chat.id, closeDeleteDialog, closeManagement, deleteHistory, deleteUser, openChat, userId]); + }, [closeDeleteDialog, closeManagement, deleteContact, userId]); if (!user) { return undefined; @@ -213,7 +204,7 @@ export default memo(withGlobal( const isMuted = selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)); return { - user, chat, progress, isMuted, + user, progress, isMuted, }; }, )(ManageUser)); diff --git a/src/global/types.ts b/src/global/types.ts index 49205167..a738e918 100644 --- a/src/global/types.ts +++ b/src/global/types.ts @@ -529,7 +529,7 @@ export type ActionTypes = ( // users 'loadFullUser' | 'openUserInfo' | 'loadNearestCountry' | 'loadTopUsers' | 'loadContactList' | 'loadCurrentUser' | 'updateProfile' | 'checkUsername' | 'addContact' | 'updateContact' | - 'deleteUser' | 'loadUser' | 'setUserSearchQuery' | 'loadCommonChats' | + 'deleteContact' | 'loadUser' | 'setUserSearchQuery' | 'loadCommonChats' | // chat creation 'createChannel' | 'createGroupChat' | 'resetChatCreation' | // settings diff --git a/src/modules/actions/api/users.ts b/src/modules/actions/api/users.ts index cf93f621..eabd09e4 100644 --- a/src/modules/actions/api/users.ts +++ b/src/modules/actions/api/users.ts @@ -114,10 +114,10 @@ addReducer('updateContact', (global, actions, payload) => { void updateContact(userId, isMuted, firstName, lastName); }); -addReducer('deleteUser', (global, actions, payload) => { +addReducer('deleteContact', (global, actions, payload) => { const { userId } = payload!; - void deleteUser(userId); + void deleteContact(userId); }); async function loadTopUsers() { @@ -210,7 +210,7 @@ async function updateContact( setGlobal(updateManagementProgress(getGlobal(), ManagementProgress.Complete)); } -async function deleteUser(userId: string) { +async function deleteContact(userId: string) { const global = getGlobal(); const user = selectUser(global, userId); @@ -220,7 +220,7 @@ async function deleteUser(userId: string) { const { id, accessHash } = user; - await callApi('deleteUser', { id, accessHash }); + await callApi('deleteContact', { id, accessHash }); } addReducer('loadProfilePhotos', (global, actions, payload) => { diff --git a/src/modules/actions/apiUpdaters/users.ts b/src/modules/actions/apiUpdaters/users.ts index 40e102fd..e6a5c065 100644 --- a/src/modules/actions/apiUpdaters/users.ts +++ b/src/modules/actions/apiUpdaters/users.ts @@ -2,7 +2,7 @@ import { addReducer, getGlobal, setGlobal } from '../../../lib/teact/teactn'; import { ApiUpdate, ApiUserStatus } from '../../../api/types'; -import { deleteUser, replaceUserStatuses, updateUser } from '../../reducers'; +import { deleteContact, replaceUserStatuses, updateUser } from '../../reducers'; import { throttle } from '../../../util/schedulers'; const STATUS_UPDATE_THROTTLE = 3000; @@ -29,8 +29,8 @@ function flushStatusUpdates() { addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { switch (update['@type']) { - case 'deleteUser': { - return deleteUser(global, update.id); + case 'deleteContact': { + return deleteContact(global, update.id); } case 'updateUser': { diff --git a/src/modules/reducers/users.ts b/src/modules/reducers/users.ts index 6a91fd12..1fd08af4 100644 --- a/src/modules/reducers/users.ts +++ b/src/modules/reducers/users.ts @@ -136,10 +136,9 @@ export function updateSelectedUserId(global: GlobalState, selectedId?: string): }; } -export function deleteUser(global: GlobalState, userId: string): GlobalState { +export function deleteContact(global: GlobalState, userId: string): GlobalState { const { byId } = global.users; const { userIds } = global.contactList || {}; - delete byId[userId]; global = { ...global, @@ -148,7 +147,13 @@ export function deleteUser(global: GlobalState, userId: string): GlobalState { }, }; - return replaceUsers(global, byId); + return replaceUsers(global, { + ...byId, + [userId]: { + ...byId[userId], + isContact: undefined, + }, + }); } export function updateUserSearch(