signal-desktop/ts/state/selectors/message.ts

1798 lines
47 KiB
TypeScript
Raw Normal View History

2022-01-26 23:05:26 +00:00
// Copyright 2021-2022 Signal Messenger, LLC
// 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';
2022-07-01 00:52:03 +00:00
import { createSelector, createSelectorCreator } from 'reselect';
import filesize from 'filesize';
import getDirection from 'direction';
import emojiRegex from 'emoji-regex';
import LinkifyIt from 'linkify-it';
import type {
LastMessageStatus,
MessageReactionType,
ShallowChallengeError,
} from '../../model-types.d';
import type { TimelineItemType } from '../../components/conversation/TimelineItem';
import type { PropsData } from '../../components/conversation/Message';
2022-11-04 13:22:07 +00:00
import type { PropsData as TimelineMessagePropsData } from '../../components/conversation/TimelineMessage';
import { TextDirection } from '../../components/conversation/Message';
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 {
PropsData as GroupNotificationProps,
ChangeType,
} from '../../components/conversation/GroupNotification';
import type { PropsType as ProfileChangeNotificationPropsType } from '../../components/conversation/ProfileChangeNotification';
import type { QuotedAttachmentType } from '../../components/conversation/Quote';
import { getDomain, isStickerPack } from '../../types/LinkPreview';
2021-10-26 22:59:08 +00:00
import type { UUIDStringType } from '../../types/UUID';
import type { EmbeddedContactType } from '../../types/EmbeddedContact';
import { embeddedContactSelector } from '../../types/EmbeddedContact';
2022-11-10 04:59:36 +00:00
import type { AssertProps, HydratedBodyRangesType } from '../../types/Util';
import type { LinkPreviewType } from '../../types/message/LinkPreviews';
import { getMentionsRegex } from '../../types/Message';
import { CallMode } from '../../types/Calling';
2021-07-09 19:36:10 +00:00
import { SignalService as Proto } from '../../protobuf';
import type { AttachmentType } from '../../types/Attachment';
import { isVoiceMessage, canBeDownloaded } from '../../types/Attachment';
import { ReadStatus } from '../../messages/MessageReadStatus';
import type { CallingNotificationType } from '../../util/callingNotification';
2021-08-11 16:23:21 +00:00
import { memoizeByRoot } from '../../util/memoizeByRoot';
import { missingCaseError } from '../../util/missingCaseError';
import { isNotNil } from '../../util/isNotNil';
import { isMoreRecentThan } from '../../util/timestamp';
import * as iterables from '../../util/iterables';
import { strictAssert } from '../../util/assert';
2022-07-01 00:52:03 +00:00
import { getAccountSelector } from './accounts';
import {
getContactNameColorSelector,
getConversationSelector,
getSelectedMessage,
isMissingRequiredProfileSharing,
} from './conversations';
import {
getRegionCode,
getUserConversationId,
getUserNumber,
2022-07-08 20:46:25 +00:00
getUserACI,
getUserPNI,
2022-07-01 00:52:03 +00:00
} from './user';
import type {
ConversationType,
MessageWithUIFieldsType,
} from '../ducks/conversations';
import type { AccountSelectorType } from './accounts';
import type { CallSelectorType, CallStateType } from './calling';
import type {
GetConversationByIdType,
2021-08-19 20:14:41 +00:00
ContactNameColorSelectorType,
} from './conversations';
import {
SendStatus,
isDelivered,
isFailed,
isMessageJustForMe,
isRead,
isSent,
isViewed,
maxStatus,
someSendStatus,
} from '../../messages/MessageSendState';
import * as log from '../../logging/log';
import { getConversationColorAttributes } from '../../util/getConversationColorAttributes';
import { DAY, HOUR, SECOND } from '../../util/durations';
import { getStoryReplyText } from '../../util/getStoryReplyText';
import { isIncoming, isOutgoing, isStory } from '../../messages/helpers';
import { calculateExpirationTimestamp } from '../../util/expirationTimer';
2022-11-09 02:38:19 +00:00
import { isSignalConversation } from '../../util/isSignalConversation';
export { isIncoming, isOutgoing, isStory };
const THREE_HOURS = 3 * HOUR;
const linkify = LinkifyIt();
type FormattedContact = Partial<ConversationType> &
Pick<
ConversationType,
| 'acceptedMessageRequest'
| 'id'
| 'isMe'
| 'sharedGroupNames'
| 'title'
| 'type'
| 'unblurredAvatarPath'
>;
2022-11-04 13:22:07 +00:00
export type PropsForMessage = Omit<TimelineMessagePropsData, 'interactionMode'>;
type PropsForUnsupportedMessage = {
canProcessNow: boolean;
contact: FormattedContact;
};
2021-08-11 16:23:21 +00:00
export type GetPropsForBubbleOptions = Readonly<{
conversationSelector: GetConversationByIdType;
ourConversationId?: string;
2021-08-11 16:23:21 +00:00
ourNumber?: string;
2022-07-08 20:46:25 +00:00
ourACI?: UUIDStringType;
ourPNI?: UUIDStringType;
2021-08-11 16:23:21 +00:00
selectedMessageId?: string;
selectedMessageCounter?: number;
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
}>;
export function hasErrors(
message: Pick<MessageWithUIFieldsType, 'errors'>
): boolean {
return message.errors ? message.errors.length > 0 : false;
}
export function getSource(
message: MessageWithUIFieldsType,
ourNumber: string | undefined
): string | undefined {
if (isIncoming(message)) {
return message.source;
}
if (!isOutgoing(message)) {
log.warn('message.getSource: Called for non-incoming/non-outoing message');
}
return ourNumber;
}
export function getSourceDevice(
message: MessageWithUIFieldsType,
ourDeviceId: number
): string | number | undefined {
const { sourceDevice } = message;
if (isIncoming(message)) {
return sourceDevice;
}
if (!isOutgoing(message)) {
log.warn(
'message.getSourceDevice: Called for non-incoming/non-outoing message'
);
}
return sourceDevice || ourDeviceId;
}
export function getSourceUuid(
message: MessageWithUIFieldsType,
2022-07-08 20:46:25 +00:00
ourACI: string | undefined
): string | undefined {
if (isIncoming(message)) {
return message.sourceUuid;
}
if (!isOutgoing(message)) {
log.warn(
'message.getSourceUuid: Called for non-incoming/non-outoing message'
);
}
2022-07-08 20:46:25 +00:00
return ourACI;
}
2021-10-28 22:39:16 +00:00
export type GetContactOptions = Pick<
GetPropsForBubbleOptions,
2022-07-08 20:46:25 +00:00
| 'conversationSelector'
| 'ourConversationId'
| 'ourNumber'
| 'ourACI'
| 'ourPNI'
2021-10-28 22:39:16 +00:00
>;
export function getContactId(
message: MessageWithUIFieldsType,
2021-10-28 22:39:16 +00:00
{
conversationSelector,
ourConversationId,
ourNumber,
2022-07-08 20:46:25 +00:00
ourACI,
2021-10-28 22:39:16 +00:00
}: GetContactOptions
): string | undefined {
const source = getSource(message, ourNumber);
2022-07-08 20:46:25 +00:00
const sourceUuid = getSourceUuid(message, ourACI);
if (!source && !sourceUuid) {
return ourConversationId;
}
const conversation = conversationSelector(sourceUuid || source);
return conversation.id;
}
2021-08-11 16:23:21 +00:00
// TODO: DESKTOP-2145
export function getContact(
message: MessageWithUIFieldsType,
2021-08-11 16:23:21 +00:00
{
conversationSelector,
ourConversationId,
ourNumber,
2022-07-08 20:46:25 +00:00
ourACI,
2021-08-11 16:23:21 +00:00
}: GetContactOptions
): ConversationType {
const source = getSource(message, ourNumber);
2022-07-08 20:46:25 +00:00
const sourceUuid = getSourceUuid(message, ourACI);
if (!source && !sourceUuid) {
return conversationSelector(ourConversationId);
}
return conversationSelector(sourceUuid || source);
}
export function getConversation(
message: Pick<MessageWithUIFieldsType, 'conversationId'>,
conversationSelector: GetConversationByIdType
): ConversationType {
return conversationSelector(message.conversationId);
}
// Message
2021-08-11 16:23:21 +00:00
export const getAttachmentsForMessage = createSelectorCreator(memoizeByRoot)(
// `memoizeByRoot` requirement
identity,
({ sticker }: MessageWithUIFieldsType) => sticker,
({ attachments }: MessageWithUIFieldsType) => attachments,
(_, 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-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-08-11 16:23:21 +00:00
return attachments
.filter(attachment => !attachment.error || canBeDownloaded(attachment))
2021-08-11 16:23:21 +00:00
.map(attachment => getPropsForAttachment(attachment))
.filter(isNotNil);
}
);
2021-08-11 16:23:21 +00:00
export const processBodyRanges = createSelectorCreator(memoizeByRoot, isEqual)(
// `memoizeByRoot` requirement
identity,
2021-08-11 16:23:21 +00:00
(
{ bodyRanges }: Pick<MessageWithUIFieldsType, 'bodyRanges'>,
2021-08-11 16:23:21 +00:00
{ conversationSelector }: { conversationSelector: GetConversationByIdType }
2022-11-10 04:59:36 +00:00
): HydratedBodyRangesType | undefined => {
2021-08-11 16:23:21 +00:00
if (!bodyRanges) {
return undefined;
}
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);
},
2022-11-10 04:59:36 +00:00
(_, ranges): undefined | HydratedBodyRangesType => 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,
(_, convo: ConversationType): PropsData['author'] => {
2021-10-28 22:39:16 +00:00
const {
acceptedMessageRequest,
avatarPath,
badges,
2021-10-28 22:39:16 +00:00
color,
id,
isMe,
name,
phoneNumber,
profileName,
sharedGroupNames,
title,
unblurredAvatarPath,
} = convo;
const unsafe = {
acceptedMessageRequest,
avatarPath,
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,
(_, author): PropsData['author'] => author
2021-08-11 16:23:21 +00:00
);
export const getPreviewsForMessage = createSelectorCreator(memoizeByRoot)(
// `memoizeByRoot` requirement
identity,
({ preview }: MessageWithUIFieldsType) => preview,
(_, 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) : undefined,
2021-08-11 16:23:21 +00:00
}));
}
);
export const getReactionsForMessage = createSelectorCreator(
memoizeByRoot,
isEqual
)(
// `memoizeByRoot` requirement
identity,
(
{ reactions = [] }: MessageWithUIFieldsType,
2021-08-11 16:23:21 +00:00
{ conversationSelector }: { conversationSelector: GetConversationByIdType }
) => {
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;
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,
};
});
return [...formattedReactions];
2021-08-11 16:23:21 +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-11-02 23:48:38 +00:00
'body' | 'conversationId' | 'storyReaction' | 'storyReplyContext'
2022-03-16 17:30:14 +00:00
>,
{
conversationSelector,
ourConversationId,
}: {
conversationSelector: GetConversationByIdType;
ourConversationId?: string;
}
): PropsData['storyReplyContext'] => {
2022-11-02 23:48:38 +00:00
const { storyReaction, storyReplyContext } = message;
2022-03-16 17:30:14 +00:00
if (!storyReplyContext) {
return undefined;
}
const contact = conversationSelector(storyReplyContext.authorUuid);
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-11-02 23:48:38 +00:00
emoji: storyReaction?.emoji,
2022-03-16 17:30:14 +00:00
isFromMe,
rawAttachment: storyReplyContext.attachment
? processQuoteAttachment(storyReplyContext.attachment)
: undefined,
2022-07-06 19:06:20 +00:00
storyId: 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,
(
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);
const { conversationColor, customColor } =
getConversationColorAttributes(conversation);
2021-08-11 23:06:20 +00:00
return {
authorId,
authorName,
authorPhoneNumber,
authorProfileName,
authorTitle,
bodyRanges: processBodyRanges(quote, { conversationSelector }),
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
};
},
(_, 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'
2022-07-08 20:46:25 +00:00
| 'ourACI'
| 'ourPNI'
2021-08-19 20:14:41 +00:00
| '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'
| 'canReact'
2021-08-11 23:06:20 +00:00
| 'canReply'
| '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'
| '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'
| 'readStatus'
2021-08-11 23:06:20 +00:00
| 'selectedReaction'
| 'status'
| 'text'
| 'textDirection'
2021-08-11 23:06:20 +00:00
| 'timestamp'
>;
const getShallowPropsForMessage = createSelectorCreator(memoizeByRoot, isEqual)(
// `memoizeByRoot` requirement
identity,
2021-08-11 16:23:21 +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,
2022-07-08 20:46:25 +00:00
ourACI,
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;
const expirationLength = expireTimer ? expireTimer * SECOND : undefined;
2021-08-11 16:23:21 +00:00
const conversation = getConversation(message, conversationSelector);
const isGroup = conversation.type === 'group';
const { sticker } = message;
2021-08-11 16:23:21 +00:00
const isMessageTapToView = isTapToView(message);
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-10-28 22:39:16 +00:00
const authorId = getContactId(message, {
2021-08-19 20:14:41 +00:00
conversationSelector,
ourConversationId,
ourNumber,
2022-07-08 20:46:25 +00:00
ourACI,
2021-08-19 20:14:41 +00:00
});
2021-10-28 22:39:16 +00:00
const contactNameColor = contactNameColorSelector(conversationId, authorId);
2021-08-19 20:14:41 +00:00
const { conversationColor, customColor } =
getConversationColorAttributes(conversation);
2021-08-11 16:23:21 +00:00
return {
canDeleteForEveryone: canDeleteForEveryone(message),
canDownload: canDownload(message, conversationSelector),
canReact: canReact(message, ourConversationId, conversationSelector),
2021-08-11 16:23:21 +00:00
canReply: canReply(message, ourConversationId, conversationSelector),
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,
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',
customColor,
2021-08-11 16:23:21 +00:00
deletedForEveryone: message.deletedForEveryone || false,
direction: isIncoming(message) ? 'incoming' : 'outgoing',
displayLimit: message.displayLimit,
2021-08-11 16:23:21 +00:00
expirationLength,
expirationTimestamp: calculateExpirationTimestamp({
expireTimer,
expirationStartTimestamp,
}),
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,
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,
textDirection: getTextDirection(message.body),
2021-08-11 16:23:21 +00:00
timestamp: message.sent_at,
};
2021-08-11 23:06:20 +00:00
},
(_: unknown, props: ShallowPropsType) => props
);
function getTextAttachment(
message: MessageWithUIFieldsType
): AttachmentType | undefined {
return (
message.bodyAttachment && getPropsForAttachment(message.bodyAttachment)
);
}
export function cleanBodyForDirectionCheck(text: string): string {
const MENTIONS_REGEX = getMentionsRegex();
const EMOJI_REGEX = emojiRegex();
const initial = text.replace(MENTIONS_REGEX, '').replace(EMOJI_REGEX, '');
const linkMatches = linkify.match(initial);
if (!linkMatches || linkMatches.length === 0) {
return initial;
}
let result = '';
let lastIndex = 0;
linkMatches.forEach(match => {
if (lastIndex < match.index) {
result += initial.slice(lastIndex, match.index);
}
// drop the actual contents of the match
lastIndex = match.lastIndex;
});
if (lastIndex < initial.length) {
result += initial.slice(lastIndex);
}
return result;
}
function getTextDirection(body?: string): TextDirection {
if (!body) {
return TextDirection.None;
}
const cleaned = cleanBodyForDirectionCheck(body);
const direction = getDirection(cleaned);
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;
}
}
}
export const getPropsForMessage: (
message: MessageWithUIFieldsType,
options: GetPropsForMessageOptions
2022-11-04 13:22:07 +00:00
) => Omit<PropsForMessage, 'renderingContext' | 'menu' | 'contextMenu'> =
createSelectorCreator(memoizeByRoot)(
// `memoizeByRoot` requirement
identity,
getAttachmentsForMessage,
processBodyRanges,
getCachedAuthorForMessage,
getPreviewsForMessage,
getReactionsForMessage,
getPropsForQuote,
getPropsForStoryReplyContext,
getTextAttachment,
getShallowPropsForMessage,
(
_,
attachments: Array<AttachmentType>,
2022-11-10 04:59:36 +00:00
bodyRanges: HydratedBodyRangesType | undefined,
2022-11-04 13:22:07 +00:00
author: PropsData['author'],
previews: Array<LinkPreviewType>,
reactions: PropsData['reactions'],
quote: PropsData['quote'],
storyReplyContext: PropsData['storyReplyContext'],
textAttachment: PropsData['textAttachment'],
shallowProps: ShallowPropsType
): Omit<PropsForMessage, 'renderingContext' | 'menu' | 'contextMenu'> => {
return {
attachments,
author,
bodyRanges,
previews,
quote,
reactions,
storyReplyContext,
textAttachment,
...shallowProps,
};
}
);
2022-07-01 00:52:03 +00:00
// This is getPropsForMessage but wrapped in reselect's createSelector so that
// we can derive all of the selector dependencies that getPropsForMessage
// requires and you won't have to pass them in. For use within a smart/connected
// component that has access to selectors.
export const getMessagePropsSelector = createSelector(
getConversationSelector,
getUserConversationId,
2022-07-08 20:46:25 +00:00
getUserACI,
getUserPNI,
2022-07-01 00:52:03 +00:00
getUserNumber,
getRegionCode,
getAccountSelector,
getContactNameColorSelector,
getSelectedMessage,
(
conversationSelector,
ourConversationId,
2022-07-08 20:46:25 +00:00
ourACI,
ourPNI,
2022-07-01 00:52:03 +00:00
ourNumber,
regionCode,
accountSelector,
contactNameColorSelector,
selectedMessage
) =>
(message: MessageWithUIFieldsType) => {
return getPropsForMessage(message, {
accountSelector,
contactNameColorSelector,
conversationSelector,
ourConversationId,
ourNumber,
2022-07-08 20:46:25 +00:00
ourACI,
ourPNI,
2022-07-01 00:52:03 +00:00
regionCode,
selectedMessageCounter: selectedMessage?.counter,
selectedMessageId: selectedMessage?.id,
});
}
);
2021-08-11 23:06:20 +00:00
export const getBubblePropsForMessage = createSelectorCreator(memoizeByRoot)(
// `memoizeByRoot` requirement
identity,
getPropsForMessage,
(_, 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(
message: MessageWithUIFieldsType,
2021-08-11 23:06:20 +00:00
options: GetPropsForBubbleOptions
): TimelineItemType {
2022-01-26 23:05:26 +00:00
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);
}
// Unsupported Message
export function isUnsupportedMessage(
message: MessageWithUIFieldsType
): boolean {
const versionAtReceive = message.supportedVersionAtReceive;
const requiredVersion = message.requiredProtocolVersion;
return (
isNumber(versionAtReceive) &&
isNumber(requiredVersion) &&
versionAtReceive < requiredVersion
);
}
function getPropsForUnsupportedMessage(
message: MessageWithUIFieldsType,
2021-08-11 16:23:21 +00:00
options: GetContactOptions
): PropsForUnsupportedMessage {
2021-07-09 19:36:10 +00:00
const CURRENT_PROTOCOL_VERSION = Proto.DataMessage.ProtocolVersion.CURRENT;
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),
};
}
// GroupV2 Change
export function isGroupV2Change(message: MessageWithUIFieldsType): boolean {
return Boolean(message.groupV2Change);
}
function getPropsForGroupV2Change(
message: MessageWithUIFieldsType,
2022-07-08 20:46:25 +00:00
{ conversationSelector, ourACI, ourPNI }: GetPropsForBubbleOptions
): GroupsV2Props {
const change = message.groupV2Change;
if (!change) {
throw new Error('getPropsForGroupV2Change: Change is missing!');
}
const conversation = getConversation(message, conversationSelector);
return {
areWeAdmin: Boolean(conversation.areWeAdmin),
groupName: conversation?.type === 'group' ? conversation?.name : undefined,
groupMemberships: conversation.memberships,
groupBannedMemberships: conversation.bannedMemberships,
2022-07-08 20:46:25 +00:00
ourACI,
ourPNI,
change,
};
}
// GroupV1 Migration
export function isGroupV1Migration(message: MessageWithUIFieldsType): boolean {
return message.type === 'group-v1-migration';
}
function getPropsForGroupV1Migration(
message: MessageWithUIFieldsType,
2021-08-11 16:23:21 +00:00
{ conversationSelector }: GetPropsForBubbleOptions
): 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)
);
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)
);
const droppedMembers = droppedMemberIds.map(conversationId =>
conversationSelector(conversationId)
);
return {
areWeInvited,
droppedMembers,
invitedMembers,
};
}
// Note: props are null!
// Expiration Timer Update
export function isExpirationTimerUpdate(
message: Pick<MessageWithUIFieldsType, 'flags'>
): boolean {
2021-07-09 19:36:10 +00:00
const flag = Proto.DataMessage.Flags.EXPIRATION_TIMER_UPDATE;
// eslint-disable-next-line no-bitwise
return Boolean(message.flags && message.flags & flag);
}
function getPropsForTimerNotification(
message: MessageWithUIFieldsType,
2021-08-11 16:23:21 +00:00
{ ourConversationId, conversationSelector }: GetPropsForBubbleOptions
): 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
export function isKeyChange(message: MessageWithUIFieldsType): boolean {
return message.type === 'keychange';
}
function getPropsForSafetyNumberNotification(
message: MessageWithUIFieldsType,
2021-08-11 16:23:21 +00:00
{ conversationSelector }: GetPropsForBubbleOptions
): 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
export function isVerifiedChange(message: MessageWithUIFieldsType): boolean {
return message.type === 'verified-change';
}
function getPropsForVerificationNotification(
message: MessageWithUIFieldsType,
2021-08-11 16:23:21 +00:00
{ conversationSelector }: GetPropsForBubbleOptions
): 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);
}
// Group Update (V1)
export function isGroupUpdate(
message: Pick<MessageWithUIFieldsType, 'group_update'>
): boolean {
return Boolean(message.group_update);
}
function getPropsForGroupNotification(
message: MessageWithUIFieldsType,
2021-08-11 16:23:21 +00:00
options: GetContactOptions
): 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;
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) {
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);
return {
from,
changes,
};
}
// End Session
export function isEndSession(
message: Pick<MessageWithUIFieldsType, 'flags'>
): boolean {
2021-07-09 19:36:10 +00:00
const flag = Proto.DataMessage.Flags.END_SESSION;
// eslint-disable-next-line no-bitwise
return Boolean(message.flags && message.flags & flag);
}
// Call History
export function isCallHistory(message: MessageWithUIFieldsType): boolean {
return message.type === 'call-history';
}
2021-08-11 16:23:21 +00:00
export type GetPropsForCallHistoryOptions = Pick<
GetPropsForBubbleOptions,
'conversationSelector' | 'callSelector' | 'activeCall'
>;
export function getPropsForCallHistory(
message: MessageWithUIFieldsType,
2021-08-11 16:23:21 +00:00
{
conversationSelector,
callSelector,
activeCall,
}: GetPropsForCallHistoryOptions
): CallingNotificationType {
const { callHistoryDetails } = message;
if (!callHistoryDetails) {
throw new Error('getPropsForCallHistory: Missing callHistoryDetails');
}
const activeCallConversationId = activeCall?.conversationId;
switch (callHistoryDetails.callMode) {
// Old messages weren't saved with a call mode.
case undefined:
case CallMode.Direct:
return {
...callHistoryDetails,
activeCallConversationId,
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) {
log.error(
'getPropsForCallHistory: there is an unexpected non-group call; pretending it does not exist'
);
call = undefined;
}
const creator = conversationSelector(callHistoryDetails.creatorUuid);
const deviceCount = call?.peekInfo?.deviceCount ?? 0;
return {
activeCallConversationId,
callMode: CallMode.Group,
conversationId,
creator,
deviceCount,
ended:
callHistoryDetails.eraId !== call?.peekInfo?.eraId || !deviceCount,
maxDevices: call?.peekInfo?.maxDevices ?? Infinity,
startedTime: callHistoryDetails.startedTime,
};
}
default:
throw new Error(
`getPropsForCallHistory: missing case ${missingCaseError(
callHistoryDetails
)}`
);
}
}
// Profile Change
export function isProfileChange(message: MessageWithUIFieldsType): boolean {
return message.type === 'profile-change';
}
function getPropsForProfileChange(
message: MessageWithUIFieldsType,
2021-08-11 16:23:21 +00:00
{ conversationSelector }: GetPropsForBubbleOptions
): 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(
message: MessageWithUIFieldsType
): boolean {
return message.type === 'universal-timer-notification';
}
2021-08-05 23:34:49 +00:00
// Change Number Notification
export function isChangeNumberNotification(
message: MessageWithUIFieldsType
2021-08-05 23:34:49 +00:00
): boolean {
return message.type === 'change-number-notification';
}
function getPropsForChangeNumberNotification(
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,
};
}
// Chat Session Refreshed
export function isChatSessionRefreshed(
message: MessageWithUIFieldsType
): boolean {
return message.type === 'chat-session-refreshed';
}
// Note: props are null
// Delivery Issue
export function isDeliveryIssue(message: MessageWithUIFieldsType): boolean {
return message.type === 'delivery-issue';
}
function getPropsForDeliveryIssue(
message: MessageWithUIFieldsType,
2021-08-11 16:23:21 +00:00
{ conversationSelector }: GetPropsForBubbleOptions
): DeliveryIssuePropsType {
const sender = conversationSelector(message.sourceUuid);
const conversation = conversationSelector(message.conversationId);
return {
sender,
inGroup: conversation.type === 'group',
};
}
// Other utility functions
export function isTapToView(message: MessageWithUIFieldsType): boolean {
// 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(
message: Pick<
MessageWithUIFieldsType,
| 'deletedForEveryone'
| 'deletedForEveryoneFailed'
| 'deletedForEveryoneSendStatus'
| 'errors'
| 'sendStateByConversationId'
| 'type'
>,
ourConversationId: string | undefined
): LastMessageStatus | undefined {
if (!isOutgoing(message)) {
return hasErrors(message) ? 'error' : undefined;
2021-07-09 21:38:51 +00:00
}
if (getLastChallengeError(message)) {
return 'paused';
}
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';
}
}
if (
ourConversationId &&
isMessageJustForMe(sendStateByConversationId, ourConversationId)
) {
const status =
sendStateByConversationId[ourConversationId]?.status ??
SendStatus.Pending;
const sent = isSent(status);
if (
hasErrors(message) ||
someSendStatus(sendStateByConversationId, isFailed)
) {
return sent ? 'partial-sent' : 'error';
}
return sent ? 'viewed' : 'sending';
}
const sendStates = Object.values(
ourConversationId
? omit(sendStateByConversationId, ourConversationId)
: sendStateByConversationId
);
const highestSuccessfulStatus = sendStates.reduce(
(result: SendStatus, { status }) => maxStatus(result, status),
SendStatus.Pending
);
if (
hasErrors(message) ||
someSendStatus(sendStateByConversationId, isFailed)
) {
return isSent(highestSuccessfulStatus) ? 'partial-sent' : 'error';
}
if (isViewed(highestSuccessfulStatus)) {
return 'viewed';
}
if (isRead(highestSuccessfulStatus)) {
return 'read';
}
if (isDelivered(highestSuccessfulStatus)) {
return 'delivered';
}
if (isSent(highestSuccessfulStatus)) {
return 'sent';
}
return 'sending';
}
export function getPropsForEmbeddedContact(
message: MessageWithUIFieldsType,
regionCode: string | undefined,
accountSelector: (identifier?: string) => UUIDStringType | undefined
): EmbeddedContactType | undefined {
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;
return embeddedContactSelector(firstContact, {
regionCode,
getAbsoluteAttachmentPath:
window.Signal.Migrations.getAbsoluteAttachmentPath,
firstNumber,
uuid: accountSelector(firstNumber),
});
}
export function getPropsForAttachment(
2021-06-24 19:05:27 +00:00
attachment: AttachmentType
): AttachmentType | undefined {
if (!attachment) {
return undefined;
}
const { path, pending, size, screenshot, thumbnail } = attachment;
return {
...attachment,
2021-06-24 19:05:27 +00:00
fileSize: size ? filesize(size) : undefined,
isVoiceMessage: isVoiceMessage(attachment),
pending,
2021-06-24 19:05:27 +00:00
url: path
? window.Signal.Migrations.getAbsoluteAttachmentPath(path)
: undefined,
screenshot: screenshot?.path
? {
...screenshot,
url: window.Signal.Migrations.getAbsoluteAttachmentPath(
screenshot.path
),
}
2021-06-24 19:05:27 +00:00
: undefined,
thumbnail: thumbnail?.path
? {
...thumbnail,
url: window.Signal.Migrations.getAbsoluteAttachmentPath(
thumbnail.path
),
}
2021-06-24 19:05:27 +00:00
: undefined,
};
}
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,
};
}
function canReplyOrReact(
2021-07-01 23:57:10 +00:00
message: Pick<
MessageWithUIFieldsType,
2022-07-01 00:52:03 +00:00
| 'canReplyToStory'
| 'deletedForEveryone'
| 'sendStateByConversationId'
| 'type'
2021-07-01 23:57:10 +00:00
>,
ourConversationId: string | undefined,
conversation: undefined | Readonly<ConversationType>
): boolean {
const { deletedForEveryone, sendStateByConversationId } = message;
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) {
return false;
}
2022-11-09 02:38:19 +00:00
if (isSignalConversation(conversation)) {
return false;
}
2021-07-01 23:57:10 +00:00
if (isOutgoing(message)) {
return (
isMessageJustForMe(sendStateByConversationId, ourConversationId) ||
someSendStatus(
ourConversationId
? omit(sendStateByConversationId, ourConversationId)
: sendStateByConversationId,
isSent
)
);
}
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"
2022-07-01 00:52:03 +00:00
if (isIncoming(message)) {
return true;
}
2022-07-01 00:52:03 +00:00
if (isStory(message)) {
2022-09-21 19:19:16 +00:00
return (
Boolean(message.canReplyToStory) && conversation.id !== ourConversationId
);
2022-07-01 00:52:03 +00:00
}
// Fail safe.
return false;
}
export function canReply(
message: Pick<
MessageWithUIFieldsType,
2022-07-01 00:52:03 +00:00
| 'canReplyToStory'
| 'conversationId'
| 'deletedForEveryone'
| 'sendStateByConversationId'
| 'type'
>,
ourConversationId: string | undefined,
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<
MessageWithUIFieldsType,
| 'conversationId'
| 'deletedForEveryone'
| 'sendStateByConversationId'
| 'type'
>,
ourConversationId: string | undefined,
conversationSelector: GetConversationByIdType
): boolean {
const conversation = getConversation(message, conversationSelector);
return canReplyOrReact(message, ourConversationId, conversation);
}
export function canDeleteForEveryone(
message: Pick<
MessageWithUIFieldsType,
'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) &&
// Is it sent to anyone?
someSendStatus(message.sendStateByConversationId, isSent)
);
}
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)
);
}
export function canDownload(
message: MessageWithUIFieldsType,
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(
message: Pick<MessageWithUIFieldsType, 'errors'>
): 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();
}