Management: Fix chat history being deleted along with deleted contact (#1632)

This commit is contained in:
Alexander Zinchuk 2022-01-13 01:11:41 +01:00
parent 34933fd064
commit 9251b468a5
9 changed files with 28 additions and 32 deletions

View File

@ -28,7 +28,7 @@ export {
export { export {
fetchFullUser, fetchNearestCountry, fetchTopUsers, fetchContactList, fetchUsers, fetchFullUser, fetchNearestCountry, fetchTopUsers, fetchContactList, fetchUsers,
addContact, updateContact, deleteUser, fetchProfilePhotos, fetchCommonChats, addContact, updateContact, deleteContact, fetchProfilePhotos, fetchCommonChats,
} from './users'; } from './users';
export { export {

View File

@ -181,7 +181,7 @@ export function addContact({
}), true); }), true);
} }
export async function deleteUser({ export async function deleteContact({
id, id,
accessHash, accessHash,
}: { }: {
@ -200,7 +200,7 @@ export async function deleteUser({
} }
onUpdate({ onUpdate({
'@type': 'deleteUser', '@type': 'deleteContact',
id, id,
}); });
} }

View File

@ -758,7 +758,7 @@ export function updater(update: Update, originRequest?: GramJs.AnyRequest) {
.filter((e) => e instanceof GramJs.User && !e.contact) .filter((e) => e instanceof GramJs.User && !e.contact)
.forEach((user) => { .forEach((user) => {
onUpdate({ onUpdate({
'@type': 'deleteUser', '@type': 'deleteContact',
id: buildApiPeerId(user.id, 'user'), id: buildApiPeerId(user.id, 'user'),
}); });
}); });

View File

@ -284,8 +284,8 @@ export type ApiUpdateDraftMessage = {
replyingToId?: number; replyingToId?: number;
}; };
export type ApiDeleteUser = { export type ApiDeleteContact = {
'@type': 'deleteUser'; '@type': 'deleteContact';
id: string; id: string;
}; };
@ -433,7 +433,7 @@ export type ApiUpdate = (
ApiUpdateNewMessage | ApiUpdateMessage | ApiUpdateThreadInfo | ApiUpdateCommonBoxMessages | ApiUpdateChannelMessages | ApiUpdateNewMessage | ApiUpdateMessage | ApiUpdateThreadInfo | ApiUpdateCommonBoxMessages | ApiUpdateChannelMessages |
ApiUpdateDeleteMessages | ApiUpdateMessagePoll | ApiUpdateMessagePollVote | ApiUpdateDeleteHistory | ApiUpdateDeleteMessages | ApiUpdateMessagePoll | ApiUpdateMessagePollVote | ApiUpdateDeleteHistory |
ApiUpdateMessageSendSucceeded | ApiUpdateMessageSendFailed | ApiUpdateServiceNotification | ApiUpdateMessageSendSucceeded | ApiUpdateMessageSendFailed | ApiUpdateServiceNotification |
ApiDeleteUser | ApiUpdateUser | ApiUpdateUserStatus | ApiUpdateUserFullInfo | ApiUpdateDeleteProfilePhotos | ApiDeleteContact | ApiUpdateUser | ApiUpdateUserStatus | ApiUpdateUserFullInfo | ApiUpdateDeleteProfilePhotos |
ApiUpdateAvatar | ApiUpdateMessageImage | ApiUpdateDraftMessage | ApiUpdateAvatar | ApiUpdateMessageImage | ApiUpdateDraftMessage |
ApiUpdateError | ApiUpdateResetContacts | ApiUpdateError | ApiUpdateResetContacts |
ApiUpdateFavoriteStickers | ApiUpdateStickerSet | ApiUpdateFavoriteStickers | ApiUpdateStickerSet |

View File

@ -4,7 +4,7 @@ import React, {
} from '../../../lib/teact/teact'; } from '../../../lib/teact/teact';
import { getDispatch, withGlobal } from '../../../lib/teact/teactn'; import { getDispatch, withGlobal } from '../../../lib/teact/teactn';
import { ApiChat, ApiUser } from '../../../api/types'; import { ApiUser } from '../../../api/types';
import { ManagementProgress } from '../../../types'; import { ManagementProgress } from '../../../types';
import { import {
@ -33,7 +33,6 @@ type OwnProps = {
type StateProps = { type StateProps = {
user?: ApiUser; user?: ApiUser;
chat: ApiChat;
progress?: ManagementProgress; progress?: ManagementProgress;
isMuted?: boolean; isMuted?: boolean;
}; };
@ -43,7 +42,6 @@ const ERROR_FIRST_NAME_MISSING = 'Please provide first name';
const ManageUser: FC<OwnProps & StateProps> = ({ const ManageUser: FC<OwnProps & StateProps> = ({
userId, userId,
user, user,
chat,
progress, progress,
isMuted, isMuted,
onClose, onClose,
@ -51,10 +49,8 @@ const ManageUser: FC<OwnProps & StateProps> = ({
}) => { }) => {
const { const {
updateContact, updateContact,
deleteUser, deleteContact,
deleteHistory,
closeManagement, closeManagement,
openChat,
} = getDispatch(); } = getDispatch();
const [isDeleteDialogOpen, openDeleteDialog, closeDeleteDialog] = useFlag(); const [isDeleteDialogOpen, openDeleteDialog, closeDeleteDialog] = useFlag();
@ -125,15 +121,10 @@ const ManageUser: FC<OwnProps & StateProps> = ({
}, [firstName, lastName, updateContact, userId, isNotificationsEnabled]); }, [firstName, lastName, updateContact, userId, isNotificationsEnabled]);
const handleDeleteContact = useCallback(() => { const handleDeleteContact = useCallback(() => {
deleteHistory({ deleteContact({ userId });
chatId: chat.id,
shouldDeleteForAll: false,
});
deleteUser({ userId });
closeDeleteDialog(); closeDeleteDialog();
closeManagement(); closeManagement();
openChat({ id: undefined }); }, [closeDeleteDialog, closeManagement, deleteContact, userId]);
}, [chat.id, closeDeleteDialog, closeManagement, deleteHistory, deleteUser, openChat, userId]);
if (!user) { if (!user) {
return undefined; return undefined;
@ -213,7 +204,7 @@ export default memo(withGlobal<OwnProps>(
const isMuted = selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)); const isMuted = selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global));
return { return {
user, chat, progress, isMuted, user, progress, isMuted,
}; };
}, },
)(ManageUser)); )(ManageUser));

View File

@ -529,7 +529,7 @@ export type ActionTypes = (
// users // users
'loadFullUser' | 'openUserInfo' | 'loadNearestCountry' | 'loadTopUsers' | 'loadContactList' | 'loadFullUser' | 'openUserInfo' | 'loadNearestCountry' | 'loadTopUsers' | 'loadContactList' |
'loadCurrentUser' | 'updateProfile' | 'checkUsername' | 'addContact' | 'updateContact' | 'loadCurrentUser' | 'updateProfile' | 'checkUsername' | 'addContact' | 'updateContact' |
'deleteUser' | 'loadUser' | 'setUserSearchQuery' | 'loadCommonChats' | 'deleteContact' | 'loadUser' | 'setUserSearchQuery' | 'loadCommonChats' |
// chat creation // chat creation
'createChannel' | 'createGroupChat' | 'resetChatCreation' | 'createChannel' | 'createGroupChat' | 'resetChatCreation' |
// settings // settings

View File

@ -114,10 +114,10 @@ addReducer('updateContact', (global, actions, payload) => {
void updateContact(userId, isMuted, firstName, lastName); void updateContact(userId, isMuted, firstName, lastName);
}); });
addReducer('deleteUser', (global, actions, payload) => { addReducer('deleteContact', (global, actions, payload) => {
const { userId } = payload!; const { userId } = payload!;
void deleteUser(userId); void deleteContact(userId);
}); });
async function loadTopUsers() { async function loadTopUsers() {
@ -210,7 +210,7 @@ async function updateContact(
setGlobal(updateManagementProgress(getGlobal(), ManagementProgress.Complete)); setGlobal(updateManagementProgress(getGlobal(), ManagementProgress.Complete));
} }
async function deleteUser(userId: string) { async function deleteContact(userId: string) {
const global = getGlobal(); const global = getGlobal();
const user = selectUser(global, userId); const user = selectUser(global, userId);
@ -220,7 +220,7 @@ async function deleteUser(userId: string) {
const { id, accessHash } = user; const { id, accessHash } = user;
await callApi('deleteUser', { id, accessHash }); await callApi('deleteContact', { id, accessHash });
} }
addReducer('loadProfilePhotos', (global, actions, payload) => { addReducer('loadProfilePhotos', (global, actions, payload) => {

View File

@ -2,7 +2,7 @@ import { addReducer, getGlobal, setGlobal } from '../../../lib/teact/teactn';
import { ApiUpdate, ApiUserStatus } from '../../../api/types'; import { ApiUpdate, ApiUserStatus } from '../../../api/types';
import { deleteUser, replaceUserStatuses, updateUser } from '../../reducers'; import { deleteContact, replaceUserStatuses, updateUser } from '../../reducers';
import { throttle } from '../../../util/schedulers'; import { throttle } from '../../../util/schedulers';
const STATUS_UPDATE_THROTTLE = 3000; const STATUS_UPDATE_THROTTLE = 3000;
@ -29,8 +29,8 @@ function flushStatusUpdates() {
addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { addReducer('apiUpdate', (global, actions, update: ApiUpdate) => {
switch (update['@type']) { switch (update['@type']) {
case 'deleteUser': { case 'deleteContact': {
return deleteUser(global, update.id); return deleteContact(global, update.id);
} }
case 'updateUser': { case 'updateUser': {

View File

@ -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 { byId } = global.users;
const { userIds } = global.contactList || {}; const { userIds } = global.contactList || {};
delete byId[userId];
global = { global = {
...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( export function updateUserSearch(