1
0

data.js 6.6 KB

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