expiring_messages.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * vim: ts=4:sw=4:expandtab
  3. */
  4. ;(function() {
  5. 'use strict';
  6. window.Whisper = window.Whisper || {};
  7. function destroyExpiredMessages() {
  8. // Load messages that have expired and destroy them
  9. var expired = new Whisper.MessageCollection();
  10. expired.on('add', function(message) {
  11. console.log('message', message.get('sent_at'), 'expired');
  12. message.destroy();
  13. message.getConversation().trigger('expired', message);
  14. });
  15. expired.on('reset', checkExpiringMessages);
  16. expired.fetchExpired();
  17. }
  18. var timeout;
  19. function checkExpiringMessages() {
  20. // Look up the next expiring message and set a timer to destroy it
  21. var expiring = new Whisper.MessageCollection();
  22. expiring.once('add', function(next) {
  23. var expires_at = next.get('expires_at');
  24. console.log('next message expires', new Date(expires_at));
  25. var wait = expires_at - Date.now();
  26. if (wait < 0) { wait = 0; }
  27. clearTimeout(timeout);
  28. timeout = setTimeout(destroyExpiredMessages, wait);
  29. });
  30. expiring.fetchNextExpiring();
  31. }
  32. window.ExpiringMessagesListener = {
  33. init: function() {
  34. checkExpiringMessages();
  35. window.events.on('timetravel', checkExpiringMessages);
  36. },
  37. update: checkExpiringMessages
  38. };
  39. var TimerOption = Backbone.Model.extend({
  40. getName: function() {
  41. return i18n([
  42. 'timerOption', this.get('time'), this.get('unit'),
  43. ].join('_')) || moment.duration(this.get('time'), this.get('unit')).humanize();
  44. },
  45. getAbbreviated: function() {
  46. return i18n([
  47. 'timerOption', this.get('time'), this.get('unit'), 'abbreviated'
  48. ].join('_'));
  49. }
  50. });
  51. Whisper.ExpirationTimerOptions = new (Backbone.Collection.extend({
  52. model: TimerOption,
  53. getName: function(seconds) {
  54. if (!seconds) {
  55. seconds = 0;
  56. }
  57. var o = this.findWhere({seconds: seconds});
  58. if (o) { return o.getName(); }
  59. else {
  60. return [seconds, 'seconds'].join(' ');
  61. }
  62. },
  63. getAbbreviated: function(seconds) {
  64. if (!seconds) {
  65. seconds = 0;
  66. }
  67. var o = this.findWhere({seconds: seconds});
  68. if (o) { return o.getAbbreviated(); }
  69. else {
  70. return [seconds, 's'].join('');
  71. }
  72. }
  73. }))([
  74. [ 0, 'seconds' ],
  75. [ 5, 'seconds' ],
  76. [ 10, 'seconds' ],
  77. [ 30, 'seconds' ],
  78. [ 1, 'minute' ],
  79. [ 5, 'minutes' ],
  80. [ 30, 'minutes' ],
  81. [ 1, 'hour' ],
  82. [ 6, 'hours' ],
  83. [ 12, 'hours' ],
  84. [ 1, 'day' ],
  85. [ 1, 'week' ],
  86. ].map(function(o) {
  87. var duration = moment.duration(o[0], o[1]); // 5, 'seconds'
  88. return {
  89. time: o[0],
  90. unit: o[1],
  91. seconds: duration.asSeconds()
  92. };
  93. }));
  94. })();