workflow.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. ;
  20. function initHljs() {
  21. var xhr = new XMLHttpRequest();
  22. xhr.timeout = 1000 * 60 * 1; // 3 min timeout
  23. xhr.onreadystatechange = function(e) {
  24. if (xhr.readyState === 4) {
  25. var script = document.createElement("script");
  26. script.innerHTML = xhr.response;
  27. script.language = "text/javascript";
  28. document.head.innerHTML += '<link href="hljs-androidstudio.css" rel="stylesheet"/>';
  29. document.body.appendChild(script);
  30. }
  31. };
  32. xhr.open('GET', 'highlight.pack.js', true);
  33. xhr.send(null);
  34. }
  35. /**
  36. * @param {Room} room
  37. * @param {function(boolean)} cb
  38. **/
  39. function fetchHistory(room, cb) {
  40. var xhr = new XMLHttpRequest();
  41. xhr.open('GET', 'api/hist?room=' +room.id, true);
  42. xhr.send(null);
  43. }
  44. function onConfigUpdated() {
  45. if (!CONFIG.services.length) {
  46. Settings.setClosable(false).display(Settings.pages.services);
  47. }
  48. }
  49. function poll(callback) {
  50. var xhr = new XMLHttpRequest();
  51. xhr.timeout = 1000 * 60 * 1; // 3 min timeout
  52. xhr.onreadystatechange = function(e) {
  53. if (xhr.readyState === 4) {
  54. if (xhr.status === 0) {
  55. if (NEXT_RETRY) {
  56. NEXT_RETRY = 0;
  57. onNetworkStateUpdated(true);
  58. }
  59. poll(callback); // retry on timeout
  60. return;
  61. }
  62. var resp = null,
  63. success = Math.floor(xhr.status / 100) === 2;
  64. if (success) {
  65. if (NEXT_RETRY) {
  66. NEXT_RETRY = 0;
  67. onNetworkStateUpdated(true);
  68. }
  69. resp = xhr.response;
  70. try {
  71. resp = JSON.parse(/** @type {string} */ (resp));
  72. } catch (exp) {
  73. resp = null;
  74. }
  75. } else {
  76. if (NEXT_RETRY) {
  77. NEXT_RETRY += Math.floor((NEXT_RETRY || 5)/2);
  78. NEXT_RETRY = Math.min(60, NEXT_RETRY);
  79. } else {
  80. NEXT_RETRY = 5;
  81. onNetworkStateUpdated(false);
  82. }
  83. }
  84. callback(success, resp);
  85. }
  86. };
  87. xhr.open('GET', 'api?v=' +DATA.lastServerVersion, true);
  88. xhr.send(null);
  89. }
  90. function outOfSync() {
  91. DATA.lastServerVersion = 0;
  92. }
  93. /**
  94. * @param {Room} room
  95. **/
  96. function sendTyping(room) {
  97. var xhr = new XMLHttpRequest(),
  98. url = 'api/typing?room=' +room.id;
  99. xhr.open('POST', url, true);
  100. xhr.send(null);
  101. }
  102. /**
  103. * @param {boolean} success
  104. * @param {*} response
  105. **/
  106. function onPollResponse(success, response) {
  107. if (success) {
  108. if (response) {
  109. DATA.update(response);
  110. }
  111. startPolling();
  112. } else {
  113. setTimeout(startPolling, NEXT_RETRY * 1000);
  114. }
  115. }
  116. function startPolling() {
  117. poll(onPollResponse);
  118. }
  119. /**
  120. * @param {Room} room
  121. **/
  122. function selectRoom(room) {
  123. if (SELECTED_ROOM)
  124. unselectRoom();
  125. document.getElementById("room_" +room.id).classList.add(R.klass.selected);
  126. document.body.classList.remove(R.klass.noRoomSelected);
  127. SELECTED_ROOM = room;
  128. SELECTED_CONTEXT = /** @type {SimpleChatSystem} */ (DATA.context.getChannelContext(room.id));
  129. onRoomSelected();
  130. createContextBackground(SELECTED_CONTEXT.getChatContext().team.id, SELECTED_CONTEXT.getChatContext().users, function(imgData) {
  131. document.getElementById(R.id.context).style.backgroundImage = 'url(' +imgData +')';
  132. });
  133. if (SELECTED_ROOM.lastMsg !== 0 && !DATA.history[SELECTED_ROOM.id])
  134. fetchHistory(SELECTED_ROOM, function(success) {});
  135. }
  136. function unselectRoom() {
  137. document.getElementById("room_" +SELECTED_ROOM.id).classList.remove(R.klass.selected);
  138. }
  139. /**
  140. * @param {Room} chan
  141. * @param {string} filename
  142. * @param {File} file
  143. * @param {function(string?)} callback
  144. **/
  145. function uploadFile(chan, filename, file, callback) {
  146. var fileReader = new FileReader(),
  147. formData = new FormData(),
  148. xhr = new XMLHttpRequest();
  149. formData.append("file", file);
  150. formData.append("filename", filename);
  151. xhr.onreadystatechange = function() {
  152. if (xhr.readyState === 4) {
  153. if (xhr.status === 204) {
  154. callback(null);
  155. } else {
  156. callback(xhr.statusText);
  157. }
  158. }
  159. };
  160. xhr.open('POST', 'api/file?room=' +chan.id);
  161. xhr.send(formData);
  162. }
  163. /**
  164. * @param {string} payload
  165. * @param {string} serviceId
  166. * @param {(function((string|null)))=} callback
  167. **/
  168. function sendCommand(payload, serviceId, callback) {
  169. var xhr = new XMLHttpRequest();
  170. if (callback) {
  171. xhr.onreadystatechange = function() {
  172. if (xhr.readyState === 4) {
  173. if (xhr.status === 204) {
  174. callback(null);
  175. } else {
  176. callback(xhr.statusText);
  177. }
  178. }
  179. };
  180. }
  181. xhr.open('POST', "api/attachmentAction?serviceId=" +serviceId);
  182. xhr.send(JSON.stringify(payload));
  183. }
  184. function getActionPayload(channelId, msg, attachment, action) {
  185. var payload = {
  186. "actions": [ action ],
  187. "attachment_id": attachment["id"],
  188. "callback_id": attachment["callback_id"],
  189. "channel_id": channelId,
  190. "is_ephemeral": msg instanceof NoticeMessage,
  191. "message_ts": msg["id"]
  192. };
  193. return payload;
  194. }
  195. /**
  196. * @param {Room} chan
  197. * @param {Command!} cmd
  198. * @param {string} args
  199. **/
  200. function doCommand(chan, cmd, args) {
  201. var xhr = new XMLHttpRequest(),
  202. url = 'api/cmd?room=' +chan.id +"&cmd=" +encodeURIComponent(cmd.name.substr(1)) +"&args=" +encodeURIComponent(args);
  203. xhr.open('POST', url, true);
  204. xhr.send(null);
  205. }
  206. /**
  207. * @param {Room} chan
  208. * @param {string} msg
  209. * @param {Message|null=} replyTo
  210. **/
  211. function sendMsg(chan, msg, replyTo) {
  212. var xhr = new XMLHttpRequest();
  213. var url = 'api/msg?room=' +chan.id +"&text=" +encodeURIComponent(msg);
  214. if (replyTo) {
  215. var sender = DATA.context.getUser(replyTo.userId),
  216. footer = chan.isPrivate ? locale.message : chan.name;
  217. var attachment = {
  218. "fallback": replyTo.text,
  219. "author_name": sender.name,
  220. "text": replyTo.text,
  221. "footer": footer,
  222. "ts": replyTo.ts
  223. };
  224. url += "&attachments=" +encodeURIComponent(JSON.stringify([attachment]));
  225. }
  226. xhr.open('POST', url, true);
  227. xhr.send(null);
  228. }
  229. /**
  230. * @param {string} input
  231. * @param {boolean=} skipCommand
  232. * @return {boolean} true on recognized input
  233. **/
  234. function onTextEntered(input, skipCommand) {
  235. var success = true;
  236. if (EDITING) {
  237. editMsg(SELECTED_ROOM, input, EDITING);
  238. return true;
  239. }
  240. if (input[0] === '/' && skipCommand !== true) {
  241. var endCmd = input.indexOf(' '),
  242. cmd = input.substr(0, endCmd === -1 ? undefined : endCmd),
  243. args = endCmd === -1 ? "" : input.substr(endCmd),
  244. ctx = SELECTED_CONTEXT,
  245. cliCmdObject = CLIENT_COMMANDS.getCommand(cmd);
  246. if (cliCmdObject) {
  247. cliCmdObject.exec(ctx, SELECTED_ROOM, args.trim());
  248. return true;
  249. } else if (ctx) {
  250. var cmdObject = ctx.getChatContext().commands.data[cmd];
  251. if (cmdObject) {
  252. doCommand(SELECTED_ROOM, cmdObject, args.trim());
  253. return true;
  254. }
  255. }
  256. return false;
  257. }
  258. sendMsg(SELECTED_ROOM, input, REPLYING_TO);
  259. return true;
  260. }
  261. /**
  262. * @param {Room} chan
  263. * @param {string} text
  264. * @param {Message|null=} msg
  265. **/
  266. function editMsg(chan, text, msg) {
  267. var xhr = new XMLHttpRequest();
  268. var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id +"&text=" +encodeURIComponent(text);
  269. xhr.open('PUT', url, true);
  270. xhr.send(null);
  271. }
  272. /**
  273. * @param {Room} chan
  274. * @param {Message|null=} msg
  275. **/
  276. function removeMsg(chan, msg) {
  277. var xhr = new XMLHttpRequest();
  278. var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id;
  279. xhr.open('DELETE', url, true);
  280. xhr.send(null);
  281. }
  282. /**
  283. * @param {Room} chan
  284. * @param {string} id
  285. * @param {number} ts
  286. **/
  287. function sendReadMarker(chan, id, ts) {
  288. var xhr = new XMLHttpRequest();
  289. var url = 'api/markread?room=' +chan.id +"&id=" +id +"&ts=" +ts;
  290. xhr.open('POST', url, true);
  291. xhr.send(null);
  292. }
  293. /**
  294. * @param {string} channelId
  295. * @param {string} msgId
  296. * @param {string} reaction
  297. **/
  298. function addReaction(channelId, msgId, reaction) {
  299. var xhr = new XMLHttpRequest();
  300. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  301. xhr.open('POST', url, true);
  302. xhr.send(null);
  303. }
  304. /**
  305. * @param {string} channelId
  306. * @param {string} msgId
  307. * @param {string} reaction
  308. **/
  309. function removeReaction(channelId, msgId, reaction) {
  310. var xhr = new XMLHttpRequest();
  311. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  312. xhr.open('DELETE', url, true);
  313. xhr.send(null);
  314. }