conversation_controller.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*global $, Whisper, Backbone, textsecure, extension*/
  2. /*
  3. * vim: ts=4:sw=4:expandtab
  4. */
  5. // This script should only be included in background.html
  6. (function () {
  7. 'use strict';
  8. window.Whisper = window.Whisper || {};
  9. var conversations = new Whisper.ConversationCollection();
  10. var inboxCollection = new (Backbone.Collection.extend({
  11. initialize: function() {
  12. this.on('change:timestamp change:name change:number', this.sort);
  13. this.listenTo(conversations, 'add change:active_at', this.addActive);
  14. this.on('add remove change:unreadCount',
  15. _.debounce(this.updateUnreadCount.bind(this), 1000)
  16. );
  17. },
  18. comparator: function(m1, m2) {
  19. var timestamp1 = m1.get('timestamp');
  20. var timestamp2 = m2.get('timestamp');
  21. if (timestamp1 && timestamp2) {
  22. return timestamp2 - timestamp1;
  23. }
  24. if (timestamp1) {
  25. return -1;
  26. }
  27. if (timestamp2) {
  28. return 1;
  29. }
  30. var title1 = m1.getTitle().toLowerCase();
  31. var title2 = m2.getTitle().toLowerCase();
  32. if (title1 === title2) {
  33. return 0;
  34. }
  35. if (title1 < title2) {
  36. return -1;
  37. }
  38. if (title1 > title2) {
  39. return 1;
  40. }
  41. },
  42. addActive: function(model) {
  43. if (model.get('active_at')) {
  44. this.add(model);
  45. } else {
  46. this.remove(model);
  47. }
  48. },
  49. updateUnreadCount: function() {
  50. var newUnreadCount = _.reduce(
  51. this.map(function(m) { return m.get('unreadCount'); }),
  52. function(item, memo) {
  53. return item + memo;
  54. },
  55. 0
  56. );
  57. storage.put("unreadCount", newUnreadCount);
  58. setUnreadCount(newUnreadCount);
  59. if (newUnreadCount === 0) {
  60. window.clearAttention();
  61. }
  62. }
  63. }))();
  64. window.getInboxCollection = function() {
  65. return inboxCollection;
  66. };
  67. window.ConversationController = {
  68. get: function(id) {
  69. return conversations.get(id);
  70. },
  71. add: function(attrs) {
  72. return conversations.add(attrs, {merge: true});
  73. },
  74. create: function(attrs) {
  75. if (typeof attrs !== 'object') {
  76. throw new Error('ConversationController.create requires an object, got', attrs);
  77. }
  78. var conversation = conversations.add(attrs, {merge: true});
  79. return conversation;
  80. },
  81. findOrCreatePrivateById: function(id) {
  82. var conversation = conversations.add({ id: id, type: 'private' });
  83. return new Promise(function(resolve, reject) {
  84. conversation.fetch().then(function() {
  85. resolve(conversation);
  86. }).fail(function() {
  87. var saved = conversation.save(); // false or indexedDBRequest
  88. if (saved) {
  89. saved.then(function() {
  90. resolve(conversation);
  91. }).fail(reject);
  92. } else {
  93. reject();
  94. }
  95. });
  96. });
  97. },
  98. updateInbox: function() {
  99. return conversations.fetchActive();
  100. }
  101. };
  102. })();