workflow.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* jshint sub: true */
  2. var
  3. /**
  4. * @type {number} next period to wait before next retry in case of failure, in seconds
  5. **/
  6. NEXT_RETRY = 0,
  7. /**
  8. * @type {Room|null}
  9. **/
  10. SELECTED_ROOM = null,
  11. /**
  12. * @type {SimpleChatSystem|null}
  13. **/
  14. SELECTED_CONTEXT = null,
  15. /** @type {Message|null} */
  16. REPLYING_TO = null,
  17. /** @type {Message|null} */
  18. EDITING = null,
  19. /** @const @type {number} */
  20. KEEP_MESSAGES = 100
  21. ;
  22. function initHljs() {
  23. new HttpRequest("highlight.pack.js")
  24. .callbackSuccess(function(code, head, resp) {
  25. var script = document.createElement("script"),
  26. link = document.createElement("link");
  27. script.innerHTML = resp;
  28. script.language = "text/javascript";
  29. link.href = "hljs-androidstudio.css";
  30. link.rel = "stylesheet";
  31. document.head.appendChild(link);
  32. document.body.appendChild(script);
  33. })
  34. .callbackError(function() {
  35. console.error("Failure loading hljs required files");
  36. })
  37. .setTimeout(1000 * 60 * 1) // 1 min timeout
  38. .send();
  39. }
  40. /**
  41. * @param {Room} room
  42. * @param {function(boolean)} cb
  43. **/
  44. function fetchHistory(room, cb) {
  45. new HttpRequest("api/hist?room=" +room.id)
  46. .setResponseType(HttpRequestResponseType.JSON)
  47. .callbackSuccess(function(code, status, resp) {
  48. if (resp) {
  49. var history = DATA.history[room.id],
  50. updated;
  51. if (!history) {
  52. history = DATA.history[room.id] = new UiRoomHistory(room, KEEP_MESSAGES, /** @type {Array} */ (resp), Date.now());
  53. updated = true;
  54. } else {
  55. updated = !!history.pushAll(/** @type {Array} */ (resp), Date.now());
  56. }
  57. if (updated) {
  58. onMsgReceived(DATA.context.getChannelContext(room.id).getChatContext(), room, /** @type {Array} */ (resp));
  59. if (room === SELECTED_ROOM)
  60. onRoomUpdated();
  61. }
  62. }
  63. }, this)
  64. .callback(function() {
  65. // TODO ui stop loading
  66. })
  67. .send();
  68. }
  69. function onConfigUpdated() {
  70. if (isObjectEmpty(CONFIG.services)) {
  71. Settings.setClosable(false).display(Settings.pages.services);
  72. }
  73. loadEmojiProvider(CONFIG.getEmojiProvider());
  74. document.getElementById(R.id.stylesheet).innerHTML = CONFIG.compileCSS();
  75. }
  76. function poll(callback) {
  77. new HttpRequest("api?v=" +DATA.lastServerVersion)
  78. .callback(function(statusCode, statusText, resp) {
  79. var success = Math.floor(statusCode / 100) === 2;
  80. if (success) {
  81. if (NEXT_RETRY) {
  82. NEXT_RETRY = 0;
  83. onNetworkStateUpdated(true);
  84. }
  85. } else {
  86. if (NEXT_RETRY) {
  87. NEXT_RETRY += Math.floor((NEXT_RETRY || 5)/2);
  88. NEXT_RETRY = Math.min(60, NEXT_RETRY);
  89. } else {
  90. NEXT_RETRY = 5;
  91. onNetworkStateUpdated(false);
  92. }
  93. }
  94. callback(success, resp);
  95. })
  96. .setResponseType(HttpRequestResponseType.JSON)
  97. .setTimeout(1000 * 60 * 1)
  98. .send();
  99. }
  100. function outOfSync() {
  101. DATA.lastServerVersion = 0;
  102. }
  103. /**
  104. * @param {Room} room
  105. **/
  106. function sendTyping(room) {
  107. new HttpRequest(HttpRequestMethod.POST, "api/typing?room=" +room.id).send();
  108. }
  109. /**
  110. * @param {boolean} success
  111. * @param {*} response
  112. **/
  113. function onPollResponse(success, response) {
  114. if (success) {
  115. if (response) {
  116. DATA.update(response);
  117. }
  118. startPolling();
  119. } else {
  120. setTimeout(startPolling, NEXT_RETRY * 1000);
  121. }
  122. }
  123. function startPolling() {
  124. poll(onPollResponse);
  125. }
  126. /**
  127. * @param {Room} room
  128. **/
  129. function selectRoom(room) {
  130. if (SELECTED_ROOM)
  131. unselectRoom();
  132. document.getElementById("room_" +room.id).classList.add(R.klass.selected);
  133. document.body.classList.remove(R.klass.noRoomSelected);
  134. SELECTED_ROOM = room;
  135. SELECTED_CONTEXT = /** @type {SimpleChatSystem} */ (DATA.context.getChannelContext(room.id));
  136. onRoomSelected();
  137. roomInfo.hide();
  138. createContextBackground(SELECTED_CONTEXT.getChatContext().team.id, SELECTED_CONTEXT.getChatContext().users, function(imgData) {
  139. document.getElementById(R.id.context).style.backgroundImage = 'url(' +imgData +')';
  140. });
  141. if (!DATA.history[SELECTED_ROOM.id] || DATA.history[SELECTED_ROOM.id].messages.length < KEEP_MESSAGES)
  142. fetchHistory(SELECTED_ROOM, function(success) {});
  143. document.getElementById(R.id.mainSection).classList.remove(R.klass.noRoomSelected);
  144. }
  145. function setRoomFromHashBang() {
  146. var hashId = document.location.hash.substr(1),
  147. room = DATA.context.getChannel(hashId);
  148. if (room && room !== SELECTED_ROOM)
  149. selectRoom(room);
  150. else {
  151. var user = DATA.context.getUser(hashId);
  152. if (user && user.privateRoom)
  153. selectRoom(user.privateRoom);
  154. }
  155. }
  156. /**
  157. * @param {Room} room
  158. **/
  159. function starChannel(room) {
  160. new HttpRequest(HttpRequestMethod.POST, "api/starChannel?room=" +room.id).send();
  161. }
  162. /**
  163. * @param {Room} room
  164. **/
  165. function unstarChannel(room) {
  166. new HttpRequest(HttpRequestMethod.POST, "api/unstarChannel?room=" +room.id).send();
  167. }
  168. function unselectRoom() {
  169. document.getElementById("room_" +SELECTED_ROOM.id).classList.remove(R.klass.selected);
  170. document.getElementById(R.id.mainSection).classList.add(R.klass.noRoomSelected);
  171. }
  172. /**
  173. * @param {Room} chan
  174. * @param {string} filename
  175. * @param {File} file
  176. * @param {function(string?)} callback
  177. **/
  178. function uploadFile(chan, filename, file, callback) {
  179. var fileReader = new FileReader(),
  180. formData = new FormData();
  181. formData.append("file", file);
  182. formData.append("filename", filename);
  183. new HttpRequest(HttpRequestMethod.POST, "api/file?room=" +chan.id)
  184. .callbackSuccess(function() { callback(null); })
  185. .callbackError(function(sCode, sText, resp) { callback(sText); })
  186. .send(formData);
  187. }
  188. /**
  189. * @param {string} payload
  190. * @param {string} serviceId
  191. * @param {(function((string|null)))=} callback
  192. **/
  193. function sendCommand(payload, serviceId, callback) {
  194. var xhr = new HttpRequest(HttpRequestMethod.POST, "api/attachmentAction?serviceId=" +serviceId);
  195. if (callback)
  196. xhr.callbackSuccess(function() {
  197. callback(null);
  198. }).callbackError(function(code, head, resp) {
  199. callback(head);
  200. });
  201. xhr.send(JSON.stringify(payload));
  202. }
  203. function getActionPayload(channelId, msg, attachment, action) {
  204. var payload = {
  205. "actions": [ action ],
  206. "attachment_id": attachment["id"],
  207. "callback_id": attachment["callback_id"],
  208. "channel_id": channelId,
  209. "is_ephemeral": msg instanceof NoticeMessage,
  210. "message_ts": msg["id"]
  211. };
  212. return payload;
  213. }
  214. /**
  215. * @param {Room} chan
  216. * @param {Command!} cmd
  217. * @param {string} args
  218. **/
  219. function doCommand(chan, cmd, args) {
  220. new HttpRequest(HttpRequestMethod.POST, "api/cmd?room=" +chan.id +"&cmd=" +encodeURIComponent(cmd.name.substr(1)) +"&args=" +encodeURIComponent(args)).send();
  221. }
  222. /**
  223. * @param {Room} chan
  224. * @param {string} msg
  225. * @param {Message|null=} replyTo
  226. **/
  227. function sendMsg(chan, msg, replyTo) {
  228. var url = 'api/msg?room=' +chan.id +"&text=" +encodeURIComponent(msg);
  229. if (replyTo) {
  230. var sender = DATA.context.getUser(replyTo.userId),
  231. footer = chan.isPrivate ? locale.message : chan.name;
  232. var attachment = {
  233. "fallback": replyTo.text,
  234. "author_name": sender.getName(),
  235. "text": replyTo.text,
  236. "footer": footer,
  237. "ts": replyTo.ts
  238. };
  239. url += "&attachments=" +encodeURIComponent(JSON.stringify([attachment]));
  240. }
  241. new HttpRequest(HttpRequestMethod.POST, url).send();
  242. }
  243. /**
  244. * @param {Room} chan
  245. * @param {string} text
  246. * @param {Message} msg
  247. **/
  248. function editMsg(chan, text, msg) {
  249. new HttpRequest(HttpRequestMethod.PUT, "api/msg?room=" +chan.id +"&ts=" +msg.id +"&text=" +encodeURIComponent(text)).send();
  250. }
  251. /**
  252. * @param {Room} chan
  253. * @param {Message} msg
  254. **/
  255. function removeMsg(chan, msg) {
  256. new HttpRequest(HttpRequestMethod.DELETE, "api/msg?room=" +chan.id +"&ts=" +msg.id).send();
  257. }
  258. /**
  259. * @param {Room} chan
  260. * @param {Message} msg
  261. **/
  262. function pinMsg(chan, msg) {
  263. new HttpRequest(HttpRequestMethod.POST, "api/pinMsg?room=" +chan.id +"&msgId=" +msg.id).send();
  264. }
  265. /**
  266. * @param {Room} chan
  267. * @param {Message} msg
  268. **/
  269. function starMsg(chan, msg) {
  270. new HttpRequest(HttpRequestMethod.POST, "api/starMsg?room=" +chan.id +"&msgId=" +msg.id).send();
  271. }
  272. /**
  273. * @param {Room} chan
  274. * @param {Message} msg
  275. **/
  276. function unpinMsg(chan, msg) {
  277. new HttpRequest(HttpRequestMethod.DELETE, "api/pinMsg?room=" +chan.id +"&msgId=" +msg.id).send();
  278. }
  279. /**
  280. * @param {Room} chan
  281. * @param {Message} msg
  282. **/
  283. function unstarMsg(chan, msg) {
  284. new HttpRequest(HttpRequestMethod.DELETE, "api/starMsg?room=" +chan.id +"&msgId=" +msg.id).send();
  285. }
  286. /**
  287. * @param {Room} chan
  288. * @param {string} id
  289. * @param {number} ts
  290. **/
  291. function sendReadMarker(chan, id, ts) {
  292. new HttpRequest(HttpRequestMethod.POST, "api/markread?room=" +chan.id +"&id=" +id +"&ts=" +ts).send();
  293. }
  294. /**
  295. * @param {string} channelId
  296. * @param {string} msgId
  297. * @param {string} reaction
  298. **/
  299. function addReaction(channelId, msgId, reaction) {
  300. new HttpRequest(HttpRequestMethod.POST, "api/reaction?room=" +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction)).send();
  301. }
  302. /**
  303. * @param {string} channelId
  304. * @param {string} msgId
  305. * @param {string} reaction
  306. **/
  307. function removeReaction(channelId, msgId, reaction) {
  308. new HttpRequest(HttpRequestMethod.DELETE, "api/reaction?room=" +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction)).send();
  309. }
  310. function logout() {
  311. new HttpRequest(HttpRequestMethod.POST, "api/logout").send();
  312. document.cookie = "sessID=;Path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT;";
  313. document.location.reload();
  314. }
  315. /**
  316. * @this {Element}
  317. **/
  318. function filterChanList() {
  319. var chans = {},
  320. matchingChans = [],
  321. val = this.value;
  322. DATA.context.foreachChannels(function(chan) {
  323. chans[chan.id] = chan.matchString(val, Utils);
  324. });
  325. for (var chanId in chans) {
  326. var chanDom = document.getElementById("room_" +chanId);
  327. if (chanDom) {
  328. if (chans[chanId].name + chans[chanId].members + chans[chanId].topic +chans[chanId].purpose) {
  329. chanDom.classList.remove(R.klass.hidden);
  330. matchingChans.push(chanId);
  331. } else {
  332. chanDom.classList.add(R.klass.hidden);
  333. }
  334. }
  335. }
  336. //TODO sort
  337. }