2015-09-17 06:13:17 +00:00
|
|
|
/*global $, Whisper, Backbone, textsecure, extension*/
|
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This script should only be included in background.html
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
|
|
|
var conversations = new Whisper.ConversationCollection();
|
|
|
|
var inboxCollection = new (Backbone.Collection.extend({
|
|
|
|
initialize: function() {
|
2016-03-25 17:39:36 +00:00
|
|
|
this.on('change:timestamp change:name change:number', this.sort);
|
2015-09-17 06:13:17 +00:00
|
|
|
|
|
|
|
this.listenTo(conversations, 'add change:active_at', this.addActive);
|
2016-04-11 18:24:18 +00:00
|
|
|
|
|
|
|
this.on('add remove change:unreadCount',
|
|
|
|
_.debounce(this.updateUnreadCount.bind(this), 1000)
|
|
|
|
);
|
2015-09-17 06:13:17 +00:00
|
|
|
},
|
2016-03-18 04:42:03 +00:00
|
|
|
comparator: function(m1, m2) {
|
|
|
|
var timestamp1 = m1.get('timestamp');
|
|
|
|
var timestamp2 = m2.get('timestamp');
|
|
|
|
if (timestamp1 && timestamp2) {
|
|
|
|
return timestamp2 - timestamp1;
|
|
|
|
}
|
|
|
|
if (timestamp1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (timestamp2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
var title1 = m1.getTitle().toLowerCase();
|
|
|
|
var title2 = m2.getTitle().toLowerCase();
|
|
|
|
if (title1 === title2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (title1 < title2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (title1 > title2) {
|
|
|
|
return 1;
|
|
|
|
}
|
2015-09-17 06:13:17 +00:00
|
|
|
},
|
|
|
|
addActive: function(model) {
|
|
|
|
if (model.get('active_at')) {
|
|
|
|
this.add(model);
|
|
|
|
} else {
|
|
|
|
this.remove(model);
|
|
|
|
}
|
|
|
|
},
|
2016-04-11 18:24:18 +00:00
|
|
|
updateUnreadCount: function() {
|
2016-02-20 01:19:04 +00:00
|
|
|
var newUnreadCount = _.reduce(
|
|
|
|
this.map(function(m) { return m.get('unreadCount'); }),
|
|
|
|
function(item, memo) {
|
|
|
|
return item + memo;
|
|
|
|
},
|
|
|
|
0
|
|
|
|
);
|
2015-11-10 15:56:27 +00:00
|
|
|
storage.put("unreadCount", newUnreadCount);
|
2015-11-14 11:47:21 +00:00
|
|
|
|
|
|
|
setUnreadCount(newUnreadCount);
|
2016-04-11 22:27:39 +00:00
|
|
|
if (newUnreadCount === 0) {
|
|
|
|
window.clearAttention();
|
|
|
|
}
|
2015-09-17 06:13:17 +00:00
|
|
|
}
|
|
|
|
}))();
|
|
|
|
|
|
|
|
window.getInboxCollection = function() {
|
|
|
|
return inboxCollection;
|
|
|
|
};
|
|
|
|
|
|
|
|
window.ConversationController = {
|
|
|
|
get: function(id) {
|
|
|
|
return conversations.get(id);
|
|
|
|
},
|
2016-09-21 23:26:42 +00:00
|
|
|
add: function(attrs) {
|
|
|
|
return conversations.add(attrs, {merge: true});
|
|
|
|
},
|
2015-09-17 06:13:17 +00:00
|
|
|
create: function(attrs) {
|
2015-09-17 18:41:49 +00:00
|
|
|
if (typeof attrs !== 'object') {
|
|
|
|
throw new Error('ConversationController.create requires an object, got', attrs);
|
|
|
|
}
|
2015-09-17 21:25:49 +00:00
|
|
|
var conversation = conversations.add(attrs, {merge: true});
|
2015-09-17 06:13:17 +00:00
|
|
|
return conversation;
|
|
|
|
},
|
|
|
|
findOrCreatePrivateById: function(id) {
|
2017-07-21 17:59:41 +00:00
|
|
|
var conversation = conversations.add({
|
|
|
|
id: id,
|
|
|
|
type: 'private'
|
|
|
|
});
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
conversation.fetch().then(function() {
|
|
|
|
resolve(conversation);
|
|
|
|
}, function() {
|
|
|
|
conversation.save().then(function() {
|
|
|
|
resolve(conversation);
|
|
|
|
}, reject);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
findOrCreateById: function(id) {
|
|
|
|
var conversation = conversations.add({
|
|
|
|
id: id
|
|
|
|
});
|
2015-09-17 06:13:17 +00:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
conversation.fetch().then(function() {
|
|
|
|
resolve(conversation);
|
2017-07-21 17:59:41 +00:00
|
|
|
}, function() {
|
|
|
|
conversation.save().then(function() {
|
|
|
|
resolve(conversation);
|
|
|
|
}, reject);
|
2015-09-17 06:13:17 +00:00
|
|
|
});
|
|
|
|
});
|
2015-09-17 22:25:45 +00:00
|
|
|
},
|
2017-06-15 19:27:41 +00:00
|
|
|
getAllGroupsInvolvingId: function(id) {
|
|
|
|
return conversations.filter(function(conversation) {
|
|
|
|
return !conversation.isPrivate() && conversation.hasMember(id);
|
|
|
|
});
|
|
|
|
},
|
2015-09-17 22:25:45 +00:00
|
|
|
updateInbox: function() {
|
2016-03-18 03:58:56 +00:00
|
|
|
return conversations.fetchActive();
|
2015-09-17 06:13:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|