data.js 6.9 KB

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