1
0

data.js 9.5 KB

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