Refactor outbound delivery state

This commit is contained in:
Evan Hahn 2021-07-09 16:38:51 -05:00 committed by GitHub
parent 831ec98418
commit 9c48a95eb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 3200 additions and 697 deletions

View file

@ -1,7 +1,7 @@
// Copyright 2020-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { isNumber, noop } from 'lodash';
import { has, isNumber, noop } from 'lodash';
import { bindActionCreators } from 'redux';
import { render } from 'react-dom';
import {
@ -82,6 +82,10 @@ import { Reactions } from './messageModifiers/Reactions';
import { ReadReceipts } from './messageModifiers/ReadReceipts';
import { ReadSyncs } from './messageModifiers/ReadSyncs';
import { ViewSyncs } from './messageModifiers/ViewSyncs';
import {
SendStateByConversationId,
SendStatus,
} from './messages/MessageSendState';
import * as AttachmentDownloads from './messageModifiers/AttachmentDownloads';
import {
SystemTraySetting,
@ -3151,15 +3155,39 @@ export async function startApp(): Promise<void> {
const now = Date.now();
const timestamp = data.timestamp || now;
const ourId = window.ConversationController.getOurConversationIdOrThrow();
const { unidentifiedStatus = [] } = data;
let sentTo: Array<string> = [];
const sendStateByConversationId: SendStateByConversationId = unidentifiedStatus.reduce(
(result: SendStateByConversationId, { destinationUuid, destination }) => {
const conversationId = window.ConversationController.ensureContactIds({
uuid: destinationUuid,
e164: destination,
highTrust: true,
});
if (!conversationId || conversationId === ourId) {
return result;
}
return {
...result,
[conversationId]: {
status: SendStatus.Pending,
updatedAt: timestamp,
},
};
},
{
[ourId]: {
status: SendStatus.Sent,
updatedAt: timestamp,
},
}
);
let unidentifiedDeliveries: Array<string> = [];
if (unidentifiedStatus.length) {
sentTo = unidentifiedStatus
.map(item => item.destinationUuid || item.destination)
.filter(isNotNil);
const unidentified = window._.filter(data.unidentifiedStatus, item =>
Boolean(item.unidentified)
);
@ -3174,13 +3202,12 @@ export async function startApp(): Promise<void> {
sourceDevice: data.device,
sent_at: timestamp,
serverTimestamp: data.serverTimestamp,
sent_to: sentTo,
received_at: data.receivedAtCounter,
received_at_ms: data.receivedAtDate,
conversationId: descriptor.id,
timestamp,
type: 'outgoing',
sent: true,
sendStateByConversationId,
unidentifiedDeliveries,
expirationStartTimestamp: Math.min(
data.expirationStartTimestamp || timestamp,
@ -3559,33 +3586,6 @@ export async function startApp(): Promise<void> {
window.log.warn('background onError: Doing nothing with incoming error');
}
function isInList(
conversation: ConversationModel,
list: Array<string | undefined | null> | undefined
): boolean {
const uuid = conversation.get('uuid');
const e164 = conversation.get('e164');
const id = conversation.get('id');
if (!list) {
return false;
}
if (list.includes(id)) {
return true;
}
if (uuid && list.includes(uuid)) {
return true;
}
if (e164 && list.includes(e164)) {
return true;
}
return false;
}
async function archiveSessionOnMatch({
requesterUuid,
requesterDevice,
@ -3707,11 +3707,9 @@ export async function startApp(): Promise<void> {
return false;
}
if (!isInList(requesterConversation, message.get('sent_to'))) {
return false;
}
return true;
const sendStateByConversationId =
message.get('sendStateByConversationId') || {};
return has(sendStateByConversationId, requesterConversation.id);
});
if (!targetMessage) {