Refactor read state tracking
Adds support for handling early arriving read receipts. // FREEBIE
This commit is contained in:
parent
d1e9534542
commit
0cd7f84a05
4 changed files with 55 additions and 25 deletions
|
@ -395,6 +395,7 @@
|
|||
|
||||
<script type='text/javascript' src='js/notifications.js'></script>
|
||||
<script type='text/javascript' src='js/delivery_receipts.js'></script>
|
||||
<script type='text/javascript' src='js/read_receipts.js'></script>
|
||||
<script type='text/javascript' src='js/libphonenumber-util.js'></script>
|
||||
<script type='text/javascript' src='js/models/messages.js'></script>
|
||||
<script type='text/javascript' src='js/models/conversations.js'></script>
|
||||
|
|
|
@ -245,31 +245,8 @@
|
|||
function onReadReceipt(ev) {
|
||||
var timestamp = ev.timestamp.toNumber();
|
||||
var sender = ev.sender;
|
||||
var messages = new Whisper.MessageCollection();
|
||||
var groups = new Whisper.ConversationCollection();
|
||||
console.log('read receipt ', sender, timestamp);
|
||||
groups.fetchGroups(sender).then(function() {
|
||||
messages.fetchSentAt(timestamp).then(function() {
|
||||
var ids = groups.pluck('id');
|
||||
ids.push(sender);
|
||||
var message = messages.find(function(message) {
|
||||
return (message.isIncoming() && message.isUnread() &&
|
||||
_.contains(ids, message.get('conversationId')));
|
||||
});
|
||||
if (message) {
|
||||
message.markRead().then(function() {
|
||||
var conversation = ConversationController.get({
|
||||
id: message.get('conversationId')
|
||||
});
|
||||
|
||||
if (conversation) {
|
||||
// notify frontend listeners
|
||||
conversation.trigger('read', message);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
Whisper.ReadReceipts.add({sender: sender, timestamp: timestamp});
|
||||
}
|
||||
|
||||
// lazy hack
|
||||
|
|
|
@ -326,7 +326,12 @@
|
|||
}
|
||||
attributes.active_at = now;
|
||||
if (type === 'incoming') {
|
||||
attributes.unreadCount = conversation.get('unreadCount') + 1;
|
||||
// experimental
|
||||
if (Whisper.ReadReceipts.forMessage(message)) {
|
||||
message.unset('unread');
|
||||
} else {
|
||||
attributes.unreadCount = conversation.get('unreadCount') + 1;
|
||||
}
|
||||
}
|
||||
conversation.set(attributes);
|
||||
|
||||
|
|
47
js/read_receipts.js
Normal file
47
js/read_receipts.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* 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) {
|
||||
this.remove(receipt);
|
||||
message.markRead().then(function() {
|
||||
var conversation = ConversationController.get({
|
||||
id: message.get('conversationId')
|
||||
});
|
||||
|
||||
if (conversation) {
|
||||
// notify frontend listeners
|
||||
conversation.trigger('read', message);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('No message for read receipt');
|
||||
}
|
||||
}.bind(this));
|
||||
}
|
||||
}))();
|
||||
})();
|
Loading…
Reference in a new issue