data.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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[i];
  58. if (!ctx)
  59. ctx = this.context[i] = new SimpleChatSystem();
  60. ctx.updateStatic(data["static"], now);
  61. }
  62. }
  63. this.context.foreachChannels(function(chan) {
  64. if (chan.lastMsg === chan.lastRead) {
  65. var pos = HIGHLIGHTED_CHANS.indexOf(chan);
  66. if (pos !== -1)
  67. HIGHLIGHTED_CHANS.splice(pos, 1);
  68. }
  69. });
  70. if (data["live"]) {
  71. for (var i in data["live"]) {
  72. var history = this.history[i];
  73. if (!history)
  74. history = this.history[i] = new UiRoomHistory(i, 250, data["live"][i], now);
  75. else
  76. history.pushAll(data["live"][i], now);
  77. }
  78. for (var roomId in data["live"]) {
  79. var ctx = this.context.getChannelContext(roomId).getChatContext(),
  80. chan = ctx.channels[roomId];
  81. if (chan) {
  82. if (this.history[roomId].messages.length)
  83. chan.lastMsg = Math.max(chan.lastMsg, this.history[roomId].lastMessage().ts);
  84. if (!chan.archived) {
  85. onMsgReceived(ctx, chan, data["live"][roomId]);
  86. if (SELECTED_ROOM && data["live"][SELECTED_ROOM.id])
  87. onRoomUpdated();
  88. }
  89. } else {
  90. outOfSync();
  91. }
  92. }
  93. }
  94. if (data["static"]) {
  95. onContextUpdated();
  96. for (var i in data["static"])
  97. if (data["static"][i]["typing"]) {
  98. onTypingUpdated();
  99. break;
  100. }
  101. }
  102. };
  103. setInterval(function() {
  104. if (DATA.context.cleanTyping(Date.now()))
  105. onTypingUpdated();
  106. }, 1000);
  107. /**
  108. * @param {ChatContext} ctx
  109. * @param {string} text
  110. * @return {boolean}
  111. **/
  112. function isHighlighted(ctx, text) {
  113. var highlights = ctx.self.prefs.highlights;
  114. for (var i =0, nbHighlights = highlights.length; i < nbHighlights; i++)
  115. if (text.indexOf(highlights[i]) !== -1) {
  116. return true;
  117. }
  118. return false;
  119. }
  120. /**
  121. * @param {ChatContext} ctx
  122. * @param {Room} chan
  123. * @param {Array.<*>} msg
  124. **/
  125. function onMsgReceived(ctx, chan, msg) {
  126. if (chan !== SELECTED_ROOM || !window.hasFocus) {
  127. var selfReg = new RegExp("<@" +ctx.self.id) // FIXME remove context id
  128. ,highligted = false
  129. ,areNew = false
  130. ,newHighlited = false;
  131. msg.forEach(function(i) {
  132. if (parseFloat(i["ts"]) <= chan.lastRead) {
  133. return;
  134. }
  135. areNew = true;
  136. if (chan instanceof PrivateMessageRoom || (i["text"] && (i["text"].match(selfReg) || isHighlighted(ctx, i["text"])))) {
  137. if (HIGHLIGHTED_CHANS.indexOf(chan) === -1) {
  138. newHighlited = true;
  139. HIGHLIGHTED_CHANS.push(chan);
  140. }
  141. highligted = true;
  142. }
  143. });
  144. if (areNew) {
  145. updateTitle();
  146. var dom = document.getElementById("room_" +chan.id);
  147. if (dom) {
  148. dom.classList.add(R.klass.unread);
  149. if (highligted)
  150. dom.classList.add(R.klass.unreadHi);
  151. }
  152. if (newHighlited && !window.hasFocus) {
  153. // TODO setting
  154. spawnNotification();
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * @param {Room} room
  161. **/
  162. function markRoomAsRead(room) {
  163. var highlightIndex = HIGHLIGHTED_CHANS.indexOf(room);
  164. if (room.lastMsg > room.lastRead) {
  165. sendReadMArker(room, room.lastMsg);
  166. room.lastRead = room.lastMsg;
  167. }
  168. if (highlightIndex >= 0) {
  169. HIGHLIGHTED_CHANS.splice(highlightIndex, 1);
  170. updateTitle();
  171. }
  172. var roomLi = document.getElementById("room_" +room.id);
  173. roomLi.classList.remove(R.klass.unread);
  174. roomLi.classList.remove(R.klass.unreadHi);
  175. }
  176. DATA = new SlackWrapper();