data.js 4.1 KB

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