2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-09-09 18:35:00 +00:00
|
|
|
import {
|
|
|
|
isEmpty,
|
|
|
|
isNumber,
|
2022-11-30 00:53:39 +00:00
|
|
|
isObject,
|
2022-09-09 18:35:00 +00:00
|
|
|
mapValues,
|
|
|
|
maxBy,
|
|
|
|
noop,
|
|
|
|
omit,
|
2022-11-30 00:53:39 +00:00
|
|
|
partition,
|
|
|
|
pick,
|
2022-09-09 18:35:00 +00:00
|
|
|
union,
|
|
|
|
} from 'lodash';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { v4 as generateUuid } from 'uuid';
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2020-09-24 20:57:54 +00:00
|
|
|
CustomError,
|
2021-04-14 22:15:57 +00:00
|
|
|
MessageAttributesType,
|
2021-10-29 23:19:44 +00:00
|
|
|
MessageReactionType,
|
2021-04-14 22:15:57 +00:00
|
|
|
QuotedMessageType,
|
2020-09-24 20:57:54 +00:00
|
|
|
} from '../model-types.d';
|
2023-07-20 00:17:13 +00:00
|
|
|
import { filter, find, map, repeat, zipObject } from '../util/iterables';
|
2023-04-11 03:54:43 +00:00
|
|
|
import * as GoogleChrome from '../util/GoogleChrome';
|
2023-08-07 16:36:37 +00:00
|
|
|
import type { DeleteAttributesType } from '../messageModifiers/Deletes';
|
2022-02-22 20:34:57 +00:00
|
|
|
import type { SentEventData } from '../textsecure/messageReceiverEvents';
|
2021-07-19 22:44:49 +00:00
|
|
|
import { isNotNil } from '../util/isNotNil';
|
|
|
|
import { isNormalNumber } from '../util/isNormalNumber';
|
2023-10-04 00:12:57 +00:00
|
|
|
import { strictAssert } from '../util/assert';
|
|
|
|
import { hydrateStoryContext } from '../util/hydrateStoryContext';
|
2022-12-21 18:41:48 +00:00
|
|
|
import { drop } from '../util/drop';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationModel } from './conversations';
|
|
|
|
import type {
|
2021-07-19 22:44:49 +00:00
|
|
|
ProcessedDataMessage,
|
|
|
|
ProcessedQuote,
|
|
|
|
ProcessedUnidentifiedDeliveryStatus,
|
2021-07-20 01:10:09 +00:00
|
|
|
CallbackResultType,
|
2021-07-19 22:44:49 +00:00
|
|
|
} from '../textsecure/Types.d';
|
2021-07-20 01:10:09 +00:00
|
|
|
import { SendMessageProtoError } from '../textsecure/Errors';
|
2022-05-11 20:59:58 +00:00
|
|
|
import { getUserLanguages } from '../util/userLanguages';
|
2023-05-16 17:37:12 +00:00
|
|
|
import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp';
|
2023-10-04 00:09:31 +00:00
|
|
|
import { copyCdnFields } from '../util/attachments';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ReactionType } from '../types/Reactions';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { ServiceIdString } from '../types/ServiceId';
|
2023-09-14 17:04:48 +00:00
|
|
|
import { normalizeServiceId } from '../types/ServiceId';
|
|
|
|
import { isAciString } from '../util/isAciString';
|
2021-10-29 23:19:44 +00:00
|
|
|
import * as reactionUtil from '../reactions/util';
|
2021-09-24 00:49:05 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2023-04-20 16:31:59 +00:00
|
|
|
import type { AttachmentType } from '../types/Attachment';
|
2021-10-26 19:15:33 +00:00
|
|
|
import { isImage, isVideo } from '../types/Attachment';
|
2021-10-05 22:10:08 +00:00
|
|
|
import { stringToMIMEType } from '../types/MIME';
|
2021-09-24 00:49:05 +00:00
|
|
|
import * as MIME from '../types/MIME';
|
2021-08-12 18:15:55 +00:00
|
|
|
import { ReadStatus } from '../messages/MessageReadStatus';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { SendStateByConversationId } from '../messages/MessageSendState';
|
2021-07-19 22:44:49 +00:00
|
|
|
import {
|
|
|
|
SendActionType,
|
|
|
|
SendStatus,
|
|
|
|
isSent,
|
|
|
|
sendStateReducer,
|
|
|
|
someSendStatus,
|
|
|
|
} from '../messages/MessageSendState';
|
2021-08-12 18:15:55 +00:00
|
|
|
import { migrateLegacyReadStatus } from '../messages/migrateLegacyReadStatus';
|
2021-07-19 22:44:49 +00:00
|
|
|
import { migrateLegacySendAttributes } from '../messages/migrateLegacySendAttributes';
|
|
|
|
import { getOwn } from '../util/getOwn';
|
2021-08-12 18:15:55 +00:00
|
|
|
import { markRead, markViewed } from '../services/MessageUpdater';
|
2021-06-07 16:39:13 +00:00
|
|
|
import {
|
|
|
|
isDirectConversation,
|
2022-02-22 20:34:57 +00:00
|
|
|
isGroup,
|
2021-06-07 16:39:13 +00:00
|
|
|
isGroupV1,
|
|
|
|
isMe,
|
|
|
|
} from '../util/whatTypeOfConversation';
|
|
|
|
import { handleMessageSend } from '../util/handleMessageSend';
|
|
|
|
import { getSendOptions } from '../util/getSendOptions';
|
2023-07-20 00:17:13 +00:00
|
|
|
import { modifyTargetMessage } from '../util/modifyTargetMessage';
|
2021-06-17 17:15:10 +00:00
|
|
|
import {
|
|
|
|
getMessagePropStatus,
|
|
|
|
hasErrors,
|
|
|
|
isCallHistory,
|
|
|
|
isChatSessionRefreshed,
|
2023-04-05 20:48:00 +00:00
|
|
|
isContactRemovedNotification,
|
2021-06-17 17:15:10 +00:00
|
|
|
isDeliveryIssue,
|
|
|
|
isEndSession,
|
|
|
|
isExpirationTimerUpdate,
|
2022-05-11 20:59:58 +00:00
|
|
|
isGiftBadge,
|
2021-06-17 17:15:10 +00:00
|
|
|
isGroupUpdate,
|
|
|
|
isGroupV1Migration,
|
|
|
|
isGroupV2Change,
|
|
|
|
isIncoming,
|
|
|
|
isKeyChange,
|
|
|
|
isOutgoing,
|
2022-03-04 21:14:52 +00:00
|
|
|
isStory,
|
2021-06-17 17:15:10 +00:00
|
|
|
isProfileChange,
|
|
|
|
isTapToView,
|
|
|
|
isUniversalTimerNotification,
|
|
|
|
isUnsupportedMessage,
|
|
|
|
isVerifiedChange,
|
2022-12-05 22:46:54 +00:00
|
|
|
isConversationMerge,
|
2023-10-23 17:40:42 +00:00
|
|
|
isPhoneNumberDiscovery,
|
2021-06-17 17:15:10 +00:00
|
|
|
} from '../state/selectors/message';
|
2023-08-21 20:08:27 +00:00
|
|
|
import type { ReactionAttributesType } from '../messageModifiers/Reactions';
|
2023-10-04 00:12:57 +00:00
|
|
|
import { isInCall } from '../state/selectors/calling';
|
2021-10-29 23:19:44 +00:00
|
|
|
import { ReactionSource } from '../reactions/ReactionSource';
|
2021-06-30 17:00:02 +00:00
|
|
|
import * as LinkPreview from '../types/LinkPreview';
|
2021-07-02 19:21:24 +00:00
|
|
|
import { SignalService as Proto } from '../protobuf';
|
2022-02-16 18:36:21 +00:00
|
|
|
import {
|
|
|
|
conversationJobQueue,
|
|
|
|
conversationQueueJobEnum,
|
|
|
|
} from '../jobs/conversationJobQueue';
|
2023-08-01 16:06:29 +00:00
|
|
|
import {
|
|
|
|
NotificationType,
|
|
|
|
notificationService,
|
|
|
|
} from '../services/notifications';
|
2023-04-20 16:31:59 +00:00
|
|
|
import type {
|
|
|
|
LinkPreviewType,
|
|
|
|
LinkPreviewWithHydratedData,
|
|
|
|
} from '../types/message/LinkPreviews';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2021-12-10 22:51:54 +00:00
|
|
|
import { cleanupMessage, deleteMessageData } from '../util/cleanup';
|
|
|
|
import {
|
|
|
|
getContact,
|
|
|
|
getSource,
|
2023-08-16 20:54:39 +00:00
|
|
|
getSourceServiceId,
|
2021-12-10 22:51:54 +00:00
|
|
|
isCustomError,
|
2022-11-30 21:47:54 +00:00
|
|
|
messageHasPaymentEvent,
|
2021-12-10 22:51:54 +00:00
|
|
|
isQuoteAMatch,
|
|
|
|
} from '../messages/helpers';
|
2022-02-08 17:30:42 +00:00
|
|
|
import { viewOnceOpenJobQueue } from '../jobs/viewOnceOpenJobQueue';
|
2022-04-22 18:35:14 +00:00
|
|
|
import { getMessageIdForLogging } from '../util/idForLogging';
|
2022-03-04 21:14:52 +00:00
|
|
|
import { hasAttachmentDownloads } from '../util/hasAttachmentDownloads';
|
|
|
|
import { queueAttachmentDownloads } from '../util/queueAttachmentDownloads';
|
2023-05-25 12:12:33 +00:00
|
|
|
import { findStoryMessages } from '../util/findStoryMessage';
|
2022-02-16 18:36:21 +00:00
|
|
|
import type { ConversationQueueJobData } from '../jobs/conversationJobQueue';
|
2022-03-29 01:10:08 +00:00
|
|
|
import { shouldDownloadStory } from '../util/shouldDownloadStory';
|
2023-04-20 16:31:59 +00:00
|
|
|
import type { EmbeddedContactWithHydratedAvatar } from '../types/EmbeddedContact';
|
2022-04-22 18:35:14 +00:00
|
|
|
import { SeenStatus } from '../MessageSeenStatus';
|
2022-04-28 22:06:28 +00:00
|
|
|
import { isNewReactionReplacingPrevious } from '../reactions/util';
|
2022-05-11 20:59:58 +00:00
|
|
|
import { parseBoostBadgeListFromServer } from '../badges/parseBadgesFromServer';
|
2022-07-13 00:37:21 +00:00
|
|
|
import type { StickerWithHydratedData } from '../types/Stickers';
|
2023-10-23 17:40:42 +00:00
|
|
|
|
2023-01-13 00:24:59 +00:00
|
|
|
import {
|
|
|
|
addToAttachmentDownloadQueue,
|
|
|
|
shouldUseAttachmentDownloadQueue,
|
|
|
|
} from '../util/attachmentDownloadQueue';
|
2022-10-20 21:39:21 +00:00
|
|
|
import dataInterface from '../sql/Client';
|
2023-03-27 23:48:57 +00:00
|
|
|
import { getQuoteBodyText } from '../util/getQuoteBodyText';
|
|
|
|
import { shouldReplyNotifyUser } from '../util/shouldReplyNotifyUser';
|
2023-04-10 16:31:45 +00:00
|
|
|
import type { RawBodyRange } from '../types/BodyRange';
|
2023-10-04 00:12:57 +00:00
|
|
|
import { BodyRange } from '../types/BodyRange';
|
2023-04-11 03:54:43 +00:00
|
|
|
import {
|
|
|
|
queueUpdateMessage,
|
|
|
|
saveNewMessageBatcher,
|
|
|
|
} from '../util/messageBatcher';
|
2023-10-04 00:12:57 +00:00
|
|
|
import { getSenderIdentifier } from '../util/getSenderIdentifier';
|
|
|
|
import { getNotificationDataForMessage } from '../util/getNotificationDataForMessage';
|
|
|
|
import { getNotificationTextForMessage } from '../util/getNotificationTextForMessage';
|
|
|
|
import { getMessageAuthorText } from '../util/getMessageAuthorText';
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
/* eslint-disable more/no-then */
|
|
|
|
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const { Message: TypedMessage } = window.Signal.Types;
|
2021-12-10 22:51:54 +00:00
|
|
|
const { upgradeMessageSchema } = window.Signal.Migrations;
|
2022-03-04 21:14:52 +00:00
|
|
|
const { getMessageBySender } = window.Signal.Data;
|
2020-09-29 22:55:56 +00:00
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
|
|
|
|
CURRENT_PROTOCOL_VERSION?: number;
|
|
|
|
|
2020-10-06 17:06:34 +00:00
|
|
|
// Set when sending some sync messages, so we get the functionality of
|
|
|
|
// send(), without zombie messages going into the database.
|
|
|
|
doNotSave?: boolean;
|
2022-08-02 19:31:55 +00:00
|
|
|
// Set when sending stories, so we get the functionality of send() but we are
|
|
|
|
// able to send the sync message elsewhere.
|
|
|
|
doNotSendSyncMessage?: boolean;
|
2020-10-06 17:06:34 +00:00
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
INITIAL_PROTOCOL_VERSION?: number;
|
|
|
|
|
2022-11-01 18:58:07 +00:00
|
|
|
deletingForEveryone?: boolean;
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
isSelected?: boolean;
|
|
|
|
|
2021-08-17 00:16:00 +00:00
|
|
|
private pendingMarkRead?: number;
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
syncPromise?: Promise<CallbackResultType | void>;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
cachedOutgoingContactData?: Array<EmbeddedContactWithHydratedAvatar>;
|
2022-04-11 20:57:44 +00:00
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
cachedOutgoingPreviewData?: Array<LinkPreviewWithHydratedData>;
|
2021-08-31 20:58:39 +00:00
|
|
|
|
2022-07-13 00:37:21 +00:00
|
|
|
cachedOutgoingQuoteData?: QuotedMessageType;
|
2021-08-31 20:58:39 +00:00
|
|
|
|
2022-07-13 00:37:21 +00:00
|
|
|
cachedOutgoingStickerData?: StickerWithHydratedData;
|
2021-08-31 20:58:39 +00:00
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
public registerLocations: Set<string>;
|
|
|
|
|
2023-01-12 20:58:53 +00:00
|
|
|
constructor(attributes: MessageAttributesType) {
|
|
|
|
super(attributes);
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
if (!this.id && attributes.id) {
|
|
|
|
this.id = attributes.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.registerLocations = new Set();
|
|
|
|
|
2023-01-12 20:58:53 +00:00
|
|
|
// Note that we intentionally don't use `initialize()` method because it
|
|
|
|
// isn't compatible with esnext output of esbuild.
|
2022-11-30 00:53:39 +00:00
|
|
|
if (isObject(attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
this.set(
|
|
|
|
TypedMessage.initializeSchemaVersion({
|
2022-06-13 21:39:35 +00:00
|
|
|
message: attributes as MessageAttributesType,
|
2021-09-17 18:27:53 +00:00
|
|
|
logger: log,
|
2020-09-24 20:57:54 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-12 18:15:55 +00:00
|
|
|
const readStatus = migrateLegacyReadStatus(this.attributes);
|
|
|
|
if (readStatus !== undefined) {
|
2022-04-29 23:42:47 +00:00
|
|
|
this.set(
|
|
|
|
{
|
|
|
|
readStatus,
|
|
|
|
seenStatus:
|
|
|
|
readStatus === ReadStatus.Unread
|
|
|
|
? SeenStatus.Unseen
|
|
|
|
: SeenStatus.Seen,
|
|
|
|
},
|
|
|
|
{ silent: true }
|
|
|
|
);
|
2021-08-12 18:15:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 17:27:20 +00:00
|
|
|
const ourConversationId =
|
|
|
|
window.ConversationController.getOurConversationId();
|
|
|
|
if (ourConversationId) {
|
|
|
|
const sendStateByConversationId = migrateLegacySendAttributes(
|
|
|
|
this.attributes,
|
|
|
|
window.ConversationController.get.bind(window.ConversationController),
|
|
|
|
ourConversationId
|
|
|
|
);
|
|
|
|
if (sendStateByConversationId) {
|
|
|
|
this.set('sendStateByConversationId', sendStateByConversationId, {
|
|
|
|
silent: true,
|
|
|
|
});
|
|
|
|
}
|
2021-07-19 22:44:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 19:21:24 +00:00
|
|
|
this.CURRENT_PROTOCOL_VERSION = Proto.DataMessage.ProtocolVersion.CURRENT;
|
|
|
|
this.INITIAL_PROTOCOL_VERSION = Proto.DataMessage.ProtocolVersion.INITIAL;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-10-25 20:44:50 +00:00
|
|
|
this.on('change', this.updateMessageCache);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 20:44:50 +00:00
|
|
|
updateMessageCache(): void {
|
2023-10-04 00:12:57 +00:00
|
|
|
window.MessageCache.setAttributes({
|
|
|
|
messageId: this.id,
|
|
|
|
messageAttributes: this.attributes,
|
|
|
|
skipSaveToDatabase: true,
|
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 21:44:57 +00:00
|
|
|
getSenderIdentifier(): string {
|
2023-10-04 00:12:57 +00:00
|
|
|
return getSenderIdentifier(this.attributes);
|
2021-03-04 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getReceivedAt(): number {
|
|
|
|
// We would like to get the received_at_ms ideally since received_at is
|
|
|
|
// now an incrementing counter for messages and not the actual time that
|
|
|
|
// the message was received. If this field doesn't exist on the message
|
|
|
|
// then we can trust received_at.
|
|
|
|
return Number(this.get('received_at_ms') || this.get('received_at'));
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
isNormalBubble(): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
const { attributes } = this;
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
return (
|
2021-06-17 17:15:10 +00:00
|
|
|
!isCallHistory(attributes) &&
|
|
|
|
!isChatSessionRefreshed(attributes) &&
|
2023-04-05 20:48:00 +00:00
|
|
|
!isContactRemovedNotification(attributes) &&
|
2022-12-05 22:46:54 +00:00
|
|
|
!isConversationMerge(attributes) &&
|
2021-06-17 17:15:10 +00:00
|
|
|
!isEndSession(attributes) &&
|
|
|
|
!isExpirationTimerUpdate(attributes) &&
|
|
|
|
!isGroupUpdate(attributes) &&
|
|
|
|
!isGroupV1Migration(attributes) &&
|
2022-12-05 22:46:54 +00:00
|
|
|
!isGroupV2Change(attributes) &&
|
2021-06-17 17:15:10 +00:00
|
|
|
!isKeyChange(attributes) &&
|
2023-10-23 17:40:42 +00:00
|
|
|
!isPhoneNumberDiscovery(attributes) &&
|
2021-06-17 17:15:10 +00:00
|
|
|
!isProfileChange(attributes) &&
|
|
|
|
!isUniversalTimerNotification(attributes) &&
|
|
|
|
!isUnsupportedMessage(attributes) &&
|
|
|
|
!isVerifiedChange(attributes)
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-11 20:32:00 +00:00
|
|
|
async hydrateStoryContext(
|
2023-07-21 22:10:32 +00:00
|
|
|
inMemoryMessage?: MessageAttributesType,
|
|
|
|
{
|
|
|
|
shouldSave,
|
|
|
|
}: {
|
|
|
|
shouldSave?: boolean;
|
|
|
|
} = {}
|
2022-10-11 20:32:00 +00:00
|
|
|
): Promise<void> {
|
2023-10-04 00:12:57 +00:00
|
|
|
await hydrateStoryContext(this.id, inMemoryMessage, { shouldSave });
|
2022-03-16 17:30:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
// Dependencies of prop-generation functions
|
|
|
|
getConversation(): ConversationModel | undefined {
|
|
|
|
return window.ConversationController.get(this.get('conversationId'));
|
|
|
|
}
|
|
|
|
|
2023-04-10 16:31:45 +00:00
|
|
|
getNotificationData(): {
|
|
|
|
emoji?: string;
|
|
|
|
text: string;
|
|
|
|
bodyRanges?: ReadonlyArray<RawBodyRange>;
|
|
|
|
} {
|
2023-10-04 00:12:57 +00:00
|
|
|
return getNotificationDataForMessage(this.attributes);
|
2022-08-25 16:16:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
getNotificationText(): string {
|
2023-10-04 00:12:57 +00:00
|
|
|
return getNotificationTextForMessage(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// General
|
|
|
|
idForLogging(): string {
|
2022-03-04 21:14:52 +00:00
|
|
|
return getMessageIdForLogging(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 23:44:20 +00:00
|
|
|
override defaults(): Partial<MessageAttributesType> {
|
2020-09-24 20:57:54 +00:00
|
|
|
return {
|
|
|
|
timestamp: new Date().getTime(),
|
|
|
|
attachments: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-12 23:44:20 +00:00
|
|
|
override validate(attributes: Record<string, unknown>): void {
|
2020-09-24 20:57:54 +00:00
|
|
|
const required = ['conversationId', 'received_at', 'sent_at'];
|
2022-11-30 00:53:39 +00:00
|
|
|
const missing = required.filter(attr => !attributes[attr]);
|
2020-09-24 20:57:54 +00:00
|
|
|
if (missing.length) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(`Message missing attributes: ${missing}`);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
merge(model: MessageModel): void {
|
|
|
|
const attributes = model.attributes || model;
|
|
|
|
this.set(attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async cleanup(): Promise<void> {
|
2021-12-10 22:51:54 +00:00
|
|
|
await cleanupMessage(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async deleteData(): Promise<void> {
|
2021-12-10 22:51:54 +00:00
|
|
|
await deleteMessageData(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isValidTapToView(): boolean {
|
|
|
|
const body = this.get('body');
|
|
|
|
if (body) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const attachments = this.get('attachments');
|
|
|
|
if (!attachments || attachments.length !== 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const firstAttachment = attachments[0];
|
|
|
|
if (
|
2023-04-11 03:54:43 +00:00
|
|
|
!GoogleChrome.isImageTypeSupported(firstAttachment.contentType) &&
|
|
|
|
!GoogleChrome.isVideoTypeSupported(firstAttachment.contentType)
|
2020-09-24 20:57:54 +00:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const quote = this.get('quote');
|
|
|
|
const sticker = this.get('sticker');
|
|
|
|
const contact = this.get('contact');
|
|
|
|
const preview = this.get('preview');
|
|
|
|
|
|
|
|
if (
|
|
|
|
quote ||
|
|
|
|
sticker ||
|
|
|
|
(contact && contact.length > 0) ||
|
|
|
|
(preview && preview.length > 0)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:07:53 +00:00
|
|
|
async markViewOnceMessageViewed(options?: {
|
|
|
|
fromSync?: boolean;
|
|
|
|
}): Promise<void> {
|
2020-09-24 20:57:54 +00:00
|
|
|
const { fromSync } = options || {};
|
|
|
|
|
|
|
|
if (!this.isValidTapToView()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-22 17:07:53 +00:00
|
|
|
`markViewOnceMessageViewed: Message ${this.idForLogging()} is not a valid tap to view message!`
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.isErased()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-22 17:07:53 +00:00
|
|
|
`markViewOnceMessageViewed: Message ${this.idForLogging()} is already erased!`
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-12 18:15:55 +00:00
|
|
|
if (this.get('readStatus') !== ReadStatus.Viewed) {
|
|
|
|
this.set(markViewed(this.attributes));
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await this.eraseContents();
|
|
|
|
|
|
|
|
if (!fromSync) {
|
2022-02-08 17:30:42 +00:00
|
|
|
const senderE164 = getSource(this.attributes);
|
2023-08-16 20:54:39 +00:00
|
|
|
const senderAci = getSourceServiceId(this.attributes);
|
2022-02-08 17:30:42 +00:00
|
|
|
const timestamp = this.get('sent_at');
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
if (senderAci === undefined || !isAciString(senderAci)) {
|
|
|
|
throw new Error('markViewOnceMessageViewed: senderAci is undefined');
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
if (window.ConversationController.areWePrimaryDevice()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-22 17:07:53 +00:00
|
|
|
'markViewOnceMessageViewed: We are primary device; not sending view once open sync'
|
2021-07-15 23:48:09 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-02-08 17:30:42 +00:00
|
|
|
try {
|
|
|
|
await viewOnceOpenJobQueue.add({
|
|
|
|
viewOnceOpens: [
|
|
|
|
{
|
|
|
|
senderE164,
|
2023-08-16 20:54:39 +00:00
|
|
|
senderAci,
|
2022-02-08 17:30:42 +00:00
|
|
|
timestamp,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'markViewOnceMessageViewed: Failed to queue view once open sync',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
async doubleCheckMissingQuoteReference(): Promise<void> {
|
|
|
|
const logId = this.idForLogging();
|
2022-05-10 19:02:21 +00:00
|
|
|
|
|
|
|
const storyId = this.get('storyId');
|
|
|
|
if (storyId) {
|
|
|
|
log.warn(
|
|
|
|
`doubleCheckMissingQuoteReference/${logId}: missing story reference`
|
|
|
|
);
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
const message = window.MessageCache.__DEPRECATED$getById(storyId);
|
2022-05-10 19:02:21 +00:00
|
|
|
if (!message) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get('storyReplyContext')) {
|
2023-10-19 17:14:19 +00:00
|
|
|
this.set('storyReplyContext', undefined);
|
2022-05-10 19:02:21 +00:00
|
|
|
}
|
2023-07-21 22:10:32 +00:00
|
|
|
await this.hydrateStoryContext(message.attributes, { shouldSave: true });
|
2022-05-10 19:02:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
const quote = this.get('quote');
|
|
|
|
if (!quote) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(`doubleCheckMissingQuoteReference/${logId}: Missing quote!`);
|
2021-06-17 17:15:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const { authorAci, author, id: sentAt, referencedMessageNotFound } = quote;
|
|
|
|
const contact = window.ConversationController.get(authorAci || author);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
// Is the quote really without a reference? Check with our in memory store
|
|
|
|
// first to make sure it's not there.
|
|
|
|
if (referencedMessageNotFound && contact) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-06-17 17:15:10 +00:00
|
|
|
`doubleCheckMissingQuoteReference/${logId}: Verifying reference to ${sentAt}`
|
|
|
|
);
|
2023-10-04 00:12:57 +00:00
|
|
|
const inMemoryMessages = window.MessageCache.__DEPRECATED$filterBySentAt(
|
2021-06-17 17:15:10 +00:00
|
|
|
Number(sentAt)
|
|
|
|
);
|
2023-04-20 16:31:59 +00:00
|
|
|
let matchingMessage = find(inMemoryMessages, message =>
|
2021-12-10 22:51:54 +00:00
|
|
|
isQuoteAMatch(message.attributes, this.get('conversationId'), quote)
|
2021-06-22 23:05:05 +00:00
|
|
|
);
|
2023-04-20 16:31:59 +00:00
|
|
|
if (!matchingMessage) {
|
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
|
|
|
Number(sentAt)
|
|
|
|
);
|
|
|
|
const found = messages.find(item =>
|
|
|
|
isQuoteAMatch(item, this.get('conversationId'), quote)
|
|
|
|
);
|
|
|
|
if (found) {
|
2023-10-04 00:12:57 +00:00
|
|
|
matchingMessage = window.MessageCache.__DEPRECATED$register(
|
|
|
|
found.id,
|
|
|
|
found,
|
|
|
|
'doubleCheckMissingQuoteReference'
|
|
|
|
);
|
2023-04-20 16:31:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 23:05:05 +00:00
|
|
|
if (!matchingMessage) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-06-17 17:15:10 +00:00
|
|
|
`doubleCheckMissingQuoteReference/${logId}: No match for ${sentAt}.`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set({
|
|
|
|
quote: {
|
|
|
|
...quote,
|
|
|
|
referencedMessageNotFound: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-06-17 17:15:10 +00:00
|
|
|
`doubleCheckMissingQuoteReference/${logId}: Found match for ${sentAt}, updating.`
|
|
|
|
);
|
|
|
|
|
2021-06-22 23:05:05 +00:00
|
|
|
await this.copyQuoteContentFromOriginal(matchingMessage, quote);
|
2021-06-17 17:15:10 +00:00
|
|
|
this.set({
|
|
|
|
quote: {
|
|
|
|
...quote,
|
|
|
|
referencedMessageNotFound: false,
|
|
|
|
},
|
|
|
|
});
|
2023-04-11 03:54:43 +00:00
|
|
|
queueUpdateMessage(this.attributes);
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
isErased(): boolean {
|
|
|
|
return Boolean(this.get('isErased'));
|
|
|
|
}
|
|
|
|
|
|
|
|
async eraseContents(
|
|
|
|
additionalProperties = {},
|
|
|
|
shouldPersist = true
|
|
|
|
): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`Erasing data for message ${this.idForLogging()}`);
|
2020-10-09 14:43:40 +00:00
|
|
|
|
|
|
|
// Note: There are cases where we want to re-erase a given message. For example, when
|
|
|
|
// a viewed (or outgoing) View-Once message is deleted for everyone.
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await this.deleteData();
|
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-09-24 20:57:54 +00:00
|
|
|
`Error erasing data for message ${this.idForLogging()}:`,
|
2022-11-22 18:43:43 +00:00
|
|
|
Errors.toLogFormat(error)
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set({
|
2023-03-27 23:48:57 +00:00
|
|
|
attachments: [],
|
2020-09-24 20:57:54 +00:00
|
|
|
body: '',
|
2020-09-30 19:41:00 +00:00
|
|
|
bodyRanges: undefined,
|
2020-09-24 20:57:54 +00:00
|
|
|
contact: [],
|
2023-03-27 23:48:57 +00:00
|
|
|
editHistory: undefined,
|
|
|
|
isErased: true,
|
2020-09-24 20:57:54 +00:00
|
|
|
preview: [],
|
2023-03-27 23:48:57 +00:00
|
|
|
quote: undefined,
|
|
|
|
sticker: undefined,
|
2020-09-24 20:57:54 +00:00
|
|
|
...additionalProperties,
|
|
|
|
});
|
2023-08-22 21:01:36 +00:00
|
|
|
this.getConversation()?.debouncedUpdateLastMessage();
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
if (shouldPersist) {
|
2021-12-20 21:04:02 +00:00
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2021-07-15 23:48:09 +00:00
|
|
|
|
|
|
|
await window.Signal.Data.deleteSentProtoByMessageId(this.id);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 23:44:20 +00:00
|
|
|
override isEmpty(): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
const { attributes } = this;
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
// Core message types - we check for all four because they can each stand alone
|
|
|
|
const hasBody = Boolean(this.get('body'));
|
|
|
|
const hasAttachment = (this.get('attachments') || []).length > 0;
|
|
|
|
const hasEmbeddedContact = (this.get('contact') || []).length > 0;
|
|
|
|
const isSticker = Boolean(this.get('sticker'));
|
|
|
|
|
|
|
|
// Rendered sync messages
|
2021-06-17 17:15:10 +00:00
|
|
|
const isCallHistoryValue = isCallHistory(attributes);
|
|
|
|
const isChatSessionRefreshedValue = isChatSessionRefreshed(attributes);
|
|
|
|
const isDeliveryIssueValue = isDeliveryIssue(attributes);
|
2022-05-11 20:59:58 +00:00
|
|
|
const isGiftBadgeValue = isGiftBadge(attributes);
|
2021-06-17 17:15:10 +00:00
|
|
|
const isGroupUpdateValue = isGroupUpdate(attributes);
|
|
|
|
const isGroupV2ChangeValue = isGroupV2Change(attributes);
|
|
|
|
const isEndSessionValue = isEndSession(attributes);
|
|
|
|
const isExpirationTimerUpdateValue = isExpirationTimerUpdate(attributes);
|
|
|
|
const isVerifiedChangeValue = isVerifiedChange(attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
// Placeholder messages
|
2021-06-17 17:15:10 +00:00
|
|
|
const isUnsupportedMessageValue = isUnsupportedMessage(attributes);
|
|
|
|
const isTapToViewValue = isTapToView(attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
// Errors
|
2021-06-17 17:15:10 +00:00
|
|
|
const hasErrorsValue = hasErrors(attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
// Locally-generated notifications
|
2021-06-17 17:15:10 +00:00
|
|
|
const isKeyChangeValue = isKeyChange(attributes);
|
|
|
|
const isProfileChangeValue = isProfileChange(attributes);
|
2021-11-11 22:43:05 +00:00
|
|
|
const isUniversalTimerNotificationValue =
|
|
|
|
isUniversalTimerNotification(attributes);
|
2022-12-05 22:46:54 +00:00
|
|
|
const isConversationMergeValue = isConversationMerge(attributes);
|
2023-10-23 17:40:42 +00:00
|
|
|
const isPhoneNumberDiscoveryValue = isPhoneNumberDiscovery(attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-11-30 21:47:54 +00:00
|
|
|
const isPayment = messageHasPaymentEvent(attributes);
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
// Note: not all of these message types go through message.handleDataMessage
|
|
|
|
|
|
|
|
const hasSomethingToDisplay =
|
|
|
|
// Core message types
|
|
|
|
hasBody ||
|
|
|
|
hasAttachment ||
|
|
|
|
hasEmbeddedContact ||
|
|
|
|
isSticker ||
|
2022-11-30 21:47:54 +00:00
|
|
|
isPayment ||
|
2020-09-24 20:57:54 +00:00
|
|
|
// Rendered sync messages
|
2021-06-17 17:15:10 +00:00
|
|
|
isCallHistoryValue ||
|
|
|
|
isChatSessionRefreshedValue ||
|
|
|
|
isDeliveryIssueValue ||
|
2022-05-11 20:59:58 +00:00
|
|
|
isGiftBadgeValue ||
|
2021-06-17 17:15:10 +00:00
|
|
|
isGroupUpdateValue ||
|
|
|
|
isGroupV2ChangeValue ||
|
|
|
|
isEndSessionValue ||
|
|
|
|
isExpirationTimerUpdateValue ||
|
|
|
|
isVerifiedChangeValue ||
|
2020-09-24 20:57:54 +00:00
|
|
|
// Placeholder messages
|
2021-06-17 17:15:10 +00:00
|
|
|
isUnsupportedMessageValue ||
|
|
|
|
isTapToViewValue ||
|
2020-09-24 20:57:54 +00:00
|
|
|
// Errors
|
2021-06-17 17:15:10 +00:00
|
|
|
hasErrorsValue ||
|
2020-09-24 20:57:54 +00:00
|
|
|
// Locally-generated notifications
|
2021-06-17 17:15:10 +00:00
|
|
|
isKeyChangeValue ||
|
|
|
|
isProfileChangeValue ||
|
2022-12-05 22:46:54 +00:00
|
|
|
isUniversalTimerNotificationValue ||
|
2023-10-23 17:40:42 +00:00
|
|
|
isConversationMergeValue ||
|
|
|
|
isPhoneNumberDiscoveryValue;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
return !hasSomethingToDisplay;
|
|
|
|
}
|
|
|
|
|
|
|
|
isUnidentifiedDelivery(
|
|
|
|
contactId: string,
|
2021-07-19 22:44:49 +00:00
|
|
|
unidentifiedDeliveriesSet: Readonly<Set<string>>
|
2020-09-24 20:57:54 +00:00
|
|
|
): boolean {
|
2021-06-17 17:15:10 +00:00
|
|
|
if (isIncoming(this.attributes)) {
|
|
|
|
return Boolean(this.get('unidentifiedDeliveryReceived'));
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
return unidentifiedDeliveriesSet.has(contactId);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async saveErrors(
|
|
|
|
providedErrors: Error | Array<Error>,
|
|
|
|
options: { skipSave?: boolean } = {}
|
|
|
|
): Promise<void> {
|
|
|
|
const { skipSave } = options;
|
|
|
|
|
|
|
|
let errors: Array<CustomError>;
|
|
|
|
|
|
|
|
if (!(providedErrors instanceof Array)) {
|
|
|
|
errors = [providedErrors];
|
|
|
|
} else {
|
|
|
|
errors = providedErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
errors.forEach(e => {
|
2022-11-22 18:43:43 +00:00
|
|
|
log.error('Message.saveErrors:', Errors.toLogFormat(e));
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
|
|
|
errors = errors.map(e => {
|
2020-10-09 15:06:06 +00:00
|
|
|
// Note: in our environment, instanceof can be scary, so we have a backup check
|
|
|
|
// (Node.js vs Browser context).
|
|
|
|
// We check instanceof second because typescript believes that anything that comes
|
|
|
|
// through here must be an instance of Error, so e is 'never' after that check.
|
|
|
|
if ((e.message && e.stack) || e instanceof Error) {
|
2022-11-30 00:53:39 +00:00
|
|
|
return pick(
|
2020-09-24 20:57:54 +00:00
|
|
|
e,
|
|
|
|
'name',
|
|
|
|
'message',
|
|
|
|
'code',
|
|
|
|
'number',
|
2020-10-09 15:06:06 +00:00
|
|
|
'identifier',
|
2021-05-06 00:09:29 +00:00
|
|
|
'retryAfter',
|
|
|
|
'data',
|
2020-09-24 20:57:54 +00:00
|
|
|
'reason'
|
|
|
|
) as Required<Error>;
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
});
|
|
|
|
errors = errors.concat(this.get('errors') || []);
|
|
|
|
|
|
|
|
this.set({ errors });
|
|
|
|
|
2020-10-06 17:06:34 +00:00
|
|
|
if (!skipSave && !this.doNotSave) {
|
2021-12-20 21:04:02 +00:00
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 01:15:25 +00:00
|
|
|
markRead(readAt?: number, options = {}): void {
|
|
|
|
this.set(markRead(this.attributes, readAt, options));
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
async retrySend(): Promise<void> {
|
2020-09-24 20:57:54 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
const conversation = this.getConversation()!;
|
|
|
|
|
2022-11-16 22:10:11 +00:00
|
|
|
let currentConversationRecipients: Set<string> | undefined;
|
|
|
|
|
|
|
|
const { storyDistributionListId } = this.attributes;
|
|
|
|
|
|
|
|
if (storyDistributionListId) {
|
|
|
|
const storyDistribution =
|
|
|
|
await dataInterface.getStoryDistributionWithMembers(
|
|
|
|
storyDistributionListId
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!storyDistribution) {
|
|
|
|
this.markFailed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentConversationRecipients = new Set(
|
|
|
|
storyDistribution.members
|
2023-08-16 20:54:39 +00:00
|
|
|
.map(serviceId => window.ConversationController.get(serviceId)?.id)
|
2022-11-16 22:10:11 +00:00
|
|
|
.filter(isNotNil)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
currentConversationRecipients = conversation.getMemberConversationIds();
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
// Determine retry recipients and get their most up-to-date addressing information
|
2021-07-19 22:44:49 +00:00
|
|
|
const oldSendStateByConversationId =
|
|
|
|
this.get('sendStateByConversationId') || {};
|
|
|
|
|
|
|
|
const newSendStateByConversationId = { ...oldSendStateByConversationId };
|
|
|
|
for (const [conversationId, sendState] of Object.entries(
|
|
|
|
oldSendStateByConversationId
|
|
|
|
)) {
|
|
|
|
if (isSent(sendState.status)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
const recipient = window.ConversationController.get(conversationId);
|
|
|
|
if (
|
|
|
|
!recipient ||
|
|
|
|
(!currentConversationRecipients.has(conversationId) &&
|
|
|
|
!isMe(recipient.attributes))
|
|
|
|
) {
|
2021-07-19 22:44:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
newSendStateByConversationId[conversationId] = sendStateReducer(
|
|
|
|
sendState,
|
|
|
|
{
|
|
|
|
type: SendActionType.ManuallyRetried,
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('sendStateByConversationId', newSendStateByConversationId);
|
|
|
|
|
2022-12-23 00:48:09 +00:00
|
|
|
if (isStory(this.attributes)) {
|
|
|
|
await conversationJobQueue.add(
|
|
|
|
{
|
|
|
|
type: conversationQueueJobEnum.enum.Story,
|
|
|
|
conversationId: conversation.id,
|
|
|
|
messageIds: [this.id],
|
|
|
|
// using the group timestamp, which will differ from the 1:1 timestamp
|
|
|
|
timestamp: this.attributes.timestamp,
|
|
|
|
},
|
|
|
|
async jobToInsert => {
|
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
|
|
|
jobToInsert,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2022-12-23 00:48:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
await conversationJobQueue.add(
|
|
|
|
{
|
|
|
|
type: conversationQueueJobEnum.enum.NormalMessage,
|
|
|
|
conversationId: conversation.id,
|
|
|
|
messageId: this.id,
|
|
|
|
revision: conversation.get('revision'),
|
|
|
|
},
|
|
|
|
async jobToInsert => {
|
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
|
|
|
jobToInsert,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2022-12-23 00:48:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isReplayableError(e: Error): boolean {
|
|
|
|
return (
|
|
|
|
e.name === 'MessageError' ||
|
|
|
|
e.name === 'OutgoingMessageError' ||
|
|
|
|
e.name === 'SendMessageNetworkError' ||
|
2021-05-06 00:09:29 +00:00
|
|
|
e.name === 'SendMessageChallengeError' ||
|
2020-09-24 20:57:54 +00:00
|
|
|
e.name === 'OutgoingIdentityKeyError'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
public hasSuccessfulDelivery(): boolean {
|
2021-07-19 22:44:49 +00:00
|
|
|
const sendStateByConversationId = this.get('sendStateByConversationId');
|
|
|
|
const withoutMe = omit(
|
|
|
|
sendStateByConversationId,
|
|
|
|
window.ConversationController.getOurConversationIdOrThrow()
|
|
|
|
);
|
|
|
|
return isEmpty(withoutMe) || someSendStatus(withoutMe, isSent);
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
/**
|
|
|
|
* Change any Pending send state to Failed. Note that this will not mark successful
|
|
|
|
* sends failed.
|
|
|
|
*/
|
|
|
|
public markFailed(): void {
|
|
|
|
const now = Date.now();
|
|
|
|
this.set(
|
|
|
|
'sendStateByConversationId',
|
|
|
|
mapValues(this.get('sendStateByConversationId') || {}, sendState =>
|
|
|
|
sendStateReducer(sendState, {
|
|
|
|
type: SendActionType.Failed,
|
|
|
|
updatedAt: now,
|
|
|
|
})
|
|
|
|
)
|
2021-07-15 23:48:09 +00:00
|
|
|
);
|
2023-02-07 19:33:04 +00:00
|
|
|
|
|
|
|
this.notifyStorySendFailed();
|
|
|
|
}
|
|
|
|
|
|
|
|
public notifyStorySendFailed(): void {
|
|
|
|
if (!isStory(this.attributes)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
notificationService.add({
|
|
|
|
conversationId: this.get('conversationId'),
|
|
|
|
storyId: this.id,
|
|
|
|
messageId: this.id,
|
|
|
|
senderTitle:
|
2023-03-30 00:03:25 +00:00
|
|
|
this.getConversation()?.getTitle() ?? window.i18n('icu:Stories__mine'),
|
2023-02-07 19:33:04 +00:00
|
|
|
message: this.hasSuccessfulDelivery()
|
|
|
|
? window.i18n('icu:Stories__failed-send--partial')
|
|
|
|
: window.i18n('icu:Stories__failed-send--full'),
|
|
|
|
isExpiringMessage: false,
|
2023-06-14 20:55:50 +00:00
|
|
|
sentAt: this.get('timestamp'),
|
2023-08-01 16:06:29 +00:00
|
|
|
type: NotificationType.Message,
|
2023-02-07 19:33:04 +00:00
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
removeOutgoingErrors(incomingIdentifier: string): CustomError {
|
2021-11-11 22:43:05 +00:00
|
|
|
const incomingConversationId =
|
|
|
|
window.ConversationController.getConversationId(incomingIdentifier);
|
2022-11-30 00:53:39 +00:00
|
|
|
const errors = partition(
|
2020-09-24 20:57:54 +00:00
|
|
|
this.get('errors'),
|
|
|
|
e =>
|
|
|
|
window.ConversationController.getConversationId(
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2023-08-10 16:43:33 +00:00
|
|
|
e.serviceId || e.number!
|
2020-09-24 20:57:54 +00:00
|
|
|
) === incomingConversationId &&
|
|
|
|
(e.name === 'MessageError' ||
|
|
|
|
e.name === 'OutgoingMessageError' ||
|
|
|
|
e.name === 'SendMessageNetworkError' ||
|
2021-05-06 00:09:29 +00:00
|
|
|
e.name === 'SendMessageChallengeError' ||
|
2020-09-24 20:57:54 +00:00
|
|
|
e.name === 'OutgoingIdentityKeyError')
|
|
|
|
);
|
|
|
|
this.set({ errors: errors[1] });
|
|
|
|
return errors[0][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
async send(
|
2021-08-31 20:58:39 +00:00
|
|
|
promise: Promise<CallbackResultType | void | null>,
|
|
|
|
saveErrors?: (errors: Array<Error>) => void
|
2021-09-20 18:51:30 +00:00
|
|
|
): Promise<void> {
|
2021-07-19 22:44:49 +00:00
|
|
|
const updateLeftPane =
|
2023-08-22 21:01:36 +00:00
|
|
|
this.getConversation()?.debouncedUpdateLastMessage ?? noop;
|
2021-07-19 22:44:49 +00:00
|
|
|
|
|
|
|
updateLeftPane();
|
|
|
|
|
|
|
|
let result:
|
|
|
|
| { success: true; value: CallbackResultType }
|
|
|
|
| {
|
|
|
|
success: false;
|
2021-07-20 01:10:09 +00:00
|
|
|
value: CustomError | SendMessageProtoError;
|
2021-07-19 22:44:49 +00:00
|
|
|
};
|
|
|
|
try {
|
|
|
|
const value = await (promise as Promise<CallbackResultType>);
|
|
|
|
result = { success: true, value };
|
|
|
|
} catch (err) {
|
|
|
|
result = { success: false, value: err };
|
2021-06-16 00:44:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
updateLeftPane();
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
const attributesToUpdate: Partial<MessageAttributesType> = {};
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
// This is used by sendSyncMessage, then set to null
|
|
|
|
if ('dataMessage' in result.value && result.value.dataMessage) {
|
|
|
|
attributesToUpdate.dataMessage = result.value.dataMessage;
|
2023-04-20 16:31:59 +00:00
|
|
|
} else if ('editMessage' in result.value && result.value.editMessage) {
|
|
|
|
attributesToUpdate.dataMessage = result.value.editMessage;
|
2021-07-19 22:44:49 +00:00
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
if (!this.doNotSave) {
|
2021-12-20 21:04:02 +00:00
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2021-07-19 22:44:49 +00:00
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
const sendStateByConversationId = {
|
|
|
|
...(this.get('sendStateByConversationId') || {}),
|
|
|
|
};
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-06-02 21:25:55 +00:00
|
|
|
const sendIsNotFinal =
|
|
|
|
'sendIsNotFinal' in result.value && result.value.sendIsNotFinal;
|
|
|
|
const sendIsFinal = !sendIsNotFinal;
|
|
|
|
|
|
|
|
// Capture successful sends
|
2023-08-10 16:43:33 +00:00
|
|
|
const successfulServiceIds: Array<ServiceIdString> =
|
2022-06-02 21:25:55 +00:00
|
|
|
sendIsFinal &&
|
2023-08-10 16:43:33 +00:00
|
|
|
'successfulServiceIds' in result.value &&
|
|
|
|
Array.isArray(result.value.successfulServiceIds)
|
|
|
|
? result.value.successfulServiceIds
|
2021-07-19 22:44:49 +00:00
|
|
|
: [];
|
|
|
|
const sentToAtLeastOneRecipient =
|
2023-08-10 16:43:33 +00:00
|
|
|
result.success || Boolean(successfulServiceIds.length);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
successfulServiceIds.forEach(serviceId => {
|
|
|
|
const conversation = window.ConversationController.get(serviceId);
|
2021-07-19 22:44:49 +00:00
|
|
|
if (!conversation) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
// If we successfully sent to a user, we can remove our unregistered flag.
|
|
|
|
if (conversation.isEverUnregistered()) {
|
|
|
|
conversation.setRegistered();
|
|
|
|
}
|
2021-07-09 21:38:51 +00:00
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
const previousSendState = getOwn(
|
|
|
|
sendStateByConversationId,
|
|
|
|
conversation.id
|
|
|
|
);
|
|
|
|
if (previousSendState) {
|
|
|
|
sendStateByConversationId[conversation.id] = sendStateReducer(
|
|
|
|
previousSendState,
|
|
|
|
{
|
|
|
|
type: SendActionType.Sent,
|
|
|
|
updatedAt: Date.now(),
|
2021-07-12 23:51:45 +00:00
|
|
|
}
|
2021-07-19 22:44:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2021-07-09 21:38:51 +00:00
|
|
|
|
2022-06-02 21:25:55 +00:00
|
|
|
// Integrate sends via sealed sender
|
2021-07-19 22:44:49 +00:00
|
|
|
const previousUnidentifiedDeliveries =
|
|
|
|
this.get('unidentifiedDeliveries') || [];
|
|
|
|
const newUnidentifiedDeliveries =
|
2022-06-02 21:25:55 +00:00
|
|
|
sendIsFinal &&
|
2021-07-19 22:44:49 +00:00
|
|
|
'unidentifiedDeliveries' in result.value &&
|
|
|
|
Array.isArray(result.value.unidentifiedDeliveries)
|
|
|
|
? result.value.unidentifiedDeliveries
|
|
|
|
: [];
|
2021-07-09 21:38:51 +00:00
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
const promises: Array<Promise<unknown>> = [];
|
|
|
|
|
2022-06-02 21:25:55 +00:00
|
|
|
// Process errors
|
2021-07-19 22:44:49 +00:00
|
|
|
let errors: Array<CustomError>;
|
2021-07-27 23:11:38 +00:00
|
|
|
if (result.value instanceof SendMessageProtoError && result.value.errors) {
|
|
|
|
({ errors } = result.value);
|
|
|
|
} else if (isCustomError(result.value)) {
|
2021-07-19 22:44:49 +00:00
|
|
|
errors = [result.value];
|
|
|
|
} else if (Array.isArray(result.value.errors)) {
|
|
|
|
({ errors } = result.value);
|
|
|
|
} else {
|
|
|
|
errors = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// In groups, we don't treat unregistered users as a user-visible
|
|
|
|
// error. The message will look successful, but the details
|
|
|
|
// screen will show that we didn't send to these unregistered users.
|
|
|
|
const errorsToSave: Array<CustomError> = [];
|
|
|
|
|
|
|
|
errors.forEach(error => {
|
|
|
|
const conversation =
|
2023-08-10 16:43:33 +00:00
|
|
|
window.ConversationController.get(error.serviceId) ||
|
2021-07-19 22:44:49 +00:00
|
|
|
window.ConversationController.get(error.number);
|
|
|
|
|
2022-06-02 21:25:55 +00:00
|
|
|
if (conversation && !saveErrors && sendIsFinal) {
|
2021-07-19 22:44:49 +00:00
|
|
|
const previousSendState = getOwn(
|
|
|
|
sendStateByConversationId,
|
|
|
|
conversation.id
|
|
|
|
);
|
|
|
|
if (previousSendState) {
|
|
|
|
sendStateByConversationId[conversation.id] = sendStateReducer(
|
|
|
|
previousSendState,
|
|
|
|
{
|
|
|
|
type: SendActionType.Failed,
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
2023-02-07 19:33:04 +00:00
|
|
|
this.notifyStorySendFailed();
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2021-07-19 22:44:49 +00:00
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
let shouldSaveError = true;
|
|
|
|
switch (error.name) {
|
|
|
|
case 'OutgoingIdentityKeyError': {
|
|
|
|
if (conversation) {
|
|
|
|
promises.push(conversation.getProfiles());
|
|
|
|
}
|
|
|
|
break;
|
2021-06-16 00:44:14 +00:00
|
|
|
}
|
2021-07-19 22:44:49 +00:00
|
|
|
case 'UnregisteredUserError':
|
2022-02-22 20:34:57 +00:00
|
|
|
if (conversation && isGroup(conversation.attributes)) {
|
|
|
|
shouldSaveError = false;
|
|
|
|
}
|
2021-07-19 22:44:49 +00:00
|
|
|
// If we just found out that we couldn't send to a user because they are no
|
|
|
|
// longer registered, we will update our unregistered flag. In groups we
|
|
|
|
// will not event try to send to them for 6 hours. And we will never try
|
|
|
|
// to fetch them on startup again.
|
|
|
|
//
|
|
|
|
// The way to discover registration once more is:
|
|
|
|
// 1) any attempt to send to them in 1:1 conversation
|
|
|
|
// 2) the six-hour time period has passed and we send in a group again
|
|
|
|
conversation?.setUnregistered();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldSaveError) {
|
|
|
|
errorsToSave.push(error);
|
|
|
|
}
|
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
attributesToUpdate.sendStateByConversationId = sendStateByConversationId;
|
2022-11-01 18:58:07 +00:00
|
|
|
// Only update the expirationStartTimestamp if we don't already have one set
|
|
|
|
if (!this.get('expirationStartTimestamp')) {
|
|
|
|
attributesToUpdate.expirationStartTimestamp = sentToAtLeastOneRecipient
|
|
|
|
? Date.now()
|
|
|
|
: undefined;
|
|
|
|
}
|
2021-07-19 22:44:49 +00:00
|
|
|
attributesToUpdate.unidentifiedDeliveries = union(
|
|
|
|
previousUnidentifiedDeliveries,
|
|
|
|
newUnidentifiedDeliveries
|
|
|
|
);
|
|
|
|
// We may overwrite this in the `saveErrors` call below.
|
|
|
|
attributesToUpdate.errors = [];
|
|
|
|
|
|
|
|
this.set(attributesToUpdate);
|
2021-08-31 20:58:39 +00:00
|
|
|
if (saveErrors) {
|
|
|
|
saveErrors(errorsToSave);
|
|
|
|
} else {
|
|
|
|
// We skip save because we'll save in the next step.
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.saveErrors(errorsToSave, { skipSave: true });
|
2021-08-31 20:58:39 +00:00
|
|
|
}
|
2021-07-19 22:44:49 +00:00
|
|
|
|
|
|
|
if (!this.doNotSave) {
|
2021-12-20 21:04:02 +00:00
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2021-07-19 22:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateLeftPane();
|
|
|
|
|
2022-08-02 19:31:55 +00:00
|
|
|
if (sentToAtLeastOneRecipient && !this.doNotSendSyncMessage) {
|
2021-07-19 22:44:49 +00:00
|
|
|
promises.push(this.sendSyncMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
const isTotalSuccess: boolean =
|
|
|
|
result.success && !this.get('errors')?.length;
|
|
|
|
if (isTotalSuccess) {
|
2023-04-20 16:31:59 +00:00
|
|
|
delete this.cachedOutgoingContactData;
|
2021-08-31 20:58:39 +00:00
|
|
|
delete this.cachedOutgoingPreviewData;
|
|
|
|
delete this.cachedOutgoingQuoteData;
|
|
|
|
delete this.cachedOutgoingStickerData;
|
|
|
|
}
|
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
updateLeftPane();
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 20:58:39 +00:00
|
|
|
async sendSyncMessageOnly(
|
2021-09-24 00:49:05 +00:00
|
|
|
dataMessage: Uint8Array,
|
2021-08-31 20:58:39 +00:00
|
|
|
saveErrors?: (errors: Array<Error>) => void
|
2022-02-16 18:36:21 +00:00
|
|
|
): Promise<CallbackResultType | void> {
|
2020-09-24 20:57:54 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
const conv = this.getConversation()!;
|
|
|
|
this.set({ dataMessage });
|
|
|
|
|
2021-06-16 00:44:14 +00:00
|
|
|
const updateLeftPane = conv?.debouncedUpdateLastMessage;
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
try {
|
2021-07-12 23:51:45 +00:00
|
|
|
this.set({
|
2021-07-19 22:44:49 +00:00
|
|
|
// This is the same as a normal send()
|
2021-07-12 23:51:45 +00:00
|
|
|
expirationStartTimestamp: Date.now(),
|
2022-05-31 23:20:45 +00:00
|
|
|
errors: [],
|
2021-07-12 23:51:45 +00:00
|
|
|
});
|
2021-07-15 23:48:09 +00:00
|
|
|
const result = await this.sendSyncMessage();
|
2020-09-24 20:57:54 +00:00
|
|
|
this.set({
|
|
|
|
// We have to do this afterward, since we didn't have a previous send!
|
2021-07-15 23:48:09 +00:00
|
|
|
unidentifiedDeliveries:
|
|
|
|
result && result.unidentifiedDeliveries
|
|
|
|
? result.unidentifiedDeliveries
|
|
|
|
: undefined,
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
2022-02-16 18:36:21 +00:00
|
|
|
return result;
|
|
|
|
} catch (error) {
|
|
|
|
const resultErrors = error?.errors;
|
2021-08-31 20:58:39 +00:00
|
|
|
const errors = Array.isArray(resultErrors)
|
|
|
|
? resultErrors
|
|
|
|
: [new Error('Unknown error')];
|
|
|
|
if (saveErrors) {
|
|
|
|
saveErrors(errors);
|
|
|
|
} else {
|
|
|
|
// We don't save because we're about to save below.
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.saveErrors(errors, { skipSave: true });
|
2021-08-31 20:58:39 +00:00
|
|
|
}
|
2022-02-16 18:36:21 +00:00
|
|
|
throw error;
|
2020-09-24 20:57:54 +00:00
|
|
|
} finally {
|
2021-12-20 21:04:02 +00:00
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-06-16 00:44:14 +00:00
|
|
|
if (updateLeftPane) {
|
|
|
|
updateLeftPane();
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
async sendSyncMessage(): Promise<CallbackResultType | void> {
|
2021-11-11 22:43:05 +00:00
|
|
|
const ourConversation =
|
|
|
|
window.ConversationController.getOurConversationOrThrow();
|
2021-07-15 23:48:09 +00:00
|
|
|
const sendOptions = await getSendOptions(ourConversation.attributes, {
|
|
|
|
syncMessage: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (window.ConversationController.areWePrimaryDevice()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-15 23:48:09 +00:00
|
|
|
'sendSyncMessage: We are primary device; not sending sync message'
|
|
|
|
);
|
|
|
|
this.set({ dataMessage: undefined });
|
|
|
|
return;
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
const { messaging } = window.textsecure;
|
|
|
|
if (!messaging) {
|
|
|
|
throw new Error('sendSyncMessage: messaging not available!');
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
this.syncPromise = this.syncPromise || Promise.resolve();
|
|
|
|
const next = async () => {
|
|
|
|
const dataMessage = this.get('dataMessage');
|
|
|
|
if (!dataMessage) {
|
2021-07-15 23:48:09 +00:00
|
|
|
return;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2023-06-13 18:32:48 +00:00
|
|
|
const isEditedMessage = Boolean(this.get('editHistory'));
|
|
|
|
const isUpdate = Boolean(this.get('synced')) && !isEditedMessage;
|
2020-09-24 20:57:54 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
const conv = this.getConversation()!;
|
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
const sendEntries = Object.entries(
|
|
|
|
this.get('sendStateByConversationId') || {}
|
|
|
|
);
|
|
|
|
const sentEntries = filter(sendEntries, ([_conversationId, { status }]) =>
|
|
|
|
isSent(status)
|
|
|
|
);
|
|
|
|
const allConversationIdsSentTo = map(
|
|
|
|
sentEntries,
|
|
|
|
([conversationId]) => conversationId
|
|
|
|
);
|
|
|
|
const conversationIdsSentTo = filter(
|
|
|
|
allConversationIdsSentTo,
|
|
|
|
conversationId => conversationId !== ourConversation.id
|
|
|
|
);
|
|
|
|
|
|
|
|
const unidentifiedDeliveries = this.get('unidentifiedDeliveries') || [];
|
|
|
|
const maybeConversationsWithSealedSender = map(
|
|
|
|
unidentifiedDeliveries,
|
|
|
|
identifier => window.ConversationController.get(identifier)
|
|
|
|
);
|
|
|
|
const conversationsWithSealedSender = filter(
|
|
|
|
maybeConversationsWithSealedSender,
|
|
|
|
isNotNil
|
|
|
|
);
|
|
|
|
const conversationIdsWithSealedSender = new Set(
|
|
|
|
map(conversationsWithSealedSender, c => c.id)
|
|
|
|
);
|
|
|
|
|
2023-05-16 17:37:12 +00:00
|
|
|
const timestamp = getMessageSentTimestamp(this.attributes, { log });
|
2023-04-20 16:31:59 +00:00
|
|
|
|
2023-05-11 15:47:41 +00:00
|
|
|
const encodedContent = isEditedMessage
|
|
|
|
? {
|
|
|
|
encodedEditMessage: dataMessage,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
encodedDataMessage: dataMessage,
|
|
|
|
};
|
|
|
|
|
2021-07-15 23:48:09 +00:00
|
|
|
return handleMessageSend(
|
2022-06-13 21:39:35 +00:00
|
|
|
messaging.sendSyncMessage({
|
2023-05-11 15:47:41 +00:00
|
|
|
...encodedContent,
|
2023-04-20 16:31:59 +00:00
|
|
|
timestamp,
|
2021-07-02 18:34:17 +00:00
|
|
|
destination: conv.get('e164'),
|
2023-08-10 16:43:33 +00:00
|
|
|
destinationServiceId: conv.getServiceId(),
|
2021-07-02 18:34:17 +00:00
|
|
|
expirationStartTimestamp:
|
|
|
|
this.get('expirationStartTimestamp') || null,
|
2021-07-19 22:44:49 +00:00
|
|
|
conversationIdsSentTo,
|
|
|
|
conversationIdsWithSealedSender,
|
2020-09-24 20:57:54 +00:00
|
|
|
isUpdate,
|
2021-07-02 18:34:17 +00:00
|
|
|
options: sendOptions,
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent: false,
|
2021-07-15 23:48:09 +00:00
|
|
|
}),
|
2021-07-21 00:29:44 +00:00
|
|
|
// Note: in some situations, for doNotSave messages, the message has no
|
|
|
|
// id, so we provide an empty array here.
|
|
|
|
{ messageIds: this.id ? [this.id] : [], sendType: 'sentSync' }
|
2021-07-15 23:48:09 +00:00
|
|
|
).then(async result => {
|
2021-07-19 22:44:49 +00:00
|
|
|
let newSendStateByConversationId: undefined | SendStateByConversationId;
|
|
|
|
const sendStateByConversationId =
|
|
|
|
this.get('sendStateByConversationId') || {};
|
|
|
|
const ourOldSendState = getOwn(
|
|
|
|
sendStateByConversationId,
|
|
|
|
ourConversation.id
|
|
|
|
);
|
|
|
|
if (ourOldSendState) {
|
|
|
|
const ourNewSendState = sendStateReducer(ourOldSendState, {
|
|
|
|
type: SendActionType.Sent,
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
});
|
|
|
|
if (ourNewSendState !== ourOldSendState) {
|
|
|
|
newSendStateByConversationId = {
|
|
|
|
...sendStateByConversationId,
|
|
|
|
[ourConversation.id]: ourNewSendState,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
this.set({
|
|
|
|
synced: true,
|
|
|
|
dataMessage: null,
|
2021-07-19 22:44:49 +00:00
|
|
|
...(newSendStateByConversationId
|
|
|
|
? { sendStateByConversationId: newSendStateByConversationId }
|
|
|
|
: {}),
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
2020-10-06 17:06:34 +00:00
|
|
|
|
|
|
|
// Return early, skip the save
|
|
|
|
if (this.doNotSave) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-12-20 21:04:02 +00:00
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2020-10-06 17:06:34 +00:00
|
|
|
return result;
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.syncPromise = this.syncPromise.then(next, next);
|
|
|
|
|
|
|
|
return this.syncPromise;
|
|
|
|
}
|
|
|
|
|
2021-03-19 19:09:44 +00:00
|
|
|
hasRequiredAttachmentDownloads(): boolean {
|
|
|
|
const attachments: ReadonlyArray<AttachmentType> =
|
|
|
|
this.get('attachments') || [];
|
|
|
|
|
|
|
|
const hasLongMessageAttachments = attachments.some(attachment => {
|
2021-03-25 18:36:50 +00:00
|
|
|
return MIME.isLongMessage(attachment.contentType);
|
2021-03-19 19:09:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (hasLongMessageAttachments) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sticker = this.get('sticker');
|
|
|
|
if (sticker) {
|
|
|
|
return !sticker.data || !sticker.data.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-09 19:11:52 +00:00
|
|
|
hasAttachmentDownloads(): boolean {
|
2022-03-04 21:14:52 +00:00
|
|
|
return hasAttachmentDownloads(this.attributes);
|
2021-03-09 19:11:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
async queueAttachmentDownloads(): Promise<boolean> {
|
2022-03-04 21:14:52 +00:00
|
|
|
const value = await queueAttachmentDownloads(this.attributes);
|
|
|
|
if (!value) {
|
|
|
|
return false;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 21:14:52 +00:00
|
|
|
this.set(value);
|
|
|
|
return true;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 18:51:53 +00:00
|
|
|
markAttachmentAsCorrupted(attachment: AttachmentType): void {
|
|
|
|
if (!attachment.path) {
|
|
|
|
throw new Error(
|
|
|
|
"Attachment can't be marked as corrupted because it wasn't loaded"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We intentionally don't check in quotes/stickers/contacts/... here,
|
|
|
|
// because this function should be called only for something that can
|
|
|
|
// be displayed as a generic attachment.
|
|
|
|
const attachments: ReadonlyArray<AttachmentType> =
|
|
|
|
this.get('attachments') || [];
|
|
|
|
|
|
|
|
let changed = false;
|
|
|
|
const newAttachments = attachments.map(existing => {
|
|
|
|
if (existing.path !== attachment.path) {
|
|
|
|
return existing;
|
|
|
|
}
|
|
|
|
changed = true;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...existing,
|
|
|
|
isCorrupted: true,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!changed) {
|
|
|
|
throw new Error(
|
|
|
|
"Attachment can't be marked as corrupted because it wasn't found"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('markAttachmentAsCorrupted: marking an attachment as corrupted');
|
2021-03-22 18:51:53 +00:00
|
|
|
|
|
|
|
this.set({
|
|
|
|
attachments: newAttachments,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-14 22:15:57 +00:00
|
|
|
async copyFromQuotedMessage(
|
2021-07-09 19:36:10 +00:00
|
|
|
quote: ProcessedQuote | undefined,
|
2021-04-14 22:15:57 +00:00
|
|
|
conversationId: string
|
2021-07-09 19:36:10 +00:00
|
|
|
): Promise<QuotedMessageType | undefined> {
|
2020-09-24 20:57:54 +00:00
|
|
|
if (!quote) {
|
2021-07-09 19:36:10 +00:00
|
|
|
return undefined;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 22:15:57 +00:00
|
|
|
const { id } = quote;
|
2021-07-09 19:36:10 +00:00
|
|
|
strictAssert(id, 'Quote must have an id');
|
|
|
|
|
|
|
|
const result: QuotedMessageType = {
|
2023-08-16 20:54:39 +00:00
|
|
|
...quote,
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
id,
|
|
|
|
|
|
|
|
attachments: quote.attachments.slice(),
|
2023-04-12 00:16:46 +00:00
|
|
|
bodyRanges: quote.bodyRanges?.slice(),
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
// Just placeholder values for the fields
|
|
|
|
referencedMessageNotFound: false,
|
2022-05-11 20:59:58 +00:00
|
|
|
isGiftBadge: quote.type === Proto.DataMessage.Quote.Type.GIFT_BADGE,
|
2021-07-09 19:36:10 +00:00
|
|
|
isViewOnce: false,
|
|
|
|
messageId: '',
|
|
|
|
};
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
const inMemoryMessages =
|
|
|
|
window.MessageCache.__DEPRECATED$filterBySentAt(id);
|
2021-06-22 23:05:05 +00:00
|
|
|
const matchingMessage = find(inMemoryMessages, item =>
|
2021-12-10 22:51:54 +00:00
|
|
|
isQuoteAMatch(item.attributes, conversationId, result)
|
2021-06-22 23:05:05 +00:00
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-06-22 23:05:05 +00:00
|
|
|
let queryMessage: undefined | MessageModel;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-06-22 23:05:05 +00:00
|
|
|
if (matchingMessage) {
|
|
|
|
queryMessage = matchingMessage;
|
2021-03-04 21:44:57 +00:00
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('copyFromQuotedMessage: db lookup needed', id);
|
2023-04-20 16:31:59 +00:00
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(id);
|
2021-12-10 22:51:54 +00:00
|
|
|
const found = messages.find(item =>
|
2021-07-09 19:36:10 +00:00
|
|
|
isQuoteAMatch(item, conversationId, result)
|
2021-04-14 22:15:57 +00:00
|
|
|
);
|
2021-03-04 21:44:57 +00:00
|
|
|
|
|
|
|
if (!found) {
|
2021-07-09 19:36:10 +00:00
|
|
|
result.referencedMessageNotFound = true;
|
|
|
|
return result;
|
2021-03-04 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
queryMessage = window.MessageCache.__DEPRECATED$register(
|
|
|
|
found.id,
|
|
|
|
found,
|
|
|
|
'copyFromQuotedMessage'
|
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2021-03-04 21:44:57 +00:00
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
if (queryMessage) {
|
2021-07-09 19:36:10 +00:00
|
|
|
await this.copyQuoteContentFromOriginal(queryMessage, result);
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
2021-04-14 22:15:57 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
return result;
|
2021-04-14 22:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async copyQuoteContentFromOriginal(
|
|
|
|
originalMessage: MessageModel,
|
2021-07-09 19:36:10 +00:00
|
|
|
quote: QuotedMessageType
|
2021-04-14 22:15:57 +00:00
|
|
|
): Promise<void> {
|
|
|
|
const { attachments } = quote;
|
|
|
|
const firstAttachment = attachments ? attachments[0] : undefined;
|
2023-10-04 00:09:31 +00:00
|
|
|
const firstThumbnailCdnFields = copyCdnFields(firstAttachment?.thumbnail);
|
2021-04-14 22:15:57 +00:00
|
|
|
|
2022-11-30 21:47:54 +00:00
|
|
|
if (messageHasPaymentEvent(originalMessage.attributes)) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
quote.payment = originalMessage.get('payment');
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
if (isTapToView(originalMessage.attributes)) {
|
2021-04-14 22:15:57 +00:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2021-07-09 19:36:10 +00:00
|
|
|
quote.text = undefined;
|
2021-04-14 22:15:57 +00:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2020-09-24 20:57:54 +00:00
|
|
|
quote.attachments = [
|
|
|
|
{
|
2022-07-13 00:37:21 +00:00
|
|
|
contentType: MIME.IMAGE_JPEG,
|
2020-09-24 20:57:54 +00:00
|
|
|
},
|
|
|
|
];
|
2021-06-02 16:42:19 +00:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
quote.isViewOnce = true;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2021-04-14 22:15:57 +00:00
|
|
|
return;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:59:58 +00:00
|
|
|
const isMessageAGiftBadge = isGiftBadge(originalMessage.attributes);
|
|
|
|
if (isMessageAGiftBadge !== quote.isGiftBadge) {
|
|
|
|
log.warn(
|
|
|
|
`copyQuoteContentFromOriginal: Quote.isGiftBadge: ${quote.isGiftBadge}, isGiftBadge(message): ${isMessageAGiftBadge}`
|
|
|
|
);
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
quote.isGiftBadge = isMessageAGiftBadge;
|
|
|
|
}
|
|
|
|
if (isMessageAGiftBadge) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
quote.text = undefined;
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
quote.attachments = [];
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-02 16:42:19 +00:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
quote.isViewOnce = false;
|
|
|
|
|
2021-04-14 22:15:57 +00:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2023-03-27 23:48:57 +00:00
|
|
|
quote.text = getQuoteBodyText(originalMessage.attributes, quote.id);
|
2023-05-17 22:37:35 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
quote.bodyRanges = originalMessage.attributes.bodyRanges;
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
if (firstAttachment) {
|
2022-07-13 00:37:21 +00:00
|
|
|
firstAttachment.thumbnail = null;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!firstAttachment ||
|
2021-04-14 22:15:57 +00:00
|
|
|
!firstAttachment.contentType ||
|
|
|
|
(!GoogleChrome.isImageTypeSupported(
|
2021-08-09 20:06:21 +00:00
|
|
|
stringToMIMEType(firstAttachment.contentType)
|
2021-04-14 22:15:57 +00:00
|
|
|
) &&
|
|
|
|
!GoogleChrome.isVideoTypeSupported(
|
2021-08-09 20:06:21 +00:00
|
|
|
stringToMIMEType(firstAttachment.contentType)
|
2021-04-14 22:15:57 +00:00
|
|
|
))
|
2020-09-24 20:57:54 +00:00
|
|
|
) {
|
2021-04-14 22:15:57 +00:00
|
|
|
return;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-06-17 17:15:10 +00:00
|
|
|
const schemaVersion = originalMessage.get('schemaVersion');
|
2020-09-24 20:57:54 +00:00
|
|
|
if (
|
2021-06-17 17:15:10 +00:00
|
|
|
schemaVersion &&
|
|
|
|
schemaVersion < TypedMessage.VERSION_NEEDED_FOR_DISPLAY
|
2020-09-24 20:57:54 +00:00
|
|
|
) {
|
|
|
|
const upgradedMessage = await upgradeMessageSchema(
|
2021-04-14 22:15:57 +00:00
|
|
|
originalMessage.attributes
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
2021-04-14 22:15:57 +00:00
|
|
|
originalMessage.set(upgradedMessage);
|
2021-12-20 21:04:02 +00:00
|
|
|
await window.Signal.Data.saveMessage(upgradedMessage, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-09-24 20:57:54 +00:00
|
|
|
'Problem upgrading message quoted message from database',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
2021-04-14 22:15:57 +00:00
|
|
|
return;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 22:15:57 +00:00
|
|
|
const queryAttachments = originalMessage.get('attachments') || [];
|
2020-09-24 20:57:54 +00:00
|
|
|
if (queryAttachments.length > 0) {
|
|
|
|
const queryFirst = queryAttachments[0];
|
|
|
|
const { thumbnail } = queryFirst;
|
|
|
|
|
|
|
|
if (thumbnail && thumbnail.path) {
|
|
|
|
firstAttachment.thumbnail = {
|
2023-10-04 00:09:31 +00:00
|
|
|
...firstThumbnailCdnFields,
|
2020-09-24 20:57:54 +00:00
|
|
|
...thumbnail,
|
|
|
|
copied: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 22:15:57 +00:00
|
|
|
const queryPreview = originalMessage.get('preview') || [];
|
2020-09-24 20:57:54 +00:00
|
|
|
if (queryPreview.length > 0) {
|
|
|
|
const queryFirst = queryPreview[0];
|
|
|
|
const { image } = queryFirst;
|
|
|
|
|
|
|
|
if (image && image.path) {
|
|
|
|
firstAttachment.thumbnail = {
|
2023-10-04 00:09:31 +00:00
|
|
|
...firstThumbnailCdnFields,
|
2020-09-24 20:57:54 +00:00
|
|
|
...image,
|
|
|
|
copied: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 22:15:57 +00:00
|
|
|
const sticker = originalMessage.get('sticker');
|
2020-09-24 20:57:54 +00:00
|
|
|
if (sticker && sticker.data && sticker.data.path) {
|
|
|
|
firstAttachment.thumbnail = {
|
2023-10-04 00:09:31 +00:00
|
|
|
...firstThumbnailCdnFields,
|
2020-09-24 20:57:54 +00:00
|
|
|
...sticker.data,
|
|
|
|
copied: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 20:34:57 +00:00
|
|
|
async handleDataMessage(
|
2021-07-09 19:36:10 +00:00
|
|
|
initialMessage: ProcessedDataMessage,
|
2020-09-24 20:57:54 +00:00
|
|
|
confirm: () => void,
|
2022-02-22 20:34:57 +00:00
|
|
|
options: { data?: SentEventData } = {}
|
|
|
|
): Promise<void> {
|
2020-09-24 20:57:54 +00:00
|
|
|
const { data } = options;
|
|
|
|
|
|
|
|
// This function is called from the background script in a few scenarios:
|
|
|
|
// 1. on an incoming message
|
|
|
|
// 2. on a sent message sync'd from another device
|
|
|
|
// 3. in rare cases, an incoming message can be retried, though it will
|
|
|
|
// still go through one of the previous two codepaths
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
2023-10-04 00:12:57 +00:00
|
|
|
let message: MessageModel = this;
|
2020-09-24 20:57:54 +00:00
|
|
|
const source = message.get('source');
|
2023-08-16 20:54:39 +00:00
|
|
|
const sourceServiceId = message.get('sourceServiceId');
|
2020-09-24 20:57:54 +00:00
|
|
|
const type = message.get('type');
|
|
|
|
const conversationId = message.get('conversationId');
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
const fromContact = getContact(this.attributes);
|
2021-04-29 21:24:55 +00:00
|
|
|
if (fromContact) {
|
|
|
|
fromContact.setRegistered();
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
const conversation = window.ConversationController.get(conversationId)!;
|
2022-09-13 00:52:55 +00:00
|
|
|
const idLog = `handleDataMessage/${conversation.idForLogging()} ${message.idForLogging()}`;
|
2022-11-09 18:59:32 +00:00
|
|
|
await conversation.queueJob(idLog, async () => {
|
2022-09-13 00:52:55 +00:00
|
|
|
log.info(`${idLog}: starting processing in queue`);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
// First, check for duplicates. If we find one, stop processing here.
|
2023-10-04 00:12:57 +00:00
|
|
|
const inMemoryMessage = window.MessageCache.findBySender(
|
2021-03-04 21:44:57 +00:00
|
|
|
this.getSenderIdentifier()
|
2023-10-04 00:12:57 +00:00
|
|
|
);
|
2021-03-09 19:11:52 +00:00
|
|
|
if (inMemoryMessage) {
|
2022-09-13 00:52:55 +00:00
|
|
|
log.info(`${idLog}: cache hit`, this.getSenderIdentifier());
|
2021-03-09 19:11:52 +00:00
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-09-13 00:52:55 +00:00
|
|
|
`${idLog}: duplicate check db lookup needed`,
|
2021-03-04 21:44:57 +00:00
|
|
|
this.getSenderIdentifier()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const existingMessage =
|
2021-12-10 22:51:54 +00:00
|
|
|
inMemoryMessage || (await getMessageBySender(this.attributes));
|
2020-09-24 20:57:54 +00:00
|
|
|
const isUpdate = Boolean(data && data.isRecipientUpdate);
|
|
|
|
|
2022-07-29 23:01:33 +00:00
|
|
|
const isDuplicateMessage =
|
|
|
|
existingMessage &&
|
|
|
|
(type === 'incoming' ||
|
|
|
|
(type === 'story' &&
|
|
|
|
existingMessage.storyDistributionListId ===
|
|
|
|
this.attributes.storyDistributionListId));
|
|
|
|
|
|
|
|
if (isDuplicateMessage) {
|
2022-09-13 00:52:55 +00:00
|
|
|
log.warn(`${idLog}: Received duplicate message`, this.idForLogging());
|
2020-09-24 20:57:54 +00:00
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (type === 'outgoing') {
|
|
|
|
if (isUpdate && existingMessage) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-09-13 00:52:55 +00:00
|
|
|
`${idLog}: Updating message ${message.idForLogging()} with received transcript`
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
const toUpdate = window.MessageCache.__DEPRECATED$register(
|
2020-09-24 20:57:54 +00:00
|
|
|
existingMessage.id,
|
2023-10-04 00:12:57 +00:00
|
|
|
existingMessage,
|
|
|
|
'handleDataMessage/outgoing/toUpdate'
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
2021-07-19 22:44:49 +00:00
|
|
|
|
|
|
|
const unidentifiedDeliveriesSet = new Set<string>(
|
|
|
|
toUpdate.get('unidentifiedDeliveries') ?? []
|
|
|
|
);
|
|
|
|
const sendStateByConversationId = {
|
|
|
|
...(toUpdate.get('sendStateByConversationId') || {}),
|
|
|
|
};
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const unidentifiedStatus: Array<ProcessedUnidentifiedDeliveryStatus> =
|
2022-02-22 20:34:57 +00:00
|
|
|
data && Array.isArray(data.unidentifiedStatus)
|
2021-11-11 22:43:05 +00:00
|
|
|
? data.unidentifiedStatus
|
|
|
|
: [];
|
2021-07-19 22:44:49 +00:00
|
|
|
|
|
|
|
unidentifiedStatus.forEach(
|
2023-08-10 16:43:33 +00:00
|
|
|
({ destinationServiceId, destination, unidentified }) => {
|
|
|
|
const identifier = destinationServiceId || destination;
|
2021-07-19 22:44:49 +00:00
|
|
|
if (!identifier) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const destinationConversation =
|
|
|
|
window.ConversationController.lookupOrCreate({
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: destinationServiceId,
|
2022-08-09 21:39:00 +00:00
|
|
|
e164: destination || undefined,
|
2022-01-20 22:44:25 +00:00
|
|
|
reason: `handleDataMessage(${initialMessage.timestamp})`,
|
2021-11-11 22:43:05 +00:00
|
|
|
});
|
2022-08-09 21:39:00 +00:00
|
|
|
if (!destinationConversation) {
|
2021-07-19 22:44:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-22 20:34:57 +00:00
|
|
|
const updatedAt: number =
|
|
|
|
data && isNormalNumber(data.timestamp)
|
|
|
|
? data.timestamp
|
|
|
|
: Date.now();
|
2021-07-28 18:53:18 +00:00
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
const previousSendState = getOwn(
|
|
|
|
sendStateByConversationId,
|
2022-08-09 21:39:00 +00:00
|
|
|
destinationConversation.id
|
2021-07-19 22:44:49 +00:00
|
|
|
);
|
2022-08-09 21:39:00 +00:00
|
|
|
sendStateByConversationId[destinationConversation.id] =
|
2021-11-11 22:43:05 +00:00
|
|
|
previousSendState
|
|
|
|
? sendStateReducer(previousSendState, {
|
|
|
|
type: SendActionType.Sent,
|
|
|
|
updatedAt,
|
|
|
|
})
|
|
|
|
: {
|
|
|
|
status: SendStatus.Sent,
|
|
|
|
updatedAt,
|
|
|
|
};
|
2021-07-19 22:44:49 +00:00
|
|
|
|
|
|
|
if (unidentified) {
|
|
|
|
unidentifiedDeliveriesSet.add(identifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
toUpdate.set({
|
2021-07-19 22:44:49 +00:00
|
|
|
sendStateByConversationId,
|
|
|
|
unidentifiedDeliveries: [...unidentifiedDeliveriesSet],
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
2021-12-20 21:04:02 +00:00
|
|
|
await window.Signal.Data.saveMessage(toUpdate.attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isUpdate) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2022-09-13 00:52:55 +00:00
|
|
|
`${idLog}: Received update transcript, but no existing entry for message ${message.idForLogging()}. Dropping.`
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (existingMessage) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2022-09-13 00:52:55 +00:00
|
|
|
`${idLog}: Received duplicate transcript for message ${message.idForLogging()}, but it was not an update transcript. Dropping.`
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GroupV2
|
|
|
|
|
2020-11-20 17:30:45 +00:00
|
|
|
if (initialMessage.groupV2) {
|
2021-06-07 16:39:13 +00:00
|
|
|
if (isGroupV1(conversation.attributes)) {
|
2020-11-20 17:30:45 +00:00
|
|
|
// If we received a GroupV2 message in a GroupV1 group, we migrate!
|
|
|
|
|
|
|
|
const { revision, groupChange } = initialMessage.groupV2;
|
|
|
|
await window.Signal.Groups.respondToGroupV2Migration({
|
2020-09-24 20:57:54 +00:00
|
|
|
conversation,
|
2022-03-08 21:07:52 +00:00
|
|
|
groupChange: groupChange
|
|
|
|
? {
|
|
|
|
base64: groupChange,
|
|
|
|
isTrusted: false,
|
|
|
|
}
|
|
|
|
: undefined,
|
2020-09-24 20:57:54 +00:00
|
|
|
newRevision: revision,
|
|
|
|
receivedAt: message.get('received_at'),
|
|
|
|
sentAt: message.get('sent_at'),
|
|
|
|
});
|
2020-11-20 17:30:45 +00:00
|
|
|
} else if (
|
|
|
|
initialMessage.groupV2.masterKey &&
|
|
|
|
initialMessage.groupV2.secretParams &&
|
|
|
|
initialMessage.groupV2.publicParams
|
|
|
|
) {
|
|
|
|
// Repair core GroupV2 data if needed
|
|
|
|
await conversation.maybeRepairGroupV2({
|
|
|
|
masterKey: initialMessage.groupV2.masterKey,
|
|
|
|
secretParams: initialMessage.groupV2.secretParams,
|
|
|
|
publicParams: initialMessage.groupV2.publicParams,
|
|
|
|
});
|
|
|
|
|
|
|
|
const existingRevision = conversation.get('revision');
|
2022-11-30 00:53:39 +00:00
|
|
|
const isFirstUpdate = !isNumber(existingRevision);
|
2022-07-08 20:46:25 +00:00
|
|
|
|
|
|
|
// Standard GroupV2 modification codepath
|
2020-11-20 17:30:45 +00:00
|
|
|
const isV2GroupUpdate =
|
|
|
|
initialMessage.groupV2 &&
|
2022-11-30 00:53:39 +00:00
|
|
|
isNumber(initialMessage.groupV2.revision) &&
|
2022-07-08 20:46:25 +00:00
|
|
|
(isFirstUpdate ||
|
2020-11-20 17:30:45 +00:00
|
|
|
initialMessage.groupV2.revision > existingRevision);
|
|
|
|
|
|
|
|
if (isV2GroupUpdate && initialMessage.groupV2) {
|
|
|
|
const { revision, groupChange } = initialMessage.groupV2;
|
|
|
|
try {
|
|
|
|
await window.Signal.Groups.maybeUpdateGroup({
|
|
|
|
conversation,
|
2022-03-08 21:07:52 +00:00
|
|
|
groupChange: groupChange
|
|
|
|
? {
|
|
|
|
base64: groupChange,
|
|
|
|
isTrusted: false,
|
|
|
|
}
|
|
|
|
: undefined,
|
2020-11-20 17:30:45 +00:00
|
|
|
newRevision: revision,
|
|
|
|
receivedAt: message.get('received_at'),
|
|
|
|
sentAt: message.get('sent_at'),
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2022-11-22 18:43:43 +00:00
|
|
|
const errorText = Errors.toLogFormat(error);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2022-09-13 00:52:55 +00:00
|
|
|
`${idLog}: Failed to process group update as part of message ${message.idForLogging()}: ${errorText}`
|
2020-11-20 17:30:45 +00:00
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
2020-09-24 20:57:54 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2022-08-09 21:39:00 +00:00
|
|
|
const sender = window.ConversationController.lookupOrCreate({
|
2020-09-24 20:57:54 +00:00
|
|
|
e164: source,
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: sourceServiceId,
|
2022-12-03 01:05:27 +00:00
|
|
|
reason: 'handleDataMessage',
|
2020-09-24 20:57:54 +00:00
|
|
|
})!;
|
2021-06-07 16:39:13 +00:00
|
|
|
const hasGroupV2Prop = Boolean(initialMessage.groupV2);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-05-16 14:53:54 +00:00
|
|
|
// Drop if from blocked user. Only GroupV2 messages should need to be dropped here.
|
|
|
|
const isBlocked =
|
|
|
|
(source && window.storage.blocked.isBlocked(source)) ||
|
2023-08-16 20:54:39 +00:00
|
|
|
(sourceServiceId &&
|
|
|
|
window.storage.blocked.isServiceIdBlocked(sourceServiceId));
|
2022-05-16 14:53:54 +00:00
|
|
|
if (isBlocked) {
|
|
|
|
log.info(
|
2022-09-13 00:52:55 +00:00
|
|
|
`${idLog}: Dropping message from blocked sender. hasGroupV2Prop: ${hasGroupV2Prop}`
|
2022-05-16 14:53:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
const areWeMember =
|
2023-08-10 16:43:33 +00:00
|
|
|
!conversation.get('left') && conversation.hasMember(ourAci);
|
2022-07-08 20:46:25 +00:00
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
// Drop an incoming GroupV2 message if we or the sender are not part of the group
|
2021-04-02 20:28:07 +00:00
|
|
|
// after applying the message's associated group changes.
|
2020-09-24 20:57:54 +00:00
|
|
|
if (
|
|
|
|
type === 'incoming' &&
|
2021-06-07 16:39:13 +00:00
|
|
|
!isDirectConversation(conversation.attributes) &&
|
|
|
|
hasGroupV2Prop &&
|
2023-08-16 20:54:39 +00:00
|
|
|
(!areWeMember ||
|
|
|
|
(sourceServiceId && !conversation.hasMember(sourceServiceId)))
|
2020-09-24 20:57:54 +00:00
|
|
|
) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2022-09-13 00:52:55 +00:00
|
|
|
`${idLog}: Received message destined for group, which we or the sender are not a part of. Dropping.`
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We drop incoming messages for v1 groups we already know about, which we're not
|
|
|
|
// a part of, except for group updates. Because group v1 updates haven't been
|
|
|
|
// applied by this point.
|
2020-10-06 15:45:01 +00:00
|
|
|
// Note: if we have no information about a group at all, we will accept those
|
|
|
|
// messages. We detect that via a missing 'members' field.
|
2020-09-24 20:57:54 +00:00
|
|
|
if (
|
|
|
|
type === 'incoming' &&
|
2021-06-07 16:39:13 +00:00
|
|
|
!isDirectConversation(conversation.attributes) &&
|
|
|
|
!hasGroupV2Prop &&
|
2020-10-06 15:45:01 +00:00
|
|
|
conversation.get('members') &&
|
2022-07-08 20:46:25 +00:00
|
|
|
!areWeMember
|
2020-09-24 20:57:54 +00:00
|
|
|
) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2020-09-24 20:57:54 +00:00
|
|
|
`Received message destined for group ${conversation.idForLogging()}, which we're not a part of. Dropping.`
|
|
|
|
);
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-20 20:18:35 +00:00
|
|
|
// Drop incoming messages to announcement only groups where sender is not admin
|
2023-08-10 16:43:33 +00:00
|
|
|
if (conversation.get('announcementsOnly')) {
|
|
|
|
const senderServiceId = sender.getServiceId();
|
|
|
|
if (!senderServiceId || !conversation.isAdmin(senderServiceId)) {
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
2021-07-20 20:18:35 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const messageId = message.get('id') || generateUuid();
|
2021-07-15 23:48:09 +00:00
|
|
|
|
2022-10-07 17:02:08 +00:00
|
|
|
// Send delivery receipts, but only for non-story sealed sender messages
|
|
|
|
// and not for messages from unaccepted conversations
|
2020-09-24 20:57:54 +00:00
|
|
|
if (
|
|
|
|
type === 'incoming' &&
|
|
|
|
this.get('unidentifiedDeliveryReceived') &&
|
2021-06-17 17:15:10 +00:00
|
|
|
!hasErrors(this.attributes) &&
|
2020-09-24 20:57:54 +00:00
|
|
|
conversation.getAccepted()
|
|
|
|
) {
|
|
|
|
// Note: We both queue and batch because we want to wait until we are done
|
|
|
|
// processing incoming messages to start sending outgoing delivery receipts.
|
|
|
|
// The queue can be paused easily.
|
2022-12-21 18:41:48 +00:00
|
|
|
drop(
|
|
|
|
window.Whisper.deliveryReceiptQueue.add(() => {
|
2023-08-16 20:54:39 +00:00
|
|
|
strictAssert(
|
|
|
|
isAciString(sourceServiceId),
|
|
|
|
'Incoming message must be from ACI'
|
|
|
|
);
|
2022-12-21 18:41:48 +00:00
|
|
|
window.Whisper.deliveryReceiptBatcher.add({
|
|
|
|
messageId,
|
2023-02-06 17:24:34 +00:00
|
|
|
conversationId,
|
2022-12-21 18:41:48 +00:00
|
|
|
senderE164: source,
|
2023-08-16 20:54:39 +00:00
|
|
|
senderAci: sourceServiceId,
|
2022-12-21 18:41:48 +00:00
|
|
|
timestamp: this.get('sent_at'),
|
|
|
|
isDirectConversation: isDirectConversation(
|
|
|
|
conversation.attributes
|
|
|
|
),
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 12:12:33 +00:00
|
|
|
const { storyContext } = initialMessage;
|
|
|
|
let storyContextLogId = 'no storyContext';
|
|
|
|
if (storyContext) {
|
|
|
|
storyContextLogId =
|
|
|
|
`storyContext(${storyContext.sentTimestamp}, ` +
|
2023-08-10 16:43:33 +00:00
|
|
|
`${storyContext.authorAci})`;
|
2023-05-25 12:12:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const [quote, storyQuotes] = await Promise.all([
|
2022-03-04 21:14:52 +00:00
|
|
|
this.copyFromQuotedMessage(initialMessage.quote, conversation.id),
|
2023-05-25 12:12:33 +00:00
|
|
|
findStoryMessages(conversation.id, storyContext),
|
2022-03-04 21:14:52 +00:00
|
|
|
]);
|
|
|
|
|
2023-05-25 12:12:33 +00:00
|
|
|
const storyQuote = storyQuotes.find(candidateQuote => {
|
2022-11-10 03:52:53 +00:00
|
|
|
const sendStateByConversationId =
|
2023-05-25 12:12:33 +00:00
|
|
|
candidateQuote.get('sendStateByConversationId') || {};
|
2022-11-10 03:52:53 +00:00
|
|
|
const sendState = sendStateByConversationId[sender.id];
|
|
|
|
|
2022-11-29 00:53:43 +00:00
|
|
|
const storyQuoteIsFromSelf =
|
2023-08-16 20:54:39 +00:00
|
|
|
candidateQuote.get('sourceServiceId') ===
|
2023-08-10 16:43:33 +00:00
|
|
|
window.storage.user.getCheckedAci();
|
2022-11-29 00:53:43 +00:00
|
|
|
|
2023-05-25 12:12:33 +00:00
|
|
|
if (!storyQuoteIsFromSelf) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-05-31 20:07:43 +00:00
|
|
|
|
|
|
|
// The sender is not a recipient for this story
|
2023-05-25 12:12:33 +00:00
|
|
|
if (sendState === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-05-31 20:07:43 +00:00
|
|
|
|
|
|
|
// Group replies are always allowed
|
2023-05-25 12:12:33 +00:00
|
|
|
if (!isDirectConversation(conversation.attributes)) {
|
2023-05-31 20:07:43 +00:00
|
|
|
return true;
|
2023-05-25 12:12:33 +00:00
|
|
|
}
|
2023-05-31 20:07:43 +00:00
|
|
|
|
|
|
|
// For 1:1 stories, we need to check if they can be replied to
|
2023-05-25 12:12:33 +00:00
|
|
|
return sendState.isAllowedToReplyToStory !== false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (storyContext && !storyQuote) {
|
|
|
|
if (!isDirectConversation(conversation.attributes)) {
|
2022-11-10 03:52:53 +00:00
|
|
|
log.warn(
|
2023-05-25 12:12:33 +00:00
|
|
|
`${idLog}: Received ${storyContextLogId} message in group but no matching story. Dropping.`
|
2022-11-10 03:52:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-25 12:12:33 +00:00
|
|
|
if (storyQuotes.length === 0) {
|
2022-11-10 03:52:53 +00:00
|
|
|
log.warn(
|
2023-05-25 12:12:33 +00:00
|
|
|
`${idLog}: Received ${storyContextLogId} message but no matching story. We'll try processing this message again later.`
|
2022-11-10 03:52:53 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-25 12:12:33 +00:00
|
|
|
log.warn(
|
|
|
|
`${idLog}: Received ${storyContextLogId} message in 1:1 conversation but no matching story. Dropping.`
|
|
|
|
);
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (storyQuote) {
|
2022-11-10 03:52:53 +00:00
|
|
|
const storyDistributionListId = storyQuote.get(
|
|
|
|
'storyDistributionListId'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (storyDistributionListId) {
|
|
|
|
const storyDistribution =
|
|
|
|
await dataInterface.getStoryDistributionWithMembers(
|
|
|
|
storyDistributionListId
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!storyDistribution) {
|
|
|
|
log.warn(
|
2023-05-25 12:12:33 +00:00
|
|
|
`${idLog}: Received ${storyContextLogId} message for story with no associated distribution list. Dropping.`
|
2022-11-10 03:52:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!storyDistribution.allowsReplies) {
|
|
|
|
log.warn(
|
2023-05-25 12:12:33 +00:00
|
|
|
`${idLog}: Received ${storyContextLogId} message but distribution list does not allow replies. Dropping.`
|
2022-11-10 03:52:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const withQuoteReference = {
|
2022-06-13 21:39:35 +00:00
|
|
|
...message.attributes,
|
2021-07-09 19:36:10 +00:00
|
|
|
...initialMessage,
|
2022-03-04 21:14:52 +00:00
|
|
|
quote,
|
|
|
|
storyId: storyQuote?.id,
|
2021-07-09 19:36:10 +00:00
|
|
|
};
|
2022-03-16 17:30:14 +00:00
|
|
|
|
2023-01-05 21:58:13 +00:00
|
|
|
// There are type conflicts between ModelAttributesType and protos passed in here
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const dataMessage = await upgradeMessageSchema(withQuoteReference as any);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-11-01 18:58:07 +00:00
|
|
|
const isGroupStoryReply =
|
|
|
|
isGroup(conversation.attributes) && dataMessage.storyId;
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
try {
|
|
|
|
const now = new Date().getTime();
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
const urls = LinkPreview.findLinks(dataMessage.body || '');
|
2020-09-24 20:57:54 +00:00
|
|
|
const incomingPreview = dataMessage.preview || [];
|
2022-10-31 21:28:28 +00:00
|
|
|
const preview = incomingPreview.filter((item: LinkPreviewType) => {
|
|
|
|
if (!item.image && !item.title) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Story link previews don't have to correspond to links in the
|
|
|
|
// message body.
|
|
|
|
if (isStory(message.attributes)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
urls.includes(item.url) && LinkPreview.shouldPreviewHref(item.url)
|
|
|
|
);
|
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
if (preview.length < incomingPreview.length) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2020-11-18 15:15:42 +00:00
|
|
|
`${message.idForLogging()}: Eliminated ${
|
|
|
|
preview.length - incomingPreview.length
|
|
|
|
} previews with invalid urls'`
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourPni = window.textsecure.storage.user.getCheckedPni();
|
2023-08-16 20:54:39 +00:00
|
|
|
const ourServiceIds: Set<ServiceIdString> = new Set([ourAci, ourPni]);
|
2023-05-23 21:59:07 +00:00
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
window.MessageCache.toMessageAttributes(this.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
message.set({
|
2021-07-15 23:48:09 +00:00
|
|
|
id: messageId,
|
2020-09-24 20:57:54 +00:00
|
|
|
attachments: dataMessage.attachments,
|
|
|
|
body: dataMessage.body,
|
|
|
|
bodyRanges: dataMessage.bodyRanges,
|
|
|
|
contact: dataMessage.contact,
|
|
|
|
conversationId: conversation.id,
|
|
|
|
decrypted_at: now,
|
|
|
|
errors: [],
|
|
|
|
flags: dataMessage.flags,
|
2022-05-11 20:59:58 +00:00
|
|
|
giftBadge: initialMessage.giftBadge,
|
2020-09-24 20:57:54 +00:00
|
|
|
hasAttachments: dataMessage.hasAttachments,
|
|
|
|
hasFileAttachments: dataMessage.hasFileAttachments,
|
|
|
|
hasVisualMediaAttachments: dataMessage.hasVisualMediaAttachments,
|
|
|
|
isViewOnce: Boolean(dataMessage.isViewOnce),
|
2023-05-23 21:59:07 +00:00
|
|
|
mentionsMe: (dataMessage.bodyRanges ?? []).some(bodyRange => {
|
|
|
|
if (!BodyRange.isMention(bodyRange)) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-16 20:54:39 +00:00
|
|
|
return ourServiceIds.has(
|
2023-08-10 16:43:33 +00:00
|
|
|
normalizeServiceId(
|
2023-08-16 20:54:39 +00:00
|
|
|
bodyRange.mentionAci,
|
2023-05-26 17:14:32 +00:00
|
|
|
'handleDataMessage: mentionsMe check'
|
|
|
|
)
|
|
|
|
);
|
2023-05-23 21:59:07 +00:00
|
|
|
}),
|
2020-09-24 20:57:54 +00:00
|
|
|
preview,
|
|
|
|
requiredProtocolVersion:
|
|
|
|
dataMessage.requiredProtocolVersion ||
|
|
|
|
this.INITIAL_PROTOCOL_VERSION,
|
|
|
|
supportedVersionAtReceive: this.CURRENT_PROTOCOL_VERSION,
|
2022-11-30 21:47:54 +00:00
|
|
|
payment: dataMessage.payment,
|
2020-09-24 20:57:54 +00:00
|
|
|
quote: dataMessage.quote,
|
|
|
|
schemaVersion: dataMessage.schemaVersion,
|
|
|
|
sticker: dataMessage.sticker,
|
2022-03-04 21:14:52 +00:00
|
|
|
storyId: dataMessage.storyId,
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
|
|
|
|
2022-08-10 18:48:52 +00:00
|
|
|
if (storyQuote) {
|
2023-07-21 22:10:32 +00:00
|
|
|
await this.hydrateStoryContext(storyQuote.attributes, {
|
|
|
|
shouldSave: true,
|
|
|
|
});
|
2022-08-10 18:48:52 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
const isSupported = !isUnsupportedMessage(message.attributes);
|
2020-09-24 20:57:54 +00:00
|
|
|
if (!isSupported) {
|
|
|
|
await message.eraseContents();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSupported) {
|
2023-04-15 00:52:50 +00:00
|
|
|
const attributes = {
|
2020-09-24 20:57:54 +00:00
|
|
|
...conversation.attributes,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Drop empty messages after. This needs to happen after the initial
|
|
|
|
// message.set call and after GroupV1 processing to make sure all possible
|
|
|
|
// properties are set before we determine that a message is empty.
|
|
|
|
if (message.isEmpty()) {
|
2022-09-13 00:52:55 +00:00
|
|
|
log.info(`${idLog}: Dropping empty message`);
|
2020-09-24 20:57:54 +00:00
|
|
|
confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-29 01:10:08 +00:00
|
|
|
if (isStory(message.attributes)) {
|
|
|
|
attributes.hasPostedStory = true;
|
2022-04-28 18:59:09 +00:00
|
|
|
} else {
|
|
|
|
attributes.active_at = now;
|
2022-03-29 01:10:08 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
conversation.set(attributes);
|
|
|
|
|
2022-11-01 18:58:07 +00:00
|
|
|
// Sync group story reply expiration timers with the parent story's
|
|
|
|
// expiration timer
|
|
|
|
if (isGroupStoryReply && storyQuote) {
|
|
|
|
message.set({
|
|
|
|
expireTimer: storyQuote.get('expireTimer'),
|
|
|
|
expirationStartTimestamp: storyQuote.get(
|
|
|
|
'expirationStartTimestamp'
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-01 23:48:40 +00:00
|
|
|
if (
|
|
|
|
dataMessage.expireTimer &&
|
|
|
|
!isExpirationTimerUpdate(dataMessage)
|
|
|
|
) {
|
2020-09-24 20:57:54 +00:00
|
|
|
message.set({ expireTimer: dataMessage.expireTimer });
|
2022-10-17 23:30:03 +00:00
|
|
|
if (isStory(message.attributes)) {
|
|
|
|
log.info(`${idLog}: Starting story expiration`);
|
|
|
|
message.set({
|
|
|
|
expirationStartTimestamp: dataMessage.timestamp,
|
|
|
|
});
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 02:20:01 +00:00
|
|
|
if (!hasGroupV2Prop && !isStory(message.attributes)) {
|
2021-06-17 17:15:10 +00:00
|
|
|
if (isExpirationTimerUpdate(message.attributes)) {
|
2020-09-24 20:57:54 +00:00
|
|
|
message.set({
|
|
|
|
expirationTimerUpdate: {
|
|
|
|
source,
|
2023-08-16 20:54:39 +00:00
|
|
|
sourceServiceId,
|
2022-06-13 21:39:35 +00:00
|
|
|
expireTimer: initialMessage.expireTimer,
|
2020-09-24 20:57:54 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-07-12 00:32:26 +00:00
|
|
|
if (conversation.get('expireTimer') !== dataMessage.expireTimer) {
|
|
|
|
log.info('Incoming expirationTimerUpdate changed timer', {
|
|
|
|
id: conversation.idForLogging(),
|
|
|
|
expireTimer: dataMessage.expireTimer || 'disabled',
|
2022-09-13 00:52:55 +00:00
|
|
|
source: idLog,
|
2022-07-12 00:32:26 +00:00
|
|
|
});
|
|
|
|
conversation.set({
|
|
|
|
expireTimer: dataMessage.expireTimer,
|
2022-06-20 22:43:16 +00:00
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-12 00:32:26 +00:00
|
|
|
|
|
|
|
// Note: For incoming expire timer updates (not normal messages that come
|
|
|
|
// along with an expireTimer), the conversation will be updated by this
|
|
|
|
// point and these calls will return early.
|
|
|
|
if (dataMessage.expireTimer) {
|
2022-12-21 18:41:48 +00:00
|
|
|
void conversation.updateExpirationTimer(dataMessage.expireTimer, {
|
2023-08-16 20:54:39 +00:00
|
|
|
source: sourceServiceId || source,
|
2022-07-12 00:32:26 +00:00
|
|
|
receivedAt: message.get('received_at'),
|
|
|
|
receivedAtMS: message.get('received_at_ms'),
|
|
|
|
sentAt: message.get('sent_at'),
|
|
|
|
fromGroupUpdate: isGroupUpdate(message.attributes),
|
2022-09-13 00:52:55 +00:00
|
|
|
reason: idLog,
|
2022-07-12 00:32:26 +00:00
|
|
|
});
|
|
|
|
} else if (
|
|
|
|
// We won't turn off timers for these kinds of messages:
|
|
|
|
!isGroupUpdate(message.attributes) &&
|
|
|
|
!isEndSession(message.attributes)
|
|
|
|
) {
|
2022-12-21 18:41:48 +00:00
|
|
|
void conversation.updateExpirationTimer(undefined, {
|
2023-08-16 20:54:39 +00:00
|
|
|
source: sourceServiceId || source,
|
2022-07-12 00:32:26 +00:00
|
|
|
receivedAt: message.get('received_at'),
|
|
|
|
receivedAtMS: message.get('received_at_ms'),
|
|
|
|
sentAt: message.get('sent_at'),
|
2022-09-13 00:52:55 +00:00
|
|
|
reason: idLog,
|
2022-07-12 00:32:26 +00:00
|
|
|
});
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
if (initialMessage.profileKey) {
|
|
|
|
const { profileKey } = initialMessage;
|
2020-09-24 20:57:54 +00:00
|
|
|
if (
|
|
|
|
source === window.textsecure.storage.user.getNumber() ||
|
2023-08-16 20:54:39 +00:00
|
|
|
sourceServiceId === window.textsecure.storage.user.getAci()
|
2020-09-24 20:57:54 +00:00
|
|
|
) {
|
|
|
|
conversation.set({ profileSharing: true });
|
2021-06-07 16:39:13 +00:00
|
|
|
} else if (isDirectConversation(conversation.attributes)) {
|
2022-12-21 18:41:48 +00:00
|
|
|
void conversation.setProfileKey(profileKey);
|
2020-09-24 20:57:54 +00:00
|
|
|
} else {
|
2022-08-09 21:39:00 +00:00
|
|
|
const local = window.ConversationController.lookupOrCreate({
|
2020-09-24 20:57:54 +00:00
|
|
|
e164: source,
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: sourceServiceId,
|
2022-12-03 01:05:27 +00:00
|
|
|
reason: 'handleDataMessage:setProfileKey',
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
2022-12-21 18:41:48 +00:00
|
|
|
void local?.setProfileKey(profileKey);
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
if (isTapToView(message.attributes) && type === 'outgoing') {
|
2020-09-24 20:57:54 +00:00
|
|
|
await message.eraseContents();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
type === 'incoming' &&
|
2021-06-17 17:15:10 +00:00
|
|
|
isTapToView(message.attributes) &&
|
2020-09-24 20:57:54 +00:00
|
|
|
!message.isValidTapToView()
|
|
|
|
) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2022-09-13 00:52:55 +00:00
|
|
|
`${idLog}: Received tap to view message with invalid data. Erasing contents.`
|
2020-09-24 20:57:54 +00:00
|
|
|
);
|
|
|
|
message.set({
|
|
|
|
isTapToViewInvalid: true,
|
|
|
|
});
|
|
|
|
await message.eraseContents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const conversationTimestamp = conversation.get('timestamp');
|
|
|
|
if (
|
2022-04-28 18:59:09 +00:00
|
|
|
!isStory(message.attributes) &&
|
2022-04-20 23:33:38 +00:00
|
|
|
!isGroupStoryReply &&
|
|
|
|
(!conversationTimestamp ||
|
2022-11-30 21:47:54 +00:00
|
|
|
message.get('sent_at') > conversationTimestamp) &&
|
|
|
|
messageHasPaymentEvent(message.attributes)
|
2020-09-24 20:57:54 +00:00
|
|
|
) {
|
|
|
|
conversation.set({
|
|
|
|
lastMessage: message.getNotificationText(),
|
2023-10-04 00:12:57 +00:00
|
|
|
lastMessageAuthor: getMessageAuthorText(message.attributes),
|
2020-09-24 20:57:54 +00:00
|
|
|
timestamp: message.get('sent_at'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
message = window.MessageCache.__DEPRECATED$register(
|
|
|
|
message.id,
|
|
|
|
message,
|
|
|
|
'handleDataMessage/message'
|
|
|
|
);
|
2020-09-24 20:57:54 +00:00
|
|
|
conversation.incrementMessageCount();
|
2022-09-19 20:40:03 +00:00
|
|
|
|
|
|
|
// If we sent a message in a given conversation, unarchive it!
|
|
|
|
if (type === 'outgoing') {
|
|
|
|
conversation.setArchived(false);
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
window.Signal.Data.updateConversation(conversation.attributes);
|
|
|
|
|
2022-05-11 20:59:58 +00:00
|
|
|
const reduxState = window.reduxStore.getState();
|
|
|
|
|
|
|
|
const giftBadge = message.get('giftBadge');
|
|
|
|
if (giftBadge) {
|
|
|
|
const { level } = giftBadge;
|
2022-05-16 19:54:38 +00:00
|
|
|
const { updatesUrl } = window.SignalContext.config;
|
|
|
|
strictAssert(
|
|
|
|
typeof updatesUrl === 'string',
|
|
|
|
'getProfile: expected updatesUrl to be a defined string'
|
|
|
|
);
|
|
|
|
const userLanguages = getUserLanguages(
|
2023-08-07 20:28:09 +00:00
|
|
|
window.SignalContext.getPreferredSystemLocales(),
|
|
|
|
window.SignalContext.getResolvedMessagesLocale()
|
2022-05-16 19:54:38 +00:00
|
|
|
);
|
2022-06-13 21:39:35 +00:00
|
|
|
const { messaging } = window.textsecure;
|
|
|
|
if (!messaging) {
|
2022-09-13 00:52:55 +00:00
|
|
|
throw new Error(`${idLog}: messaging is not available`);
|
2022-06-13 21:39:35 +00:00
|
|
|
}
|
2023-10-06 21:31:17 +00:00
|
|
|
const response = await messaging.server.getSubscriptionConfiguration(
|
2022-06-13 21:39:35 +00:00
|
|
|
userLanguages
|
|
|
|
);
|
2022-05-16 19:54:38 +00:00
|
|
|
const boostBadgesByLevel = parseBoostBadgeListFromServer(
|
|
|
|
response,
|
|
|
|
updatesUrl
|
|
|
|
);
|
|
|
|
const badge = boostBadgesByLevel[level];
|
|
|
|
if (!badge) {
|
|
|
|
log.error(
|
2022-09-13 00:52:55 +00:00
|
|
|
`${idLog}: gift badge with level ${level} not found on server`
|
2022-05-11 20:59:58 +00:00
|
|
|
);
|
2022-05-16 19:54:38 +00:00
|
|
|
} else {
|
|
|
|
await window.reduxActions.badges.updateOrCreate([badge]);
|
|
|
|
giftBadge.id = badge.id;
|
2022-05-11 20:59:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 21:14:52 +00:00
|
|
|
// Only queue attachments for downloads if this is a story or
|
|
|
|
// outgoing message or we've accepted the conversation
|
2021-01-29 22:58:28 +00:00
|
|
|
const attachments = this.get('attachments') || [];
|
2022-03-29 01:10:08 +00:00
|
|
|
|
|
|
|
let queueStoryForDownload = false;
|
|
|
|
if (isStory(message.attributes)) {
|
2023-08-09 00:53:06 +00:00
|
|
|
queueStoryForDownload = await shouldDownloadStory(
|
|
|
|
conversation.attributes
|
|
|
|
);
|
2022-03-29 01:10:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 22:58:28 +00:00
|
|
|
const shouldHoldOffDownload =
|
2022-03-30 20:53:30 +00:00
|
|
|
(isStory(message.attributes) && !queueStoryForDownload) ||
|
2022-04-08 17:03:10 +00:00
|
|
|
(!isStory(message.attributes) &&
|
|
|
|
(isImage(attachments) || isVideo(attachments)) &&
|
2022-03-29 01:10:08 +00:00
|
|
|
isInCall(reduxState));
|
2022-03-30 20:53:30 +00:00
|
|
|
|
2021-01-29 22:58:28 +00:00
|
|
|
if (
|
2021-03-09 19:11:52 +00:00
|
|
|
this.hasAttachmentDownloads() &&
|
2022-04-08 02:12:35 +00:00
|
|
|
(conversation.getAccepted() || isOutgoing(message.attributes)) &&
|
2021-01-29 22:58:28 +00:00
|
|
|
!shouldHoldOffDownload
|
|
|
|
) {
|
2023-01-13 00:24:59 +00:00
|
|
|
if (shouldUseAttachmentDownloadQueue()) {
|
|
|
|
addToAttachmentDownloadQueue(idLog, message);
|
2021-03-05 17:57:09 +00:00
|
|
|
} else {
|
|
|
|
await message.queueAttachmentDownloads();
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 21:52:16 +00:00
|
|
|
const isFirstRun = true;
|
|
|
|
await this.modifyTargetMessage(conversation, isFirstRun);
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-09-13 00:52:55 +00:00
|
|
|
log.info(`${idLog}: Batching save`);
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.saveAndNotify(conversation, confirm);
|
2020-09-24 20:57:54 +00:00
|
|
|
} catch (error) {
|
2022-11-22 18:43:43 +00:00
|
|
|
const errorForLog = Errors.toLogFormat(error);
|
2022-09-13 00:52:55 +00:00
|
|
|
log.error(`${idLog}: error:`, errorForLog);
|
2020-09-24 20:57:54 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-04 21:44:57 +00:00
|
|
|
async saveAndNotify(
|
|
|
|
conversation: ConversationModel,
|
|
|
|
confirm: () => void
|
|
|
|
): Promise<void> {
|
2023-04-11 03:54:43 +00:00
|
|
|
await saveNewMessageBatcher.add(this.attributes);
|
2021-03-04 21:44:57 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Message saved', this.get('sent_at'));
|
2021-03-04 21:44:57 +00:00
|
|
|
|
|
|
|
conversation.trigger('newmessage', this);
|
|
|
|
|
2021-03-15 21:52:16 +00:00
|
|
|
const isFirstRun = false;
|
|
|
|
await this.modifyTargetMessage(conversation, isFirstRun);
|
2021-03-13 01:22:36 +00:00
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
if (await shouldReplyNotifyUser(this.attributes, conversation)) {
|
2021-03-04 21:44:57 +00:00
|
|
|
await conversation.notify(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment the sent message count if this is an outgoing message
|
|
|
|
if (this.get('type') === 'outgoing') {
|
|
|
|
conversation.incrementSentMessageCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
window.Whisper.events.trigger('incrementProgress');
|
|
|
|
confirm();
|
2022-05-03 20:24:31 +00:00
|
|
|
|
2022-07-07 16:48:05 +00:00
|
|
|
if (!isStory(this.attributes)) {
|
2022-12-21 18:41:48 +00:00
|
|
|
drop(
|
|
|
|
conversation.queueJob('updateUnread', () => conversation.updateUnread())
|
|
|
|
);
|
2022-07-07 16:48:05 +00:00
|
|
|
}
|
2021-03-04 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 21:52:16 +00:00
|
|
|
// This function is called twice - once from handleDataMessage, and then again from
|
|
|
|
// saveAndNotify, a function called at the end of handleDataMessage as a cleanup for
|
|
|
|
// any missed out-of-order events.
|
2021-03-13 01:22:36 +00:00
|
|
|
async modifyTargetMessage(
|
|
|
|
conversation: ConversationModel,
|
2021-03-15 21:52:16 +00:00
|
|
|
isFirstRun: boolean
|
2021-03-13 01:22:36 +00:00
|
|
|
): Promise<void> {
|
2023-07-20 00:17:13 +00:00
|
|
|
return modifyTargetMessage(this, conversation, {
|
|
|
|
isFirstRun,
|
|
|
|
skipEdits: false,
|
|
|
|
});
|
2021-03-13 01:22:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
async handleReaction(
|
2023-08-21 20:08:27 +00:00
|
|
|
reaction: ReactionAttributesType,
|
2023-01-11 22:54:06 +00:00
|
|
|
{
|
|
|
|
storyMessage,
|
|
|
|
shouldPersist = true,
|
|
|
|
}: {
|
|
|
|
storyMessage?: MessageAttributesType;
|
|
|
|
shouldPersist?: boolean;
|
|
|
|
} = {}
|
2021-10-29 23:19:44 +00:00
|
|
|
): Promise<void> {
|
2021-06-17 17:15:10 +00:00
|
|
|
const { attributes } = this;
|
|
|
|
|
2020-09-24 20:57:54 +00:00
|
|
|
if (this.get('deletedForEveryone')) {
|
2021-10-29 23:19:44 +00:00
|
|
|
return;
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 17:11:41 +00:00
|
|
|
// We allow you to react to messages with outgoing errors only if it has sent
|
|
|
|
// successfully to at least one person.
|
|
|
|
if (
|
2021-06-17 17:15:10 +00:00
|
|
|
hasErrors(attributes) &&
|
|
|
|
(isIncoming(attributes) ||
|
|
|
|
getMessagePropStatus(
|
|
|
|
attributes,
|
2021-07-21 19:21:16 +00:00
|
|
|
window.ConversationController.getOurConversationIdOrThrow()
|
2021-06-17 17:15:10 +00:00
|
|
|
) !== 'partial-sent')
|
2020-10-12 17:11:41 +00:00
|
|
|
) {
|
2021-10-29 23:19:44 +00:00
|
|
|
return;
|
2020-10-12 17:11:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 23:19:44 +00:00
|
|
|
const conversation = this.getConversation();
|
|
|
|
if (!conversation) {
|
|
|
|
return;
|
2021-10-27 15:51:42 +00:00
|
|
|
}
|
2021-03-03 19:03:11 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const isFromThisDevice = reaction.source === ReactionSource.FromThisDevice;
|
|
|
|
const isFromSync = reaction.source === ReactionSource.FromSync;
|
2022-11-02 23:48:38 +00:00
|
|
|
const isFromSomeoneElse =
|
2023-08-21 20:08:27 +00:00
|
|
|
reaction.source === ReactionSource.FromSomeoneElse;
|
2022-11-02 23:48:38 +00:00
|
|
|
strictAssert(
|
|
|
|
isFromThisDevice || isFromSync || isFromSomeoneElse,
|
|
|
|
'Reaction can only be from this device, from sync, or from someone else'
|
|
|
|
);
|
|
|
|
|
2023-01-12 20:29:04 +00:00
|
|
|
const newReaction: MessageReactionType = {
|
2023-08-21 20:08:27 +00:00
|
|
|
emoji: reaction.remove ? undefined : reaction.emoji,
|
|
|
|
fromId: reaction.fromId,
|
|
|
|
targetTimestamp: reaction.targetTimestamp,
|
|
|
|
timestamp: reaction.timestamp,
|
2023-01-12 20:29:04 +00:00
|
|
|
isSentByConversationId: isFromThisDevice
|
|
|
|
? zipObject(conversation.getMemberConversationIds(), repeat(false))
|
|
|
|
: undefined,
|
|
|
|
};
|
|
|
|
|
2023-01-11 22:54:06 +00:00
|
|
|
// Reactions to stories are saved as separate messages, and so require a totally
|
|
|
|
// different codepath.
|
|
|
|
if (storyMessage) {
|
2022-11-02 23:48:38 +00:00
|
|
|
if (isFromThisDevice) {
|
|
|
|
log.info(
|
|
|
|
'handleReaction: sending story reaction to ' +
|
2023-01-11 22:54:06 +00:00
|
|
|
`${getMessageIdForLogging(storyMessage)} from this device`
|
2022-11-02 23:48:38 +00:00
|
|
|
);
|
|
|
|
} else {
|
2023-02-01 23:38:33 +00:00
|
|
|
if (isFromSomeoneElse) {
|
|
|
|
log.info(
|
|
|
|
'handleReaction: receiving story reaction to ' +
|
|
|
|
`${getMessageIdForLogging(storyMessage)} from someone else`
|
|
|
|
);
|
|
|
|
} else if (isFromSync) {
|
|
|
|
log.info(
|
|
|
|
'handleReaction: receiving story reaction to ' +
|
|
|
|
`${getMessageIdForLogging(storyMessage)} from another device`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const generatedMessage = reaction.storyReactionMessage;
|
2023-01-11 22:54:06 +00:00
|
|
|
strictAssert(
|
|
|
|
generatedMessage,
|
|
|
|
'Story reactions must provide storyReactionMessage'
|
|
|
|
);
|
|
|
|
const targetConversation = window.ConversationController.get(
|
|
|
|
generatedMessage.get('conversationId')
|
|
|
|
);
|
|
|
|
strictAssert(
|
|
|
|
targetConversation,
|
|
|
|
'handleReaction: targetConversation not found'
|
2022-11-02 23:48:38 +00:00
|
|
|
);
|
2021-03-03 19:03:11 +00:00
|
|
|
|
2023-01-11 22:54:06 +00:00
|
|
|
generatedMessage.set({
|
|
|
|
expireTimer: isDirectConversation(targetConversation.attributes)
|
|
|
|
? targetConversation.get('expireTimer')
|
|
|
|
: undefined,
|
|
|
|
storyId: storyMessage.id,
|
|
|
|
storyReaction: {
|
2023-08-21 20:08:27 +00:00
|
|
|
emoji: reaction.emoji,
|
|
|
|
targetAuthorAci: reaction.targetAuthorAci,
|
|
|
|
targetTimestamp: reaction.targetTimestamp,
|
2023-01-11 22:54:06 +00:00
|
|
|
},
|
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2023-07-21 22:10:32 +00:00
|
|
|
await generatedMessage.hydrateStoryContext(storyMessage, {
|
|
|
|
shouldSave: false,
|
|
|
|
});
|
2023-01-11 22:54:06 +00:00
|
|
|
// Note: generatedMessage comes with an id, so we have to force this save
|
2023-07-21 22:10:32 +00:00
|
|
|
await window.Signal.Data.saveMessage(generatedMessage.attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2023-07-21 22:10:32 +00:00
|
|
|
forceSave: true,
|
|
|
|
});
|
2023-01-11 22:54:06 +00:00
|
|
|
|
|
|
|
log.info('Reactions.onReaction adding reaction to story', {
|
|
|
|
reactionMessageId: getMessageIdForLogging(
|
|
|
|
generatedMessage.attributes
|
|
|
|
),
|
|
|
|
storyId: getMessageIdForLogging(storyMessage),
|
2023-08-21 20:08:27 +00:00
|
|
|
targetTimestamp: reaction.targetTimestamp,
|
|
|
|
timestamp: reaction.timestamp,
|
2023-01-11 22:54:06 +00:00
|
|
|
});
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
const messageToAdd = window.MessageCache.__DEPRECATED$register(
|
2023-01-11 22:54:06 +00:00
|
|
|
generatedMessage.id,
|
2023-10-04 00:12:57 +00:00
|
|
|
generatedMessage,
|
|
|
|
'generatedMessage'
|
2021-10-29 23:19:44 +00:00
|
|
|
);
|
2023-01-11 22:54:06 +00:00
|
|
|
if (isDirectConversation(targetConversation.attributes)) {
|
|
|
|
await targetConversation.addSingleMessage(messageToAdd);
|
|
|
|
if (!targetConversation.get('active_at')) {
|
|
|
|
targetConversation.set({
|
|
|
|
active_at: messageToAdd.get('timestamp'),
|
|
|
|
});
|
|
|
|
window.Signal.Data.updateConversation(
|
|
|
|
targetConversation.attributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 01:15:25 +00:00
|
|
|
|
2023-02-01 23:38:33 +00:00
|
|
|
if (isFromSomeoneElse) {
|
2023-01-11 22:54:06 +00:00
|
|
|
log.info(
|
2023-02-01 23:38:33 +00:00
|
|
|
'handleReaction: notifying for story reaction to ' +
|
2023-01-11 22:54:06 +00:00
|
|
|
`${getMessageIdForLogging(storyMessage)} from someone else`
|
2021-10-29 23:19:44 +00:00
|
|
|
);
|
2023-10-04 00:12:57 +00:00
|
|
|
if (
|
|
|
|
await shouldReplyNotifyUser(
|
|
|
|
messageToAdd.attributes,
|
|
|
|
targetConversation
|
|
|
|
)
|
|
|
|
) {
|
2023-02-01 23:38:33 +00:00
|
|
|
drop(targetConversation.notify(messageToAdd));
|
|
|
|
}
|
2021-10-29 23:19:44 +00:00
|
|
|
}
|
2023-01-11 22:54:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Reactions to all messages other than stories will update the target message
|
|
|
|
const previousLength = (this.get('reactions') || []).length;
|
2021-10-29 23:19:44 +00:00
|
|
|
|
2023-01-11 22:54:06 +00:00
|
|
|
if (isFromThisDevice) {
|
2021-10-29 23:19:44 +00:00
|
|
|
log.info(
|
2023-01-11 22:54:06 +00:00
|
|
|
`handleReaction: sending reaction to ${this.idForLogging()} ` +
|
|
|
|
'from this device'
|
2021-10-29 23:19:44 +00:00
|
|
|
);
|
|
|
|
|
2023-01-11 22:54:06 +00:00
|
|
|
const reactions = reactionUtil.addOutgoingReaction(
|
|
|
|
this.get('reactions') || [],
|
|
|
|
newReaction
|
2021-10-29 23:19:44 +00:00
|
|
|
);
|
2021-12-06 17:22:44 +00:00
|
|
|
this.set({ reactions });
|
2023-01-11 22:54:06 +00:00
|
|
|
} else {
|
|
|
|
const oldReactions = this.get('reactions') || [];
|
|
|
|
let reactions: Array<MessageReactionType>;
|
|
|
|
const oldReaction = oldReactions.find(re =>
|
|
|
|
isNewReactionReplacingPrevious(re, newReaction)
|
|
|
|
);
|
|
|
|
if (oldReaction) {
|
|
|
|
this.clearNotifications(oldReaction);
|
2021-10-29 23:19:44 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
if (reaction.remove) {
|
2023-01-11 22:54:06 +00:00
|
|
|
log.info(
|
|
|
|
'handleReaction: removing reaction for message',
|
|
|
|
this.idForLogging()
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isFromSync) {
|
|
|
|
reactions = oldReactions.filter(
|
|
|
|
re =>
|
|
|
|
!isNewReactionReplacingPrevious(re, newReaction) ||
|
2023-08-21 20:08:27 +00:00
|
|
|
re.timestamp > reaction.timestamp
|
2023-01-11 22:54:06 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
reactions = oldReactions.filter(
|
|
|
|
re => !isNewReactionReplacingPrevious(re, newReaction)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.set({ reactions });
|
|
|
|
|
|
|
|
await window.Signal.Data.removeReactionFromConversation({
|
2023-08-21 20:08:27 +00:00
|
|
|
emoji: reaction.emoji,
|
|
|
|
fromId: reaction.fromId,
|
|
|
|
targetAuthorServiceId: reaction.targetAuthorAci,
|
|
|
|
targetTimestamp: reaction.targetTimestamp,
|
2023-01-11 22:54:06 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
log.info(
|
|
|
|
'handleReaction: adding reaction for message',
|
|
|
|
this.idForLogging()
|
|
|
|
);
|
|
|
|
|
|
|
|
let reactionToAdd: MessageReactionType;
|
|
|
|
if (isFromSync) {
|
|
|
|
const ourReactions = [
|
|
|
|
newReaction,
|
2023-08-21 20:08:27 +00:00
|
|
|
...oldReactions.filter(re => re.fromId === reaction.fromId),
|
2023-01-11 22:54:06 +00:00
|
|
|
];
|
|
|
|
reactionToAdd = maxBy(ourReactions, 'timestamp') || newReaction;
|
|
|
|
} else {
|
|
|
|
reactionToAdd = newReaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
reactions = oldReactions.filter(
|
2023-08-21 20:08:27 +00:00
|
|
|
re => !isNewReactionReplacingPrevious(re, reaction)
|
2023-01-11 22:54:06 +00:00
|
|
|
);
|
|
|
|
reactions.push(reactionToAdd);
|
|
|
|
this.set({ reactions });
|
|
|
|
|
|
|
|
if (isOutgoing(this.attributes) && isFromSomeoneElse) {
|
|
|
|
void conversation.notify(this, reaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
await window.Signal.Data.addReaction({
|
|
|
|
conversationId: this.get('conversationId'),
|
2023-08-21 20:08:27 +00:00
|
|
|
emoji: reaction.emoji,
|
|
|
|
fromId: reaction.fromId,
|
2023-01-11 22:54:06 +00:00
|
|
|
messageId: this.id,
|
|
|
|
messageReceivedAt: this.get('received_at'),
|
2023-08-21 20:08:27 +00:00
|
|
|
targetAuthorAci: reaction.targetAuthorAci,
|
|
|
|
targetTimestamp: reaction.targetTimestamp,
|
2023-01-11 22:54:06 +00:00
|
|
|
});
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 22:54:06 +00:00
|
|
|
const currentLength = (this.get('reactions') || []).length;
|
|
|
|
log.info(
|
|
|
|
'handleReaction:',
|
|
|
|
`Done processing reaction for message ${this.idForLogging()}.`,
|
|
|
|
`Went from ${previousLength} to ${currentLength} reactions.`
|
|
|
|
);
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-11-02 23:48:38 +00:00
|
|
|
if (isFromThisDevice) {
|
|
|
|
let jobData: ConversationQueueJobData;
|
2023-01-11 22:54:06 +00:00
|
|
|
if (storyMessage) {
|
2022-11-02 23:48:38 +00:00
|
|
|
strictAssert(
|
|
|
|
newReaction.emoji !== undefined,
|
|
|
|
'New story reaction must have an emoji'
|
|
|
|
);
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const generatedMessage = reaction.storyReactionMessage;
|
2023-01-11 22:54:06 +00:00
|
|
|
strictAssert(
|
|
|
|
generatedMessage,
|
|
|
|
'Story reactions must provide storyReactionmessage'
|
|
|
|
);
|
2023-07-21 22:10:32 +00:00
|
|
|
|
|
|
|
await generatedMessage.hydrateStoryContext(this.attributes, {
|
|
|
|
shouldSave: false,
|
|
|
|
});
|
|
|
|
await window.Signal.Data.saveMessage(generatedMessage.attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2023-07-21 22:10:32 +00:00
|
|
|
forceSave: true,
|
|
|
|
});
|
2022-11-02 23:48:38 +00:00
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
void conversation.addSingleMessage(
|
2023-10-04 00:12:57 +00:00
|
|
|
window.MessageCache.__DEPRECATED$register(
|
2023-01-11 22:54:06 +00:00
|
|
|
generatedMessage.id,
|
2023-10-04 00:12:57 +00:00
|
|
|
generatedMessage,
|
|
|
|
'generatedMessage2'
|
2023-01-11 22:54:06 +00:00
|
|
|
)
|
2022-11-02 23:48:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
jobData = {
|
|
|
|
type: conversationQueueJobEnum.enum.NormalMessage,
|
|
|
|
conversationId: conversation.id,
|
2023-01-11 22:54:06 +00:00
|
|
|
messageId: generatedMessage.id,
|
2022-11-02 23:48:38 +00:00
|
|
|
revision: conversation.get('revision'),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
jobData = {
|
|
|
|
type: conversationQueueJobEnum.enum.Reaction,
|
|
|
|
conversationId: conversation.id,
|
|
|
|
messageId: this.id,
|
|
|
|
revision: conversation.get('revision'),
|
|
|
|
};
|
|
|
|
}
|
2021-10-29 23:19:44 +00:00
|
|
|
if (shouldPersist) {
|
2022-02-16 18:36:21 +00:00
|
|
|
await conversationJobQueue.add(jobData, async jobToInsert => {
|
2021-10-29 23:19:44 +00:00
|
|
|
log.info(
|
|
|
|
`enqueueReactionForSend: saving message ${this.idForLogging()} and job ${
|
|
|
|
jobToInsert.id
|
|
|
|
}`
|
|
|
|
);
|
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
|
|
|
jobToInsert,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-10-29 23:19:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
2022-02-16 18:36:21 +00:00
|
|
|
await conversationJobQueue.add(jobData);
|
2021-10-29 23:19:44 +00:00
|
|
|
}
|
2022-11-02 23:48:38 +00:00
|
|
|
} else if (shouldPersist && !isStory(this.attributes)) {
|
2021-12-20 21:04:02 +00:00
|
|
|
await window.Signal.Data.saveMessage(this.attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleDeleteForEveryone(
|
2023-08-07 16:36:37 +00:00
|
|
|
del: Pick<
|
|
|
|
DeleteAttributesType,
|
|
|
|
'fromId' | 'targetSentTimestamp' | 'serverTimestamp'
|
|
|
|
>,
|
2020-09-24 20:57:54 +00:00
|
|
|
shouldPersist = true
|
|
|
|
): Promise<void> {
|
2022-11-30 19:21:13 +00:00
|
|
|
if (this.deletingForEveryone || this.get('deletedForEveryone')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Handling DOE.', {
|
2022-11-30 19:21:13 +00:00
|
|
|
messageId: this.id,
|
2023-08-07 16:36:37 +00:00
|
|
|
fromId: del.fromId,
|
|
|
|
targetSentTimestamp: del.targetSentTimestamp,
|
2020-09-24 20:57:54 +00:00
|
|
|
messageServerTimestamp: this.get('serverTimestamp'),
|
2023-08-07 16:36:37 +00:00
|
|
|
deleteServerTimestamp: del.serverTimestamp,
|
2020-09-24 20:57:54 +00:00
|
|
|
});
|
|
|
|
|
2022-11-01 18:58:07 +00:00
|
|
|
try {
|
|
|
|
this.deletingForEveryone = true;
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-11-01 18:58:07 +00:00
|
|
|
// Remove any notifications for this message
|
|
|
|
notificationService.removeBy({ messageId: this.get('id') });
|
2020-09-24 20:57:54 +00:00
|
|
|
|
2022-11-01 18:58:07 +00:00
|
|
|
// Erase the contents of this message
|
|
|
|
await this.eraseContents(
|
|
|
|
{ deletedForEveryone: true, reactions: [] },
|
|
|
|
shouldPersist
|
|
|
|
);
|
|
|
|
|
|
|
|
// Update the conversation's last message in case this was the last message
|
2022-12-21 18:41:48 +00:00
|
|
|
void this.getConversation()?.updateLastMessage();
|
2022-11-01 18:58:07 +00:00
|
|
|
} finally {
|
|
|
|
this.deletingForEveryone = undefined;
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
2021-03-03 19:03:11 +00:00
|
|
|
|
2021-05-07 01:15:25 +00:00
|
|
|
clearNotifications(reaction: Partial<ReactionType> = {}): void {
|
2021-09-23 18:16:09 +00:00
|
|
|
notificationService.removeBy({
|
2021-05-07 01:15:25 +00:00
|
|
|
...reaction,
|
2021-03-03 19:03:11 +00:00
|
|
|
messageId: this.id,
|
|
|
|
});
|
|
|
|
}
|
2023-07-20 00:17:13 +00:00
|
|
|
|
|
|
|
getPendingMarkRead(): number | undefined {
|
|
|
|
return this.pendingMarkRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
setPendingMarkRead(value: number | undefined): void {
|
|
|
|
this.pendingMarkRead = value;
|
|
|
|
}
|
2020-09-24 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 16:38:20 +00:00
|
|
|
window.Whisper.Message = MessageModel;
|