2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2017 Signal Messenger, LLC
|
2021-06-17 17:15:10 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
/* eslint-disable max-classes-per-file */
|
|
|
|
|
|
|
|
import { Collection, Model } from 'backbone';
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MessageModel } from '../models/messages';
|
2021-06-17 17:15:10 +00:00
|
|
|
import { isIncoming } from '../state/selectors/message';
|
2021-07-29 14:29:07 +00:00
|
|
|
import { isMessageUnread } from '../util/isMessageUnread';
|
2021-09-23 18:16:09 +00:00
|
|
|
import { notificationService } from '../services/notifications';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { AciString } from '../types/ServiceId';
|
2023-01-13 00:24:59 +00:00
|
|
|
import { StartupQueue } from '../util/StartupQueue';
|
2023-04-11 03:54:43 +00:00
|
|
|
import { queueUpdateMessage } from '../util/messageBatcher';
|
2023-05-16 17:37:12 +00:00
|
|
|
import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2022-01-04 15:27:16 +00:00
|
|
|
export type ReadSyncAttributesType = {
|
2021-06-17 17:15:10 +00:00
|
|
|
senderId: string;
|
2022-01-04 15:27:16 +00:00
|
|
|
sender?: string;
|
2023-08-10 16:43:33 +00:00
|
|
|
senderAci: AciString;
|
2021-06-17 17:15:10 +00:00
|
|
|
timestamp: number;
|
|
|
|
readAt: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReadSyncModel extends Model<ReadSyncAttributesType> {}
|
|
|
|
|
|
|
|
let singleton: ReadSyncs | undefined;
|
|
|
|
|
2021-08-02 16:47:59 +00:00
|
|
|
async function maybeItIsAReactionReadSync(sync: ReadSyncModel): Promise<void> {
|
2021-06-17 17:15:10 +00:00
|
|
|
const readReaction = await window.Signal.Data.markReactionAsRead(
|
2023-08-10 16:43:33 +00:00
|
|
|
sync.get('senderAci'),
|
2021-08-02 16:47:59 +00:00
|
|
|
Number(sync.get('timestamp'))
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!readReaction) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-06-17 17:15:10 +00:00
|
|
|
'Nothing found for read sync',
|
2021-08-02 16:47:59 +00:00
|
|
|
sync.get('senderId'),
|
|
|
|
sync.get('sender'),
|
2023-08-10 16:43:33 +00:00
|
|
|
sync.get('senderAci'),
|
2021-08-02 16:47:59 +00:00
|
|
|
sync.get('timestamp')
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-23 18:16:09 +00:00
|
|
|
notificationService.removeBy({
|
2021-06-17 17:15:10 +00:00
|
|
|
conversationId: readReaction.conversationId,
|
|
|
|
emoji: readReaction.emoji,
|
2023-08-16 20:54:39 +00:00
|
|
|
targetAuthorAci: readReaction.targetAuthorAci,
|
2021-06-17 17:15:10 +00:00
|
|
|
targetTimestamp: readReaction.targetTimestamp,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ReadSyncs extends Collection {
|
|
|
|
static getSingleton(): ReadSyncs {
|
|
|
|
if (!singleton) {
|
|
|
|
singleton = new ReadSyncs();
|
|
|
|
}
|
|
|
|
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
forMessage(message: MessageModel): ReadSyncModel | null {
|
2022-08-09 21:39:00 +00:00
|
|
|
const sender = window.ConversationController.lookupOrCreate({
|
2021-06-17 17:15:10 +00:00
|
|
|
e164: message.get('source'),
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: message.get('sourceServiceId'),
|
2022-12-03 01:05:27 +00:00
|
|
|
reason: 'ReadSyncs.forMessage',
|
2021-06-17 17:15:10 +00:00
|
|
|
});
|
2023-05-16 17:37:12 +00:00
|
|
|
const messageTimestamp = getMessageSentTimestamp(message.attributes, {
|
|
|
|
log,
|
|
|
|
});
|
2021-08-02 16:47:59 +00:00
|
|
|
const sync = this.find(item => {
|
2021-06-17 17:15:10 +00:00
|
|
|
return (
|
2022-08-09 21:39:00 +00:00
|
|
|
item.get('senderId') === sender?.id &&
|
2023-05-16 17:37:12 +00:00
|
|
|
item.get('timestamp') === messageTimestamp
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
});
|
2021-08-02 16:47:59 +00:00
|
|
|
if (sync) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`Found early read sync for message ${sync.get('timestamp')}`);
|
2021-08-02 16:47:59 +00:00
|
|
|
this.remove(sync);
|
|
|
|
return sync;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-08-02 16:47:59 +00:00
|
|
|
async onSync(sync: ReadSyncModel): Promise<void> {
|
2021-06-17 17:15:10 +00:00
|
|
|
try {
|
2023-04-20 16:31:59 +00:00
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
|
|
|
sync.get('timestamp')
|
|
|
|
);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
const found = messages.find(item => {
|
2022-08-09 21:39:00 +00:00
|
|
|
const sender = window.ConversationController.lookupOrCreate({
|
2021-12-10 22:51:54 +00:00
|
|
|
e164: item.source,
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: item.sourceServiceId,
|
2022-12-03 01:05:27 +00:00
|
|
|
reason: 'ReadSyncs.onSync',
|
2021-06-17 17:15:10 +00:00
|
|
|
});
|
|
|
|
|
2022-08-09 21:39:00 +00:00
|
|
|
return isIncoming(item) && sender?.id === sync.get('senderId');
|
2021-06-17 17:15:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!found) {
|
2021-08-02 16:47:59 +00:00
|
|
|
await maybeItIsAReactionReadSync(sync);
|
2021-06-17 17:15:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-23 18:16:09 +00:00
|
|
|
notificationService.removeBy({ messageId: found.id });
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
const message = window.MessageController.register(found.id, found);
|
2021-08-17 00:16:00 +00:00
|
|
|
const readAt = Math.min(sync.get('readAt'), Date.now());
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
// If message is unread, we mark it read. Otherwise, we update the expiration
|
|
|
|
// timer to the time specified by the read sync if it's earlier than
|
|
|
|
// the previous read time.
|
2021-07-29 14:29:07 +00:00
|
|
|
if (isMessageUnread(message.attributes)) {
|
2021-06-17 17:15:10 +00:00
|
|
|
// TODO DESKTOP-1509: use MessageUpdater.markRead once this is TS
|
|
|
|
message.markRead(readAt, { skipSave: true });
|
|
|
|
|
2023-02-24 19:03:17 +00:00
|
|
|
const updateConversation = async () => {
|
2021-06-17 17:15:10 +00:00
|
|
|
// onReadMessage may result in messages older than this one being
|
|
|
|
// marked read. We want those messages to have the same expire timer
|
|
|
|
// start time as this one, so we pass the readAt value through.
|
2022-12-21 18:41:48 +00:00
|
|
|
void message.getConversation()?.onReadMessage(message, readAt);
|
2021-06-17 17:15:10 +00:00
|
|
|
};
|
|
|
|
|
2023-02-24 19:03:17 +00:00
|
|
|
// only available during initialization
|
|
|
|
if (StartupQueue.isAvailable()) {
|
2021-06-17 17:15:10 +00:00
|
|
|
const conversation = message.getConversation();
|
|
|
|
if (conversation) {
|
2023-01-13 00:24:59 +00:00
|
|
|
StartupQueue.add(
|
2021-06-17 17:15:10 +00:00
|
|
|
conversation.get('id'),
|
2021-08-17 00:16:00 +00:00
|
|
|
message.get('sent_at'),
|
2021-06-17 17:15:10 +00:00
|
|
|
updateConversation
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-24 19:03:17 +00:00
|
|
|
// not awaiting since we don't want to block work happening in the
|
|
|
|
// eventHandlerQueue
|
|
|
|
void updateConversation();
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const now = Date.now();
|
|
|
|
const existingTimestamp = message.get('expirationStartTimestamp');
|
|
|
|
const expirationStartTimestamp = Math.min(
|
|
|
|
now,
|
|
|
|
Math.min(existingTimestamp || now, readAt || now)
|
|
|
|
);
|
|
|
|
message.set({ expirationStartTimestamp });
|
|
|
|
}
|
|
|
|
|
2023-04-11 03:54:43 +00:00
|
|
|
queueUpdateMessage(message.attributes);
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-08-02 16:47:59 +00:00
|
|
|
this.remove(sync);
|
2021-06-17 17:15:10 +00:00
|
|
|
} catch (error) {
|
2022-11-22 18:43:43 +00:00
|
|
|
log.error('ReadSyncs.onSync error:', Errors.toLogFormat(error));
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|