2021-06-17 17:15:10 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
import { isNumber, isObject, map, omit, reduce } from 'lodash';
|
2021-06-17 17:15:10 +00:00
|
|
|
import filesize from 'filesize';
|
|
|
|
|
|
|
|
import {
|
|
|
|
LastMessageStatus,
|
|
|
|
MessageAttributesType,
|
|
|
|
ShallowChallengeError,
|
|
|
|
} from '../../model-types.d';
|
|
|
|
|
|
|
|
import { TimelineItemType } from '../../components/conversation/TimelineItem';
|
|
|
|
import { PropsData } from '../../components/conversation/Message';
|
|
|
|
import { PropsData as TimerNotificationProps } from '../../components/conversation/TimerNotification';
|
|
|
|
import { PropsData as SafetyNumberNotificationProps } from '../../components/conversation/SafetyNumberNotification';
|
|
|
|
import { PropsData as VerificationNotificationProps } from '../../components/conversation/VerificationNotification';
|
|
|
|
import { PropsDataType as GroupsV2Props } from '../../components/conversation/GroupV2Change';
|
|
|
|
import { PropsDataType as GroupV1MigrationPropsType } from '../../components/conversation/GroupV1Migration';
|
|
|
|
import { PropsDataType as DeliveryIssuePropsType } from '../../components/conversation/DeliveryIssueNotification';
|
|
|
|
import {
|
|
|
|
PropsData as GroupNotificationProps,
|
|
|
|
ChangeType,
|
|
|
|
} from '../../components/conversation/GroupNotification';
|
|
|
|
import { PropsType as ProfileChangeNotificationPropsType } from '../../components/conversation/ProfileChangeNotification';
|
|
|
|
import { QuotedAttachmentType } from '../../components/conversation/Quote';
|
|
|
|
|
2021-06-30 17:00:02 +00:00
|
|
|
import { getDomain, isStickerPack } from '../../types/LinkPreview';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
import { ContactType, contactSelector } from '../../types/Contact';
|
|
|
|
import { BodyRangesType } from '../../types/Util';
|
|
|
|
import { LinkPreviewType } from '../../types/message/LinkPreviews';
|
|
|
|
import { ConversationColors } from '../../types/Colors';
|
|
|
|
import { CallMode } from '../../types/Calling';
|
2021-07-09 19:36:10 +00:00
|
|
|
import { SignalService as Proto } from '../../protobuf';
|
2021-06-17 17:15:10 +00:00
|
|
|
import { AttachmentType, isVoiceMessage } from '../../types/Attachment';
|
|
|
|
|
|
|
|
import { CallingNotificationType } from '../../util/callingNotification';
|
|
|
|
import { missingCaseError } from '../../util/missingCaseError';
|
|
|
|
import { isNotNil } from '../../util/isNotNil';
|
2021-08-02 19:26:48 +00:00
|
|
|
import { isMoreRecentThan } from '../../util/timestamp';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
import { ConversationType } from '../ducks/conversations';
|
|
|
|
|
|
|
|
import { CallSelectorType, CallStateType } from './calling';
|
|
|
|
import {
|
|
|
|
GetConversationByIdType,
|
|
|
|
isMissingRequiredProfileSharing,
|
|
|
|
} from './conversations';
|
2021-07-19 22:44:49 +00:00
|
|
|
import {
|
|
|
|
SendStatus,
|
|
|
|
isDelivered,
|
|
|
|
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-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
const THREE_HOURS = 3 * 60 * 60 * 1000;
|
|
|
|
|
|
|
|
type FormattedContact = Partial<ConversationType> &
|
|
|
|
Pick<
|
|
|
|
ConversationType,
|
|
|
|
| 'acceptedMessageRequest'
|
|
|
|
| 'id'
|
|
|
|
| 'isMe'
|
|
|
|
| 'sharedGroupNames'
|
|
|
|
| 'title'
|
|
|
|
| 'type'
|
|
|
|
| 'unblurredAvatarPath'
|
|
|
|
>;
|
|
|
|
type PropsForMessage = Omit<PropsData, 'interactionMode'>;
|
|
|
|
type PropsForUnsupportedMessage = {
|
|
|
|
canProcessNow: boolean;
|
|
|
|
contact: FormattedContact;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Top-level prop generation for the message bubble
|
|
|
|
export function getPropsForBubble(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType,
|
|
|
|
ourConversationId: string,
|
|
|
|
ourNumber: string | undefined,
|
|
|
|
ourUuid: string | undefined,
|
|
|
|
regionCode: string,
|
|
|
|
selectedMessageId: string | undefined,
|
|
|
|
selectedMessageCounter: number | undefined,
|
|
|
|
callSelector: CallSelectorType,
|
|
|
|
activeCall: CallStateType | undefined,
|
|
|
|
accountSelector: (identifier?: string) => boolean
|
|
|
|
): TimelineItemType {
|
|
|
|
if (isUnsupportedMessage(message)) {
|
|
|
|
return {
|
|
|
|
type: 'unsupportedMessage',
|
|
|
|
data: getPropsForUnsupportedMessage(
|
|
|
|
message,
|
|
|
|
conversationSelector,
|
|
|
|
ourConversationId,
|
|
|
|
ourNumber,
|
|
|
|
ourUuid
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isGroupV2Change(message)) {
|
|
|
|
return {
|
|
|
|
type: 'groupV2Change',
|
|
|
|
data: getPropsForGroupV2Change(
|
|
|
|
message,
|
|
|
|
conversationSelector,
|
|
|
|
ourConversationId
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isGroupV1Migration(message)) {
|
|
|
|
return {
|
|
|
|
type: 'groupV1Migration',
|
|
|
|
data: getPropsForGroupV1Migration(message, conversationSelector),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isMessageHistoryUnsynced(message)) {
|
|
|
|
return {
|
|
|
|
type: 'linkNotification',
|
|
|
|
data: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isExpirationTimerUpdate(message)) {
|
|
|
|
return {
|
|
|
|
type: 'timerNotification',
|
|
|
|
data: getPropsForTimerNotification(
|
|
|
|
message,
|
|
|
|
conversationSelector,
|
|
|
|
ourConversationId
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isKeyChange(message)) {
|
|
|
|
return {
|
|
|
|
type: 'safetyNumberNotification',
|
|
|
|
data: getPropsForSafetyNumberNotification(message, conversationSelector),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isVerifiedChange(message)) {
|
|
|
|
return {
|
|
|
|
type: 'verificationNotification',
|
|
|
|
data: getPropsForVerificationNotification(message, conversationSelector),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isGroupUpdate(message)) {
|
|
|
|
return {
|
|
|
|
type: 'groupNotification',
|
|
|
|
data: getPropsForGroupNotification(
|
|
|
|
message,
|
|
|
|
conversationSelector,
|
|
|
|
ourConversationId,
|
|
|
|
ourNumber,
|
|
|
|
ourUuid
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isEndSession(message)) {
|
|
|
|
return {
|
|
|
|
type: 'resetSessionNotification',
|
|
|
|
data: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isCallHistory(message)) {
|
|
|
|
return {
|
|
|
|
type: 'callHistory',
|
|
|
|
data: getPropsForCallHistory(
|
|
|
|
message,
|
|
|
|
conversationSelector,
|
|
|
|
callSelector,
|
|
|
|
activeCall
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isProfileChange(message)) {
|
|
|
|
return {
|
|
|
|
type: 'profileChange',
|
|
|
|
data: getPropsForProfileChange(message, conversationSelector),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isUniversalTimerNotification(message)) {
|
|
|
|
return {
|
|
|
|
type: 'universalTimerNotification',
|
|
|
|
data: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isChatSessionRefreshed(message)) {
|
|
|
|
return {
|
|
|
|
type: 'chatSessionRefreshed',
|
|
|
|
data: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isDeliveryIssue(message)) {
|
|
|
|
return {
|
|
|
|
type: 'deliveryIssue',
|
|
|
|
data: getPropsForDeliveryIssue(message, conversationSelector),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'message',
|
|
|
|
data: getPropsForMessage(
|
|
|
|
message,
|
|
|
|
conversationSelector,
|
|
|
|
ourConversationId,
|
|
|
|
ourNumber,
|
|
|
|
ourUuid,
|
|
|
|
selectedMessageId,
|
|
|
|
selectedMessageCounter,
|
|
|
|
regionCode,
|
|
|
|
accountSelector
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-30 00:51:57 +00:00
|
|
|
export function isIncoming(
|
|
|
|
message: Pick<MessageAttributesType, 'type'>
|
|
|
|
): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return message.type === 'incoming';
|
|
|
|
}
|
|
|
|
|
2021-06-30 00:51:57 +00:00
|
|
|
export function isOutgoing(
|
|
|
|
message: Pick<MessageAttributesType, 'type'>
|
|
|
|
): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return message.type === 'outgoing';
|
|
|
|
}
|
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
export function hasErrors(
|
|
|
|
message: Pick<MessageAttributesType, 'errors'>
|
|
|
|
): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return message.errors ? message.errors.length > 0 : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSource(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
ourNumber: string | undefined
|
|
|
|
): string | undefined {
|
|
|
|
if (isIncoming(message)) {
|
|
|
|
return message.source;
|
|
|
|
}
|
2021-08-02 21:19:18 +00:00
|
|
|
if (!isOutgoing(message)) {
|
|
|
|
window.log.warn(
|
|
|
|
'message.getSource: Called for non-incoming/non-outoing message'
|
|
|
|
);
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
return ourNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSourceDevice(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
ourDeviceId: number
|
|
|
|
): string | number | undefined {
|
|
|
|
const { sourceDevice } = message;
|
|
|
|
|
|
|
|
if (isIncoming(message)) {
|
|
|
|
return sourceDevice;
|
|
|
|
}
|
2021-08-02 21:19:18 +00:00
|
|
|
if (!isOutgoing(message)) {
|
|
|
|
window.log.warn(
|
|
|
|
'message.getSourceDevice: Called for non-incoming/non-outoing message'
|
|
|
|
);
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
return sourceDevice || ourDeviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSourceUuid(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
ourUuid: string | undefined
|
|
|
|
): string | undefined {
|
|
|
|
if (isIncoming(message)) {
|
|
|
|
return message.sourceUuid;
|
|
|
|
}
|
2021-08-02 21:19:18 +00:00
|
|
|
if (!isOutgoing(message)) {
|
|
|
|
window.log.warn(
|
|
|
|
'message.getSourceUuid: Called for non-incoming/non-outoing message'
|
|
|
|
);
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
return ourUuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getContactId(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType,
|
|
|
|
ourConversationId: string,
|
|
|
|
ourNumber: string | undefined,
|
|
|
|
ourUuid: string | undefined
|
|
|
|
): string | undefined {
|
|
|
|
const source = getSource(message, ourNumber);
|
|
|
|
const sourceUuid = getSourceUuid(message, ourUuid);
|
|
|
|
|
|
|
|
if (!source && !sourceUuid) {
|
|
|
|
return ourConversationId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const conversation = conversationSelector(sourceUuid || source);
|
|
|
|
return conversation.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getContact(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType,
|
|
|
|
ourConversationId: string,
|
|
|
|
ourNumber: string | undefined,
|
|
|
|
ourUuid: string | undefined
|
|
|
|
): 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-06-22 23:16:50 +00:00
|
|
|
message: Pick<MessageAttributesType, 'conversationId'>,
|
2021-06-17 17:15:10 +00:00
|
|
|
conversationSelector: GetConversationByIdType
|
|
|
|
): ConversationType {
|
|
|
|
return conversationSelector(message.conversationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message
|
|
|
|
|
|
|
|
export function getPropsForMessage(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType,
|
|
|
|
ourConversationId: string,
|
|
|
|
ourNumber: string | undefined,
|
|
|
|
ourUuid: string | undefined,
|
|
|
|
selectedMessageId: string | undefined,
|
|
|
|
selectedMessageCounter: number | undefined,
|
|
|
|
regionCode: string,
|
|
|
|
accountSelector: (identifier?: string) => boolean
|
2021-06-29 19:58:29 +00:00
|
|
|
): Omit<PropsForMessage, 'renderingContext'> {
|
2021-06-17 17:15:10 +00:00
|
|
|
const contact = getContact(
|
|
|
|
message,
|
|
|
|
conversationSelector,
|
|
|
|
ourConversationId,
|
|
|
|
ourNumber,
|
|
|
|
ourUuid
|
|
|
|
);
|
|
|
|
|
|
|
|
const { expireTimer, expirationStartTimestamp } = message;
|
|
|
|
const expirationLength = expireTimer ? expireTimer * 1000 : undefined;
|
|
|
|
const expirationTimestamp =
|
|
|
|
expirationStartTimestamp && expirationLength
|
|
|
|
? expirationStartTimestamp + expirationLength
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
const conversation = getConversation(message, conversationSelector);
|
|
|
|
const isGroup = conversation.type === 'group';
|
|
|
|
const { sticker } = message;
|
|
|
|
|
|
|
|
const isMessageTapToView = isTapToView(message);
|
|
|
|
|
|
|
|
const reactions = (message.reactions || []).map(re => {
|
|
|
|
const c = conversationSelector(re.fromId);
|
|
|
|
|
|
|
|
return {
|
|
|
|
emoji: re.emoji,
|
|
|
|
timestamp: re.timestamp,
|
|
|
|
from: c,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const selectedReaction = (
|
|
|
|
(message.reactions || []).find(re => re.fromId === ourConversationId) || {}
|
|
|
|
).emoji;
|
|
|
|
|
|
|
|
const isSelected = message.id === selectedMessageId;
|
|
|
|
|
|
|
|
return {
|
|
|
|
attachments: getAttachmentsForMessage(message),
|
|
|
|
author: contact,
|
|
|
|
bodyRanges: processBodyRanges(message.bodyRanges, conversationSelector),
|
|
|
|
canDeleteForEveryone: canDeleteForEveryone(message),
|
|
|
|
canDownload: canDownload(message, conversationSelector),
|
2021-07-19 22:44:49 +00:00
|
|
|
canReply: canReply(message, ourConversationId, conversationSelector),
|
2021-06-17 17:15:10 +00:00
|
|
|
contact: getPropsForEmbeddedContact(message, regionCode, accountSelector),
|
|
|
|
conversationColor: conversation?.conversationColor ?? ConversationColors[0],
|
|
|
|
conversationId: message.conversationId,
|
|
|
|
conversationType: isGroup ? 'group' : 'direct',
|
|
|
|
customColor: conversation?.customColor,
|
|
|
|
deletedForEveryone: message.deletedForEveryone || false,
|
|
|
|
direction: isIncoming(message) ? 'incoming' : 'outgoing',
|
|
|
|
expirationLength,
|
|
|
|
expirationTimestamp,
|
|
|
|
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,
|
|
|
|
previews: getPropsForPreview(message),
|
|
|
|
quote: getPropsForQuote(message, conversationSelector, ourConversationId),
|
|
|
|
reactions,
|
|
|
|
selectedReaction,
|
2021-07-21 19:21:16 +00:00
|
|
|
status: getMessagePropStatus(message, ourConversationId),
|
2021-06-17 17:15:10 +00:00
|
|
|
text: createNonBreakingLastSeparator(message.body),
|
|
|
|
textPending: message.bodyPending,
|
|
|
|
timestamp: message.sent_at,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function processBodyRanges(
|
|
|
|
bodyRanges: BodyRangesType | undefined,
|
|
|
|
conversationSelector: GetConversationByIdType
|
|
|
|
): BodyRangesType | undefined {
|
|
|
|
if (!bodyRanges) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsupported Message
|
|
|
|
|
|
|
|
export function isUnsupportedMessage(message: MessageAttributesType): boolean {
|
|
|
|
const versionAtReceive = message.supportedVersionAtReceive;
|
|
|
|
const requiredVersion = message.requiredProtocolVersion;
|
|
|
|
|
|
|
|
return (
|
|
|
|
isNumber(versionAtReceive) &&
|
|
|
|
isNumber(requiredVersion) &&
|
|
|
|
versionAtReceive < requiredVersion
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForUnsupportedMessage(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType,
|
|
|
|
ourConversationId: string,
|
|
|
|
ourNumber: string | undefined,
|
|
|
|
ourUuid: string | undefined
|
|
|
|
): 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,
|
|
|
|
contact: getContact(
|
|
|
|
message,
|
|
|
|
conversationSelector,
|
|
|
|
ourConversationId,
|
|
|
|
ourNumber,
|
|
|
|
ourUuid
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// GroupV2 Change
|
|
|
|
|
|
|
|
export function isGroupV2Change(message: MessageAttributesType): boolean {
|
|
|
|
return Boolean(message.groupV2Change);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForGroupV2Change(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType,
|
|
|
|
ourConversationId: string
|
|
|
|
): GroupsV2Props {
|
|
|
|
const change = message.groupV2Change;
|
|
|
|
|
|
|
|
if (!change) {
|
|
|
|
throw new Error('getPropsForGroupV2Change: Change is missing!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const conversation = getConversation(message, conversationSelector);
|
|
|
|
|
|
|
|
return {
|
|
|
|
groupName: conversation?.type === 'group' ? conversation?.name : undefined,
|
|
|
|
ourConversationId,
|
|
|
|
change,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// GroupV1 Migration
|
|
|
|
|
|
|
|
export function isGroupV1Migration(message: MessageAttributesType): boolean {
|
|
|
|
return message.type === 'group-v1-migration';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForGroupV1Migration(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType
|
|
|
|
): 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 =>
|
|
|
|
conversationSelector(item.conversationId)
|
|
|
|
);
|
|
|
|
const droppedMembers = droppedGV2MemberIds.map(conversationId =>
|
|
|
|
conversationSelector(conversationId)
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
areWeInvited: false,
|
|
|
|
droppedMembers,
|
|
|
|
invitedMembers,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
areWeInvited,
|
|
|
|
droppedMemberIds,
|
|
|
|
invitedMembers: rawInvitedMembers,
|
|
|
|
} = migration;
|
|
|
|
const invitedMembers = rawInvitedMembers.map(item =>
|
|
|
|
conversationSelector(item.conversationId)
|
|
|
|
);
|
|
|
|
const droppedMembers = droppedMemberIds.map(conversationId =>
|
|
|
|
conversationSelector(conversationId)
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
areWeInvited,
|
|
|
|
droppedMembers,
|
|
|
|
invitedMembers,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message History Unsynced
|
|
|
|
|
|
|
|
export function isMessageHistoryUnsynced(
|
|
|
|
message: MessageAttributesType
|
|
|
|
): boolean {
|
|
|
|
return message.type === 'message-history-unsynced';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: props are null!
|
|
|
|
|
|
|
|
// Expiration Timer Update
|
|
|
|
|
|
|
|
export function isExpirationTimerUpdate(
|
2021-07-01 23:48:40 +00:00
|
|
|
message: Pick<MessageAttributesType, '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(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType,
|
|
|
|
ourConversationId: string
|
|
|
|
): 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: MessageAttributesType): boolean {
|
|
|
|
return message.type === 'keychange';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForSafetyNumberNotification(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType
|
|
|
|
): 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: MessageAttributesType): boolean {
|
|
|
|
return message.type === 'verified-change';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForVerificationNotification(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType
|
|
|
|
): VerificationNotificationProps {
|
|
|
|
const type = message.verified ? 'markVerified' : 'markNotVerified';
|
|
|
|
const isLocal = message.local || false;
|
|
|
|
const identifier = message.verifiedChanged;
|
|
|
|
|
|
|
|
return {
|
|
|
|
type,
|
|
|
|
isLocal,
|
|
|
|
contact: conversationSelector(identifier),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group Update (V1)
|
|
|
|
|
2021-06-30 00:51:57 +00:00
|
|
|
export function isGroupUpdate(
|
|
|
|
message: Pick<MessageAttributesType, 'group_update'>
|
|
|
|
): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
return Boolean(message.group_update);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForGroupNotification(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType,
|
|
|
|
ourConversationId: string,
|
|
|
|
ourNumber: string | undefined,
|
|
|
|
ourUuid: string | undefined
|
|
|
|
): GroupNotificationProps {
|
|
|
|
const groupUpdate = message.group_update;
|
|
|
|
if (!groupUpdate) {
|
|
|
|
throw new Error(
|
|
|
|
'getPropsForGroupNotification: Message missing group_update'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const from = getContact(
|
|
|
|
message,
|
|
|
|
conversationSelector,
|
|
|
|
ourConversationId,
|
|
|
|
ourNumber,
|
|
|
|
ourUuid
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
from,
|
|
|
|
changes,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// End Session
|
|
|
|
|
2021-06-30 00:51:57 +00:00
|
|
|
export function isEndSession(
|
|
|
|
message: Pick<MessageAttributesType, 'flags'>
|
|
|
|
): 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
|
|
|
|
|
|
|
|
export function isCallHistory(message: MessageAttributesType): boolean {
|
|
|
|
return message.type === 'call-history';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPropsForCallHistory(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType,
|
|
|
|
callSelector: CallSelectorType,
|
|
|
|
activeCall: CallStateType | undefined
|
|
|
|
): CallingNotificationType {
|
|
|
|
const { callHistoryDetails } = message;
|
|
|
|
if (!callHistoryDetails) {
|
|
|
|
throw new Error('getPropsForCallHistory: Missing callHistoryDetails');
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (callHistoryDetails.callMode) {
|
|
|
|
// Old messages weren't saved with a call mode.
|
|
|
|
case undefined:
|
|
|
|
case CallMode.Direct:
|
|
|
|
return {
|
|
|
|
...callHistoryDetails,
|
|
|
|
callMode: CallMode.Direct,
|
|
|
|
};
|
|
|
|
case CallMode.Group: {
|
|
|
|
const { conversationId } = message;
|
|
|
|
if (!conversationId) {
|
|
|
|
throw new Error('getPropsForCallHistory: missing conversation ID');
|
|
|
|
}
|
|
|
|
|
|
|
|
const creator = conversationSelector(callHistoryDetails.creatorUuid);
|
|
|
|
let call = callSelector(conversationId);
|
|
|
|
if (call && call.callMode !== CallMode.Group) {
|
|
|
|
window.log.error(
|
|
|
|
'getPropsForCallHistory: there is an unexpected non-group call; pretending it does not exist'
|
|
|
|
);
|
|
|
|
call = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
activeCallConversationId: activeCall?.conversationId,
|
|
|
|
callMode: CallMode.Group,
|
|
|
|
conversationId,
|
|
|
|
creator,
|
|
|
|
deviceCount: call?.peekInfo.deviceCount ?? 0,
|
|
|
|
ended: callHistoryDetails.eraId !== call?.peekInfo.eraId,
|
|
|
|
maxDevices: call?.peekInfo.maxDevices ?? Infinity,
|
|
|
|
startedTime: callHistoryDetails.startedTime,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new Error(
|
|
|
|
`getPropsForCallHistory: missing case ${missingCaseError(
|
|
|
|
callHistoryDetails
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Profile Change
|
|
|
|
|
|
|
|
export function isProfileChange(message: MessageAttributesType): boolean {
|
|
|
|
return message.type === 'profile-change';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForProfileChange(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType
|
|
|
|
): 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: MessageAttributesType
|
|
|
|
): boolean {
|
|
|
|
return message.type === 'universal-timer-notification';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chat Session Refreshed
|
|
|
|
|
|
|
|
export function isChatSessionRefreshed(
|
|
|
|
message: MessageAttributesType
|
|
|
|
): boolean {
|
|
|
|
return message.type === 'chat-session-refreshed';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: props are null
|
|
|
|
|
|
|
|
// Delivery Issue
|
|
|
|
|
|
|
|
export function isDeliveryIssue(message: MessageAttributesType): boolean {
|
|
|
|
return message.type === 'delivery-issue';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPropsForDeliveryIssue(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
conversationSelector: GetConversationByIdType
|
|
|
|
): 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
|
|
|
|
|
|
|
|
export function isTapToView(message: MessageAttributesType): boolean {
|
|
|
|
// If a message is deleted for everyone, that overrides all other styling
|
|
|
|
if (message.deletedForEveryone) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Boolean(message.isViewOnce || message.messageTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createNonBreakingLastSeparator(text?: string): string {
|
|
|
|
if (!text) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const nbsp = '\xa0';
|
|
|
|
const regex = /(\S)( +)(\S+\s*)$/;
|
|
|
|
return text.replace(regex, (_match, start, spaces, end) => {
|
|
|
|
const newSpaces =
|
|
|
|
end.length < 12
|
|
|
|
? reduce(spaces, accumulator => accumulator + nbsp, '')
|
|
|
|
: spaces;
|
|
|
|
return `${start}${newSpaces}${end}`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getMessagePropStatus(
|
2021-07-19 22:44:49 +00:00
|
|
|
message: Pick<
|
|
|
|
MessageAttributesType,
|
|
|
|
'type' | 'errors' | 'sendStateByConversationId'
|
|
|
|
>,
|
2021-07-21 19:21:16 +00:00
|
|
|
ourConversationId: string
|
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';
|
|
|
|
}
|
|
|
|
|
|
|
|
const { sendStateByConversationId = {} } = message;
|
|
|
|
|
|
|
|
if (isMessageJustForMe(sendStateByConversationId, ourConversationId)) {
|
|
|
|
const status =
|
|
|
|
sendStateByConversationId[ourConversationId]?.status ??
|
|
|
|
SendStatus.Pending;
|
|
|
|
const sent = isSent(status);
|
|
|
|
if (hasErrors(message)) {
|
|
|
|
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(
|
|
|
|
omit(sendStateByConversationId, ourConversationId)
|
|
|
|
);
|
|
|
|
const highestSuccessfulStatus = sendStates.reduce(
|
|
|
|
(result: SendStatus, { status }) => maxStatus(result, status),
|
|
|
|
SendStatus.Pending
|
|
|
|
);
|
|
|
|
|
|
|
|
if (hasErrors(message)) {
|
|
|
|
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(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
regionCode: string,
|
|
|
|
accountSelector: (identifier?: string) => boolean
|
|
|
|
): ContactType | 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 contactSelector(firstContact, {
|
|
|
|
regionCode,
|
|
|
|
getAbsoluteAttachmentPath:
|
|
|
|
window.Signal.Migrations.getAbsoluteAttachmentPath,
|
|
|
|
firstNumber,
|
|
|
|
isNumberOnSignal: accountSelector(firstNumber),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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 getPropsForPreview(
|
|
|
|
message: MessageAttributesType
|
|
|
|
): Array<LinkPreviewType> {
|
|
|
|
const previews = message.preview || [];
|
|
|
|
|
|
|
|
return previews.map(preview => ({
|
|
|
|
...preview,
|
|
|
|
isStickerPack: isStickerPack(preview.url),
|
|
|
|
domain: getDomain(preview.url),
|
|
|
|
image: preview.image ? getPropsForAttachment(preview.image) : null,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPropsForQuote(
|
2021-06-22 23:16:50 +00:00
|
|
|
message: Pick<MessageAttributesType, 'conversationId' | 'quote'>,
|
2021-06-17 17:15:10 +00:00
|
|
|
conversationSelector: GetConversationByIdType,
|
|
|
|
ourConversationId: string | undefined
|
|
|
|
): PropsData['quote'] {
|
|
|
|
const { quote } = message;
|
|
|
|
if (!quote) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
author,
|
|
|
|
authorUuid,
|
|
|
|
bodyRanges,
|
|
|
|
id: sentAt,
|
|
|
|
isViewOnce,
|
|
|
|
referencedMessageNotFound,
|
|
|
|
text,
|
|
|
|
} = 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);
|
|
|
|
|
|
|
|
return {
|
|
|
|
authorId,
|
|
|
|
authorName,
|
|
|
|
authorPhoneNumber,
|
|
|
|
authorProfileName,
|
|
|
|
authorTitle,
|
|
|
|
bodyRanges: processBodyRanges(bodyRanges, conversationSelector),
|
|
|
|
conversationColor: conversation.conversationColor ?? ConversationColors[0],
|
|
|
|
customColor: conversation.customColor,
|
|
|
|
isFromMe,
|
|
|
|
rawAttachment: firstAttachment
|
|
|
|
? processQuoteAttachment(firstAttachment)
|
|
|
|
: undefined,
|
|
|
|
isViewOnce,
|
|
|
|
referencedMessageNotFound,
|
|
|
|
sentAt: Number(sentAt),
|
|
|
|
text: createNonBreakingLastSeparator(text),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function canReply(
|
2021-07-01 23:57:10 +00:00
|
|
|
message: Pick<
|
|
|
|
MessageAttributesType,
|
2021-07-19 22:44:49 +00:00
|
|
|
| 'conversationId'
|
|
|
|
| 'deletedForEveryone'
|
|
|
|
| 'sendStateByConversationId'
|
|
|
|
| 'type'
|
2021-07-01 23:57:10 +00:00
|
|
|
>,
|
2021-07-19 22:44:49 +00:00
|
|
|
ourConversationId: string,
|
2021-06-17 17:15:10 +00:00
|
|
|
conversationSelector: GetConversationByIdType
|
|
|
|
): boolean {
|
|
|
|
const conversation = getConversation(message, conversationSelector);
|
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 GroupV1 groups have been disabled, we can't reply.
|
|
|
|
if (conversation.isGroupV1AndDisabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If mandatory profile sharing is enabled, and we haven't shared yet, then
|
|
|
|
// we can't reply.
|
|
|
|
if (isMissingRequiredProfileSharing(conversation)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We cannot reply if we haven't accepted the message request
|
|
|
|
if (!conversation.acceptedMessageRequest) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We cannot reply if this message is deleted for everyone
|
2021-07-01 23:57:10 +00:00
|
|
|
if (deletedForEveryone) {
|
2021-06-17 17:15:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-20 20:18:35 +00:00
|
|
|
// Groups where only admins can send messages
|
|
|
|
if (conversation.announcementsOnly && !conversation.areWeAdmin) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-01 23:57:10 +00:00
|
|
|
// We can reply if this is outgoing and sent to at least one recipient
|
|
|
|
if (isOutgoing(message)) {
|
2021-07-19 22:44:49 +00:00
|
|
|
return (
|
|
|
|
isMessageJustForMe(sendStateByConversationId, ourConversationId) ||
|
|
|
|
someSendStatus(omit(sendStateByConversationId, ourConversationId), isSent)
|
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 23:57:10 +00:00
|
|
|
// We can reply to incoming messages
|
|
|
|
if (isIncoming(message)) {
|
2021-06-17 17:15:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail safe.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-02 19:26:48 +00:00
|
|
|
export function canDeleteForEveryone(
|
|
|
|
message: Pick<
|
|
|
|
MessageAttributesType,
|
|
|
|
'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 pending/sent to anyone?
|
|
|
|
someSendStatus(
|
|
|
|
message.sendStateByConversationId,
|
|
|
|
sendStatus => sendStatus !== SendStatus.Failed
|
|
|
|
)
|
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function canDownload(
|
|
|
|
message: MessageAttributesType,
|
|
|
|
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 getAttachmentsForMessage(
|
|
|
|
message: MessageAttributesType
|
|
|
|
): Array<AttachmentType> {
|
|
|
|
const { sticker } = message;
|
|
|
|
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 [];
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
const attachments = message.attachments || [];
|
|
|
|
return attachments
|
|
|
|
.filter(attachment => !attachment.error)
|
|
|
|
.map(attachment => getPropsForAttachment(attachment))
|
|
|
|
.filter(isNotNil);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getLastChallengeError(
|
2021-07-19 22:44:49 +00:00
|
|
|
message: Pick<MessageAttributesType, '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();
|
|
|
|
}
|