2023-11-17 18:16:48 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { isNumber, sortBy } from 'lodash';
|
|
|
|
|
|
|
|
import { strictAssert } from './assert';
|
|
|
|
|
2023-12-08 20:35:31 +00:00
|
|
|
import type { EditHistoryType, MessageAttributesType } from '../model-types';
|
2023-11-17 18:16:48 +00:00
|
|
|
import type { LoggerType } from '../types/Logging';
|
|
|
|
|
|
|
|
// The tricky bit for this function is if we are on our second+ attempt to send a given
|
|
|
|
// edit, we're still sending that edit.
|
|
|
|
export function getTargetOfThisEditTimestamp({
|
|
|
|
message,
|
|
|
|
targetTimestamp,
|
|
|
|
}: {
|
2023-12-16 01:43:31 +00:00
|
|
|
message: MessageAttributesType;
|
2023-11-17 18:16:48 +00:00
|
|
|
targetTimestamp: number;
|
|
|
|
}): number {
|
2023-12-16 01:43:31 +00:00
|
|
|
const { timestamp: originalTimestamp, editHistory } = message;
|
|
|
|
if (!editHistory) {
|
|
|
|
return originalTimestamp;
|
|
|
|
}
|
2023-11-17 18:16:48 +00:00
|
|
|
|
|
|
|
const sentItems = editHistory.filter(item => {
|
|
|
|
return item.timestamp <= targetTimestamp;
|
|
|
|
});
|
|
|
|
const mostRecent = sortBy(
|
|
|
|
sentItems,
|
|
|
|
(item: EditHistoryType) => item.timestamp
|
|
|
|
);
|
|
|
|
|
|
|
|
const { length } = mostRecent;
|
|
|
|
|
|
|
|
// We want the second-to-last item, because we may have partially sent targetTimestamp
|
|
|
|
if (length > 1) {
|
|
|
|
return mostRecent[length - 2].timestamp;
|
|
|
|
}
|
|
|
|
// If there's only one item, we'll use it
|
|
|
|
if (length > 0) {
|
|
|
|
return mostRecent[length - 1].timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a good failover in case we ever stop duplicating data in editHistory
|
|
|
|
return originalTimestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPropForTimestamp<T extends keyof EditHistoryType>({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop,
|
|
|
|
targetTimestamp,
|
|
|
|
}: {
|
|
|
|
log: LoggerType;
|
2023-12-16 01:43:31 +00:00
|
|
|
message: MessageAttributesType;
|
2023-11-17 18:16:48 +00:00
|
|
|
prop: T;
|
|
|
|
targetTimestamp: number;
|
|
|
|
}): EditHistoryType[T] {
|
2023-12-16 01:43:31 +00:00
|
|
|
const logId = `getPropForTimestamp(${targetTimestamp}})`;
|
2023-12-08 20:35:31 +00:00
|
|
|
|
2023-12-16 01:43:31 +00:00
|
|
|
const { editHistory } = message;
|
2023-11-17 18:16:48 +00:00
|
|
|
const targetEdit = editHistory?.find(
|
|
|
|
item => item.timestamp === targetTimestamp
|
|
|
|
);
|
|
|
|
if (!targetEdit) {
|
|
|
|
if (editHistory) {
|
|
|
|
log.warn(`${logId}: No edit found, using top-level data`);
|
|
|
|
}
|
2023-12-16 01:43:31 +00:00
|
|
|
return message[prop];
|
2023-11-17 18:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return targetEdit[prop];
|
|
|
|
}
|
|
|
|
|
2023-12-16 01:43:31 +00:00
|
|
|
export function getChangesForPropAtTimestamp<T extends keyof EditHistoryType>({
|
2023-11-17 18:16:48 +00:00
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop,
|
|
|
|
targetTimestamp,
|
|
|
|
value,
|
|
|
|
}: {
|
|
|
|
log: LoggerType;
|
2023-12-16 01:43:31 +00:00
|
|
|
message: MessageAttributesType;
|
2023-11-17 18:16:48 +00:00
|
|
|
prop: T;
|
|
|
|
targetTimestamp: number;
|
|
|
|
value: EditHistoryType[T];
|
2023-12-16 01:43:31 +00:00
|
|
|
}): Partial<MessageAttributesType> | undefined {
|
|
|
|
const logId = `getChangesForPropAtTimestamp(${targetTimestamp})`;
|
2023-11-17 18:16:48 +00:00
|
|
|
|
2023-12-16 01:43:31 +00:00
|
|
|
const { editHistory } = message;
|
|
|
|
let partialProps: Partial<MessageAttributesType> | undefined;
|
|
|
|
|
|
|
|
if (editHistory) {
|
|
|
|
const targetEditIndex = editHistory.findIndex(
|
|
|
|
item => item.timestamp === targetTimestamp
|
|
|
|
);
|
|
|
|
const targetEdit = isNumber(targetEditIndex)
|
2023-11-17 18:16:48 +00:00
|
|
|
? editHistory[targetEditIndex]
|
|
|
|
: undefined;
|
|
|
|
|
2023-12-16 01:43:31 +00:00
|
|
|
if (!targetEdit) {
|
|
|
|
if (editHistory) {
|
|
|
|
log.warn(`${logId}: No edit found, updating top-level data`);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
[prop]: value,
|
|
|
|
};
|
2023-11-17 18:16:48 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 01:43:31 +00:00
|
|
|
strictAssert(
|
|
|
|
isNumber(targetEditIndex),
|
|
|
|
'Got targetEdit, but no targetEditIndex'
|
|
|
|
);
|
2023-11-17 18:16:48 +00:00
|
|
|
|
2023-12-16 01:43:31 +00:00
|
|
|
const newEditHistory = [...editHistory];
|
|
|
|
newEditHistory[targetEditIndex] = { ...targetEdit, [prop]: value };
|
2023-11-17 18:16:48 +00:00
|
|
|
|
2023-12-16 01:43:31 +00:00
|
|
|
partialProps = {
|
|
|
|
editHistory: newEditHistory,
|
|
|
|
};
|
|
|
|
}
|
2023-11-17 18:16:48 +00:00
|
|
|
|
|
|
|
// We always edit the top-level attribute if this is the most recent send
|
2023-12-16 01:43:31 +00:00
|
|
|
const { editMessageTimestamp } = message;
|
|
|
|
if (
|
|
|
|
!editHistory ||
|
|
|
|
!editMessageTimestamp ||
|
|
|
|
editMessageTimestamp === targetTimestamp
|
|
|
|
) {
|
|
|
|
partialProps = {
|
|
|
|
...partialProps,
|
2023-11-17 18:16:48 +00:00
|
|
|
[prop]: value,
|
2023-12-16 01:43:31 +00:00
|
|
|
};
|
2023-11-17 18:16:48 +00:00
|
|
|
}
|
2023-12-16 01:43:31 +00:00
|
|
|
|
|
|
|
return partialProps;
|
2023-11-17 18:16:48 +00:00
|
|
|
}
|