data.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. var
  2. /**
  3. * @type {SlackWrapper}
  4. **/
  5. DATA
  6. /**
  7. * @type {Array.<Room>}
  8. **/
  9. ,HIGHLIGHTED_CHANS = [];
  10. ;
  11. /**
  12. * @constructor
  13. * @implements {ChatSystem}
  14. * @extends {ChatContext}
  15. **/
  16. function SimpleChatSystem() {
  17. ChatContext.call(this);
  18. };
  19. SimpleChatSystem.prototype = Object.create(ChatContext.prototype);
  20. SimpleChatSystem.prototype.constructor = SimpleChatSystem;
  21. SimpleChatSystem.prototype.getId = function() {
  22. return this.team ? this.team.id : null;
  23. };
  24. SimpleChatSystem.prototype.getChatContext = function() {
  25. return this;
  26. };
  27. SimpleChatSystem.prototype.onRequest = function() { console.error("unimplemented"); };
  28. SimpleChatSystem.prototype.sendMeMsg = function(chan, text) { console.error("unimplemented"); };
  29. SimpleChatSystem.prototype.sendMsg = function(chan, text, attachments) { console.error("unimplemented"); };
  30. SimpleChatSystem.prototype.removeMsg = function(chan, ts) { console.error("unimplemented"); };
  31. SimpleChatSystem.prototype.editMsg = function(chan, ts, newText) { console.error("unimplemented"); };
  32. SimpleChatSystem.prototype.addReaction = function(chan, ts, reaction) { console.error("unimplemented"); };
  33. SimpleChatSystem.prototype.removeReaction = function(chan, ts, reaction) { console.error("unimplemented"); };
  34. SimpleChatSystem.prototype.openUploadFileStream = function(chan, contentType, callback) { console.error("unimplemented"); };
  35. SimpleChatSystem.prototype.fetchHistory = function(chan) { console.error("unimplemented"); };
  36. SimpleChatSystem.prototype.sendTyping = function(chan) { console.error("unimplemented"); };
  37. SimpleChatSystem.prototype.markRead = function(chan, ts) { console.error("unimplemented"); };
  38. SimpleChatSystem.prototype.sendCommand = function(chan, cmd, args) { console.error("unimplemented"); };
  39. SimpleChatSystem.prototype.poll = function(knownVersion, now) { console.error("unimplemented"); return null; };
  40. /**
  41. * @constructor
  42. **/
  43. function SlackWrapper() {
  44. /** @type {number} */
  45. this.lastServerVersion = 0;
  46. /** @type {MultiChatManager} */
  47. this.context = new MultiChatManager();
  48. /** @type {!Object.<string, UiRoomHistory>} **/
  49. this.history = {};
  50. }
  51. SlackWrapper.prototype.update = function(data) {
  52. var now = Date.now();
  53. if (data["v"])
  54. this.lastServerVersion = data["v"];
  55. if (data["static"]) {
  56. for (var i in data["static"]) {
  57. var ctx = this.context.getById(i);
  58. if (!ctx) {
  59. ctx = new SimpleChatSystem();
  60. this.context.push(ctx);
  61. }
  62. ctx.getChatContext().updateStatic(data["static"][i], now);
  63. }
  64. }
  65. this.context.foreachChannels(function(chan) {
  66. if (chan.lastMsg === chan.lastRead) {
  67. var pos = HIGHLIGHTED_CHANS.indexOf(chan);
  68. if (pos !== -1)
  69. HIGHLIGHTED_CHANS.splice(pos, 1);
  70. }
  71. });
  72. if (data["live"]) {
  73. for (var i in data["live"]) {
  74. var history = this.history[i];
  75. if (!history)
  76. history = this.history[i] = new UiRoomHistory(i, 250, data["live"][i], now);
  77. else
  78. history.pushAll(data["live"][i], now);
  79. }
  80. for (var roomId in data["live"]) {
  81. var ctx = this.context.getChannelContext(roomId).getChatContext(),
  82. chan = ctx.channels[roomId];
  83. if (chan) {
  84. if (this.history[roomId].messages.length)
  85. chan.lastMsg = Math.max(chan.lastMsg, this.history[roomId].lastMessage().ts);
  86. if (!chan.archived) {
  87. onMsgReceived(ctx, chan, data["live"][roomId]);
  88. if (SELECTED_ROOM && data["live"][SELECTED_ROOM.id])
  89. onRoomUpdated();
  90. }
  91. } else {
  92. outOfSync();
  93. }
  94. }
  95. }
  96. if (data["static"]) {
  97. onContextUpdated();
  98. }
  99. var typingUpdated = false;
  100. if (data["typing"]) {
  101. this.context.contexts.forEach(function(ctx) {
  102. var chatCtx = ctx.getChatContext();
  103. typingUpdated |= chatCtx.updateTyping(data["typing"], now);
  104. }, this);
  105. }
  106. if (data["static"] || typingUpdated)
  107. onTypingUpdated();
  108. };
  109. setInterval(function() {
  110. var updated = false
  111. ,now = Date.now();
  112. DATA.context.foreachContext(function(ctx) {
  113. if (ctx.getChatContext().cleanTyping(now))
  114. updated = true;
  115. });
  116. if (updated)
  117. onTypingUpdated();
  118. }, 1000);
  119. /**
  120. * @param {ChatContext} ctx
  121. * @param {string} text
  122. * @return {boolean}
  123. **/
  124. function isHighlighted(ctx, text) {
  125. var highlights = ctx.self.prefs.highlights;
  126. for (var i =0, nbHighlights = highlights.length; i < nbHighlights; i++)
  127. if (text.indexOf(highlights[i]) !== -1) {
  128. return true;
  129. }
  130. return false;
  131. }
  132. /**
  133. * @param {ChatContext} ctx
  134. * @param {Room} chan
  135. * @param {Array.<*>} msg
  136. **/
  137. function onMsgReceived(ctx, chan, msg) {
  138. if (chan !== SELECTED_ROOM || !window.hasFocus) {
  139. var selfReg = new RegExp("<@" +ctx.self.id) // FIXME remove context id
  140. ,highligted = false
  141. ,areNew = false
  142. ,newHighlited = false;
  143. msg.forEach(function(i) {
  144. if (parseFloat(i["ts"]) <= chan.lastRead) {
  145. return;
  146. }
  147. areNew = true;
  148. if (chan instanceof PrivateMessageRoom || (i["text"] && (i["text"].match(selfReg) || isHighlighted(ctx, i["text"])))) {
  149. if (HIGHLIGHTED_CHANS.indexOf(chan) === -1) {
  150. newHighlited = true;
  151. HIGHLIGHTED_CHANS.push(chan);
  152. }
  153. highligted = true;
  154. }
  155. });
  156. if (areNew) {
  157. updateTitle();
  158. var dom = document.getElementById("room_" +chan.id);
  159. if (dom) {
  160. dom.classList.add(R.klass.unread);
  161. if (highligted)
  162. dom.classList.add(R.klass.unreadHi);
  163. }
  164. if (newHighlited && !window.hasFocus) {
  165. // TODO setting
  166. spawnNotification();
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * @param {Room} room
  173. **/
  174. function markRoomAsRead(room) {
  175. var highlightIndex = HIGHLIGHTED_CHANS.indexOf(room);
  176. if (room.lastMsg > room.lastRead) {
  177. sendReadMArker(room, room.lastMsg);
  178. room.lastRead = room.lastMsg;
  179. }
  180. if (highlightIndex >= 0) {
  181. HIGHLIGHTED_CHANS.splice(highlightIndex, 1);
  182. updateTitle();
  183. }
  184. var roomLi = document.getElementById("room_" +room.id);
  185. roomLi.classList.remove(R.klass.unread);
  186. roomLi.classList.remove(R.klass.unreadHi);
  187. }
  188. DATA = new SlackWrapper();