data.js 3.8 KB

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