2016-04-11 22:11:20 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
|
|
|
*/
|
|
|
|
;(function() {
|
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
Whisper.ReadReceipts = new (Backbone.Collection.extend({
|
|
|
|
initialize: function() {
|
|
|
|
this.on('add', this.onReceipt);
|
|
|
|
},
|
|
|
|
forMessage: function(message) {
|
|
|
|
var receipt = this.findWhere({
|
|
|
|
sender: message.get('source'),
|
|
|
|
timestamp: message.get('sent_at')
|
|
|
|
});
|
|
|
|
if (receipt) {
|
|
|
|
console.log('Found early read receipt for message');
|
|
|
|
this.remove(receipt);
|
|
|
|
return receipt;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onReceipt: function(receipt) {
|
|
|
|
var messages = new Whisper.MessageCollection();
|
|
|
|
messages.fetchSentAt(receipt.get('timestamp')).then(function() {
|
|
|
|
var message = messages.find(function(message) {
|
|
|
|
return (message.isIncoming() && message.isUnread() &&
|
|
|
|
message.get('source') === receipt.get('sender'));
|
|
|
|
});
|
|
|
|
if (message) {
|
2016-09-21 22:06:31 +00:00
|
|
|
message.markRead(receipt.get('read_at')).then(function() {
|
2017-06-07 21:34:05 +00:00
|
|
|
this.notifyConversation(message);
|
2017-07-17 22:46:00 +00:00
|
|
|
this.remove(receipt);
|
2017-06-07 21:34:05 +00:00
|
|
|
}.bind(this));
|
2016-04-11 22:11:20 +00:00
|
|
|
} else {
|
2017-07-24 19:27:48 +00:00
|
|
|
console.log(
|
|
|
|
'No message for read receipt',
|
|
|
|
receipt.get('sender'), receipt.get('timestamp')
|
|
|
|
);
|
2016-04-11 22:11:20 +00:00
|
|
|
}
|
|
|
|
}.bind(this));
|
2017-06-07 21:34:05 +00:00
|
|
|
},
|
|
|
|
notifyConversation: function(message) {
|
|
|
|
var conversation = ConversationController.get({
|
|
|
|
id: message.get('conversationId')
|
|
|
|
});
|
|
|
|
|
|
|
|
if (conversation) {
|
|
|
|
conversation.onReadMessage(message);
|
|
|
|
}
|
|
|
|
},
|
2016-04-11 22:11:20 +00:00
|
|
|
}))();
|
|
|
|
})();
|