Do not confirm messages until we have handled them

This commit is contained in:
Josh Perez 2023-08-21 16:08:27 -04:00 committed by GitHub
parent 29aa188c0f
commit 04f716986c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 990 additions and 960 deletions

View file

@ -5,7 +5,6 @@ import type { MessageAttributesType } from '../model-types.d';
import * as Errors from '../types/errors';
import * as log from '../logging/log';
import { drop } from '../util/drop';
import { filter, size } from '../util/iterables';
import { getContactId } from '../messages/helpers';
import { handleEditMessage } from '../util/handleEditMessage';
import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp';
@ -22,6 +21,11 @@ export type EditAttributesType = {
const edits = new Map<string, EditAttributesType>();
function remove(edit: EditAttributesType): void {
edits.delete(edit.envelopeId);
edit.removeFromMessageReceiverCache();
}
export function forMessage(
messageAttributes: Pick<
MessageAttributesType,
@ -34,22 +38,22 @@ export function forMessage(
>
): Array<EditAttributesType> {
const sentAt = getMessageSentTimestamp(messageAttributes, { log });
const matchingEdits = filter(edits, ([_envelopeId, item]) => {
const editValues = Array.from(edits.values());
const matchingEdits = editValues.filter(item => {
return (
item.targetSentTimestamp === sentAt &&
item.fromId === getContactId(messageAttributes)
);
});
if (size(matchingEdits) > 0) {
const result: Array<EditAttributesType> = [];
if (matchingEdits.length > 0) {
const editsLogIds: Array<number> = [];
Array.from(matchingEdits).forEach(([envelopeId, item]) => {
result.push(item);
const result = matchingEdits.map(item => {
editsLogIds.push(item.message.sent_at);
edits.delete(envelopeId);
item.removeFromMessageReceiverCache();
remove(item);
return item;
});
log.info(
@ -99,7 +103,6 @@ export async function onEdit(edit: EditAttributesType): Promise<void> {
if (!targetMessage) {
log.info(`${logId}: No message`);
return;
}
@ -110,11 +113,11 @@ export async function onEdit(edit: EditAttributesType): Promise<void> {
await handleEditMessage(message.attributes, edit);
edits.delete(edit.envelopeId);
edit.removeFromMessageReceiverCache();
remove(edit);
})
);
} catch (error) {
remove(edit);
log.error(`${logId} error:`, Errors.toLogFormat(error));
}
}