data.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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) { 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. for (var i in data["static"])
  99. if (data["static"][i]["typing"]) {
  100. onTypingUpdated();
  101. break;
  102. }
  103. }
  104. };
  105. setInterval(function() {
  106. var updated = false
  107. ,now = Date.now();
  108. DATA.context.foreachContext(function(ctx) {
  109. if (ctx.getChatContext().cleanTyping(now))
  110. updated = true;
  111. });
  112. if (updated)
  113. onTypingUpdated();
  114. }, 1000);
  115. /**
  116. * @param {ChatContext} ctx
  117. * @param {string} text
  118. * @return {boolean}
  119. **/
  120. function isHighlighted(ctx, text) {
  121. var highlights = ctx.self.prefs.highlights;
  122. for (var i =0, nbHighlights = highlights.length; i < nbHighlights; i++)
  123. if (text.indexOf(highlights[i]) !== -1) {
  124. return true;
  125. }
  126. return false;
  127. }
  128. /**
  129. * @param {ChatContext} ctx
  130. * @param {Room} chan
  131. * @param {Array.<*>} msg
  132. **/
  133. function onMsgReceived(ctx, chan, msg) {
  134. if (chan !== SELECTED_ROOM || !window.hasFocus) {
  135. var selfReg = new RegExp("<@" +ctx.self.id) // FIXME remove context id
  136. ,highligted = false
  137. ,areNew = false
  138. ,newHighlited = false;
  139. msg.forEach(function(i) {
  140. if (parseFloat(i["ts"]) <= chan.lastRead) {
  141. return;
  142. }
  143. areNew = true;
  144. if (chan instanceof PrivateMessageRoom || (i["text"] && (i["text"].match(selfReg) || isHighlighted(ctx, i["text"])))) {
  145. if (HIGHLIGHTED_CHANS.indexOf(chan) === -1) {
  146. newHighlited = true;
  147. HIGHLIGHTED_CHANS.push(chan);
  148. }
  149. highligted = true;
  150. }
  151. });
  152. if (areNew) {
  153. updateTitle();
  154. var dom = document.getElementById("room_" +chan.id);
  155. if (dom) {
  156. dom.classList.add(R.klass.unread);
  157. if (highligted)
  158. dom.classList.add(R.klass.unreadHi);
  159. }
  160. if (newHighlited && !window.hasFocus) {
  161. // TODO setting
  162. spawnNotification();
  163. }
  164. }
  165. }
  166. }
  167. /**
  168. * @param {Room} room
  169. **/
  170. function markRoomAsRead(room) {
  171. var highlightIndex = HIGHLIGHTED_CHANS.indexOf(room);
  172. if (room.lastMsg > room.lastRead) {
  173. sendReadMArker(room, room.lastMsg);
  174. room.lastRead = room.lastMsg;
  175. }
  176. if (highlightIndex >= 0) {
  177. HIGHLIGHTED_CHANS.splice(highlightIndex, 1);
  178. updateTitle();
  179. }
  180. var roomLi = document.getElementById("room_" +room.id);
  181. roomLi.classList.remove(R.klass.unread);
  182. roomLi.classList.remove(R.klass.unreadHi);
  183. }
  184. DATA = new SlackWrapper();