2021-12-10 22:51:54 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-07-24 20:14:11 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
import * as log from '../logging/log';
|
|
|
|
import type { ConversationModel } from '../models/conversations';
|
|
|
|
import type {
|
|
|
|
CustomError,
|
2024-07-24 20:14:11 +00:00
|
|
|
ReadonlyMessageAttributesType,
|
2024-06-10 18:44:15 +00:00
|
|
|
QuotedAttachmentType,
|
2021-12-10 22:51:54 +00:00
|
|
|
QuotedMessageType,
|
|
|
|
} from '../model-types.d';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { ServiceIdString } from '../types/ServiceId';
|
2022-11-30 21:47:54 +00:00
|
|
|
import { PaymentEventKind } from '../types/Payment';
|
|
|
|
import type { AnyPaymentEvent } from '../types/Payment';
|
|
|
|
import type { LocalizerType } from '../types/Util';
|
|
|
|
import { missingCaseError } from '../util/missingCaseError';
|
2022-07-18 20:01:43 +00:00
|
|
|
|
|
|
|
export function isIncoming(
|
2024-07-24 20:14:11 +00:00
|
|
|
message: Pick<ReadonlyMessageAttributesType, 'type'>
|
2022-07-18 20:01:43 +00:00
|
|
|
): boolean {
|
|
|
|
return message.type === 'incoming';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isOutgoing(
|
2024-07-24 20:14:11 +00:00
|
|
|
message: Pick<ReadonlyMessageAttributesType, 'type'>
|
2022-07-18 20:01:43 +00:00
|
|
|
): boolean {
|
|
|
|
return message.type === 'outgoing';
|
|
|
|
}
|
|
|
|
|
2024-07-24 20:14:11 +00:00
|
|
|
export function isStory(
|
|
|
|
message: Pick<ReadonlyMessageAttributesType, 'type'>
|
|
|
|
): boolean {
|
2022-07-18 20:01:43 +00:00
|
|
|
return message.type === 'story';
|
|
|
|
}
|
2021-12-10 22:51:54 +00:00
|
|
|
|
2024-07-24 20:14:11 +00:00
|
|
|
export type MessageAttributesWithPaymentEvent = ReadonlyMessageAttributesType &
|
|
|
|
ReadonlyDeep<{
|
|
|
|
payment: AnyPaymentEvent;
|
|
|
|
}>;
|
2022-11-30 21:47:54 +00:00
|
|
|
|
|
|
|
export function messageHasPaymentEvent(
|
2024-07-24 20:14:11 +00:00
|
|
|
message: ReadonlyMessageAttributesType
|
2022-11-30 21:47:54 +00:00
|
|
|
): message is MessageAttributesWithPaymentEvent {
|
|
|
|
return message.payment != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPaymentEventNotificationText(
|
2024-07-24 20:14:11 +00:00
|
|
|
payment: ReadonlyDeep<AnyPaymentEvent>,
|
2022-11-30 21:47:54 +00:00
|
|
|
senderTitle: string,
|
|
|
|
conversationTitle: string | null,
|
|
|
|
senderIsMe: boolean,
|
|
|
|
i18n: LocalizerType
|
|
|
|
): string {
|
|
|
|
if (payment.kind === PaymentEventKind.Notification) {
|
|
|
|
return i18n('icu:payment-event-notification-label');
|
|
|
|
}
|
|
|
|
return getPaymentEventDescription(
|
|
|
|
payment,
|
|
|
|
senderTitle,
|
|
|
|
conversationTitle,
|
|
|
|
senderIsMe,
|
|
|
|
i18n
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPaymentEventDescription(
|
2024-07-24 20:14:11 +00:00
|
|
|
payment: ReadonlyDeep<AnyPaymentEvent>,
|
2022-11-30 21:47:54 +00:00
|
|
|
senderTitle: string,
|
|
|
|
conversationTitle: string | null,
|
|
|
|
senderIsMe: boolean,
|
|
|
|
i18n: LocalizerType
|
|
|
|
): string {
|
|
|
|
const { kind } = payment;
|
|
|
|
if (kind === PaymentEventKind.Notification) {
|
|
|
|
if (senderIsMe) {
|
|
|
|
if (conversationTitle != null) {
|
|
|
|
return i18n('icu:payment-event-notification-message-you-label', {
|
|
|
|
receiver: conversationTitle,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return i18n(
|
|
|
|
'icu:payment-event-notification-message-you-label-without-receiver'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return i18n('icu:payment-event-notification-message-label', {
|
|
|
|
sender: senderTitle,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (kind === PaymentEventKind.ActivationRequest) {
|
|
|
|
if (senderIsMe) {
|
|
|
|
if (conversationTitle != null) {
|
|
|
|
return i18n('icu:payment-event-activation-request-you-label', {
|
|
|
|
receiver: conversationTitle,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return i18n(
|
|
|
|
'icu:payment-event-activation-request-you-label-without-receiver'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return i18n('icu:payment-event-activation-request-label', {
|
|
|
|
sender: senderTitle,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (kind === PaymentEventKind.Activation) {
|
|
|
|
if (senderIsMe) {
|
|
|
|
return i18n('icu:payment-event-activated-you-label');
|
|
|
|
}
|
|
|
|
return i18n('icu:payment-event-activated-label', {
|
|
|
|
sender: senderTitle,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
throw missingCaseError(kind);
|
|
|
|
}
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
export function isQuoteAMatch(
|
2024-07-24 20:14:11 +00:00
|
|
|
message: ReadonlyMessageAttributesType | null | undefined,
|
2021-12-10 22:51:54 +00:00
|
|
|
conversationId: string,
|
2024-07-24 20:14:11 +00:00
|
|
|
quote: ReadonlyDeep<Pick<QuotedMessageType, 'id' | 'authorAci' | 'author'>>
|
|
|
|
): message is ReadonlyMessageAttributesType {
|
2021-12-10 22:51:54 +00:00
|
|
|
if (!message) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const { authorAci, id } = quote;
|
2021-12-10 22:51:54 +00:00
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
const isSameTimestamp =
|
|
|
|
message.sent_at === id ||
|
|
|
|
message.editHistory?.some(({ timestamp }) => timestamp === id) ||
|
|
|
|
false;
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
return (
|
2023-04-20 16:31:59 +00:00
|
|
|
isSameTimestamp &&
|
2021-12-10 22:51:54 +00:00
|
|
|
message.conversationId === conversationId &&
|
2024-09-09 23:29:19 +00:00
|
|
|
getSourceServiceId(message) === authorAci
|
2021-12-10 22:51:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:44:15 +00:00
|
|
|
export const shouldTryToCopyFromQuotedMessage = ({
|
|
|
|
referencedMessageNotFound,
|
|
|
|
quoteAttachment,
|
|
|
|
}: {
|
|
|
|
referencedMessageNotFound: boolean;
|
2024-07-24 20:14:11 +00:00
|
|
|
quoteAttachment: ReadonlyDeep<QuotedAttachmentType> | undefined;
|
2024-06-10 18:44:15 +00:00
|
|
|
}): boolean => {
|
|
|
|
// If we've tried and can't find the message, try again.
|
|
|
|
if (referencedMessageNotFound === true) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, try again in case we have not yet copied over the thumbnail from the
|
|
|
|
// original attachment (maybe it had not been downloaded when we first checked)
|
|
|
|
if (!quoteAttachment?.thumbnail) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (quoteAttachment.thumbnail.copied === true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
export function getAuthorId(
|
2024-07-24 20:14:11 +00:00
|
|
|
message: Pick<
|
|
|
|
ReadonlyMessageAttributesType,
|
|
|
|
'type' | 'source' | 'sourceServiceId'
|
|
|
|
>
|
2021-12-10 22:51:54 +00:00
|
|
|
): string | undefined {
|
|
|
|
const source = getSource(message);
|
2023-08-16 20:54:39 +00:00
|
|
|
const sourceServiceId = getSourceServiceId(message);
|
2021-12-10 22:51:54 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
if (!source && !sourceServiceId) {
|
2021-12-10 22:51:54 +00:00
|
|
|
return window.ConversationController.getOurConversationId();
|
|
|
|
}
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
const conversation = window.ConversationController.lookupOrCreate({
|
2021-12-10 22:51:54 +00:00
|
|
|
e164: source,
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: sourceServiceId,
|
2024-04-29 21:20:20 +00:00
|
|
|
reason: 'helpers.getAuthorId',
|
2021-12-10 22:51:54 +00:00
|
|
|
});
|
2022-08-09 21:39:00 +00:00
|
|
|
return conversation?.id;
|
2021-12-10 22:51:54 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 21:20:20 +00:00
|
|
|
export function getAuthor(
|
2024-07-24 20:14:11 +00:00
|
|
|
message: ReadonlyMessageAttributesType
|
2021-12-10 22:51:54 +00:00
|
|
|
): ConversationModel | undefined {
|
2024-04-29 21:20:20 +00:00
|
|
|
const id = getAuthorId(message);
|
2021-12-10 22:51:54 +00:00
|
|
|
return window.ConversationController.get(id);
|
|
|
|
}
|
|
|
|
|
2022-10-03 23:10:20 +00:00
|
|
|
export function getSource(
|
2024-07-24 20:14:11 +00:00
|
|
|
message: Pick<ReadonlyMessageAttributesType, 'type' | 'source'>
|
2022-10-03 23:10:20 +00:00
|
|
|
): string | undefined {
|
2022-03-04 21:14:52 +00:00
|
|
|
if (isIncoming(message) || isStory(message)) {
|
2021-12-10 22:51:54 +00:00
|
|
|
return message.source;
|
|
|
|
}
|
|
|
|
if (!isOutgoing(message)) {
|
|
|
|
log.warn('Message.getSource: Called for non-incoming/non-outgoing message');
|
|
|
|
}
|
|
|
|
|
|
|
|
return window.textsecure.storage.user.getNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSourceDevice(
|
2024-07-24 20:14:11 +00:00
|
|
|
message: Pick<ReadonlyMessageAttributesType, 'type' | 'sourceDevice'>
|
2021-12-10 22:51:54 +00:00
|
|
|
): string | number | undefined {
|
|
|
|
const { sourceDevice } = message;
|
|
|
|
|
2022-03-04 21:14:52 +00:00
|
|
|
if (isIncoming(message) || isStory(message)) {
|
2021-12-10 22:51:54 +00:00
|
|
|
return sourceDevice;
|
|
|
|
}
|
|
|
|
if (!isOutgoing(message)) {
|
|
|
|
log.warn(
|
|
|
|
'Message.getSourceDevice: Called for non-incoming/non-outgoing message'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sourceDevice || window.textsecure.storage.user.getDeviceId();
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
export function getSourceServiceId(
|
2024-07-24 20:14:11 +00:00
|
|
|
message: Pick<ReadonlyMessageAttributesType, 'type' | 'sourceServiceId'>
|
2023-08-10 16:43:33 +00:00
|
|
|
): ServiceIdString | undefined {
|
2022-03-04 21:14:52 +00:00
|
|
|
if (isIncoming(message) || isStory(message)) {
|
2023-08-16 20:54:39 +00:00
|
|
|
return message.sourceServiceId;
|
2021-12-10 22:51:54 +00:00
|
|
|
}
|
|
|
|
if (!isOutgoing(message)) {
|
|
|
|
log.warn(
|
2023-08-16 20:54:39 +00:00
|
|
|
'Message.getSourceServiceId: Called for non-incoming/non-outgoing message'
|
2021-12-10 22:51:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
return window.textsecure.storage.user.getAci();
|
2021-12-10 22:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const isCustomError = (e: unknown): e is CustomError =>
|
|
|
|
e instanceof Error;
|