1
0

data.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, RoomHistory>} **/
  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 RoomHistory(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.<Message!>} 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. ,typingUpdated = false;
  93. msg.forEach(function(i) {
  94. if (parseFloat(i["ts"]) <= chan.lastRead) {
  95. return;
  96. }
  97. areNew = true;
  98. if (chan instanceof PrivateMessageRoom || i.text.match(selfReg) || isHighlighted(i.text)) {
  99. if (HIGHLIGHTED_CHANS.indexOf(chan) === -1) {
  100. newHighlited = true;
  101. HIGHLIGHTED_CHANS.push(chan);
  102. }
  103. highligted = true;
  104. }
  105. if (SLACK.context.typing[chan.id] && SLACK.context.typing[chan.id][i.userId]) {
  106. SLACK.context.typing[chan.id][i.userId] = 0;
  107. typingUpdated = true;
  108. }
  109. });
  110. if (areNew) {
  111. updateTitle();
  112. var dom = document.getElementById("room_" +chan.id);
  113. if (dom) {
  114. dom.classList.add(R.klass.unread);
  115. if (highligted)
  116. dom.classList.add(R.klass.unreadHi);
  117. }
  118. if (newHighlited && !window.hasFocus) {
  119. // TODO setting
  120. spawnNotification();
  121. }
  122. }
  123. }
  124. if (typingUpdated)
  125. onTypingUpdated();
  126. }
  127. /**
  128. * @param {Room} room
  129. **/
  130. function markRoomAsRead(room) {
  131. var highlightIndex = HIGHLIGHTED_CHANS.indexOf(room);
  132. if (room.lastMsg > room.lastRead) {
  133. sendReadMArker(room, room.lastMsg);
  134. room.lastRead = room.lastMsg;
  135. }
  136. if (highlightIndex >= 0) {
  137. HIGHLIGHTED_CHANS.splice(highlightIndex, 1);
  138. updateTitle();
  139. }
  140. var roomLi = document.getElementById("room_" +room.id);
  141. roomLi.classList.remove(R.klass.unread);
  142. roomLi.classList.remove(R.klass.unreadHi);
  143. }
  144. SLACK = new SlackWrapper();