Do not confirm messages until we have handled them
This commit is contained in:
parent
29aa188c0f
commit
04f716986c
16 changed files with 990 additions and 960 deletions
|
@ -1,136 +1,142 @@
|
|||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
/* eslint-disable max-classes-per-file */
|
||||
|
||||
import { Collection, Model } from 'backbone';
|
||||
|
||||
import type { MessageModel } from '../models/messages';
|
||||
import { ReadStatus } from '../messages/MessageReadStatus';
|
||||
import { markViewed } from '../services/MessageUpdater';
|
||||
import { isDownloaded } from '../types/Attachment';
|
||||
import type { AciString } from '../types/ServiceId';
|
||||
import type { MessageModel } from '../models/messages';
|
||||
import * as Errors from '../types/errors';
|
||||
import { isIncoming } from '../state/selectors/message';
|
||||
import { notificationService } from '../services/notifications';
|
||||
import { queueAttachmentDownloads } from '../util/queueAttachmentDownloads';
|
||||
import * as log from '../logging/log';
|
||||
import { GiftBadgeStates } from '../components/conversation/Message';
|
||||
import { queueUpdateMessage } from '../util/messageBatcher';
|
||||
import { ReadStatus } from '../messages/MessageReadStatus';
|
||||
import { getMessageIdForLogging } from '../util/idForLogging';
|
||||
import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp';
|
||||
import { isDownloaded } from '../types/Attachment';
|
||||
import { isIncoming } from '../state/selectors/message';
|
||||
import { markViewed } from '../services/MessageUpdater';
|
||||
import { notificationService } from '../services/notifications';
|
||||
import { queueAttachmentDownloads } from '../util/queueAttachmentDownloads';
|
||||
import { queueUpdateMessage } from '../util/messageBatcher';
|
||||
|
||||
export type ViewSyncAttributesType = {
|
||||
senderId: string;
|
||||
senderE164?: string;
|
||||
envelopeId: string;
|
||||
removeFromMessageReceiverCache: () => unknown;
|
||||
senderAci: AciString;
|
||||
senderE164?: string;
|
||||
senderId: string;
|
||||
timestamp: number;
|
||||
viewedAt: number;
|
||||
};
|
||||
|
||||
class ViewSyncModel extends Model<ViewSyncAttributesType> {}
|
||||
const viewSyncs = new Map<string, ViewSyncAttributesType>();
|
||||
|
||||
let singleton: ViewSyncs | undefined;
|
||||
function remove(sync: ViewSyncAttributesType): void {
|
||||
viewSyncs.delete(sync.envelopeId);
|
||||
sync.removeFromMessageReceiverCache();
|
||||
}
|
||||
|
||||
export class ViewSyncs extends Collection {
|
||||
static getSingleton(): ViewSyncs {
|
||||
if (!singleton) {
|
||||
singleton = new ViewSyncs();
|
||||
}
|
||||
export function forMessage(
|
||||
message: MessageModel
|
||||
): Array<ViewSyncAttributesType> {
|
||||
const logId = `ViewSyncs.forMessage(${getMessageIdForLogging(
|
||||
message.attributes
|
||||
)})`;
|
||||
|
||||
return singleton;
|
||||
const sender = window.ConversationController.lookupOrCreate({
|
||||
e164: message.get('source'),
|
||||
serviceId: message.get('sourceServiceId'),
|
||||
reason: logId,
|
||||
});
|
||||
const messageTimestamp = getMessageSentTimestamp(message.attributes, {
|
||||
log,
|
||||
});
|
||||
|
||||
const viewSyncValues = Array.from(viewSyncs.values());
|
||||
|
||||
const matchingSyncs = viewSyncValues.filter(item => {
|
||||
return item.senderId === sender?.id && item.timestamp === messageTimestamp;
|
||||
});
|
||||
|
||||
if (matchingSyncs.length > 0) {
|
||||
log.info(
|
||||
`${logId}: Found ${matchingSyncs.length} early view sync(s) for message ${messageTimestamp}`
|
||||
);
|
||||
}
|
||||
matchingSyncs.forEach(sync => {
|
||||
remove(sync);
|
||||
});
|
||||
|
||||
forMessage(message: MessageModel): Array<ViewSyncModel> {
|
||||
const sender = window.ConversationController.lookupOrCreate({
|
||||
e164: message.get('source'),
|
||||
serviceId: message.get('sourceServiceId'),
|
||||
reason: 'ViewSyncs.forMessage',
|
||||
});
|
||||
const messageTimestamp = getMessageSentTimestamp(message.attributes, {
|
||||
log,
|
||||
});
|
||||
const syncs = this.filter(item => {
|
||||
return (
|
||||
item.get('senderId') === sender?.id &&
|
||||
item.get('timestamp') === messageTimestamp
|
||||
);
|
||||
});
|
||||
if (syncs.length) {
|
||||
log.info(
|
||||
`Found ${syncs.length} early view sync(s) for message ${messageTimestamp}`
|
||||
);
|
||||
this.remove(syncs);
|
||||
}
|
||||
return syncs;
|
||||
}
|
||||
return matchingSyncs;
|
||||
}
|
||||
|
||||
async onSync(sync: ViewSyncModel): Promise<void> {
|
||||
try {
|
||||
const messages = await window.Signal.Data.getMessagesBySentAt(
|
||||
sync.get('timestamp')
|
||||
);
|
||||
export async function onSync(sync: ViewSyncAttributesType): Promise<void> {
|
||||
viewSyncs.set(sync.envelopeId, sync);
|
||||
|
||||
const found = messages.find(item => {
|
||||
const sender = window.ConversationController.lookupOrCreate({
|
||||
e164: item.source,
|
||||
serviceId: item.sourceServiceId,
|
||||
reason: 'ViewSyncs.onSync',
|
||||
});
|
||||
const logId = `ViewSyncs.onSync(timestamp=${sync.timestamp})`;
|
||||
|
||||
return sender?.id === sync.get('senderId');
|
||||
try {
|
||||
const messages = await window.Signal.Data.getMessagesBySentAt(
|
||||
sync.timestamp
|
||||
);
|
||||
|
||||
const found = messages.find(item => {
|
||||
const sender = window.ConversationController.lookupOrCreate({
|
||||
e164: item.source,
|
||||
serviceId: item.sourceServiceId,
|
||||
reason: logId,
|
||||
});
|
||||
|
||||
if (!found) {
|
||||
log.info(
|
||||
'Nothing found for view sync',
|
||||
sync.get('senderId'),
|
||||
sync.get('senderE164'),
|
||||
sync.get('senderAci'),
|
||||
sync.get('timestamp')
|
||||
return sender?.id === sync.senderId;
|
||||
});
|
||||
|
||||
if (!found) {
|
||||
log.info(
|
||||
`${logId}: nothing found`,
|
||||
sync.senderId,
|
||||
sync.senderE164,
|
||||
sync.senderAci
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
notificationService.removeBy({ messageId: found.id });
|
||||
|
||||
const message = window.MessageController.register(found.id, found);
|
||||
let didChangeMessage = false;
|
||||
|
||||
if (message.get('readStatus') !== ReadStatus.Viewed) {
|
||||
didChangeMessage = true;
|
||||
message.set(markViewed(message.attributes, sync.viewedAt));
|
||||
|
||||
const attachments = message.get('attachments');
|
||||
if (!attachments?.every(isDownloaded)) {
|
||||
const updatedFields = await queueAttachmentDownloads(
|
||||
message.attributes
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
notificationService.removeBy({ messageId: found.id });
|
||||
|
||||
const message = window.MessageController.register(found.id, found);
|
||||
let didChangeMessage = false;
|
||||
|
||||
if (message.get('readStatus') !== ReadStatus.Viewed) {
|
||||
didChangeMessage = true;
|
||||
message.set(markViewed(message.attributes, sync.get('viewedAt')));
|
||||
|
||||
const attachments = message.get('attachments');
|
||||
if (!attachments?.every(isDownloaded)) {
|
||||
const updatedFields = await queueAttachmentDownloads(
|
||||
message.attributes
|
||||
);
|
||||
if (updatedFields) {
|
||||
message.set(updatedFields);
|
||||
}
|
||||
if (updatedFields) {
|
||||
message.set(updatedFields);
|
||||
}
|
||||
}
|
||||
|
||||
const giftBadge = message.get('giftBadge');
|
||||
if (giftBadge) {
|
||||
didChangeMessage = true;
|
||||
message.set({
|
||||
giftBadge: {
|
||||
...giftBadge,
|
||||
state: isIncoming(message.attributes)
|
||||
? GiftBadgeStates.Redeemed
|
||||
: GiftBadgeStates.Opened,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (didChangeMessage) {
|
||||
queueUpdateMessage(message.attributes);
|
||||
}
|
||||
|
||||
this.remove(sync);
|
||||
} catch (error) {
|
||||
log.error('ViewSyncs.onSync error:', Errors.toLogFormat(error));
|
||||
}
|
||||
|
||||
const giftBadge = message.get('giftBadge');
|
||||
if (giftBadge) {
|
||||
didChangeMessage = true;
|
||||
message.set({
|
||||
giftBadge: {
|
||||
...giftBadge,
|
||||
state: isIncoming(message.attributes)
|
||||
? GiftBadgeStates.Redeemed
|
||||
: GiftBadgeStates.Opened,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (didChangeMessage) {
|
||||
queueUpdateMessage(message.attributes);
|
||||
}
|
||||
|
||||
remove(sync);
|
||||
} catch (error) {
|
||||
remove(sync);
|
||||
log.error(`${logId} error:`, Errors.toLogFormat(error));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue