data.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. var pins = {};
  68. if (data["static"][i]["channels"])
  69. data["static"][i]["channels"].forEach(function(chanInfo) {
  70. if (chanInfo["pins"]) {
  71. pins[chanInfo["id"]] = chanInfo["pins"];
  72. chanInfo["pins"] = undefined;
  73. }
  74. });
  75. ctx.getChatContext().updateStatic(data["static"][i], now);
  76. for (var chanId in pins) {
  77. var msgs = [],
  78. histo = this.history[chanId];
  79. if (!histo)
  80. histo = this.history[chanId] = new UiRoomHistory(chanId, 250, null, now);
  81. pins[chanId].forEach(function(msg) {
  82. msgs.push(histo.messageFactory(msg, now));
  83. });
  84. ctx.getChatContext().channels[chanId].pins = msgs;
  85. }
  86. }
  87. }
  88. this.context.foreachChannels(function(chan) {
  89. if (chan.lastMsg === chan.lastRead) {
  90. var pos = HIGHLIGHTED_CHANS.indexOf(chan);
  91. if (pos !== -1)
  92. HIGHLIGHTED_CHANS.splice(pos, 1);
  93. }
  94. });
  95. if (data["live"]) {
  96. for (i in data["live"]) {
  97. var history = this.history[i];
  98. if (!history)
  99. history = this.history[i] = new UiRoomHistory(i, 250, data["live"][i], now);
  100. else
  101. history.pushAll(data["live"][i], now);
  102. }
  103. for (var roomId in data["live"]) {
  104. var liveCtx = this.context.getChannelContext(roomId).getChatContext(),
  105. chan = liveCtx.channels[roomId];
  106. if (chan) {
  107. if (this.history[roomId].messages.length)
  108. chan.setLastMsg(this.history[roomId].lastMessage().ts, now);
  109. if (!chan.archived) {
  110. onMsgReceived(liveCtx, chan, data["live"][roomId]);
  111. if (SELECTED_ROOM && data["live"][SELECTED_ROOM.id])
  112. onRoomUpdated();
  113. }
  114. } else {
  115. outOfSync();
  116. }
  117. }
  118. }
  119. if (data["static"]) {
  120. onContextUpdated();
  121. }
  122. var typingUpdated = false;
  123. if (data["typing"]) {
  124. this.context.contexts.forEach(function(ctx) {
  125. var chatCtx = ctx.getChatContext();
  126. typingUpdated |= chatCtx.updateTyping(data["typing"], now);
  127. }, this);
  128. }
  129. if (data["static"] || typingUpdated)
  130. onTypingUpdated();
  131. if (data["config"]) {
  132. CONFIG = new Config(data["config"]);
  133. onConfigUpdated();
  134. }
  135. 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"]) {
  136. var arr = data["static"][SELECTED_CONTEXT.team.id]["channels"];
  137. i =0;
  138. for (var nbChan = arr.length; i < nbChan; i++) {
  139. if (arr[i].id === SELECTED_ROOM.id) {
  140. onRoomUpdated();
  141. break;
  142. }
  143. }
  144. }
  145. };
  146. setInterval(function() {
  147. var updated = false,
  148. now = Date.now();
  149. DATA.context.foreachContext(function(ctx) {
  150. if (ctx.getChatContext().cleanTyping(now))
  151. updated = true;
  152. });
  153. if (updated)
  154. onTypingUpdated();
  155. }, 1000);
  156. /**
  157. * @param {ChatContext} ctx
  158. * @param {string} text
  159. * @return {boolean}
  160. **/
  161. function isHighlighted(ctx, text) {
  162. var highlights = ctx.self.prefs.highlights;
  163. for (var i =0, nbHighlights = highlights.length; i < nbHighlights; i++)
  164. if (text.indexOf(highlights[i]) !== -1) {
  165. return true;
  166. }
  167. return false;
  168. }
  169. /**
  170. * @param {ChatContext} ctx
  171. * @param {Room} chan
  172. * @param {Array.<*>} msg
  173. **/
  174. function onMsgReceived(ctx, chan, msg) {
  175. if (chan !== SELECTED_ROOM || !window.hasFocus) {
  176. var selfId = ctx.self ? ctx.self.id : null,
  177. selfReg = selfId ? new RegExp("<@" +selfId) : null, // FIXME remove context id
  178. highligted = false,
  179. areNew = false,
  180. newHighlited = false;
  181. msg.forEach(function(i) {
  182. if (parseFloat(i["ts"]) <= chan.lastRead) {
  183. return;
  184. }
  185. if (i["user"] !== ctx.self.id) {
  186. areNew = true;
  187. if (chan instanceof PrivateMessageRoom || (i["text"] && ((selfReg && i["text"].match(selfReg)) || isHighlighted(ctx, i["text"])))) {
  188. if (HIGHLIGHTED_CHANS.indexOf(chan) === -1) {
  189. newHighlited = true;
  190. HIGHLIGHTED_CHANS.push(chan);
  191. }
  192. highligted = true;
  193. }
  194. }
  195. });
  196. if (areNew) {
  197. updateTitle();
  198. var dom = document.getElementById("room_" +chan.id);
  199. if (dom) {
  200. dom.classList.add(R.klass.unread);
  201. if (highligted)
  202. dom.classList.add(R.klass.unreadHi);
  203. }
  204. if (newHighlited && !window.hasFocus) {
  205. // TODO setting
  206. spawnNotification();
  207. }
  208. }
  209. }
  210. msg.forEach(function(m) {
  211. if (!selfId || selfId === m.userId) {
  212. for (var i =0, nbPending = PENDING_MESSAGES.length; i < nbPending; i++) {
  213. var current = PENDING_MESSAGES[i];
  214. if (current.channel === chan.id && !!m["isMeMessage"] === current.isMe && ((m["pendingId"] === current.pendingId && current.pendingId) || (!current.pendingId && m["text"].trim() === current.text))) {
  215. PENDING_MESSAGES.splice(i, 1);
  216. return;
  217. }
  218. }
  219. }
  220. });
  221. }
  222. /**
  223. * @param {Room} room
  224. **/
  225. function markRoomAsRead(room) {
  226. var highlightIndex = HIGHLIGHTED_CHANS.indexOf(room);
  227. if (room.lastMsg > room.lastRead) {
  228. var history = DATA.history[room.id];
  229. if (history) {
  230. var lastMsg = history.lastMessage();
  231. if (lastMsg) {
  232. sendReadMarker(room, lastMsg.id, lastMsg.ts);
  233. room.lastRead = lastMsg.ts;
  234. }
  235. }
  236. }
  237. if (highlightIndex >= 0) {
  238. HIGHLIGHTED_CHANS.splice(highlightIndex, 1);
  239. updateTitle();
  240. }
  241. var roomLi = document.getElementById("room_" +room.id);
  242. roomLi.classList.remove(R.klass.unread);
  243. roomLi.classList.remove(R.klass.unreadHi);
  244. }
  245. function invalidateAllMessages() {
  246. for (var i in DATA.history)
  247. DATA.history[i].invalidateAllMessages();
  248. if (SELECTED_ROOM)
  249. onRoomUpdated();
  250. }
  251. DATA = new SlackWrapper();