signal-desktop/js/view_syncs.js

66 lines
1.5 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() {
'use strict';
window.Whisper = window.Whisper || {};
Whisper.ViewSyncs = new (Backbone.Collection.extend({
forMessage(message) {
const sync = this.findWhere({
source: message.get('source'),
timestamp: message.get('sent_at'),
});
if (sync) {
window.log.info('Found early view sync for message');
this.remove(sync);
return sync;
}
return null;
},
async onSync(sync) {
try {
const messages = await window.Signal.Data.getMessagesBySentAt(
sync.get('timestamp'),
{
MessageCollection: Whisper.MessageCollection,
}
);
const found = messages.find(
item => item.get('source') === sync.get('source')
);
const syncSource = sync.get('source');
const syncTimestamp = sync.get('timestamp');
const wasMessageFound = Boolean(found);
window.log.info('Receive view sync:', {
syncSource,
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
);
}
},
}))();
})();