data.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.starMsg = function(chan, msgId) { console.error("unimplemented"); };
  32. SimpleChatSystem.prototype.unstarMsg = function(chan, msgId) { console.error("unimplemented"); };
  33. SimpleChatSystem.prototype.pinMsg = function(chan, msgId) { console.error("unimplemented"); };
  34. SimpleChatSystem.prototype.unpinMsg = function(chan, msgId) { console.error("unimplemented"); };
  35. SimpleChatSystem.prototype.editMsg = function(chan, ts, newText) { console.error("unimplemented"); };
  36. SimpleChatSystem.prototype.addReaction = function(chan, ts, reaction) { console.error("unimplemented"); };
  37. SimpleChatSystem.prototype.removeReaction = function(chan, ts, reaction) { console.error("unimplemented"); };
  38. SimpleChatSystem.prototype.openUploadFileStream = function(chan, contentType, callback) { console.error("unimplemented"); };
  39. SimpleChatSystem.prototype.fetchHistory = function(chan) { console.error("unimplemented"); };
  40. SimpleChatSystem.prototype.sendTyping = function(chan) { console.error("unimplemented"); };
  41. SimpleChatSystem.prototype.markRead = function(chan, ts) { console.error("unimplemented"); };
  42. SimpleChatSystem.prototype.sendCommand = function(chan, cmd, args) { console.error("unimplemented"); };
  43. SimpleChatSystem.prototype.poll = function(knownVersion, now) { console.error("unimplemented"); return null; };
  44. /**
  45. * @constructor
  46. **/
  47. function SlackWrapper() {
  48. /** @type {number} */
  49. this.lastServerVersion = 0;
  50. /** @type {MultiChatManager} */
  51. this.context = new MultiChatManager();
  52. /** @type {!Object.<string, UiRoomHistory>} **/
  53. this.history = {};
  54. }
  55. SlackWrapper.prototype.update = function(data) {
  56. var now = Date.now(),
  57. i;
  58. if (data["v"])
  59. this.lastServerVersion = data["v"];
  60. if (data["static"]) {
  61. for (i in data["static"]) {
  62. var ctx = this.context.getById(i);
  63. if (!ctx) {
  64. ctx = new SimpleChatSystem();
  65. this.context.push(ctx);
  66. }
  67. ctx.getChatContext().updateStatic(data["static"][i], now);
  68. }
  69. }
  70. this.context.foreachChannels(function(chan) {
  71. if (chan.lastMsg === chan.lastRead) {
  72. var pos = HIGHLIGHTED_CHANS.indexOf(chan);
  73. if (pos !== -1)
  74. HIGHLIGHTED_CHANS.splice(pos, 1);
  75. }
  76. });
  77. if (data["live"]) {
  78. for (i in data["live"]) {
  79. var history = this.history[i];
  80. if (!history)
  81. history = this.history[i] = new UiRoomHistory(i, 250, data["live"][i], now);
  82. else
  83. history.pushAll(data["live"][i], now);
  84. }
  85. for (var roomId in data["live"]) {
  86. var liveCtx = this.context.getChannelContext(roomId).getChatContext(),
  87. chan = liveCtx.channels[roomId];
  88. if (chan) {
  89. if (this.history[roomId].messages.length)
  90. chan.lastMsg = Math.max(chan.lastMsg, this.history[roomId].lastMessage().ts);
  91. if (!chan.archived) {
  92. onMsgReceived(liveCtx, chan, data["live"][roomId]);
  93. if (SELECTED_ROOM && data["live"][SELECTED_ROOM.id])
  94. onRoomUpdated();
  95. }
  96. } else {
  97. outOfSync();
  98. }
  99. }
  100. }
  101. if (data["static"]) {
  102. onContextUpdated();
  103. }
  104. var typingUpdated = false;
  105. if (data["typing"]) {
  106. this.context.contexts.forEach(function(ctx) {
  107. var chatCtx = ctx.getChatContext();
  108. typingUpdated |= chatCtx.updateTyping(data["typing"], now);
  109. }, this);
  110. }
  111. if (data["static"] || typingUpdated)
  112. onTypingUpdated();
  113. if (data["config"]) {
  114. CONFIG = new Config(data["config"]);
  115. onConfigUpdated();
  116. }
  117. if (SELECTED_CONTEXT && SELECTED_ROOM && data["static"] && data["static"][SELECTED_CONTEXT.team.id] && data["static"][SELECTED_CONTEXT.team.id]["channels"] && data["static"][SELECTED_CONTEXT.team.id]["channels"]) {
  118. var arr = data["static"][SELECTED_CONTEXT.team.id]["channels"];
  119. i =0;
  120. for (var nbChan = arr.length; i < nbChan; i++) {
  121. if (arr[i].id === SELECTED_ROOM.id) {
  122. onRoomUpdated();
  123. break;
  124. }
  125. }
  126. }
  127. };
  128. setInterval(function() {
  129. var updated = false,
  130. now = Date.now();
  131. DATA.context.foreachContext(function(ctx) {
  132. if (ctx.getChatContext().cleanTyping(now))
  133. updated = true;
  134. });
  135. if (updated)
  136. onTypingUpdated();
  137. }, 1000);
  138. /**
  139. * @param {ChatContext} ctx
  140. * @param {string} text
  141. * @return {boolean}
  142. **/
  143. function isHighlighted(ctx, text) {
  144. var highlights = ctx.self.prefs.highlights;
  145. for (var i =0, nbHighlights = highlights.length; i < nbHighlights; i++)
  146. if (text.indexOf(highlights[i]) !== -1) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. /**
  152. * @param {ChatContext} ctx
  153. * @param {Room} chan
  154. * @param {Array.<*>} msg
  155. **/
  156. function onMsgReceived(ctx, chan, msg) {
  157. if (chan !== SELECTED_ROOM || !window.hasFocus) {
  158. var selfReg = new RegExp("<@" +ctx.self.id), // FIXME remove context id
  159. highligted = false,
  160. areNew = false,
  161. newHighlited = false;
  162. msg.forEach(function(i) {
  163. if (parseFloat(i["ts"]) <= chan.lastRead) {
  164. return;
  165. }
  166. areNew = true;
  167. if (chan instanceof PrivateMessageRoom || (i["text"] && (i["text"].match(selfReg) || isHighlighted(ctx, i["text"])))) {
  168. if (HIGHLIGHTED_CHANS.indexOf(chan) === -1) {
  169. newHighlited = true;
  170. HIGHLIGHTED_CHANS.push(chan);
  171. }
  172. highligted = true;
  173. }
  174. });
  175. if (areNew) {
  176. updateTitle();
  177. var dom = document.getElementById("room_" +chan.id);
  178. if (dom) {
  179. dom.classList.add(R.klass.unread);
  180. if (highligted)
  181. dom.classList.add(R.klass.unreadHi);
  182. }
  183. if (newHighlited && !window.hasFocus) {
  184. // TODO setting
  185. spawnNotification();
  186. }
  187. }
  188. }
  189. }
  190. /**
  191. * @param {Room} room
  192. **/
  193. function markRoomAsRead(room) {
  194. var highlightIndex = HIGHLIGHTED_CHANS.indexOf(room);
  195. if (room.lastMsg > room.lastRead) {
  196. var history = DATA.history[room.id];
  197. if (history) {
  198. var lastMsg = history.last();
  199. if (lastMsg) {
  200. sendReadMarker(room, lastMsg.id, lastMsg.ts);
  201. room.lastRead = lastMsg.ts;
  202. }
  203. }
  204. }
  205. if (highlightIndex >= 0) {
  206. HIGHLIGHTED_CHANS.splice(highlightIndex, 1);
  207. updateTitle();
  208. }
  209. var roomLi = document.getElementById("room_" +room.id);
  210. roomLi.classList.remove(R.klass.unread);
  211. roomLi.classList.remove(R.klass.unreadHi);
  212. }
  213. function invalidateAllMessages() {
  214. for (var i in DATA.history)
  215. DATA.history[i].invalidateAllMessages();
  216. if (SELECTED_ROOM)
  217. onRoomUpdated();
  218. }
  219. DATA = new SlackWrapper();