From f5eb17e0d1a31eda0b76ed7a4dc1606fb9435574 Mon Sep 17 00:00:00 2001 From: Evan Hahn <69474926+EvanHahn-Signal@users.noreply.github.com> Date: Mon, 24 Jan 2022 19:39:18 -0600 Subject: [PATCH] Fix stuck safety number modal overlay --- ts/messages/getMessagesById.ts | 8 ++-- ts/state/ducks/conversations.ts | 68 ++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/ts/messages/getMessagesById.ts b/ts/messages/getMessagesById.ts index 96d0962eeb..5a9b515187 100644 --- a/ts/messages/getMessagesById.ts +++ b/ts/messages/getMessagesById.ts @@ -1,4 +1,4 @@ -// Copyright 2021 Signal Messenger, LLC +// Copyright 2021-2022 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import * as log from '../logging/log'; @@ -7,19 +7,19 @@ import type { MessageAttributesType } from '../model-types.d'; import * as Errors from '../types/errors'; export async function getMessagesById( - messageIds: ReadonlyArray + messageIds: Iterable ): Promise> { const messagesFromMemory: Array = []; const messageIdsToLookUpInDatabase: Array = []; - messageIds.forEach(messageId => { + for (const messageId of messageIds) { const message = window.MessageController.getById(messageId); if (message) { messagesFromMemory.push(message); } else { messageIdsToLookUpInDatabase.push(messageId); } - }); + } let rawMessagesFromDatabase: Array; try { diff --git a/ts/state/ducks/conversations.ts b/ts/state/ducks/conversations.ts index 5bb5dfb9cc..e0c7bb8d98 100644 --- a/ts/state/ducks/conversations.ts +++ b/ts/state/ducks/conversations.ts @@ -369,12 +369,17 @@ export const getConversationCallMode = ( return CallMode.None; }; +const retryMessages = async (messageIds: Iterable): Promise => { + const messages = await getMessagesById(messageIds); + await Promise.all(messages.map(message => message.retrySend())); +}; + // Actions +const CLEAR_MESSAGES_PENDING_CONVERSATION_VERIFICATION = + 'conversations/CLEAR_MESSAGES_PENDING_CONVERSATION_VERIFICATION'; export const COLORS_CHANGED = 'conversations/COLORS_CHANGED'; export const COLOR_SELECTED = 'conversations/COLOR_SELECTED'; -const CANCEL_MESSAGES_PENDING_CONVERSATION_VERIFICATION = - 'conversations/CANCEL_MESSAGES_PENDING_CONVERSATION_VERIFICATION'; const COMPOSE_TOGGLE_EDITING_AVATAR = 'conversations/compose/COMPOSE_TOGGLE_EDITING_AVATAR'; const COMPOSE_ADD_AVATAR = 'conversations/compose/ADD_AVATAR'; @@ -386,9 +391,6 @@ const MESSAGE_STOPPED_BY_MISSING_VERIFICATION = const REPLACE_AVATARS = 'conversations/REPLACE_AVATARS'; const UPDATE_USERNAME_SAVE_STATE = 'conversations/UPDATE_USERNAME_SAVE_STATE'; -type CancelMessagesPendingConversationVerificationActionType = { - type: typeof CANCEL_MESSAGES_PENDING_CONVERSATION_VERIFICATION; -}; type CantAddContactToGroupActionType = { type: 'CANT_ADD_CONTACT_TO_GROUP'; payload: { @@ -399,6 +401,9 @@ type ClearGroupCreationErrorActionType = { type: 'CLEAR_GROUP_CREATION_ERROR' }; type ClearInvitedUuidsForNewlyCreatedGroupActionType = { type: 'CLEAR_INVITED_UUIDS_FOR_NEWLY_CREATED_GROUP'; }; +type ClearMessagesPendingConversationVerificationActionType = { + type: typeof CLEAR_MESSAGES_PENDING_CONVERSATION_VERIFICATION; +}; type CloseCantAddContactToGroupModalActionType = { type: 'CLOSE_CANT_ADD_CONTACT_TO_GROUP_MODAL'; }; @@ -730,11 +735,11 @@ type ReplaceAvatarsActionType = { }; }; export type ConversationActionType = - | CancelMessagesPendingConversationVerificationActionType | CantAddContactToGroupActionType | ClearChangedMessagesActionType | ClearGroupCreationErrorActionType | ClearInvitedUuidsForNewlyCreatedGroupActionType + | ClearMessagesPendingConversationVerificationActionType | ClearSelectedMessageActionType | ClearUnreadMetricsActionType | CloseCantAddContactToGroupModalActionType @@ -1243,19 +1248,39 @@ function verifyConversationsStoppingMessageSend(): ThunkAction< void, RootStateType, unknown, - never + ClearMessagesPendingConversationVerificationActionType > { - return async (_dispatch, getState) => { - const conversationIds = Object.keys( - getState().conversations.outboundMessagesPendingConversationVerification + return async (dispatch, getState) => { + const { outboundMessagesPendingConversationVerification } = + getState().conversations; + + const allMessageIds = new Set(); + const promises: Array> = []; + + Object.entries(outboundMessagesPendingConversationVerification).forEach( + ([conversationId, messageIds]) => { + for (const messageId of messageIds) { + allMessageIds.add(messageId); + } + + const conversation = window.ConversationController.get(conversationId); + if (!conversation) { + return; + } + if (conversation.isUnverified()) { + promises.push(conversation.setVerifiedDefault()); + } + promises.push(conversation.setApproved()); + } ); - await Promise.all( - conversationIds.map(async conversationId => { - const conversation = window.ConversationController.get(conversationId); - await conversation?.setVerifiedDefault(); - }) - ); + promises.push(retryMessages(allMessageIds)); + + dispatch({ + type: CLEAR_MESSAGES_PENDING_CONVERSATION_VERIFICATION, + }); + + await Promise.all(promises); }; } @@ -1317,7 +1342,7 @@ function cancelMessagesPendingConversationVerification(): ThunkAction< void, RootStateType, unknown, - CancelMessagesPendingConversationVerificationActionType + ClearMessagesPendingConversationVerificationActionType > { return async (dispatch, getState) => { const messageIdsPending = getMessageIdsPendingBecauseOfVerification( @@ -1329,7 +1354,7 @@ function cancelMessagesPendingConversationVerification(): ThunkAction< }); dispatch({ - type: CANCEL_MESSAGES_PENDING_CONVERSATION_VERIFICATION, + type: CLEAR_MESSAGES_PENDING_CONVERSATION_VERIFICATION, }); await window.Signal.Data.saveMessages( @@ -1384,10 +1409,7 @@ function conversationChanged( id ) ?? []; if (messageIdsPending.length) { - const messagesPending = await getMessagesById(messageIdsPending); - messagesPending.forEach(message => { - message.retrySend(); - }); + retryMessages(messageIdsPending); } } @@ -2237,7 +2259,7 @@ export function reducer( state: Readonly = getEmptyState(), action: Readonly ): ConversationsStateType { - if (action.type === CANCEL_MESSAGES_PENDING_CONVERSATION_VERIFICATION) { + if (action.type === CLEAR_MESSAGES_PENDING_CONVERSATION_VERIFICATION) { return { ...state, outboundMessagesPendingConversationVerification: {},