123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- /*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() {
- this.on('change:timestamp change:name change:number', this.sort);
- this.listenTo(conversations, 'add change:active_at', this.addActive);
- this.on('add remove change:unreadCount',
- _.debounce(this.updateUnreadCount.bind(this), 1000)
- );
- },
- 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;
- }
- },
- addActive: function(model) {
- if (model.get('active_at')) {
- this.add(model);
- } else {
- this.remove(model);
- }
- },
- updateUnreadCount: function() {
- var newUnreadCount = _.reduce(
- this.map(function(m) { return m.get('unreadCount'); }),
- function(item, memo) {
- return item + memo;
- },
- 0
- );
- storage.put("unreadCount", newUnreadCount);
- setUnreadCount(newUnreadCount);
- if (newUnreadCount === 0) {
- window.clearAttention();
- }
- }
- }))();
- window.getInboxCollection = function() {
- return inboxCollection;
- };
- window.ConversationController = {
- get: function(id) {
- return conversations.get(id);
- },
- add: function(attrs) {
- return conversations.add(attrs, {merge: true});
- },
- create: function(attrs) {
- if (typeof attrs !== 'object') {
- throw new Error('ConversationController.create requires an object, got', attrs);
- }
- var conversation = conversations.add(attrs, {merge: true});
- return conversation;
- },
- findOrCreatePrivateById: function(id) {
- var conversation = conversations.add({ id: id, type: 'private' });
- return new Promise(function(resolve, reject) {
- conversation.fetch().then(function() {
- resolve(conversation);
- }).fail(function() {
- var saved = conversation.save(); // false or indexedDBRequest
- if (saved) {
- saved.then(function() {
- resolve(conversation);
- }).fail(reject);
- } else {
- reject();
- }
- });
- });
- },
- updateInbox: function() {
- return conversations.fetchActive();
- }
- };
- })();
|