workflow.js 11 KB

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