2022-01-26 23:05:26 +00:00
|
|
|
// Copyright 2021-2022 Signal Messenger, LLC
|
2021-06-17 17:15:10 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-03-21 21:46:20 +00:00
|
|
|
import { identity, isEqual, isNumber, isObject, map, omit, pick } from 'lodash';
|
2021-08-11 23:06:20 +00:00
|
|
|
import { createSelectorCreator } from 'reselect';
|
2021-06-17 17:15:10 +00:00
|
|
|
import filesize from 'filesize';
|
2022-03-23 20:23:28 +00:00
|
|
|
import getDirection from 'direction';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2021-06-17 17:15:10 +00:00
|
|
|
LastMessageStatus,
|
2021-10-29 23:19:44 +00:00
|
|
|
MessageReactionType,
|
2021-06-17 17:15:10 +00:00
|
|
|
ShallowChallengeError,
|
|
|
|
} from '../../model-types.d';
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { TimelineItemType } from '../../components/conversation/TimelineItem';
|
|
|
|
import type { PropsData } from '../../components/conversation/Message';
|
2022-03-23 20:23:28 +00:00
|
|
|
import { TextDirection } from '../../components/conversation/Message';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { PropsData as TimerNotificationProps } from '../../components/conversation/TimerNotification';
|
|
|
|
import type { PropsData as ChangeNumberNotificationProps } from '../../components/conversation/ChangeNumberNotification';
|
|
|
|
import type { PropsData as SafetyNumberNotificationProps } from '../../components/conversation/SafetyNumberNotification';
|
|
|
|
import type { PropsData as VerificationNotificationProps } from '../../components/conversation/VerificationNotification';
|
|
|
|
import type { PropsDataType as GroupsV2Props } from '../../components/conversation/GroupV2Change';
|
|
|
|
import type { PropsDataType as GroupV1MigrationPropsType } from '../../components/conversation/GroupV1Migration';
|
|
|
|
import type { PropsDataType as DeliveryIssuePropsType } from '../../components/conversation/DeliveryIssueNotification';
|
|
|
|
import type {
|
2021-06-17 17:15:10 +00:00
|
|
|
PropsData as GroupNotificationProps,
|
|
|
|
ChangeType,
|
|
|
|
} from '../../components/conversation/GroupNotification';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { PropsType as ProfileChangeNotificationPropsType } from '../../components/conversation/ProfileChangeNotification';
|
|
|
|
import type { QuotedAttachmentType } from '../../components/conversation/Quote';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-06-30 17:00:02 +00:00
|
|
|
import { getDomain, isStickerPack } from '../../types/LinkPreview';
|
2021-10-26 22:59:08 +00:00
|
|
|
import type { UUIDStringType } from '../../types/UUID';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { EmbeddedContactType } from '../../types/EmbeddedContact';
|
|
|
|
import { embeddedContactSelector } from '../../types/EmbeddedContact';
|
|
|
|
import type { AssertProps, BodyRangesType } from '../../types/Util';
|
|
|
|
import type { LinkPreviewType } from '../../types/message/LinkPreviews';
|
2021-06-17 17:15:10 +00:00
|
|
|
import { CallMode } from '../../types/Calling';
|
2021-07-09 19:36:10 +00:00
|
|
|
import { SignalService as Proto } from '../../protobuf';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { AttachmentType } from '../../types/Attachment';
|
|
|
|
import { isVoiceMessage } from '../../types/Attachment';
|
2021-08-12 18:15:55 +00:00
|
|
|
import { ReadStatus } from '../../messages/MessageReadStatus';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { CallingNotificationType } from '../../util/callingNotification';
|
2021-08-11 16:23:21 +00:00
|
|
|
import { memoizeByRoot } from '../../util/memoizeByRoot';
|
2021-06-17 17:15:10 +00:00
|
|
|
import { missingCaseError } from '../../util/missingCaseError';
|
|
|
|
import { isNotNil } from '../../util/isNotNil';
|
2021-08-02 19:26:48 +00:00
|
|
|
import { isMoreRecentThan } from '../../util/timestamp';
|
2021-10-29 23:19:44 +00:00
|
|
|
import * as iterables from '../../util/iterables';
|
|
|
|
import { strictAssert } from '../../util/assert';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-11-11 23:45:47 +00:00
|
|
|
import type {
|
|
|
|
ConversationType,
|
|
|
|
MessageWithUIFieldsType,
|
|
|
|
} from '../ducks/conversations';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { AccountSelectorType } from './accounts';
|
|
|
|
import type { CallSelectorType, CallStateType } from './calling';
|
|
|
|
import type {
|
2021-06-17 17:15:10 +00:00
|
|
|
GetConversationByIdType,
|
2021-08-19 20:14:41 +00:00
|
|
|
ContactNameColorSelectorType,
|
2021-06-17 17:15:10 +00:00
|
|
|
} from './conversations';
|
2021-10-26 19:15:33 +00:00
|
|
|
import { isMissingRequiredProfileSharing } from './conversations';
|
2021-07-19 22:44:49 +00:00
|
|
|
import {
|
|
|
|
SendStatus,
|
|
|
|
isDelivered,
|
2021-08-31 20:58:39 +00:00
|
|
|
isFailed,
|
2021-07-19 22:44:49 +00:00
|
|
|
isMessageJustForMe,
|
|
|
|
isRead,
|
|
|
|
isSent,
|
2021-07-27 15:42:25 +00:00
|
|
|
isViewed,
|
2021-07-19 22:44:49 +00:00
|
|
|
maxStatus,
|
|
|
|
someSendStatus,
|
|
|
|
} from '../../messages/MessageSendState';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../../logging/log';
|
2021-12-04 02:10:03 +00:00
|
|
|
import { getConversationColorAttributes } from '../../util/getConversationColorAttributes';
|
2022-03-04 19:22:31 +00:00
|
|
|
import { DAY, HOUR } from '../../util/durations';
|
2022-05-10 19:02:21 +00:00
|
|
|
import { getStoryReplyText } from '../../util/getStoryReplyText';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2022-03-04 19:22:31 +00:00
|
|
|
const THREE_HOURS = 3 * HOUR;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
type FormattedContact = Partial<ConversationType> &
|
|
|
|
Pick<
|
|
|
|
ConversationType,
|
|
|
|
| 'acceptedMessageRequest'
|
|
|
|
| 'id'
|
|
|
|
| 'isMe'
|
|
|
|
| 'sharedGroupNames'
|
|
|
|
| 'title'
|
|
|
|
| 'type'
|
|
|
|
| 'unblurredAvatarPath'
|
|
|
|
>;
|
|
|
|
type PropsForMessage = Omit<PropsData, 'interactionMode'>;
|
|
|
|
type PropsForUnsupportedMessage = {
|
|
|
|
canProcessNow: boolean;
|
|
|
|
contact: FormattedContact;
|
|
|
|
};
|
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
export type GetPropsForBubbleOptions = Readonly<{
|
|
|
|
conversationSelector: GetConversationByIdType;
|
2022-02-23 18:48:40 +00:00
|
|
|
ourConversationId?: string;
|
2021-08-11 16:23:21 +00:00
|
|
|
ourNumber?: string;
|
2022-02-23 18:48:40 +00:00
|
|
|
ourUuid?: UUIDStringType;
|
2021-08-11 16:23:21 +00:00
|
|
|
selectedMessageId?: string;
|
|
|
|
selectedMessageCounter?: number;
|
2022-02-23 18:48:40 +00:00
|
|
|
regionCode?: string;
|
2021-08-11 16:23:21 +00:00
|
|
|
callSelector: CallSelectorType;
|
|
|
|
activeCall?: CallStateType;
|
2021-08-19 20:14:41 +00:00
|
|
|
accountSelector: AccountSelectorType;
|
|
|
|
contactNameColorSelector: ContactNameColorSelectorType;
|
2021-08-11 16:23:21 +00:00
|
|
|
}>;
|
|
|
|
|
2021-06-30 00:51:57 +00:00
|
|
|
export function isIncoming(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: Pick<MessageWithUIFieldsType, 'type'>
|
2021-06-30 00:51:57 +00:00
|
|
|
): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return message.type === 'incoming';
|
|
|
|
}
|
|
|
|
|
2021-06-30 00:51:57 +00:00
|
|
|
export function isOutgoing(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: Pick<MessageWithUIFieldsType, 'type'>
|
2021-06-30 00:51:57 +00:00
|
|
|
): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return message.type === 'outgoing';
|
|
|
|
}
|
|
|
|
|
2022-03-04 21:14:52 +00:00
|
|
|
export function isStory(
|
|
|
|
message: Pick<MessageWithUIFieldsType, 'type'>
|
|
|
|
): boolean {
|
|
|
|
return message.type === 'story';
|
|
|
|
}
|
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
export function hasErrors(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: Pick<MessageWithUIFieldsType, 'errors'>
|
2021-07-19 22:44:49 +00:00
|
|
|
): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return message.errors ? message.errors.length > 0 : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSource(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-06-17 17:15:10 +00:00
|
|
|
ourNumber: string | undefined
|
|
|
|
): string | undefined {
|
|
|
|
if (isIncoming(message)) {
|
|
|
|
return message.source;
|
|
|
|
}
|
2021-08-02 21:19:18 +00:00
|
|
|
if (!isOutgoing(message)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn('message.getSource: Called for non-incoming/non-outoing message');
|
2021-08-02 21:19:18 +00:00
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
return ourNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSourceDevice(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-06-17 17:15:10 +00:00
|
|
|
ourDeviceId: number
|
|
|
|
): string | number | undefined {
|
|
|
|
const { sourceDevice } = message;
|
|
|
|
|
|
|
|
if (isIncoming(message)) {
|
|
|
|
return sourceDevice;
|
|
|
|
}
|
2021-08-02 21:19:18 +00:00
|
|
|
if (!isOutgoing(message)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-08-02 21:19:18 +00:00
|
|
|
'message.getSourceDevice: Called for non-incoming/non-outoing message'
|
|
|
|
);
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
return sourceDevice || ourDeviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSourceUuid(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-06-17 17:15:10 +00:00
|
|
|
ourUuid: string | undefined
|
|
|
|
): string | undefined {
|
|
|
|
if (isIncoming(message)) {
|
|
|
|
return message.sourceUuid;
|
|
|
|
}
|
2021-08-02 21:19:18 +00:00
|
|
|
if (!isOutgoing(message)) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-08-02 21:19:18 +00:00
|
|
|
'message.getSourceUuid: Called for non-incoming/non-outoing message'
|
|
|
|
);
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
return ourUuid;
|
|
|
|
}
|
|
|
|
|
2021-10-28 22:39:16 +00:00
|
|
|
export type GetContactOptions = Pick<
|
|
|
|
GetPropsForBubbleOptions,
|
|
|
|
'conversationSelector' | 'ourConversationId' | 'ourNumber' | 'ourUuid'
|
|
|
|
>;
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
export function getContactId(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-10-28 22:39:16 +00:00
|
|
|
{
|
|
|
|
conversationSelector,
|
|
|
|
ourConversationId,
|
|
|
|
ourNumber,
|
|
|
|
ourUuid,
|
|
|
|
}: GetContactOptions
|
2022-02-23 18:48:40 +00:00
|
|
|
): string | undefined {
|
2021-06-17 17:15:10 +00:00
|
|
|
const source = getSource(message, ourNumber);
|
|
|
|
const sourceUuid = getSourceUuid(message, ourUuid);
|
|
|
|
|
|
|
|
if (!source && !sourceUuid) {
|
|
|
|
return ourConversationId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const conversation = conversationSelector(sourceUuid || source);
|
|
|
|
return conversation.id;
|
|
|
|
}
|
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
// TODO: DESKTOP-2145
|
2021-06-17 17:15:10 +00:00
|
|
|
export function getContact(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-08-11 16:23:21 +00:00
|
|
|
{
|
|
|
|
conversationSelector,
|
|
|
|
ourConversationId,
|
|
|
|
ourNumber,
|
|
|
|
ourUuid,
|
|
|
|
}: GetContactOptions
|
2021-06-17 17:15:10 +00:00
|
|
|
): ConversationType {
|
|
|
|
const source = getSource(message, ourNumber);
|
|
|
|
const sourceUuid = getSourceUuid(message, ourUuid);
|
|
|
|
|
|
|
|
if (!source && !sourceUuid) {
|
|
|
|
return conversationSelector(ourConversationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return conversationSelector(sourceUuid || source);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getConversation(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: Pick<MessageWithUIFieldsType, 'conversationId'>,
|
2021-06-17 17:15:10 +00:00
|
|
|
conversationSelector: GetConversationByIdType
|
|
|
|
): ConversationType {
|
|
|
|
return conversationSelector(message.conversationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message
|
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
export const getAttachmentsForMessage = createSelectorCreator(memoizeByRoot)(
|
|
|
|
// `memoizeByRoot` requirement
|
|
|
|
identity,
|
|
|
|
|
2021-11-11 23:45:47 +00:00
|
|
|
({ sticker }: MessageWithUIFieldsType) => sticker,
|
|
|
|
({ attachments }: MessageWithUIFieldsType) => attachments,
|
2021-11-12 22:37:44 +00:00
|
|
|
(_, sticker, attachments = []): Array<AttachmentType> => {
|
2021-08-11 16:23:21 +00:00
|
|
|
if (sticker && sticker.data) {
|
|
|
|
const { data } = sticker;
|
|
|
|
|
|
|
|
// We don't show anything if we don't have the sticker or the blurhash...
|
|
|
|
if (!data.blurHash && (data.pending || !data.path)) {
|
|
|
|
return [];
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
...data,
|
|
|
|
// We want to show the blurhash for stickers, not the spinner
|
|
|
|
pending: false,
|
|
|
|
url: data.path
|
|
|
|
? window.Signal.Migrations.getAbsoluteAttachmentPath(data.path)
|
|
|
|
: undefined,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
return attachments
|
|
|
|
.filter(attachment => !attachment.error)
|
|
|
|
.map(attachment => getPropsForAttachment(attachment))
|
|
|
|
.filter(isNotNil);
|
|
|
|
}
|
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
export const processBodyRanges = createSelectorCreator(memoizeByRoot, isEqual)(
|
|
|
|
// `memoizeByRoot` requirement
|
|
|
|
identity,
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
(
|
2021-11-11 23:45:47 +00:00
|
|
|
{ bodyRanges }: Pick<MessageWithUIFieldsType, 'bodyRanges'>,
|
2021-08-11 16:23:21 +00:00
|
|
|
{ conversationSelector }: { conversationSelector: GetConversationByIdType }
|
|
|
|
): BodyRangesType | undefined => {
|
|
|
|
if (!bodyRanges) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
return bodyRanges
|
|
|
|
.filter(range => range.mentionUuid)
|
|
|
|
.map(range => {
|
|
|
|
const conversation = conversationSelector(range.mentionUuid);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...range,
|
|
|
|
conversationID: conversation.id,
|
|
|
|
replacementText: conversation.title,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.sort((a, b) => b.start - a.start);
|
|
|
|
},
|
2021-11-12 22:37:44 +00:00
|
|
|
(_, ranges): undefined | BodyRangesType => ranges
|
2021-08-11 16:23:21 +00:00
|
|
|
);
|
|
|
|
|
2021-10-28 22:39:16 +00:00
|
|
|
const getAuthorForMessage = createSelectorCreator(memoizeByRoot)(
|
2021-08-11 16:23:21 +00:00
|
|
|
// `memoizeByRoot` requirement
|
|
|
|
identity,
|
|
|
|
|
2021-10-28 22:39:16 +00:00
|
|
|
getContact,
|
|
|
|
|
2021-11-12 22:37:44 +00:00
|
|
|
(_, convo: ConversationType): PropsData['author'] => {
|
2021-10-28 22:39:16 +00:00
|
|
|
const {
|
|
|
|
acceptedMessageRequest,
|
|
|
|
avatarPath,
|
2021-11-15 22:53:42 +00:00
|
|
|
badges,
|
2021-10-28 22:39:16 +00:00
|
|
|
color,
|
|
|
|
id,
|
|
|
|
isMe,
|
|
|
|
name,
|
|
|
|
phoneNumber,
|
|
|
|
profileName,
|
|
|
|
sharedGroupNames,
|
|
|
|
title,
|
|
|
|
unblurredAvatarPath,
|
|
|
|
} = convo;
|
|
|
|
|
|
|
|
const unsafe = {
|
|
|
|
acceptedMessageRequest,
|
|
|
|
avatarPath,
|
2021-11-15 22:53:42 +00:00
|
|
|
badges,
|
2021-10-28 22:39:16 +00:00
|
|
|
color,
|
|
|
|
id,
|
|
|
|
isMe,
|
|
|
|
name,
|
|
|
|
phoneNumber,
|
|
|
|
profileName,
|
|
|
|
sharedGroupNames,
|
|
|
|
title,
|
|
|
|
unblurredAvatarPath,
|
|
|
|
};
|
2021-08-11 16:23:21 +00:00
|
|
|
|
|
|
|
const safe: AssertProps<PropsData['author'], typeof unsafe> = unsafe;
|
|
|
|
|
|
|
|
return safe;
|
2021-10-28 22:39:16 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const getCachedAuthorForMessage = createSelectorCreator(memoizeByRoot, isEqual)(
|
|
|
|
// `memoizeByRoot` requirement
|
|
|
|
identity,
|
|
|
|
getAuthorForMessage,
|
2021-11-12 22:37:44 +00:00
|
|
|
(_, author): PropsData['author'] => author
|
2021-08-11 16:23:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
export const getPreviewsForMessage = createSelectorCreator(memoizeByRoot)(
|
|
|
|
// `memoizeByRoot` requirement
|
|
|
|
identity,
|
2021-11-11 23:45:47 +00:00
|
|
|
({ preview }: MessageWithUIFieldsType) => preview,
|
2021-11-12 22:37:44 +00:00
|
|
|
(_, previews = []): Array<LinkPreviewType> => {
|
2021-08-11 16:23:21 +00:00
|
|
|
return previews.map(preview => ({
|
|
|
|
...preview,
|
|
|
|
isStickerPack: isStickerPack(preview.url),
|
|
|
|
domain: getDomain(preview.url),
|
|
|
|
image: preview.image ? getPropsForAttachment(preview.image) : null,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getReactionsForMessage = createSelectorCreator(
|
|
|
|
memoizeByRoot,
|
|
|
|
isEqual
|
|
|
|
)(
|
|
|
|
// `memoizeByRoot` requirement
|
|
|
|
identity,
|
|
|
|
|
|
|
|
(
|
2021-11-11 23:45:47 +00:00
|
|
|
{ reactions = [] }: MessageWithUIFieldsType,
|
2021-08-11 16:23:21 +00:00
|
|
|
{ conversationSelector }: { conversationSelector: GetConversationByIdType }
|
|
|
|
) => {
|
2021-10-29 23:19:44 +00:00
|
|
|
const reactionBySender = new Map<string, MessageReactionType>();
|
|
|
|
for (const reaction of reactions) {
|
|
|
|
const existingReaction = reactionBySender.get(reaction.fromId);
|
|
|
|
if (
|
|
|
|
!existingReaction ||
|
|
|
|
reaction.timestamp > existingReaction.timestamp
|
|
|
|
) {
|
|
|
|
reactionBySender.set(reaction.fromId, reaction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const reactionsWithEmpties = reactionBySender.values();
|
|
|
|
const reactionsWithEmoji = iterables.filter(
|
|
|
|
reactionsWithEmpties,
|
|
|
|
re => re.emoji
|
|
|
|
);
|
|
|
|
const formattedReactions = iterables.map(reactionsWithEmoji, re => {
|
2021-08-11 16:23:21 +00:00
|
|
|
const c = conversationSelector(re.fromId);
|
|
|
|
|
|
|
|
type From = NonNullable<PropsData['reactions']>[0]['from'];
|
|
|
|
|
|
|
|
const unsafe = pick(c, [
|
|
|
|
'acceptedMessageRequest',
|
|
|
|
'avatarPath',
|
2021-11-17 21:11:46 +00:00
|
|
|
'badges',
|
2021-08-11 16:23:21 +00:00
|
|
|
'color',
|
|
|
|
'id',
|
|
|
|
'isMe',
|
|
|
|
'name',
|
|
|
|
'phoneNumber',
|
|
|
|
'profileName',
|
|
|
|
'sharedGroupNames',
|
|
|
|
'title',
|
|
|
|
]);
|
|
|
|
|
|
|
|
const from: AssertProps<From, typeof unsafe> = unsafe;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-10-29 23:19:44 +00:00
|
|
|
strictAssert(re.emoji, 'Expected all reactions to have an emoji');
|
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
return {
|
|
|
|
emoji: re.emoji,
|
|
|
|
timestamp: re.timestamp,
|
|
|
|
from,
|
|
|
|
};
|
|
|
|
});
|
2021-10-29 23:19:44 +00:00
|
|
|
|
|
|
|
return [...formattedReactions];
|
2021-08-11 16:23:21 +00:00
|
|
|
},
|
|
|
|
|
2021-11-12 22:37:44 +00:00
|
|
|
(_, reactions): PropsData['reactions'] => reactions
|
2021-08-11 16:23:21 +00:00
|
|
|
);
|
|
|
|
|
2022-03-16 17:30:14 +00:00
|
|
|
export const getPropsForStoryReplyContext = createSelectorCreator(
|
|
|
|
memoizeByRoot,
|
|
|
|
isEqual
|
|
|
|
)(
|
|
|
|
// `memoizeByRoot` requirement
|
|
|
|
identity,
|
|
|
|
|
|
|
|
(
|
|
|
|
message: Pick<
|
|
|
|
MessageWithUIFieldsType,
|
2022-05-10 19:02:21 +00:00
|
|
|
'body' | 'conversationId' | 'storyReactionEmoji' | 'storyReplyContext'
|
2022-03-16 17:30:14 +00:00
|
|
|
>,
|
|
|
|
{
|
|
|
|
conversationSelector,
|
|
|
|
ourConversationId,
|
|
|
|
}: {
|
|
|
|
conversationSelector: GetConversationByIdType;
|
|
|
|
ourConversationId?: string;
|
|
|
|
}
|
|
|
|
): PropsData['storyReplyContext'] => {
|
2022-05-10 19:02:21 +00:00
|
|
|
const { storyReactionEmoji, storyReplyContext } = message;
|
2022-03-16 17:30:14 +00:00
|
|
|
if (!storyReplyContext) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const contact = conversationSelector(storyReplyContext.authorUuid);
|
|
|
|
|
2022-05-10 19:02:21 +00:00
|
|
|
const authorTitle = contact.firstName || contact.title;
|
2022-03-16 17:30:14 +00:00
|
|
|
const isFromMe = contact.id === ourConversationId;
|
|
|
|
|
|
|
|
const conversation = getConversation(message, conversationSelector);
|
|
|
|
|
|
|
|
const { conversationColor, customColor } =
|
|
|
|
getConversationColorAttributes(conversation);
|
|
|
|
|
|
|
|
return {
|
|
|
|
authorTitle,
|
|
|
|
conversationColor,
|
|
|
|
customColor,
|
2022-05-10 19:02:21 +00:00
|
|
|
emoji: storyReactionEmoji,
|
2022-03-16 17:30:14 +00:00
|
|
|
isFromMe,
|
|
|
|
rawAttachment: storyReplyContext.attachment
|
|
|
|
? processQuoteAttachment(storyReplyContext.attachment)
|
|
|
|
: undefined,
|
2022-05-10 19:02:21 +00:00
|
|
|
referencedMessageNotFound: !storyReplyContext.messageId,
|
|
|
|
text: getStoryReplyText(window.i18n, storyReplyContext.attachment),
|
2022-03-16 17:30:14 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
(_, storyReplyContext): PropsData['storyReplyContext'] => storyReplyContext
|
|
|
|
);
|
|
|
|
|
2021-08-11 23:06:20 +00:00
|
|
|
export const getPropsForQuote = createSelectorCreator(memoizeByRoot, isEqual)(
|
|
|
|
// `memoizeByRoot` requirement
|
|
|
|
identity,
|
|
|
|
|
|
|
|
(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: Pick<MessageWithUIFieldsType, 'conversationId' | 'quote'>,
|
2021-08-11 23:06:20 +00:00
|
|
|
{
|
|
|
|
conversationSelector,
|
|
|
|
ourConversationId,
|
|
|
|
}: {
|
|
|
|
conversationSelector: GetConversationByIdType;
|
|
|
|
ourConversationId?: string;
|
|
|
|
}
|
|
|
|
): PropsData['quote'] => {
|
|
|
|
const { quote } = message;
|
|
|
|
if (!quote) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
author,
|
|
|
|
authorUuid,
|
|
|
|
id: sentAt,
|
|
|
|
isViewOnce,
|
2022-05-11 20:59:58 +00:00
|
|
|
isGiftBadge: isTargetGiftBadge,
|
2021-08-11 23:06:20 +00:00
|
|
|
referencedMessageNotFound,
|
2022-03-21 21:46:20 +00:00
|
|
|
text = '',
|
2021-08-11 23:06:20 +00:00
|
|
|
} = quote;
|
|
|
|
|
|
|
|
const contact = conversationSelector(authorUuid || author);
|
|
|
|
|
|
|
|
const authorId = contact.id;
|
|
|
|
const authorName = contact.name;
|
|
|
|
const authorPhoneNumber = contact.phoneNumber;
|
|
|
|
const authorProfileName = contact.profileName;
|
|
|
|
const authorTitle = contact.title;
|
|
|
|
const isFromMe = authorId === ourConversationId;
|
|
|
|
|
|
|
|
const firstAttachment = quote.attachments && quote.attachments[0];
|
|
|
|
const conversation = getConversation(message, conversationSelector);
|
|
|
|
|
2021-12-04 02:10:03 +00:00
|
|
|
const { conversationColor, customColor } =
|
|
|
|
getConversationColorAttributes(conversation);
|
2021-08-25 00:15:12 +00:00
|
|
|
|
2021-08-11 23:06:20 +00:00
|
|
|
return {
|
|
|
|
authorId,
|
|
|
|
authorName,
|
|
|
|
authorPhoneNumber,
|
|
|
|
authorProfileName,
|
|
|
|
authorTitle,
|
|
|
|
bodyRanges: processBodyRanges(quote, { conversationSelector }),
|
2021-12-04 02:10:03 +00:00
|
|
|
conversationColor,
|
|
|
|
customColor,
|
2021-08-11 23:06:20 +00:00
|
|
|
isFromMe,
|
|
|
|
rawAttachment: firstAttachment
|
|
|
|
? processQuoteAttachment(firstAttachment)
|
|
|
|
: undefined,
|
2022-05-11 20:59:58 +00:00
|
|
|
isGiftBadge: Boolean(isTargetGiftBadge),
|
2021-08-11 23:06:20 +00:00
|
|
|
isViewOnce,
|
|
|
|
referencedMessageNotFound,
|
|
|
|
sentAt: Number(sentAt),
|
2022-03-21 21:46:20 +00:00
|
|
|
text,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2021-11-12 22:37:44 +00:00
|
|
|
(_, quote): PropsData['quote'] => quote
|
2021-08-11 23:06:20 +00:00
|
|
|
);
|
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
export type GetPropsForMessageOptions = Pick<
|
|
|
|
GetPropsForBubbleOptions,
|
|
|
|
| 'conversationSelector'
|
|
|
|
| 'ourConversationId'
|
2021-08-19 20:14:41 +00:00
|
|
|
| 'ourUuid'
|
|
|
|
| 'ourNumber'
|
2021-08-11 16:23:21 +00:00
|
|
|
| 'selectedMessageId'
|
|
|
|
| 'selectedMessageCounter'
|
|
|
|
| 'regionCode'
|
|
|
|
| 'accountSelector'
|
2021-08-19 20:14:41 +00:00
|
|
|
| 'contactNameColorSelector'
|
2021-08-11 16:23:21 +00:00
|
|
|
>;
|
|
|
|
|
2021-08-11 23:06:20 +00:00
|
|
|
type ShallowPropsType = Pick<
|
|
|
|
PropsForMessage,
|
|
|
|
| 'canDeleteForEveryone'
|
|
|
|
| 'canDownload'
|
2022-02-02 20:01:22 +00:00
|
|
|
| 'canReact'
|
2021-08-11 23:06:20 +00:00
|
|
|
| 'canReply'
|
2022-03-04 19:22:31 +00:00
|
|
|
| 'canRetry'
|
|
|
|
| 'canRetryDeleteForEveryone'
|
2021-08-11 23:06:20 +00:00
|
|
|
| 'contact'
|
2021-08-19 20:14:41 +00:00
|
|
|
| 'contactNameColor'
|
2021-08-11 23:06:20 +00:00
|
|
|
| 'conversationColor'
|
|
|
|
| 'conversationId'
|
2022-05-11 20:59:58 +00:00
|
|
|
| 'conversationTitle'
|
2021-08-11 23:06:20 +00:00
|
|
|
| 'conversationType'
|
|
|
|
| 'customColor'
|
|
|
|
| 'deletedForEveryone'
|
|
|
|
| 'direction'
|
2021-11-11 23:45:47 +00:00
|
|
|
| 'displayLimit'
|
2021-08-11 23:06:20 +00:00
|
|
|
| 'expirationLength'
|
|
|
|
| 'expirationTimestamp'
|
2022-05-11 20:59:58 +00:00
|
|
|
| 'giftBadge'
|
2021-08-11 23:06:20 +00:00
|
|
|
| 'id'
|
|
|
|
| 'isBlocked'
|
|
|
|
| 'isMessageRequestAccepted'
|
|
|
|
| 'isSelected'
|
|
|
|
| 'isSelectedCounter'
|
|
|
|
| 'isSticker'
|
|
|
|
| 'isTapToView'
|
|
|
|
| 'isTapToViewError'
|
|
|
|
| 'isTapToViewExpired'
|
2021-08-12 18:15:55 +00:00
|
|
|
| 'readStatus'
|
2021-08-11 23:06:20 +00:00
|
|
|
| 'selectedReaction'
|
|
|
|
| 'status'
|
|
|
|
| 'text'
|
2022-03-23 20:23:28 +00:00
|
|
|
| 'textDirection'
|
2021-08-11 23:06:20 +00:00
|
|
|
| 'textPending'
|
|
|
|
| 'timestamp'
|
|
|
|
>;
|
|
|
|
|
|
|
|
const getShallowPropsForMessage = createSelectorCreator(memoizeByRoot, isEqual)(
|
|
|
|
// `memoizeByRoot` requirement
|
|
|
|
identity,
|
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-08-11 16:23:21 +00:00
|
|
|
{
|
2021-08-11 23:06:20 +00:00
|
|
|
accountSelector,
|
2021-08-11 16:23:21 +00:00
|
|
|
conversationSelector,
|
|
|
|
ourConversationId,
|
2021-08-19 20:14:41 +00:00
|
|
|
ourNumber,
|
|
|
|
ourUuid,
|
2021-08-11 23:06:20 +00:00
|
|
|
regionCode,
|
2021-08-11 16:23:21 +00:00
|
|
|
selectedMessageId,
|
|
|
|
selectedMessageCounter,
|
2021-08-19 20:14:41 +00:00
|
|
|
contactNameColorSelector,
|
2021-08-11 16:23:21 +00:00
|
|
|
}: GetPropsForMessageOptions
|
2021-08-11 23:06:20 +00:00
|
|
|
): ShallowPropsType => {
|
2021-08-19 20:14:41 +00:00
|
|
|
const { expireTimer, expirationStartTimestamp, conversationId } = message;
|
2021-08-11 16:23:21 +00:00
|
|
|
const expirationLength = expireTimer ? expireTimer * 1000 : undefined;
|
|
|
|
const expirationTimestamp =
|
|
|
|
expirationStartTimestamp && expirationLength
|
|
|
|
? expirationStartTimestamp + expirationLength
|
|
|
|
: undefined;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
const conversation = getConversation(message, conversationSelector);
|
|
|
|
const isGroup = conversation.type === 'group';
|
|
|
|
const { sticker } = message;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
const isMessageTapToView = isTapToView(message);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-08-11 23:06:20 +00:00
|
|
|
const isSelected = message.id === selectedMessageId;
|
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
const selectedReaction = (
|
|
|
|
(message.reactions || []).find(re => re.fromId === ourConversationId) ||
|
|
|
|
{}
|
|
|
|
).emoji;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-10-28 22:39:16 +00:00
|
|
|
const authorId = getContactId(message, {
|
2021-08-19 20:14:41 +00:00
|
|
|
conversationSelector,
|
|
|
|
ourConversationId,
|
|
|
|
ourNumber,
|
|
|
|
ourUuid,
|
|
|
|
});
|
2021-10-28 22:39:16 +00:00
|
|
|
const contactNameColor = contactNameColorSelector(conversationId, authorId);
|
2021-08-19 20:14:41 +00:00
|
|
|
|
2021-12-04 02:10:03 +00:00
|
|
|
const { conversationColor, customColor } =
|
|
|
|
getConversationColorAttributes(conversation);
|
2021-08-25 00:15:12 +00:00
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
return {
|
|
|
|
canDeleteForEveryone: canDeleteForEveryone(message),
|
|
|
|
canDownload: canDownload(message, conversationSelector),
|
2022-02-02 20:01:22 +00:00
|
|
|
canReact: canReact(message, ourConversationId, conversationSelector),
|
2021-08-11 16:23:21 +00:00
|
|
|
canReply: canReply(message, ourConversationId, conversationSelector),
|
2022-03-04 19:22:31 +00:00
|
|
|
canRetry: hasErrors(message),
|
|
|
|
canRetryDeleteForEveryone: canRetryDeleteForEveryone(message),
|
2021-08-11 16:23:21 +00:00
|
|
|
contact: getPropsForEmbeddedContact(message, regionCode, accountSelector),
|
2021-08-19 20:14:41 +00:00
|
|
|
contactNameColor,
|
2021-12-04 02:10:03 +00:00
|
|
|
conversationColor,
|
2021-08-19 20:14:41 +00:00
|
|
|
conversationId,
|
2022-05-11 20:59:58 +00:00
|
|
|
conversationTitle: conversation.title,
|
2021-08-11 16:23:21 +00:00
|
|
|
conversationType: isGroup ? 'group' : 'direct',
|
2021-12-04 02:10:03 +00:00
|
|
|
customColor,
|
2021-08-11 16:23:21 +00:00
|
|
|
deletedForEveryone: message.deletedForEveryone || false,
|
|
|
|
direction: isIncoming(message) ? 'incoming' : 'outgoing',
|
2021-11-11 23:45:47 +00:00
|
|
|
displayLimit: message.displayLimit,
|
2021-08-11 16:23:21 +00:00
|
|
|
expirationLength,
|
|
|
|
expirationTimestamp,
|
2022-05-11 20:59:58 +00:00
|
|
|
giftBadge: message.giftBadge,
|
2021-08-11 16:23:21 +00:00
|
|
|
id: message.id,
|
|
|
|
isBlocked: conversation.isBlocked || false,
|
|
|
|
isMessageRequestAccepted: conversation?.acceptedMessageRequest ?? true,
|
|
|
|
isSelected,
|
|
|
|
isSelectedCounter: isSelected ? selectedMessageCounter : undefined,
|
|
|
|
isSticker: Boolean(sticker),
|
|
|
|
isTapToView: isMessageTapToView,
|
|
|
|
isTapToViewError:
|
|
|
|
isMessageTapToView && isIncoming(message) && message.isTapToViewInvalid,
|
|
|
|
isTapToViewExpired: isMessageTapToView && message.isErased,
|
2021-08-12 18:15:55 +00:00
|
|
|
readStatus: message.readStatus ?? ReadStatus.Read,
|
2021-08-11 16:23:21 +00:00
|
|
|
selectedReaction,
|
|
|
|
status: getMessagePropStatus(message, ourConversationId),
|
2022-03-21 21:46:20 +00:00
|
|
|
text: message.body,
|
2022-03-23 20:23:28 +00:00
|
|
|
textDirection: getTextDirection(message.body),
|
2021-08-11 16:23:21 +00:00
|
|
|
textPending: message.bodyPending,
|
|
|
|
timestamp: message.sent_at,
|
|
|
|
};
|
2021-08-11 23:06:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
(_: unknown, props: ShallowPropsType) => props
|
|
|
|
);
|
|
|
|
|
2022-03-23 20:23:28 +00:00
|
|
|
function getTextDirection(body?: string): TextDirection {
|
|
|
|
if (!body) {
|
|
|
|
return TextDirection.None;
|
|
|
|
}
|
|
|
|
|
|
|
|
const direction = getDirection(body);
|
|
|
|
switch (direction) {
|
|
|
|
case 'ltr':
|
|
|
|
return TextDirection.LeftToRight;
|
|
|
|
case 'rtl':
|
|
|
|
return TextDirection.RightToLeft;
|
|
|
|
case 'neutral':
|
|
|
|
return TextDirection.Default;
|
|
|
|
default: {
|
|
|
|
const unexpected: never = direction;
|
|
|
|
log.warn(`getTextDirection: unexpected direction ${unexpected}`);
|
|
|
|
return TextDirection.None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 22:37:44 +00:00
|
|
|
export const getPropsForMessage: (
|
|
|
|
message: MessageWithUIFieldsType,
|
|
|
|
options: GetPropsForMessageOptions
|
|
|
|
) => Omit<PropsForMessage, 'renderingContext'> = createSelectorCreator(
|
|
|
|
memoizeByRoot
|
|
|
|
)(
|
2021-08-11 23:06:20 +00:00
|
|
|
// `memoizeByRoot` requirement
|
|
|
|
identity,
|
|
|
|
|
|
|
|
getAttachmentsForMessage,
|
|
|
|
processBodyRanges,
|
2021-10-28 22:39:16 +00:00
|
|
|
getCachedAuthorForMessage,
|
2021-08-11 23:06:20 +00:00
|
|
|
getPreviewsForMessage,
|
|
|
|
getReactionsForMessage,
|
|
|
|
getPropsForQuote,
|
2022-03-16 17:30:14 +00:00
|
|
|
getPropsForStoryReplyContext,
|
2021-08-11 23:06:20 +00:00
|
|
|
getShallowPropsForMessage,
|
|
|
|
(
|
2021-11-12 22:37:44 +00:00
|
|
|
_,
|
2021-08-11 23:06:20 +00:00
|
|
|
attachments: Array<AttachmentType>,
|
|
|
|
bodyRanges: BodyRangesType | undefined,
|
|
|
|
author: PropsData['author'],
|
|
|
|
previews: Array<LinkPreviewType>,
|
|
|
|
reactions: PropsData['reactions'],
|
|
|
|
quote: PropsData['quote'],
|
2022-03-16 17:30:14 +00:00
|
|
|
storyReplyContext: PropsData['storyReplyContext'],
|
2021-08-11 23:06:20 +00:00
|
|
|
shallowProps: ShallowPropsType
|
|
|
|
): Omit<PropsForMessage, 'renderingContext'> => {
|
|
|
|
return {
|
|
|
|
attachments,
|
|
|
|
author,
|
|
|
|
bodyRanges,
|
|
|
|
previews,
|
|
|
|
quote,
|
|
|
|
reactions,
|
2022-03-16 17:30:14 +00:00
|
|
|
storyReplyContext,
|
2021-08-11 23:06:20 +00:00
|
|
|
...shallowProps,
|
|
|
|
};
|
2021-08-11 16:23:21 +00:00
|
|
|
}
|
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-08-11 23:06:20 +00:00
|
|
|
export const getBubblePropsForMessage = createSelectorCreator(memoizeByRoot)(
|
|
|
|
// `memoizeByRoot` requirement
|
|
|
|
identity,
|
|
|
|
|
|
|
|
getPropsForMessage,
|
2021-11-12 22:37:44 +00:00
|
|
|
|
|
|
|
(_, data): TimelineItemType => ({
|
2021-08-11 23:06:20 +00:00
|
|
|
type: 'message' as const,
|
|
|
|
data,
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp: data.timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Top-level prop generation for the message bubble
|
|
|
|
export function getPropsForBubble(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-08-11 23:06:20 +00:00
|
|
|
options: GetPropsForBubbleOptions
|
|
|
|
): TimelineItemType {
|
2022-01-26 23:05:26 +00:00
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
const { received_at_ms: receivedAt, timestamp: messageTimestamp } = message;
|
|
|
|
const timestamp = receivedAt || messageTimestamp;
|
|
|
|
|
2021-08-11 23:06:20 +00:00
|
|
|
if (isUnsupportedMessage(message)) {
|
|
|
|
return {
|
|
|
|
type: 'unsupportedMessage',
|
|
|
|
data: getPropsForUnsupportedMessage(message, options),
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isGroupV2Change(message)) {
|
|
|
|
return {
|
|
|
|
type: 'groupV2Change',
|
|
|
|
data: getPropsForGroupV2Change(message, options),
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isGroupV1Migration(message)) {
|
|
|
|
return {
|
|
|
|
type: 'groupV1Migration',
|
|
|
|
data: getPropsForGroupV1Migration(message, options),
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isExpirationTimerUpdate(message)) {
|
|
|
|
return {
|
|
|
|
type: 'timerNotification',
|
|
|
|
data: getPropsForTimerNotification(message, options),
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isKeyChange(message)) {
|
|
|
|
return {
|
|
|
|
type: 'safetyNumberNotification',
|
|
|
|
data: getPropsForSafetyNumberNotification(message, options),
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isVerifiedChange(message)) {
|
|
|
|
return {
|
|
|
|
type: 'verificationNotification',
|
|
|
|
data: getPropsForVerificationNotification(message, options),
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isGroupUpdate(message)) {
|
|
|
|
return {
|
|
|
|
type: 'groupNotification',
|
|
|
|
data: getPropsForGroupNotification(message, options),
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isEndSession(message)) {
|
|
|
|
return {
|
|
|
|
type: 'resetSessionNotification',
|
|
|
|
data: null,
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isCallHistory(message)) {
|
|
|
|
return {
|
|
|
|
type: 'callHistory',
|
|
|
|
data: getPropsForCallHistory(message, options),
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isProfileChange(message)) {
|
|
|
|
return {
|
|
|
|
type: 'profileChange',
|
|
|
|
data: getPropsForProfileChange(message, options),
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isUniversalTimerNotification(message)) {
|
|
|
|
return {
|
|
|
|
type: 'universalTimerNotification',
|
|
|
|
data: null,
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isChangeNumberNotification(message)) {
|
|
|
|
return {
|
|
|
|
type: 'changeNumberNotification',
|
|
|
|
data: getPropsForChangeNumberNotification(message, options),
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isChatSessionRefreshed(message)) {
|
|
|
|
return {
|
|
|
|
type: 'chatSessionRefreshed',
|
|
|
|
data: null,
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isDeliveryIssue(message)) {
|
|
|
|
return {
|
|
|
|
type: 'deliveryIssue',
|
|
|
|
data: getPropsForDeliveryIssue(message, options),
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
2021-08-11 23:06:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return getBubblePropsForMessage(message, options);
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
// Unsupported Message
|
|
|
|
|
2021-11-11 23:45:47 +00:00
|
|
|
export function isUnsupportedMessage(
|
|
|
|
message: MessageWithUIFieldsType
|
|
|
|
): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
const versionAtReceive = message.supportedVersionAtReceive;
|
|
|
|
const requiredVersion = message.requiredProtocolVersion;
|
|
|
|
|
|
|
|
return (
|
|
|
|
isNumber(versionAtReceive) &&
|
|
|
|
isNumber(requiredVersion) &&
|
|
|
|
versionAtReceive < requiredVersion
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForUnsupportedMessage(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-08-11 16:23:21 +00:00
|
|
|
options: GetContactOptions
|
2021-06-17 17:15:10 +00:00
|
|
|
): PropsForUnsupportedMessage {
|
2021-07-09 19:36:10 +00:00
|
|
|
const CURRENT_PROTOCOL_VERSION = Proto.DataMessage.ProtocolVersion.CURRENT;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
const requiredVersion = message.requiredProtocolVersion;
|
|
|
|
const canProcessNow = Boolean(
|
|
|
|
CURRENT_PROTOCOL_VERSION &&
|
|
|
|
requiredVersion &&
|
|
|
|
CURRENT_PROTOCOL_VERSION >= requiredVersion
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
canProcessNow,
|
2021-08-11 16:23:21 +00:00
|
|
|
contact: getContact(message, options),
|
2021-06-17 17:15:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// GroupV2 Change
|
|
|
|
|
2021-11-11 23:45:47 +00:00
|
|
|
export function isGroupV2Change(message: MessageWithUIFieldsType): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return Boolean(message.groupV2Change);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForGroupV2Change(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-10-26 22:59:08 +00:00
|
|
|
{ conversationSelector, ourUuid }: GetPropsForBubbleOptions
|
2021-06-17 17:15:10 +00:00
|
|
|
): GroupsV2Props {
|
|
|
|
const change = message.groupV2Change;
|
|
|
|
|
|
|
|
if (!change) {
|
|
|
|
throw new Error('getPropsForGroupV2Change: Change is missing!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const conversation = getConversation(message, conversationSelector);
|
|
|
|
|
|
|
|
return {
|
2022-03-16 00:11:28 +00:00
|
|
|
areWeAdmin: Boolean(conversation.areWeAdmin),
|
2021-06-17 17:15:10 +00:00
|
|
|
groupName: conversation?.type === 'group' ? conversation?.name : undefined,
|
2022-03-16 00:11:28 +00:00
|
|
|
groupMemberships: conversation.memberships,
|
|
|
|
groupBannedMemberships: conversation.bannedMemberships,
|
2021-10-26 22:59:08 +00:00
|
|
|
ourUuid,
|
2021-06-17 17:15:10 +00:00
|
|
|
change,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// GroupV1 Migration
|
|
|
|
|
2021-11-11 23:45:47 +00:00
|
|
|
export function isGroupV1Migration(message: MessageWithUIFieldsType): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return message.type === 'group-v1-migration';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForGroupV1Migration(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-08-11 16:23:21 +00:00
|
|
|
{ conversationSelector }: GetPropsForBubbleOptions
|
2021-06-17 17:15:10 +00:00
|
|
|
): GroupV1MigrationPropsType {
|
|
|
|
const migration = message.groupMigration;
|
|
|
|
if (!migration) {
|
|
|
|
// Backwards-compatibility with data schema in early betas
|
|
|
|
const invitedGV2Members = message.invitedGV2Members || [];
|
|
|
|
const droppedGV2MemberIds = message.droppedGV2MemberIds || [];
|
|
|
|
|
|
|
|
const invitedMembers = invitedGV2Members.map(item =>
|
2021-10-26 22:59:08 +00:00
|
|
|
conversationSelector(item.uuid)
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
const droppedMembers = droppedGV2MemberIds.map(conversationId =>
|
|
|
|
conversationSelector(conversationId)
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
areWeInvited: false,
|
|
|
|
droppedMembers,
|
|
|
|
invitedMembers,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
areWeInvited,
|
|
|
|
droppedMemberIds,
|
|
|
|
invitedMembers: rawInvitedMembers,
|
|
|
|
} = migration;
|
|
|
|
const invitedMembers = rawInvitedMembers.map(item =>
|
2021-10-26 22:59:08 +00:00
|
|
|
conversationSelector(item.uuid)
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
const droppedMembers = droppedMemberIds.map(conversationId =>
|
|
|
|
conversationSelector(conversationId)
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
areWeInvited,
|
|
|
|
droppedMembers,
|
|
|
|
invitedMembers,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: props are null!
|
|
|
|
|
|
|
|
// Expiration Timer Update
|
|
|
|
|
|
|
|
export function isExpirationTimerUpdate(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: Pick<MessageWithUIFieldsType, 'flags'>
|
2021-06-17 17:15:10 +00:00
|
|
|
): boolean {
|
2021-07-09 19:36:10 +00:00
|
|
|
const flag = Proto.DataMessage.Flags.EXPIRATION_TIMER_UPDATE;
|
2021-06-17 17:15:10 +00:00
|
|
|
// eslint-disable-next-line no-bitwise
|
|
|
|
return Boolean(message.flags && message.flags & flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForTimerNotification(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-08-11 16:23:21 +00:00
|
|
|
{ ourConversationId, conversationSelector }: GetPropsForBubbleOptions
|
2021-06-17 17:15:10 +00:00
|
|
|
): TimerNotificationProps {
|
|
|
|
const timerUpdate = message.expirationTimerUpdate;
|
|
|
|
if (!timerUpdate) {
|
|
|
|
throw new Error(
|
|
|
|
'getPropsForTimerNotification: missing expirationTimerUpdate!'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { expireTimer, fromSync, source, sourceUuid } = timerUpdate;
|
|
|
|
const disabled = !expireTimer;
|
|
|
|
const sourceId = sourceUuid || source;
|
|
|
|
const formattedContact = conversationSelector(sourceId);
|
|
|
|
|
|
|
|
const basicProps = {
|
|
|
|
...formattedContact,
|
|
|
|
disabled,
|
|
|
|
expireTimer,
|
|
|
|
type: 'fromOther' as const,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (fromSync) {
|
|
|
|
return {
|
|
|
|
...basicProps,
|
|
|
|
type: 'fromSync' as const,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (formattedContact.id === ourConversationId) {
|
|
|
|
return {
|
|
|
|
...basicProps,
|
|
|
|
type: 'fromMe' as const,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (!sourceId) {
|
|
|
|
return {
|
|
|
|
...basicProps,
|
|
|
|
type: 'fromMember' as const,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return basicProps;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key Change
|
|
|
|
|
2021-11-11 23:45:47 +00:00
|
|
|
export function isKeyChange(message: MessageWithUIFieldsType): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return message.type === 'keychange';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForSafetyNumberNotification(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-08-11 16:23:21 +00:00
|
|
|
{ conversationSelector }: GetPropsForBubbleOptions
|
2021-06-17 17:15:10 +00:00
|
|
|
): SafetyNumberNotificationProps {
|
|
|
|
const conversation = getConversation(message, conversationSelector);
|
|
|
|
const isGroup = conversation?.type === 'group';
|
|
|
|
const identifier = message.key_changed;
|
|
|
|
const contact = conversationSelector(identifier);
|
|
|
|
|
|
|
|
return {
|
|
|
|
isGroup,
|
|
|
|
contact,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verified Change
|
|
|
|
|
2021-11-11 23:45:47 +00:00
|
|
|
export function isVerifiedChange(message: MessageWithUIFieldsType): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return message.type === 'verified-change';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForVerificationNotification(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-08-11 16:23:21 +00:00
|
|
|
{ conversationSelector }: GetPropsForBubbleOptions
|
2021-06-17 17:15:10 +00:00
|
|
|
): VerificationNotificationProps {
|
|
|
|
const type = message.verified ? 'markVerified' : 'markNotVerified';
|
|
|
|
const isLocal = message.local || false;
|
|
|
|
const identifier = message.verifiedChanged;
|
|
|
|
|
|
|
|
return {
|
|
|
|
type,
|
|
|
|
isLocal,
|
|
|
|
contact: conversationSelector(identifier),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-11 20:59:58 +00:00
|
|
|
// Gift Badge
|
|
|
|
|
|
|
|
export function isGiftBadge(
|
|
|
|
message: Pick<MessageWithUIFieldsType, 'giftBadge'>
|
|
|
|
): boolean {
|
|
|
|
return Boolean(message.giftBadge);
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
// Group Update (V1)
|
|
|
|
|
2021-06-30 00:51:57 +00:00
|
|
|
export function isGroupUpdate(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: Pick<MessageWithUIFieldsType, 'group_update'>
|
2021-06-30 00:51:57 +00:00
|
|
|
): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return Boolean(message.group_update);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForGroupNotification(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-08-11 16:23:21 +00:00
|
|
|
options: GetContactOptions
|
2021-06-17 17:15:10 +00:00
|
|
|
): GroupNotificationProps {
|
|
|
|
const groupUpdate = message.group_update;
|
|
|
|
if (!groupUpdate) {
|
|
|
|
throw new Error(
|
|
|
|
'getPropsForGroupNotification: Message missing group_update'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
const { conversationSelector } = options;
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
const changes = [];
|
|
|
|
|
|
|
|
if (
|
|
|
|
!groupUpdate.avatarUpdated &&
|
|
|
|
!groupUpdate.left &&
|
|
|
|
!groupUpdate.joined &&
|
|
|
|
!groupUpdate.name
|
|
|
|
) {
|
|
|
|
changes.push({
|
|
|
|
type: 'general' as ChangeType,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-23 16:14:11 +00:00
|
|
|
if (groupUpdate.joined?.length) {
|
2021-06-17 17:15:10 +00:00
|
|
|
changes.push({
|
|
|
|
type: 'add' as ChangeType,
|
|
|
|
contacts: map(
|
|
|
|
Array.isArray(groupUpdate.joined)
|
|
|
|
? groupUpdate.joined
|
|
|
|
: [groupUpdate.joined],
|
|
|
|
identifier => conversationSelector(identifier)
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupUpdate.left === 'You') {
|
|
|
|
changes.push({
|
|
|
|
type: 'remove' as ChangeType,
|
|
|
|
});
|
|
|
|
} else if (groupUpdate.left) {
|
|
|
|
changes.push({
|
|
|
|
type: 'remove' as ChangeType,
|
|
|
|
contacts: map(
|
|
|
|
Array.isArray(groupUpdate.left) ? groupUpdate.left : [groupUpdate.left],
|
|
|
|
identifier => conversationSelector(identifier)
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupUpdate.name) {
|
|
|
|
changes.push({
|
|
|
|
type: 'name' as ChangeType,
|
|
|
|
newName: groupUpdate.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupUpdate.avatarUpdated) {
|
|
|
|
changes.push({
|
|
|
|
type: 'avatar' as ChangeType,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
const from = getContact(message, options);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
from,
|
|
|
|
changes,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// End Session
|
|
|
|
|
2021-06-30 00:51:57 +00:00
|
|
|
export function isEndSession(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: Pick<MessageWithUIFieldsType, 'flags'>
|
2021-06-30 00:51:57 +00:00
|
|
|
): boolean {
|
2021-07-09 19:36:10 +00:00
|
|
|
const flag = Proto.DataMessage.Flags.END_SESSION;
|
2021-06-17 17:15:10 +00:00
|
|
|
// eslint-disable-next-line no-bitwise
|
|
|
|
return Boolean(message.flags && message.flags & flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call History
|
|
|
|
|
2021-11-11 23:45:47 +00:00
|
|
|
export function isCallHistory(message: MessageWithUIFieldsType): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return message.type === 'call-history';
|
|
|
|
}
|
|
|
|
|
2021-08-11 16:23:21 +00:00
|
|
|
export type GetPropsForCallHistoryOptions = Pick<
|
|
|
|
GetPropsForBubbleOptions,
|
|
|
|
'conversationSelector' | 'callSelector' | 'activeCall'
|
|
|
|
>;
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
export function getPropsForCallHistory(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-08-11 16:23:21 +00:00
|
|
|
{
|
|
|
|
conversationSelector,
|
|
|
|
callSelector,
|
|
|
|
activeCall,
|
|
|
|
}: GetPropsForCallHistoryOptions
|
2021-06-17 17:15:10 +00:00
|
|
|
): CallingNotificationType {
|
|
|
|
const { callHistoryDetails } = message;
|
|
|
|
if (!callHistoryDetails) {
|
|
|
|
throw new Error('getPropsForCallHistory: Missing callHistoryDetails');
|
|
|
|
}
|
|
|
|
|
2022-02-11 18:21:45 +00:00
|
|
|
const activeCallConversationId = activeCall?.conversationId;
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
switch (callHistoryDetails.callMode) {
|
|
|
|
// Old messages weren't saved with a call mode.
|
|
|
|
case undefined:
|
|
|
|
case CallMode.Direct:
|
|
|
|
return {
|
|
|
|
...callHistoryDetails,
|
2022-02-11 18:21:45 +00:00
|
|
|
activeCallConversationId,
|
2021-06-17 17:15:10 +00:00
|
|
|
callMode: CallMode.Direct,
|
|
|
|
};
|
|
|
|
case CallMode.Group: {
|
|
|
|
const { conversationId } = message;
|
|
|
|
if (!conversationId) {
|
|
|
|
throw new Error('getPropsForCallHistory: missing conversation ID');
|
|
|
|
}
|
|
|
|
|
|
|
|
let call = callSelector(conversationId);
|
|
|
|
if (call && call.callMode !== CallMode.Group) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-06-17 17:15:10 +00:00
|
|
|
'getPropsForCallHistory: there is an unexpected non-group call; pretending it does not exist'
|
|
|
|
);
|
|
|
|
call = undefined;
|
|
|
|
}
|
|
|
|
|
2022-02-08 19:18:51 +00:00
|
|
|
const creator = conversationSelector(callHistoryDetails.creatorUuid);
|
|
|
|
const deviceCount = call?.peekInfo?.deviceCount ?? 0;
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
return {
|
2022-02-11 18:21:45 +00:00
|
|
|
activeCallConversationId,
|
2021-06-17 17:15:10 +00:00
|
|
|
callMode: CallMode.Group,
|
|
|
|
conversationId,
|
|
|
|
creator,
|
2022-02-08 19:18:51 +00:00
|
|
|
deviceCount,
|
|
|
|
ended:
|
|
|
|
callHistoryDetails.eraId !== call?.peekInfo?.eraId || !deviceCount,
|
|
|
|
maxDevices: call?.peekInfo?.maxDevices ?? Infinity,
|
2021-06-17 17:15:10 +00:00
|
|
|
startedTime: callHistoryDetails.startedTime,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new Error(
|
|
|
|
`getPropsForCallHistory: missing case ${missingCaseError(
|
|
|
|
callHistoryDetails
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Profile Change
|
|
|
|
|
2021-11-11 23:45:47 +00:00
|
|
|
export function isProfileChange(message: MessageWithUIFieldsType): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return message.type === 'profile-change';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForProfileChange(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-08-11 16:23:21 +00:00
|
|
|
{ conversationSelector }: GetPropsForBubbleOptions
|
2021-06-17 17:15:10 +00:00
|
|
|
): ProfileChangeNotificationPropsType {
|
|
|
|
const change = message.profileChange;
|
|
|
|
const { changedId } = message;
|
|
|
|
const changedContact = conversationSelector(changedId);
|
|
|
|
|
|
|
|
if (!change) {
|
|
|
|
throw new Error('getPropsForProfileChange: profileChange is undefined');
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
changedContact,
|
|
|
|
change,
|
|
|
|
} as ProfileChangeNotificationPropsType;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Universal Timer Notification
|
|
|
|
|
|
|
|
// Note: smart, so props not generated here
|
|
|
|
|
|
|
|
export function isUniversalTimerNotification(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType
|
2021-06-17 17:15:10 +00:00
|
|
|
): boolean {
|
|
|
|
return message.type === 'universal-timer-notification';
|
|
|
|
}
|
|
|
|
|
2021-08-05 23:34:49 +00:00
|
|
|
// Change Number Notification
|
|
|
|
|
|
|
|
export function isChangeNumberNotification(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType
|
2021-08-05 23:34:49 +00:00
|
|
|
): boolean {
|
|
|
|
return message.type === 'change-number-notification';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForChangeNumberNotification(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-08-11 16:23:21 +00:00
|
|
|
{ conversationSelector }: GetPropsForBubbleOptions
|
2021-08-05 23:34:49 +00:00
|
|
|
): ChangeNumberNotificationProps {
|
|
|
|
return {
|
|
|
|
sender: conversationSelector(message.sourceUuid),
|
|
|
|
timestamp: message.sent_at,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
// Chat Session Refreshed
|
|
|
|
|
|
|
|
export function isChatSessionRefreshed(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType
|
2021-06-17 17:15:10 +00:00
|
|
|
): boolean {
|
|
|
|
return message.type === 'chat-session-refreshed';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: props are null
|
|
|
|
|
|
|
|
// Delivery Issue
|
|
|
|
|
2021-11-11 23:45:47 +00:00
|
|
|
export function isDeliveryIssue(message: MessageWithUIFieldsType): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return message.type === 'delivery-issue';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForDeliveryIssue(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-08-11 16:23:21 +00:00
|
|
|
{ conversationSelector }: GetPropsForBubbleOptions
|
2021-06-17 17:15:10 +00:00
|
|
|
): DeliveryIssuePropsType {
|
|
|
|
const sender = conversationSelector(message.sourceUuid);
|
2021-07-27 20:30:41 +00:00
|
|
|
const conversation = conversationSelector(message.conversationId);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
sender,
|
2021-07-27 20:30:41 +00:00
|
|
|
inGroup: conversation.type === 'group',
|
2021-06-17 17:15:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other utility functions
|
|
|
|
|
2021-11-11 23:45:47 +00:00
|
|
|
export function isTapToView(message: MessageWithUIFieldsType): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
// If a message is deleted for everyone, that overrides all other styling
|
|
|
|
if (message.deletedForEveryone) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Boolean(message.isViewOnce || message.messageTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getMessagePropStatus(
|
2021-07-19 22:44:49 +00:00
|
|
|
message: Pick<
|
2021-11-11 23:45:47 +00:00
|
|
|
MessageWithUIFieldsType,
|
2022-03-04 19:22:31 +00:00
|
|
|
| 'deletedForEveryone'
|
|
|
|
| 'deletedForEveryoneFailed'
|
|
|
|
| 'deletedForEveryoneSendStatus'
|
|
|
|
| 'errors'
|
|
|
|
| 'sendStateByConversationId'
|
|
|
|
| 'type'
|
2021-07-19 22:44:49 +00:00
|
|
|
>,
|
2022-02-23 18:48:40 +00:00
|
|
|
ourConversationId: string | undefined
|
2021-06-17 17:15:10 +00:00
|
|
|
): LastMessageStatus | undefined {
|
2021-07-12 23:51:45 +00:00
|
|
|
if (!isOutgoing(message)) {
|
|
|
|
return undefined;
|
2021-07-09 21:38:51 +00:00
|
|
|
}
|
2021-07-12 23:51:45 +00:00
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
if (getLastChallengeError(message)) {
|
|
|
|
return 'paused';
|
|
|
|
}
|
|
|
|
|
2022-03-04 19:22:31 +00:00
|
|
|
const {
|
|
|
|
deletedForEveryone,
|
|
|
|
deletedForEveryoneFailed,
|
|
|
|
deletedForEveryoneSendStatus,
|
|
|
|
sendStateByConversationId = {},
|
|
|
|
} = message;
|
|
|
|
|
|
|
|
// Note: we only do anything here if deletedForEveryoneSendStatus exists, because old
|
|
|
|
// messages deleted for everyone won't have send status.
|
|
|
|
if (deletedForEveryone && deletedForEveryoneSendStatus) {
|
|
|
|
if (deletedForEveryoneFailed) {
|
|
|
|
const anySuccessfulSends = Object.values(
|
|
|
|
deletedForEveryoneSendStatus
|
|
|
|
).some(item => item);
|
|
|
|
|
|
|
|
return anySuccessfulSends ? 'partial-sent' : 'error';
|
|
|
|
}
|
|
|
|
const missingSends = Object.values(deletedForEveryoneSendStatus).some(
|
|
|
|
item => !item
|
|
|
|
);
|
|
|
|
if (missingSends) {
|
|
|
|
return 'sending';
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 22:44:49 +00:00
|
|
|
|
2022-02-23 18:48:40 +00:00
|
|
|
if (
|
|
|
|
ourConversationId &&
|
|
|
|
isMessageJustForMe(sendStateByConversationId, ourConversationId)
|
|
|
|
) {
|
2021-07-19 22:44:49 +00:00
|
|
|
const status =
|
|
|
|
sendStateByConversationId[ourConversationId]?.status ??
|
|
|
|
SendStatus.Pending;
|
|
|
|
const sent = isSent(status);
|
2021-08-31 20:58:39 +00:00
|
|
|
if (
|
|
|
|
hasErrors(message) ||
|
|
|
|
someSendStatus(sendStateByConversationId, isFailed)
|
|
|
|
) {
|
2021-07-19 22:44:49 +00:00
|
|
|
return sent ? 'partial-sent' : 'error';
|
|
|
|
}
|
2021-07-27 15:42:25 +00:00
|
|
|
return sent ? 'viewed' : 'sending';
|
2021-07-19 22:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const sendStates = Object.values(
|
2022-02-23 18:48:40 +00:00
|
|
|
ourConversationId
|
|
|
|
? omit(sendStateByConversationId, ourConversationId)
|
|
|
|
: sendStateByConversationId
|
2021-07-19 22:44:49 +00:00
|
|
|
);
|
|
|
|
const highestSuccessfulStatus = sendStates.reduce(
|
|
|
|
(result: SendStatus, { status }) => maxStatus(result, status),
|
|
|
|
SendStatus.Pending
|
|
|
|
);
|
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
if (
|
|
|
|
hasErrors(message) ||
|
|
|
|
someSendStatus(sendStateByConversationId, isFailed)
|
|
|
|
) {
|
2021-07-19 22:44:49 +00:00
|
|
|
return isSent(highestSuccessfulStatus) ? 'partial-sent' : 'error';
|
|
|
|
}
|
2021-07-27 15:42:25 +00:00
|
|
|
if (isViewed(highestSuccessfulStatus)) {
|
|
|
|
return 'viewed';
|
|
|
|
}
|
2021-07-21 19:21:16 +00:00
|
|
|
if (isRead(highestSuccessfulStatus)) {
|
2021-06-17 17:15:10 +00:00
|
|
|
return 'read';
|
|
|
|
}
|
2021-07-19 22:44:49 +00:00
|
|
|
if (isDelivered(highestSuccessfulStatus)) {
|
2021-06-17 17:15:10 +00:00
|
|
|
return 'delivered';
|
|
|
|
}
|
2021-07-19 22:44:49 +00:00
|
|
|
if (isSent(highestSuccessfulStatus)) {
|
2021-06-17 17:15:10 +00:00
|
|
|
return 'sent';
|
|
|
|
}
|
|
|
|
return 'sending';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPropsForEmbeddedContact(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2022-02-23 18:48:40 +00:00
|
|
|
regionCode: string | undefined,
|
2022-04-12 00:26:09 +00:00
|
|
|
accountSelector: (identifier?: string) => UUIDStringType | undefined
|
2021-08-20 01:56:39 +00:00
|
|
|
): EmbeddedContactType | undefined {
|
2021-06-17 17:15:10 +00:00
|
|
|
const contacts = message.contact;
|
|
|
|
if (!contacts || !contacts.length) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const firstContact = contacts[0];
|
|
|
|
const numbers = firstContact?.number;
|
|
|
|
const firstNumber = numbers && numbers[0] ? numbers[0].value : undefined;
|
|
|
|
|
2021-08-20 01:56:39 +00:00
|
|
|
return embeddedContactSelector(firstContact, {
|
2021-06-17 17:15:10 +00:00
|
|
|
regionCode,
|
|
|
|
getAbsoluteAttachmentPath:
|
|
|
|
window.Signal.Migrations.getAbsoluteAttachmentPath,
|
|
|
|
firstNumber,
|
2022-04-12 00:26:09 +00:00
|
|
|
uuid: accountSelector(firstNumber),
|
2021-06-17 17:15:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPropsForAttachment(
|
2021-06-24 19:05:27 +00:00
|
|
|
attachment: AttachmentType
|
2021-06-17 17:15:10 +00:00
|
|
|
): AttachmentType | null {
|
|
|
|
if (!attachment) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { path, pending, size, screenshot, thumbnail } = attachment;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...attachment,
|
2021-06-24 19:05:27 +00:00
|
|
|
fileSize: size ? filesize(size) : undefined,
|
2021-06-17 17:15:10 +00:00
|
|
|
isVoiceMessage: isVoiceMessage(attachment),
|
|
|
|
pending,
|
2021-06-24 19:05:27 +00:00
|
|
|
url: path
|
|
|
|
? window.Signal.Migrations.getAbsoluteAttachmentPath(path)
|
|
|
|
: undefined,
|
2021-06-17 17:15:10 +00:00
|
|
|
screenshot: screenshot
|
|
|
|
? {
|
|
|
|
...screenshot,
|
|
|
|
url: window.Signal.Migrations.getAbsoluteAttachmentPath(
|
|
|
|
screenshot.path
|
|
|
|
),
|
|
|
|
}
|
2021-06-24 19:05:27 +00:00
|
|
|
: undefined,
|
2021-06-17 17:15:10 +00:00
|
|
|
thumbnail: thumbnail
|
|
|
|
? {
|
|
|
|
...thumbnail,
|
|
|
|
url: window.Signal.Migrations.getAbsoluteAttachmentPath(
|
|
|
|
thumbnail.path
|
|
|
|
),
|
|
|
|
}
|
2021-06-24 19:05:27 +00:00
|
|
|
: undefined,
|
2021-06-17 17:15:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function processQuoteAttachment(
|
|
|
|
attachment: AttachmentType
|
|
|
|
): QuotedAttachmentType {
|
|
|
|
const { thumbnail } = attachment;
|
|
|
|
const path =
|
|
|
|
thumbnail &&
|
|
|
|
thumbnail.path &&
|
|
|
|
window.Signal.Migrations.getAbsoluteAttachmentPath(thumbnail.path);
|
|
|
|
const objectUrl = thumbnail && thumbnail.objectUrl;
|
|
|
|
|
|
|
|
const thumbnailWithObjectUrl =
|
|
|
|
(!path && !objectUrl) || !thumbnail
|
|
|
|
? undefined
|
|
|
|
: { ...thumbnail, objectUrl: path || objectUrl };
|
|
|
|
|
|
|
|
return {
|
|
|
|
...attachment,
|
|
|
|
isVoiceMessage: isVoiceMessage(attachment),
|
|
|
|
thumbnail: thumbnailWithObjectUrl,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-29 23:19:44 +00:00
|
|
|
function canReplyOrReact(
|
2021-07-01 23:57:10 +00:00
|
|
|
message: Pick<
|
2021-11-11 23:45:47 +00:00
|
|
|
MessageWithUIFieldsType,
|
2021-10-29 23:19:44 +00:00
|
|
|
'deletedForEveryone' | 'sendStateByConversationId' | 'type'
|
2021-07-01 23:57:10 +00:00
|
|
|
>,
|
2022-02-23 18:48:40 +00:00
|
|
|
ourConversationId: string | undefined,
|
2021-10-29 23:19:44 +00:00
|
|
|
conversation: undefined | Readonly<ConversationType>
|
2021-06-17 17:15:10 +00:00
|
|
|
): boolean {
|
2021-07-19 22:44:49 +00:00
|
|
|
const { deletedForEveryone, sendStateByConversationId } = message;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
if (!conversation) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conversation.isGroupV1AndDisabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isMissingRequiredProfileSharing(conversation)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!conversation.acceptedMessageRequest) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-01 23:57:10 +00:00
|
|
|
if (deletedForEveryone) {
|
2021-06-17 17:15:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-01 23:57:10 +00:00
|
|
|
if (isOutgoing(message)) {
|
2021-07-19 22:44:49 +00:00
|
|
|
return (
|
|
|
|
isMessageJustForMe(sendStateByConversationId, ourConversationId) ||
|
2022-02-23 18:48:40 +00:00
|
|
|
someSendStatus(
|
|
|
|
ourConversationId
|
|
|
|
? omit(sendStateByConversationId, ourConversationId)
|
|
|
|
: sendStateByConversationId,
|
|
|
|
isSent
|
|
|
|
)
|
2021-07-19 22:44:49 +00:00
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 21:14:52 +00:00
|
|
|
// If we get past all the other checks above then we can always reply or
|
|
|
|
// react if the message type is "incoming" | "story"
|
|
|
|
if (isIncoming(message) || isStory(message)) {
|
2021-06-17 17:15:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail safe.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-29 23:19:44 +00:00
|
|
|
export function canReply(
|
|
|
|
message: Pick<
|
2021-11-11 23:45:47 +00:00
|
|
|
MessageWithUIFieldsType,
|
2021-10-29 23:19:44 +00:00
|
|
|
| 'conversationId'
|
|
|
|
| 'deletedForEveryone'
|
|
|
|
| 'sendStateByConversationId'
|
|
|
|
| 'type'
|
|
|
|
>,
|
2022-02-23 18:48:40 +00:00
|
|
|
ourConversationId: string | undefined,
|
2021-10-29 23:19:44 +00:00
|
|
|
conversationSelector: GetConversationByIdType
|
|
|
|
): boolean {
|
|
|
|
const conversation = getConversation(message, conversationSelector);
|
|
|
|
if (
|
|
|
|
!conversation ||
|
|
|
|
(conversation.announcementsOnly && !conversation.areWeAdmin)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return canReplyOrReact(message, ourConversationId, conversation);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function canReact(
|
|
|
|
message: Pick<
|
2021-11-11 23:45:47 +00:00
|
|
|
MessageWithUIFieldsType,
|
2021-10-29 23:19:44 +00:00
|
|
|
| 'conversationId'
|
|
|
|
| 'deletedForEveryone'
|
|
|
|
| 'sendStateByConversationId'
|
|
|
|
| 'type'
|
|
|
|
>,
|
2022-02-23 18:48:40 +00:00
|
|
|
ourConversationId: string | undefined,
|
2021-10-29 23:19:44 +00:00
|
|
|
conversationSelector: GetConversationByIdType
|
|
|
|
): boolean {
|
|
|
|
const conversation = getConversation(message, conversationSelector);
|
|
|
|
return canReplyOrReact(message, ourConversationId, conversation);
|
|
|
|
}
|
|
|
|
|
2021-08-02 19:26:48 +00:00
|
|
|
export function canDeleteForEveryone(
|
|
|
|
message: Pick<
|
2021-11-11 23:45:47 +00:00
|
|
|
MessageWithUIFieldsType,
|
2021-08-02 19:26:48 +00:00
|
|
|
'type' | 'deletedForEveryone' | 'sent_at' | 'sendStateByConversationId'
|
|
|
|
>
|
|
|
|
): boolean {
|
|
|
|
return (
|
|
|
|
// Is this a message I sent?
|
|
|
|
isOutgoing(message) &&
|
|
|
|
// Has the message already been deleted?
|
|
|
|
!message.deletedForEveryone &&
|
|
|
|
// Is it too old to delete?
|
|
|
|
isMoreRecentThan(message.sent_at, THREE_HOURS) &&
|
2021-12-02 21:28:19 +00:00
|
|
|
// Is it sent to anyone?
|
|
|
|
someSendStatus(message.sendStateByConversationId, isSent)
|
2021-08-02 19:26:48 +00:00
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 19:22:31 +00:00
|
|
|
export function canRetryDeleteForEveryone(
|
|
|
|
message: Pick<
|
|
|
|
MessageWithUIFieldsType,
|
|
|
|
'deletedForEveryone' | 'deletedForEveryoneFailed' | 'sent_at'
|
|
|
|
>
|
|
|
|
): boolean {
|
|
|
|
return Boolean(
|
|
|
|
message.deletedForEveryone &&
|
|
|
|
message.deletedForEveryoneFailed &&
|
|
|
|
// Is it too old to delete?
|
|
|
|
isMoreRecentThan(message.sent_at, DAY)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
export function canDownload(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: MessageWithUIFieldsType,
|
2021-06-17 17:15:10 +00:00
|
|
|
conversationSelector: GetConversationByIdType
|
|
|
|
): boolean {
|
|
|
|
if (isOutgoing(message)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const conversation = getConversation(message, conversationSelector);
|
|
|
|
const isAccepted = Boolean(
|
|
|
|
conversation && conversation.acceptedMessageRequest
|
|
|
|
);
|
|
|
|
if (!isAccepted) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that all attachments are downloadable
|
|
|
|
const { attachments } = message;
|
|
|
|
if (attachments && attachments.length) {
|
|
|
|
return attachments.every(attachment => Boolean(attachment.path));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getLastChallengeError(
|
2021-11-11 23:45:47 +00:00
|
|
|
message: Pick<MessageWithUIFieldsType, 'errors'>
|
2021-06-17 17:15:10 +00:00
|
|
|
): ShallowChallengeError | undefined {
|
|
|
|
const { errors } = message;
|
|
|
|
if (!errors) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const challengeErrors = errors
|
|
|
|
.filter((error): error is ShallowChallengeError => {
|
|
|
|
return (
|
|
|
|
error.name === 'SendMessageChallengeError' &&
|
|
|
|
isNumber(error.retryAfter) &&
|
|
|
|
isObject(error.data)
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.sort((a, b) => a.retryAfter - b.retryAfter);
|
|
|
|
|
|
|
|
return challengeErrors.pop();
|
|
|
|
}
|