1
0

workflow.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 xhr = new XMLHttpRequest();
  164. if (callback) {
  165. xhr.onreadystatechange = function() {
  166. if (xhr.readyState === 4) {
  167. if (xhr.status === 204) {
  168. callback(null);
  169. } else {
  170. callback(xhr.statusText);
  171. }
  172. }
  173. }
  174. }
  175. xhr.open('POST', "api/attachmentAction?serviceId=" +serviceId);
  176. xhr.send(JSON.stringify(payload));
  177. }
  178. function getActionPayload(channelId, msg, attachment, action) {
  179. var payload = {
  180. "actions": [ action ]
  181. ,"attachment_id": attachment["id"]
  182. ,"callback_id": attachment["callback_id"]
  183. ,"channel_id": channelId
  184. ,"is_ephemeral": msg instanceof NoticeMessage
  185. ,"message_ts": msg["id"]
  186. };
  187. return payload;
  188. }
  189. /**
  190. * @param {Room} chan
  191. * @param {Command!} cmd
  192. * @param {string} args
  193. **/
  194. function doCommand(chan, cmd, args) {
  195. var xhr = new XMLHttpRequest()
  196. ,url = 'api/cmd?room=' +chan.id +"&cmd=" +encodeURIComponent(cmd.name.substr(1)) +"&args=" +encodeURIComponent(args);
  197. xhr.open('POST', url, true);
  198. xhr.send(null);
  199. }
  200. /**
  201. * @param {Room} chan
  202. * @param {string} msg
  203. * @param {Message|null=} replyTo
  204. **/
  205. function sendMsg(chan, msg, replyTo) {
  206. var xhr = new XMLHttpRequest();
  207. var url = 'api/msg?room=' +chan.id +"&text=" +encodeURIComponent(msg);
  208. if (replyTo) {
  209. var sender = DATA.context.getUser(replyTo.userId)
  210. ,footer = "Message";
  211. if (chan.isPrivate) {
  212. footer = "Private message";
  213. } else {
  214. footer = chan.name;
  215. }
  216. var attachment = {
  217. "fallback": replyTo.text
  218. ,"author_name": "<@" +sender.id +"|" +sender.name +">"
  219. ,"text": replyTo.text
  220. ,"footer": footer
  221. ,"ts": replyTo.ts
  222. };
  223. url += "&attachments=" +encodeURIComponent(JSON.stringify([attachment]));
  224. }
  225. xhr.open('POST', url, true);
  226. xhr.send(null);
  227. }
  228. /**
  229. * @param {string} input
  230. * @param {boolean=} skipCommand
  231. * @return {boolean} true on recognized input
  232. **/
  233. function onTextEntered(input, skipCommand) {
  234. var success = true;
  235. if (EDITING) {
  236. editMsg(SELECTED_ROOM, input, EDITING);
  237. return true;
  238. }
  239. if (input[0] === '/' && skipCommand !== true) {
  240. var endCmd = input.indexOf(' ')
  241. ,cmd = input.substr(0, endCmd === -1 ? undefined : endCmd)
  242. ,args = endCmd === -1 ? "" : input.substr(endCmd)
  243. ,ctx = SELECTED_CONTEXT
  244. ,cmdObject = ctx ? ctx.getChatContext().commands.data[cmd] : null;
  245. if (cmdObject) {
  246. doCommand(SELECTED_ROOM, cmdObject, args.trim());
  247. return true;
  248. }
  249. return false;
  250. }
  251. sendMsg(SELECTED_ROOM, input, REPLYING_TO);
  252. return true;
  253. }
  254. /**
  255. * @param {Room} chan
  256. * @param {string} text
  257. * @param {Message|null=} msg
  258. **/
  259. function editMsg(chan, text, msg) {
  260. var xhr = new XMLHttpRequest();
  261. var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id +"&text=" +encodeURIComponent(text);
  262. xhr.open('PUT', url, true);
  263. xhr.send(null);
  264. }
  265. /**
  266. * @param {Room} chan
  267. * @param {Message|null=} msg
  268. **/
  269. function removeMsg(chan, msg) {
  270. var xhr = new XMLHttpRequest();
  271. var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id;
  272. xhr.open('DELETE', url, true);
  273. xhr.send(null);
  274. }
  275. /**
  276. * @param {Room} chan
  277. * @param {number} ts
  278. **/
  279. function sendReadMArker(chan, ts) {
  280. var xhr = new XMLHttpRequest();
  281. var url = 'api/markread?room=' +chan.id +"&ts=" +ts;
  282. xhr.open('POST', url, true);
  283. xhr.send(null);
  284. }
  285. /**
  286. * @param {string} channelId
  287. * @param {string} msgId
  288. * @param {string} reaction
  289. **/
  290. function addReaction(channelId, msgId, reaction) {
  291. var xhr = new XMLHttpRequest();
  292. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  293. xhr.open('POST', url, true);
  294. xhr.send(null);
  295. }
  296. /**
  297. * @param {string} channelId
  298. * @param {string} msgId
  299. * @param {string} reaction
  300. **/
  301. function removeReaction(channelId, msgId, reaction) {
  302. var xhr = new XMLHttpRequest();
  303. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  304. xhr.open('DELETE', url, true);
  305. xhr.send(null);
  306. }