2021-06-17 17:15:10 +00:00
|
|
|
// Copyright 2019-2021 Signal Messenger, LLC
|
|
|
|
// 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-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2022-01-04 15:27:16 +00:00
|
|
|
export type ViewOnceOpenSyncAttributesType = {
|
2021-06-17 17:15:10 +00:00
|
|
|
source?: string;
|
|
|
|
sourceUuid: string;
|
|
|
|
timestamp: number;
|
|
|
|
};
|
|
|
|
|
2021-07-22 17:07:53 +00:00
|
|
|
class ViewOnceOpenSyncModel extends Model<ViewOnceOpenSyncAttributesType> {}
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-07-22 17:07:53 +00:00
|
|
|
let singleton: ViewOnceOpenSyncs | undefined;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-07-22 17:07:53 +00:00
|
|
|
export class ViewOnceOpenSyncs extends Collection<ViewOnceOpenSyncModel> {
|
|
|
|
static getSingleton(): ViewOnceOpenSyncs {
|
2021-06-17 17:15:10 +00:00
|
|
|
if (!singleton) {
|
2021-07-22 17:07:53 +00:00
|
|
|
singleton = new ViewOnceOpenSyncs();
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:07:53 +00:00
|
|
|
forMessage(message: MessageModel): ViewOnceOpenSyncModel | null {
|
2021-06-17 17:15:10 +00:00
|
|
|
const syncBySourceUuid = this.find(item => {
|
|
|
|
return (
|
|
|
|
item.get('sourceUuid') === message.get('sourceUuid') &&
|
|
|
|
item.get('timestamp') === message.get('sent_at')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
if (syncBySourceUuid) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Found early view once open sync for message');
|
2021-06-17 17:15:10 +00:00
|
|
|
this.remove(syncBySourceUuid);
|
|
|
|
return syncBySourceUuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
const syncBySource = this.find(item => {
|
|
|
|
return (
|
|
|
|
item.get('source') === message.get('source') &&
|
|
|
|
item.get('timestamp') === message.get('sent_at')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
if (syncBySource) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Found early view once open sync for message');
|
2021-06-17 17:15:10 +00:00
|
|
|
this.remove(syncBySource);
|
|
|
|
return syncBySource;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:07:53 +00:00
|
|
|
async onSync(sync: ViewOnceOpenSyncModel): Promise<void> {
|
2021-06-17 17:15:10 +00:00
|
|
|
try {
|
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
2021-12-10 22:51:54 +00:00
|
|
|
sync.get('timestamp')
|
2021-06-17 17:15:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const found = messages.find(item => {
|
2021-12-10 22:51:54 +00:00
|
|
|
const itemSourceUuid = item.sourceUuid;
|
2021-06-17 17:15:10 +00:00
|
|
|
const syncSourceUuid = sync.get('sourceUuid');
|
2021-12-10 22:51:54 +00:00
|
|
|
const itemSource = item.source;
|
2021-06-17 17:15:10 +00:00
|
|
|
const syncSource = sync.get('source');
|
|
|
|
|
|
|
|
return Boolean(
|
|
|
|
(itemSourceUuid &&
|
|
|
|
syncSourceUuid &&
|
|
|
|
itemSourceUuid === syncSourceUuid) ||
|
|
|
|
(itemSource && syncSource && itemSource === syncSource)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const syncSource = sync.get('source');
|
|
|
|
const syncSourceUuid = sync.get('sourceUuid');
|
|
|
|
const syncTimestamp = sync.get('timestamp');
|
|
|
|
const wasMessageFound = Boolean(found);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Receive view once open sync:', {
|
2021-06-17 17:15:10 +00:00
|
|
|
syncSource,
|
|
|
|
syncSourceUuid,
|
|
|
|
syncTimestamp,
|
|
|
|
wasMessageFound,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = window.MessageController.register(found.id, found);
|
2021-07-22 17:07:53 +00:00
|
|
|
await message.markViewOnceMessageViewed({ fromSync: true });
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
this.remove(sync);
|
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-07-22 17:07:53 +00:00
|
|
|
'ViewOnceOpenSyncs.onSync error:',
|
2021-06-17 17:15:10 +00:00
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|