Move getUntrustedContacts out of conversation_view
This commit is contained in:
parent
96c4cc4bcf
commit
936ce91b2e
19 changed files with 410 additions and 414 deletions
67
ts/util/blockSendUntilConversationsAreVerified.ts
Normal file
67
ts/util/blockSendUntilConversationsAreVerified.ts
Normal file
|
@ -0,0 +1,67 @@
|
|||
// Copyright 2022 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type { ConversationModel } from '../models/conversations';
|
||||
import type { SafetyNumberChangeSource } from '../components/SafetyNumberChangeDialog';
|
||||
import * as log from '../logging/log';
|
||||
import { explodePromise } from './explodePromise';
|
||||
import { getConversationIdForLogging } from './idForLogging';
|
||||
|
||||
export async function blockSendUntilConversationsAreVerified(
|
||||
conversations: Array<ConversationModel>,
|
||||
source?: SafetyNumberChangeSource
|
||||
): Promise<boolean> {
|
||||
const conversationsToPause = new Map<string, Set<string>>();
|
||||
|
||||
await Promise.all(
|
||||
conversations.map(async conversation => {
|
||||
if (!conversation) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uuidsStoppingSend = new Set<string>();
|
||||
|
||||
await conversation.updateVerified();
|
||||
const unverifieds = conversation.getUnverified();
|
||||
|
||||
if (unverifieds.length) {
|
||||
unverifieds.forEach(unverifiedConversation => {
|
||||
const uuid = unverifiedConversation.get('uuid');
|
||||
if (uuid) {
|
||||
uuidsStoppingSend.add(uuid);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const untrusted = conversation.getUntrusted();
|
||||
if (untrusted.length) {
|
||||
untrusted.forEach(untrustedConversation => {
|
||||
const uuid = untrustedConversation.get('uuid');
|
||||
if (uuid) {
|
||||
uuidsStoppingSend.add(uuid);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (uuidsStoppingSend.size) {
|
||||
log.info('blockSendUntilConversationsAreVerified: blocking send', {
|
||||
id: getConversationIdForLogging(conversation.attributes),
|
||||
untrustedCount: uuidsStoppingSend.size,
|
||||
});
|
||||
conversationsToPause.set(conversation.id, uuidsStoppingSend);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
if (conversationsToPause.size) {
|
||||
const explodedPromise = explodePromise<boolean>();
|
||||
window.reduxActions.globalModals.showBlockingSafetyNumberChangeDialog(
|
||||
conversationsToPause,
|
||||
explodedPromise,
|
||||
source
|
||||
);
|
||||
return explodedPromise.promise;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type { ConversationModel } from '../models/conversations';
|
||||
|
||||
export async function markAllAsApproved(
|
||||
untrusted: ReadonlyArray<ConversationModel>
|
||||
): Promise<void> {
|
||||
await Promise.all(untrusted.map(contact => contact.setApproved()));
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type { ConversationModel } from '../models/conversations';
|
||||
|
||||
export async function markAllAsVerifiedDefault(
|
||||
unverified: ReadonlyArray<ConversationModel>
|
||||
): Promise<void> {
|
||||
await Promise.all(
|
||||
unverified.map(contact => {
|
||||
if (contact.isUnverified()) {
|
||||
return contact.setVerifiedDefault();
|
||||
}
|
||||
|
||||
return null;
|
||||
})
|
||||
);
|
||||
}
|
|
@ -2,15 +2,14 @@
|
|||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type { AttachmentType } from '../types/Attachment';
|
||||
import type { ConversationModel } from '../models/conversations';
|
||||
import type { LinkPreviewType } from '../types/message/LinkPreviews';
|
||||
import type { MessageAttributesType } from '../model-types.d';
|
||||
import * as log from '../logging/log';
|
||||
import { SafetyNumberChangeSource } from '../components/SafetyNumberChangeDialog';
|
||||
import { blockSendUntilConversationsAreVerified } from './blockSendUntilConversationsAreVerified';
|
||||
import { getMessageIdForLogging } from './idForLogging';
|
||||
import { markAllAsApproved } from './markAllAsApproved';
|
||||
import { markAllAsVerifiedDefault } from './markAllAsVerifiedDefault';
|
||||
import { isNotNil } from './isNotNil';
|
||||
import { resetLinkPreview } from '../services/LinkPreview';
|
||||
import { showSafetyNumberChangeDialog } from '../shims/showSafetyNumberChangeDialog';
|
||||
|
||||
export async function maybeForwardMessage(
|
||||
messageAttributes: MessageAttributesType,
|
||||
|
@ -29,9 +28,9 @@ export async function maybeForwardMessage(
|
|||
});
|
||||
}
|
||||
|
||||
const conversations = conversationIds.map(id =>
|
||||
window.ConversationController.get(id)
|
||||
);
|
||||
const conversations = conversationIds
|
||||
.map(id => window.ConversationController.get(id))
|
||||
.filter(isNotNil);
|
||||
|
||||
const cannotSend = conversations.some(
|
||||
conversation =>
|
||||
|
@ -42,69 +41,16 @@ export async function maybeForwardMessage(
|
|||
}
|
||||
|
||||
// Verify that all contacts that we're forwarding
|
||||
// to are verified and trusted
|
||||
const unverifiedContacts: Array<ConversationModel> = [];
|
||||
const untrustedContacts: Array<ConversationModel> = [];
|
||||
await Promise.all(
|
||||
conversations.map(async conversation => {
|
||||
if (conversation) {
|
||||
await conversation.updateVerified();
|
||||
const unverifieds = conversation.getUnverified();
|
||||
if (unverifieds.length) {
|
||||
unverifieds.forEach(unverifiedConversation =>
|
||||
unverifiedContacts.push(unverifiedConversation)
|
||||
);
|
||||
}
|
||||
|
||||
const untrusted = conversation.getUntrusted();
|
||||
if (untrusted.length) {
|
||||
untrusted.forEach(untrustedConversation =>
|
||||
untrustedContacts.push(untrustedConversation)
|
||||
);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
// to are verified and trusted.
|
||||
// If there are any unverified or untrusted contacts, show the
|
||||
// SendAnywayDialog and if we're fine with sending then mark all as
|
||||
// verified and trusted and continue the send.
|
||||
const iffyConversations = [...unverifiedContacts, ...untrustedContacts];
|
||||
if (iffyConversations.length) {
|
||||
const forwardMessageModal = document.querySelector<HTMLElement>(
|
||||
'.module-ForwardMessageModal'
|
||||
);
|
||||
if (forwardMessageModal) {
|
||||
forwardMessageModal.style.display = 'none';
|
||||
}
|
||||
const sendAnyway = await new Promise(resolve => {
|
||||
showSafetyNumberChangeDialog({
|
||||
contacts: iffyConversations,
|
||||
reject: () => {
|
||||
resolve(false);
|
||||
},
|
||||
resolve: () => {
|
||||
resolve(true);
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
if (!sendAnyway) {
|
||||
if (forwardMessageModal) {
|
||||
forwardMessageModal.style.display = 'block';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
let verifyPromise: Promise<void> | undefined;
|
||||
let approvePromise: Promise<void> | undefined;
|
||||
if (unverifiedContacts.length) {
|
||||
verifyPromise = markAllAsVerifiedDefault(unverifiedContacts);
|
||||
}
|
||||
if (untrustedContacts.length) {
|
||||
approvePromise = markAllAsApproved(untrustedContacts);
|
||||
}
|
||||
await Promise.all([verifyPromise, approvePromise]);
|
||||
const canSend = await blockSendUntilConversationsAreVerified(
|
||||
conversations,
|
||||
SafetyNumberChangeSource.MessageSend
|
||||
);
|
||||
if (!canSend) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const sendMessageOptions = { dontClearDraft: true };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue