data.js 4.1 KB

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