2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-07-19 22:44:49 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-02-27 16:01:25 +00:00
|
|
|
import memoizee from 'memoizee';
|
2021-07-19 22:44:49 +00:00
|
|
|
import { makeEnumParser } from '../util/enum';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `SendStatus` represents the send status of a message to a single recipient. For
|
|
|
|
* example, if a message is sent to 5 people, there would be 5 `SendStatus`es.
|
|
|
|
*
|
|
|
|
* Under normal conditions, the status will go down this list, in order:
|
|
|
|
*
|
|
|
|
* 1. `Pending`; the message has not been sent, and we are continuing to try
|
|
|
|
* 2. `Sent`; the message has been delivered to the server
|
|
|
|
* 3. `Delivered`; we've received a delivery receipt
|
|
|
|
* 4. `Read`; we've received a read receipt (not applicable if the recipient has disabled
|
|
|
|
* sending these receipts)
|
|
|
|
* 5. `Viewed`; we've received a viewed receipt (not applicable for all message types, or
|
|
|
|
* if the recipient has disabled sending these receipts)
|
|
|
|
*
|
|
|
|
* There's also a `Failed` state, which represents an error we don't want to recover from.
|
|
|
|
*
|
|
|
|
* There are some unusual cases where messages don't follow this pattern. For example, if
|
|
|
|
* we receive a read receipt before we receive a delivery receipt, we might skip the
|
|
|
|
* Delivered state. However, we should never go "backwards".
|
|
|
|
*
|
|
|
|
* Be careful when changing these values, as they are persisted.
|
|
|
|
*/
|
|
|
|
export enum SendStatus {
|
|
|
|
Failed = 'Failed',
|
|
|
|
Pending = 'Pending',
|
|
|
|
Sent = 'Sent',
|
|
|
|
Delivered = 'Delivered',
|
|
|
|
Read = 'Read',
|
|
|
|
Viewed = 'Viewed',
|
2024-09-12 23:48:27 +00:00
|
|
|
Skipped = 'Skipped',
|
2021-07-19 22:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const parseMessageSendStatus = makeEnumParser(
|
|
|
|
SendStatus,
|
|
|
|
SendStatus.Pending
|
|
|
|
);
|
|
|
|
|
2023-12-08 20:35:31 +00:00
|
|
|
export const UNDELIVERED_SEND_STATUSES = [
|
|
|
|
SendStatus.Pending,
|
|
|
|
SendStatus.Failed,
|
|
|
|
];
|
|
|
|
|
2024-09-12 23:48:27 +00:00
|
|
|
export type VisibleSendStatus =
|
|
|
|
| SendStatus.Failed
|
|
|
|
| SendStatus.Pending
|
|
|
|
| SendStatus.Sent
|
|
|
|
| SendStatus.Delivered
|
|
|
|
| SendStatus.Read
|
|
|
|
| SendStatus.Viewed;
|
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
const STATUS_NUMBERS: Record<SendStatus, number> = {
|
|
|
|
[SendStatus.Failed]: 0,
|
|
|
|
[SendStatus.Pending]: 1,
|
|
|
|
[SendStatus.Sent]: 2,
|
|
|
|
[SendStatus.Delivered]: 3,
|
|
|
|
[SendStatus.Read]: 4,
|
|
|
|
[SendStatus.Viewed]: 5,
|
2024-09-12 23:48:27 +00:00
|
|
|
[SendStatus.Skipped]: 6,
|
2021-07-19 22:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const maxStatus = (a: SendStatus, b: SendStatus): SendStatus =>
|
|
|
|
STATUS_NUMBERS[a] > STATUS_NUMBERS[b] ? a : b;
|
|
|
|
|
2022-10-14 21:04:26 +00:00
|
|
|
export const isPending = (status: SendStatus): boolean =>
|
|
|
|
status === SendStatus.Pending;
|
2021-07-27 15:42:25 +00:00
|
|
|
export const isViewed = (status: SendStatus): boolean =>
|
|
|
|
status === SendStatus.Viewed;
|
2021-07-19 22:44:49 +00:00
|
|
|
export const isRead = (status: SendStatus): boolean =>
|
|
|
|
STATUS_NUMBERS[status] >= STATUS_NUMBERS[SendStatus.Read];
|
|
|
|
export const isDelivered = (status: SendStatus): boolean =>
|
|
|
|
STATUS_NUMBERS[status] >= STATUS_NUMBERS[SendStatus.Delivered];
|
|
|
|
export const isSent = (status: SendStatus): boolean =>
|
|
|
|
STATUS_NUMBERS[status] >= STATUS_NUMBERS[SendStatus.Sent];
|
2021-08-31 20:58:39 +00:00
|
|
|
export const isFailed = (status: SendStatus): boolean =>
|
|
|
|
status === SendStatus.Failed;
|
2024-09-12 23:48:27 +00:00
|
|
|
export const isSkipped = (status: SendStatus): boolean =>
|
|
|
|
status === SendStatus.Skipped;
|
2021-07-19 22:44:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* `SendState` combines `SendStatus` and a timestamp. You can use it to show things to the
|
|
|
|
* user such as "this message was delivered at 6:09pm".
|
|
|
|
*
|
|
|
|
* The timestamp may be undefined if reading old data, which did not store a timestamp.
|
|
|
|
*/
|
|
|
|
export type SendState = Readonly<{
|
2022-08-02 19:31:55 +00:00
|
|
|
// When sending a story to multiple distribution lists at once, we need to
|
|
|
|
// de-duplicate the recipients. The story should only be sent once to each
|
|
|
|
// recipient in the list so the recipient only sees it rendered once.
|
|
|
|
isAlreadyIncludedInAnotherDistributionList?: boolean;
|
2022-07-01 00:52:03 +00:00
|
|
|
isAllowedToReplyToStory?: boolean;
|
2021-07-19 22:44:49 +00:00
|
|
|
status:
|
|
|
|
| SendStatus.Pending
|
|
|
|
| SendStatus.Failed
|
|
|
|
| SendStatus.Sent
|
|
|
|
| SendStatus.Delivered
|
|
|
|
| SendStatus.Read
|
2024-09-12 23:48:27 +00:00
|
|
|
| SendStatus.Viewed
|
|
|
|
| SendStatus.Skipped;
|
2021-07-19 22:44:49 +00:00
|
|
|
updatedAt?: number;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The reducer advances the little `SendState` state machine. It mostly follows the steps
|
|
|
|
* in the `SendStatus` documentation above, but it also handles edge cases.
|
|
|
|
*/
|
|
|
|
export function sendStateReducer(
|
|
|
|
state: Readonly<SendState>,
|
|
|
|
action: Readonly<SendAction>
|
|
|
|
): SendState {
|
|
|
|
const oldStatus = state.status;
|
|
|
|
let newStatus: SendStatus;
|
|
|
|
|
|
|
|
if (
|
|
|
|
oldStatus === SendStatus.Pending &&
|
|
|
|
action.type === SendActionType.Failed
|
|
|
|
) {
|
|
|
|
newStatus = SendStatus.Failed;
|
|
|
|
} else {
|
|
|
|
newStatus = maxStatus(oldStatus, STATE_TRANSITIONS[action.type]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newStatus === oldStatus
|
|
|
|
? state
|
|
|
|
: {
|
2022-11-10 03:52:53 +00:00
|
|
|
...state,
|
2021-07-19 22:44:49 +00:00
|
|
|
status: newStatus,
|
|
|
|
updatedAt: action.updatedAt,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum SendActionType {
|
|
|
|
Failed,
|
|
|
|
ManuallyRetried,
|
|
|
|
Sent,
|
|
|
|
GotDeliveryReceipt,
|
|
|
|
GotReadReceipt,
|
|
|
|
GotViewedReceipt,
|
|
|
|
}
|
|
|
|
|
|
|
|
export type SendAction = Readonly<{
|
|
|
|
type:
|
|
|
|
| SendActionType.Failed
|
|
|
|
| SendActionType.ManuallyRetried
|
|
|
|
| SendActionType.Sent
|
|
|
|
| SendActionType.GotDeliveryReceipt
|
|
|
|
| SendActionType.GotReadReceipt
|
|
|
|
| SendActionType.GotViewedReceipt;
|
|
|
|
// `updatedAt?: number` makes it easier to forget the property. With this type, you have
|
|
|
|
// to explicitly say it's missing.
|
|
|
|
updatedAt: undefined | number;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
const STATE_TRANSITIONS: Record<SendActionType, SendStatus> = {
|
|
|
|
[SendActionType.Failed]: SendStatus.Failed,
|
|
|
|
[SendActionType.ManuallyRetried]: SendStatus.Pending,
|
|
|
|
[SendActionType.Sent]: SendStatus.Sent,
|
|
|
|
[SendActionType.GotDeliveryReceipt]: SendStatus.Delivered,
|
|
|
|
[SendActionType.GotReadReceipt]: SendStatus.Read,
|
|
|
|
[SendActionType.GotViewedReceipt]: SendStatus.Viewed,
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SendStateByConversationId = Record<string, SendState>;
|
|
|
|
|
2024-02-27 16:01:25 +00:00
|
|
|
/** Test all of sendStateByConversationId for predicate */
|
2021-07-19 22:44:49 +00:00
|
|
|
export const someSendStatus = (
|
2024-02-27 16:01:25 +00:00
|
|
|
sendStateByConversationId: SendStateByConversationId,
|
2021-07-19 22:44:49 +00:00
|
|
|
predicate: (value: SendStatus) => boolean
|
2024-02-27 16:01:25 +00:00
|
|
|
): boolean => {
|
|
|
|
return [
|
|
|
|
...summarizeMessageSendStatuses(sendStateByConversationId).statuses,
|
|
|
|
].some(predicate);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Test sendStateByConversationId, excluding ourConversationId, for predicate */
|
|
|
|
export const someRecipientSendStatus = (
|
|
|
|
sendStateByConversationId: SendStateByConversationId,
|
|
|
|
ourConversationId: string | undefined,
|
|
|
|
predicate: (value: SendStatus) => boolean
|
|
|
|
): boolean => {
|
|
|
|
return getStatusesIgnoringOurConversationId(
|
|
|
|
sendStateByConversationId,
|
|
|
|
ourConversationId
|
|
|
|
).some(predicate);
|
|
|
|
};
|
2021-07-19 22:44:49 +00:00
|
|
|
|
|
|
|
export const isMessageJustForMe = (
|
2024-02-27 16:01:25 +00:00
|
|
|
sendStateByConversationId: SendStateByConversationId,
|
2022-02-23 18:48:40 +00:00
|
|
|
ourConversationId: string | undefined
|
2021-07-19 22:44:49 +00:00
|
|
|
): boolean => {
|
2024-02-27 16:01:25 +00:00
|
|
|
const { length } = summarizeMessageSendStatuses(sendStateByConversationId);
|
|
|
|
|
|
|
|
return (
|
|
|
|
ourConversationId !== undefined &&
|
|
|
|
length === 1 &&
|
|
|
|
Object.hasOwn(sendStateByConversationId, ourConversationId)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getHighestSuccessfulRecipientStatus = (
|
|
|
|
sendStateByConversationId: SendStateByConversationId,
|
|
|
|
ourConversationId: string | undefined
|
|
|
|
): SendStatus => {
|
|
|
|
return getStatusesIgnoringOurConversationId(
|
|
|
|
sendStateByConversationId,
|
|
|
|
ourConversationId
|
|
|
|
).reduce(
|
|
|
|
(result: SendStatus, status) => maxStatus(result, status),
|
|
|
|
SendStatus.Pending
|
2021-07-19 22:44:49 +00:00
|
|
|
);
|
|
|
|
};
|
2024-02-27 16:01:25 +00:00
|
|
|
|
|
|
|
const getStatusesIgnoringOurConversationId = (
|
|
|
|
sendStateByConversationId: SendStateByConversationId,
|
|
|
|
ourConversationId: string | undefined
|
|
|
|
): Array<SendStatus> => {
|
|
|
|
const { statuses, statusesWithOnlyOneConversationId } =
|
|
|
|
summarizeMessageSendStatuses(sendStateByConversationId);
|
|
|
|
|
|
|
|
const statusesIgnoringOurConversationId = [];
|
|
|
|
|
|
|
|
for (const status of statuses) {
|
|
|
|
if (
|
|
|
|
ourConversationId &&
|
|
|
|
statusesWithOnlyOneConversationId.get(status) === ourConversationId
|
|
|
|
) {
|
|
|
|
// ignore this status; it only applies to us
|
|
|
|
} else {
|
|
|
|
statusesIgnoringOurConversationId.push(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return statusesIgnoringOurConversationId;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Looping through each value in sendStateByConversationId can be quite slow, especially
|
|
|
|
// if sendStateByConversationId is large (e.g. in a large group) and if it is actually a
|
|
|
|
// proxy (e.g. being called via useProxySelector) -- that's why we memoize it here.
|
|
|
|
const summarizeMessageSendStatuses = memoizee(
|
|
|
|
(
|
|
|
|
sendStateByConversationId: SendStateByConversationId
|
|
|
|
): {
|
|
|
|
statuses: Set<SendStatus>;
|
|
|
|
statusesWithOnlyOneConversationId: Map<SendStatus, string>;
|
|
|
|
length: number;
|
|
|
|
} => {
|
|
|
|
const statuses: Set<SendStatus> = new Set();
|
|
|
|
|
|
|
|
// We keep track of statuses with only one conversationId associated with it
|
|
|
|
// so that we can ignore a status if it is only for ourConversationId, as needed
|
|
|
|
const statusesWithOnlyOneConversationId: Map<SendStatus, string> =
|
|
|
|
new Map();
|
|
|
|
|
|
|
|
const entries = Object.entries(sendStateByConversationId);
|
|
|
|
|
|
|
|
for (const [conversationId, { status }] of entries) {
|
|
|
|
if (!statuses.has(status)) {
|
|
|
|
statuses.add(status);
|
|
|
|
statusesWithOnlyOneConversationId.set(status, conversationId);
|
|
|
|
} else {
|
|
|
|
statusesWithOnlyOneConversationId.delete(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
statuses,
|
|
|
|
statusesWithOnlyOneConversationId,
|
|
|
|
length: entries.length,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
{ max: 100 }
|
|
|
|
);
|