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)
|
|
|
|
);
|
2017-07-26 21:55:59 +00:00
|
|
|
this.startPruning();
|
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();
|
|
|
|
}
|
2017-07-26 21:55:59 +00:00
|
|
|
},
|
|
|
|
startPruning: function() {
|
|
|
|
var halfHour = 30 * 60 * 1000;
|
|
|
|
this.interval = setInterval(function() {
|
|
|
|
this.forEach(function(conversation) {
|
|
|
|
conversation.trigger('prune');
|
|
|
|
});
|
|
|
|
}.bind(this), halfHour);
|
2015-09-17 06:13:17 +00:00
|
|
|
}
|
|
|
|
}))();
|
|
|
|
|
|
|
|
window.getInboxCollection = function() {
|
|
|
|
return inboxCollection;
|
|
|
|
};
|
|
|
|
|
|
|
|
window.ConversationController = {
|
|
|
|
get: function(id) {
|
|
|
|
return conversations.get(id);
|
|
|
|
},
|
2017-09-01 16:10:41 +00:00
|
|
|
createTemporary: function(attributes) {
|
|
|
|
return conversations.add(attributes);
|
2016-09-21 23:26:42 +00:00
|
|
|
},
|
2017-09-01 16:10:41 +00:00
|
|
|
getOrCreate: function(id, type) {
|
|
|
|
var conversation = conversations.get(id);
|
|
|
|
if (conversation) {
|
|
|
|
return conversation;
|
2015-09-17 18:41:49 +00:00
|
|
|
}
|
2017-09-01 16:10:41 +00:00
|
|
|
|
|
|
|
conversation = conversations.add({
|
2017-07-21 17:59:41 +00:00
|
|
|
id: id,
|
2017-07-25 17:33:28 +00:00
|
|
|
type: type
|
2017-07-21 17:59:41 +00:00
|
|
|
});
|
2017-09-01 16:10:41 +00:00
|
|
|
conversation.initialPromise = new Promise(function(resolve, reject) {
|
|
|
|
var deferred = conversation.save();
|
|
|
|
|
|
|
|
if (!deferred) {
|
|
|
|
console.log('Conversation save failed! ', id, type);
|
|
|
|
return reject(new Error('getOrCreate: Conversation save failed'));
|
|
|
|
}
|
|
|
|
|
|
|
|
deferred.then(function() {
|
2015-09-17 06:13:17 +00:00
|
|
|
resolve(conversation);
|
2017-09-01 16:10:41 +00:00
|
|
|
}, reject);
|
|
|
|
});
|
2017-08-30 16:35:04 +00:00
|
|
|
|
2017-09-01 16:10:41 +00:00
|
|
|
return conversation;
|
|
|
|
},
|
|
|
|
getOrCreateAndWait: function(id, type) {
|
|
|
|
var conversation = this.getOrCreate(id, type);
|
2017-08-30 16:35:04 +00:00
|
|
|
|
2017-09-01 16:10:41 +00:00
|
|
|
if (conversation) {
|
|
|
|
return conversation.initialPromise.then(function() {
|
|
|
|
return conversation;
|
2015-09-17 06:13:17 +00:00
|
|
|
});
|
2017-09-01 16:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(
|
|
|
|
new Error('getOrCreateAndWait: did not get conversation')
|
|
|
|
);
|
2015-09-17 22:25:45 +00:00
|
|
|
},
|
2017-06-15 19:27:41 +00:00
|
|
|
getAllGroupsInvolvingId: function(id) {
|
2017-07-21 18:00:25 +00:00
|
|
|
var groups = new Whisper.GroupCollection();
|
|
|
|
return groups.fetchGroups(id).then(function() {
|
|
|
|
return groups.map(function(group) {
|
|
|
|
return conversations.add(group);
|
|
|
|
});
|
2017-06-15 19:27:41 +00:00
|
|
|
});
|
|
|
|
},
|
2015-09-17 22:25:45 +00:00
|
|
|
updateInbox: function() {
|
2017-09-01 16:10:41 +00:00
|
|
|
return conversations.fetch();
|
2015-09-17 06:13:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|