1
0

workflow.js 10 KB

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