From e31950841441d70697e3a09b38728b25d0f77d6e Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 7 Dec 2022 17:38:27 +0100 Subject: [PATCH] Media Viewer: Fix deleting chat photo (follow-up) (#2194) --- src/global/actions/api/chats.ts | 37 +++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/global/actions/api/chats.ts b/src/global/actions/api/chats.ts index e325afb07..cc285bde6 100644 --- a/src/global/actions/api/chats.ts +++ b/src/global/actions/api/chats.ts @@ -950,23 +950,28 @@ addActionHandler('deleteChatPhoto', async (global, actions, payload) => { const { photo, chatId } = payload; const chat = selectChat(global, chatId); if (!chat) return; - // Select next photo to set as avatar - const nextPhoto = chat.photos?.[1]; - setGlobal(updateChat(global, chatId, { - avatarHash: undefined, - fullInfo: { - ...chat.fullInfo, - profilePhoto: undefined, - }, - })); - // Set next photo as avatar - await callApi('editChatPhoto', { - chatId, - accessHash: chat.accessHash, - photo: nextPhoto, - }); + const photosToDelete = [photo]; + if (chat.avatarHash === photo.id) { + // Select next photo to set as avatar + const nextPhoto = chat.photos?.[1]; + if (nextPhoto) { + photosToDelete.push(nextPhoto); + } + setGlobal(updateChat(global, chatId, { + avatarHash: undefined, + fullInfo: { + ...chat.fullInfo, + profilePhoto: undefined, + }, + })); + // Set next photo as avatar + await callApi('editChatPhoto', { + chatId, + accessHash: chat.accessHash, + photo: nextPhoto, + }); + } // Delete references to the old photos - const photosToDelete = [photo, nextPhoto].filter(Boolean); const result = await callApi('deleteProfilePhotos', photosToDelete); if (!result) return; actions.loadFullChat({ chatId });