signal-desktop/js/view_syncs.js

87 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-06-26 19:33:13 +00:00
/* global
Backbone,
Whisper,
MessageController
*/
/* eslint-disable more/no-then */
// eslint-disable-next-line func-names
(function() {
window.Whisper = window.Whisper || {};
Whisper.ViewSyncs = new (Backbone.Collection.extend({
forMessage(message) {
2020-04-24 19:32:36 +00:00
const syncBySourceUuid = this.findWhere({
sourceUuid: message.get('sourceUuid'),
2019-06-26 19:33:13 +00:00
timestamp: message.get('sent_at'),
});
2020-04-24 19:32:36 +00:00
if (syncBySourceUuid) {
2019-06-26 19:33:13 +00:00
window.log.info('Found early view sync for message');
2020-04-24 19:32:36 +00:00
this.remove(syncBySourceUuid);
return syncBySourceUuid;
}
const syncBySource = this.findWhere({
source: message.get('source'),
timestamp: message.get('sent_at'),
});
if (syncBySource) {
window.log.info('Found early view sync for message');
this.remove(syncBySource);
return syncBySource;
2019-06-26 19:33:13 +00:00
}
return null;
},
async onSync(sync) {
try {
const messages = await window.Signal.Data.getMessagesBySentAt(
sync.get('timestamp'),
{
MessageCollection: Whisper.MessageCollection,
}
);
2020-04-24 19:32:36 +00:00
const found = messages.find(item => {
const itemSourceUuid = item.get('sourceUuid');
const syncSourceUuid = sync.get('sourceUuid');
const itemSource = item.get('source');
const syncSource = sync.get('source');
return (
(itemSourceUuid &&
syncSourceUuid &&
itemSourceUuid === syncSourceUuid) ||
(itemSource && syncSource && itemSource === syncSource)
);
});
2019-06-26 19:33:13 +00:00
const syncSource = sync.get('source');
const syncSourceUuid = sync.get('sourceUuid');
2019-06-26 19:33:13 +00:00
const syncTimestamp = sync.get('timestamp');
const wasMessageFound = Boolean(found);
window.log.info('Receive view sync:', {
syncSource,
syncSourceUuid,
2019-06-26 19:33:13 +00:00
syncTimestamp,
wasMessageFound,
});
if (!found) {
return;
}
const message = MessageController.register(found.id, found);
2019-08-05 20:53:15 +00:00
await message.markViewed({ fromSync: true });
2019-06-26 19:33:13 +00:00
this.remove(sync);
} catch (error) {
window.log.error(
'ViewSyncs.onSync error:',
error && error.stack ? error.stack : error
);
}
},
}))();
})();