2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2019 Signal Messenger, LLC
|
2021-06-17 17:15:10 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
import type { AciString } from '../types/ServiceId';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MessageModel } from '../models/messages';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataReader } from '../sql/Client';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2023-08-21 20:08:27 +00:00
|
|
|
import * as log from '../logging/log';
|
|
|
|
import { getMessageIdForLogging } from '../util/idForLogging';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2022-01-04 15:27:16 +00:00
|
|
|
export type ViewOnceOpenSyncAttributesType = {
|
2023-08-21 20:08:27 +00:00
|
|
|
removeFromMessageReceiverCache: () => unknown;
|
2021-06-17 17:15:10 +00:00
|
|
|
source?: string;
|
2023-08-10 16:43:33 +00:00
|
|
|
sourceAci: AciString;
|
2021-06-17 17:15:10 +00:00
|
|
|
timestamp: number;
|
|
|
|
};
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const viewOnceSyncs = new Map<number, ViewOnceOpenSyncAttributesType>();
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
function remove(sync: ViewOnceOpenSyncAttributesType): void {
|
|
|
|
viewOnceSyncs.delete(sync.timestamp);
|
|
|
|
sync.removeFromMessageReceiverCache();
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
export function forMessage(
|
|
|
|
message: MessageModel
|
|
|
|
): ViewOnceOpenSyncAttributesType | null {
|
|
|
|
const logId = `ViewOnceOpenSyncs.forMessage(${getMessageIdForLogging(
|
|
|
|
message.attributes
|
|
|
|
)})`;
|
|
|
|
|
|
|
|
const viewOnceSyncValues = Array.from(viewOnceSyncs.values());
|
|
|
|
|
|
|
|
const syncBySourceServiceId = viewOnceSyncValues.find(item => {
|
|
|
|
return (
|
|
|
|
item.sourceAci === message.get('sourceServiceId') &&
|
|
|
|
item.timestamp === message.get('sent_at')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (syncBySourceServiceId) {
|
|
|
|
log.info(`${logId}: Found early view once open sync for message`);
|
|
|
|
remove(syncBySourceServiceId);
|
|
|
|
return syncBySourceServiceId;
|
|
|
|
}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const syncBySource = viewOnceSyncValues.find(item => {
|
|
|
|
return (
|
|
|
|
item.source === message.get('source') &&
|
|
|
|
item.timestamp === message.get('sent_at')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
if (syncBySource) {
|
|
|
|
log.info(`${logId}: Found early view once open sync for message`);
|
|
|
|
remove(syncBySource);
|
|
|
|
return syncBySource;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function onSync(
|
|
|
|
sync: ViewOnceOpenSyncAttributesType
|
|
|
|
): Promise<void> {
|
|
|
|
viewOnceSyncs.set(sync.timestamp, sync);
|
|
|
|
|
|
|
|
const logId = `ViewOnceOpenSyncs.onSync(timestamp=${sync.timestamp})`;
|
|
|
|
|
|
|
|
try {
|
2024-07-22 18:16:33 +00:00
|
|
|
const messages = await DataReader.getMessagesBySentAt(sync.timestamp);
|
2023-08-21 20:08:27 +00:00
|
|
|
|
|
|
|
const found = messages.find(item => {
|
|
|
|
const itemSourceAci = item.sourceServiceId;
|
|
|
|
const syncSourceAci = sync.sourceAci;
|
|
|
|
const itemSource = item.source;
|
|
|
|
const syncSource = sync.source;
|
|
|
|
|
|
|
|
return Boolean(
|
|
|
|
(itemSourceAci && syncSourceAci && itemSourceAci === syncSourceAci) ||
|
|
|
|
(itemSource && syncSource && itemSource === syncSource)
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
const syncSource = sync.source;
|
|
|
|
const syncSourceAci = sync.sourceAci;
|
|
|
|
const syncTimestamp = sync.timestamp;
|
|
|
|
const wasMessageFound = Boolean(found);
|
|
|
|
log.info(`${logId} receive:`, {
|
|
|
|
syncSource,
|
|
|
|
syncSourceAci,
|
|
|
|
syncTimestamp,
|
|
|
|
wasMessageFound,
|
2021-06-17 17:15:10 +00:00
|
|
|
});
|
2023-08-21 20:08:27 +00:00
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
return;
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
const message = window.MessageCache.__DEPRECATED$register(
|
|
|
|
found.id,
|
|
|
|
found,
|
|
|
|
'ViewOnceOpenSyncs.onSync'
|
|
|
|
);
|
2023-08-21 20:08:27 +00:00
|
|
|
await message.markViewOnceMessageViewed({ fromSync: true });
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-08-21 20:08:27 +00:00
|
|
|
viewOnceSyncs.delete(sync.timestamp);
|
|
|
|
sync.removeFromMessageReceiverCache();
|
|
|
|
} catch (error) {
|
|
|
|
log.error(`${logId} error:`, Errors.toLogFormat(error));
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
}
|