1
0

data.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. var
  2. /**
  3. * @type {SlackWrapper}
  4. **/
  5. SLACK
  6. /**
  7. * @type {Object.<string, {hl: boolean, unread: boolean}>} number of unread per chan
  8. **/
  9. ,UNREAD_CHANS = {};
  10. ;
  11. /**
  12. * @constructor
  13. **/
  14. function SlackWrapper() {
  15. /** @type {number} */
  16. this.lastServerVersion = 0;
  17. /** @type {SlackData} */
  18. this.context = new SlackData(null);
  19. /** @type {!Object.<string, SlackHistory>} **/
  20. this.history = {};
  21. }
  22. SlackWrapper.prototype.update = function(data) {
  23. var now = Date.now();
  24. if (data["v"])
  25. this.lastServerVersion = data["v"];
  26. if (data["static"]) {
  27. this.context.updateStatic(data["static"], Date.now());
  28. UNREAD_CHANS = {};
  29. this.context.forEachChans(function(i) {
  30. if (i.lastMsg > i.lastRead) {
  31. UNREAD_CHANS[i.id] = {
  32. hl: false
  33. ,unread: true
  34. }
  35. }
  36. return true;
  37. });
  38. }
  39. if (data["live"]) {
  40. for (var i in data["live"]) {
  41. var history = this.history[i];
  42. if (!history)
  43. history = this.history[i] = new SlackHistory(i, 250, data["live"][i], now);
  44. else
  45. history.pushAll(data["live"][i], now);
  46. }
  47. for (var roomId in data["live"]) {
  48. var chan = this.context.getChannel(roomId);
  49. if (chan) {
  50. if (this.history[roomId].messages.length)
  51. chan.lastMsg = Math.max(chan.lastMsg, this.history[roomId].lastMessage().ts);
  52. if (!chan.archived) {
  53. onMsgReceived(chan, data["live"][roomId]);
  54. if (SELECTED_ROOM && data["live"][SELECTED_ROOM.id])
  55. onRoomUpdated();
  56. }
  57. } else {
  58. outOfSync();
  59. }
  60. }
  61. }
  62. if (data["static"]) {
  63. onContextUpdated();
  64. if (data["static"]["typing"])
  65. onTypingUpdated();
  66. }
  67. };
  68. setInterval(function() {
  69. if (SLACK.context.cleanTyping(Date.now()))
  70. onTypingUpdated();
  71. }, 1000);
  72. /**
  73. * @param {string} text
  74. * @return {boolean}
  75. **/
  76. function isHighlighted(text) {
  77. var highlights = SLACK.context.self.prefs.highlights;
  78. for (var i =0, nbHighlights = highlights.length; i < nbHighlights; i++)
  79. if (text.indexOf(highlights[i]) !== -1) {
  80. return true;
  81. }
  82. return false;
  83. }
  84. /**
  85. * @param {SlackChan|SlackGroup|SlackIms} chan
  86. * @param {Array.<*>} msg
  87. **/
  88. function onMsgReceived(chan, msg) {
  89. if (chan !== SELECTED_ROOM || !window.hasFocus) {
  90. var selfReg = new RegExp("<@" +SLACK.context.self.id)
  91. ,highligted = false
  92. ,newHighlited = false;
  93. msg.forEach(function(i) {
  94. if (parseFloat(i["ts"]) <= chan.lastRead) {
  95. return;
  96. }
  97. if (!UNREAD_CHANS[chan.id]) {
  98. UNREAD_CHANS[chan.id] = {
  99. hl: false
  100. ,unread: true
  101. };
  102. }
  103. if (chan instanceof SlackGroup || chan instanceof SlackIms || i.text.match(selfReg) || isHighlighted(i.text)) {
  104. newHighlited |= !UNREAD_CHANS[chan.id].hl;
  105. UNREAD_CHANS[chan.id].hl = true;
  106. highligted = true;
  107. }
  108. });
  109. updateTitle();
  110. var dom = document.getElementById(chan.id);
  111. if (dom) {
  112. dom.classList.add(R.klass.unread);
  113. if (highligted)
  114. dom.classList.add(R.klass.unreadHi);
  115. }
  116. if (newHighlited && !window.hasFocus) {
  117. // TODO setting
  118. spawnNotification();
  119. }
  120. }
  121. }
  122. /**
  123. * @param {SlackChan|SlackGroup|SlackIms} room
  124. **/
  125. function markRoomAsRead(room) {
  126. if (UNREAD_CHANS[room.id]) {
  127. delete UNREAD_CHANS[room.id];
  128. updateTitle();
  129. }
  130. var roomLi = document.getElementById(room.id);
  131. roomLi.classList.remove(R.klass.unread);
  132. roomLi.classList.remove(R.klass.unreadHi);
  133. }
  134. SLACK = new SlackWrapper();