workflow.js 9.1 KB

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