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({
|
|
|
|
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();
|
2017-08-04 01:10:45 +00:00
|
|
|
return messages.fetchSentAt(receipt.get('timestamp')).then(function() {
|
2016-04-11 22:11:20 +00:00
|
|
|
var message = messages.find(function(message) {
|
|
|
|
return (message.isIncoming() && message.isUnread() &&
|
|
|
|
message.get('source') === receipt.get('sender'));
|
|
|
|
});
|
|
|
|
if (message) {
|
2017-08-04 01:10:45 +00:00
|
|
|
return 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
|
|
|
}))();
|
|
|
|
})();
|