workflow.js 9.1 KB

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