workflow.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. /**
  163. * @param {Room} room
  164. **/
  165. function starChannel(room) {
  166. var xhr = new XMLHttpRequest(),
  167. url = 'api/starChannel?room=' +room.id;
  168. xhr.open('POST', url, true);
  169. xhr.send(null);
  170. }
  171. /**
  172. * @param {Room} room
  173. **/
  174. function unstarChannel(room) {
  175. var xhr = new XMLHttpRequest(),
  176. url = 'api/unstarChannel?room=' +room.id;
  177. xhr.open('POST', url, true);
  178. xhr.send(null);
  179. }
  180. function unselectRoom() {
  181. document.getElementById("room_" +SELECTED_ROOM.id).classList.remove(R.klass.selected);
  182. }
  183. /**
  184. * @param {Room} chan
  185. * @param {string} filename
  186. * @param {File} file
  187. * @param {function(string?)} callback
  188. **/
  189. function uploadFile(chan, filename, file, callback) {
  190. var fileReader = new FileReader(),
  191. formData = new FormData(),
  192. xhr = new XMLHttpRequest();
  193. formData.append("file", file);
  194. formData.append("filename", filename);
  195. xhr.onreadystatechange = function() {
  196. if (xhr.readyState === 4) {
  197. if (xhr.status === 204) {
  198. callback(null);
  199. } else {
  200. callback(xhr.statusText);
  201. }
  202. }
  203. };
  204. xhr.open('POST', 'api/file?room=' +chan.id);
  205. xhr.send(formData);
  206. }
  207. /**
  208. * @param {string} payload
  209. * @param {string} serviceId
  210. * @param {(function((string|null)))=} callback
  211. **/
  212. function sendCommand(payload, serviceId, callback) {
  213. var xhr = new XMLHttpRequest();
  214. if (callback) {
  215. xhr.onreadystatechange = function() {
  216. if (xhr.readyState === 4) {
  217. if (xhr.status === 204) {
  218. callback(null);
  219. } else {
  220. callback(xhr.statusText);
  221. }
  222. }
  223. };
  224. }
  225. xhr.open('POST', "api/attachmentAction?serviceId=" +serviceId);
  226. xhr.send(JSON.stringify(payload));
  227. }
  228. function getActionPayload(channelId, msg, attachment, action) {
  229. var payload = {
  230. "actions": [ action ],
  231. "attachment_id": attachment["id"],
  232. "callback_id": attachment["callback_id"],
  233. "channel_id": channelId,
  234. "is_ephemeral": msg instanceof NoticeMessage,
  235. "message_ts": msg["id"]
  236. };
  237. return payload;
  238. }
  239. /**
  240. * @param {Room} chan
  241. * @param {Command!} cmd
  242. * @param {string} args
  243. **/
  244. function doCommand(chan, cmd, args) {
  245. var xhr = new XMLHttpRequest(),
  246. url = 'api/cmd?room=' +chan.id +"&cmd=" +encodeURIComponent(cmd.name.substr(1)) +"&args=" +encodeURIComponent(args);
  247. xhr.open('POST', url, true);
  248. xhr.send(null);
  249. }
  250. /**
  251. * @param {Room} chan
  252. * @param {string} msg
  253. * @param {Message|null=} replyTo
  254. **/
  255. function sendMsg(chan, msg, replyTo) {
  256. var xhr = new XMLHttpRequest();
  257. var url = 'api/msg?room=' +chan.id +"&text=" +encodeURIComponent(msg);
  258. if (replyTo) {
  259. var sender = DATA.context.getUser(replyTo.userId),
  260. footer = chan.isPrivate ? locale.message : chan.name;
  261. var attachment = {
  262. "fallback": replyTo.text,
  263. "author_name": sender.name,
  264. "text": replyTo.text,
  265. "footer": footer,
  266. "ts": replyTo.ts
  267. };
  268. url += "&attachments=" +encodeURIComponent(JSON.stringify([attachment]));
  269. }
  270. xhr.open('POST', url, true);
  271. xhr.send(null);
  272. }
  273. /**
  274. * @param {string} input
  275. * @param {boolean=} skipCommand
  276. * @return {boolean} true on recognized input
  277. **/
  278. function onTextEntered(input, skipCommand) {
  279. var success = true;
  280. if (EDITING) {
  281. editMsg(SELECTED_ROOM, input, EDITING);
  282. return true;
  283. }
  284. if (input[0] === '/' && skipCommand !== true) {
  285. var endCmd = input.indexOf(' '),
  286. cmd = input.substr(0, endCmd === -1 ? undefined : endCmd),
  287. args = endCmd === -1 ? "" : input.substr(endCmd),
  288. ctx = SELECTED_CONTEXT,
  289. cliCmdObject = CLIENT_COMMANDS.getCommand(cmd);
  290. if (cliCmdObject) {
  291. cliCmdObject.exec(ctx, SELECTED_ROOM, args.trim());
  292. return true;
  293. } else if (ctx) {
  294. var cmdObject = ctx.getChatContext().commands.data[cmd];
  295. if (cmdObject) {
  296. doCommand(SELECTED_ROOM, cmdObject, args.trim());
  297. return true;
  298. }
  299. }
  300. return false;
  301. }
  302. sendMsg(SELECTED_ROOM, input, REPLYING_TO);
  303. return true;
  304. }
  305. /**
  306. * @param {Room} chan
  307. * @param {string} text
  308. * @param {Message|null=} msg
  309. **/
  310. function editMsg(chan, text, msg) {
  311. var xhr = new XMLHttpRequest();
  312. var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id +"&text=" +encodeURIComponent(text);
  313. xhr.open('PUT', url, true);
  314. xhr.send(null);
  315. }
  316. /**
  317. * @param {Room} chan
  318. * @param {Message|null=} msg
  319. **/
  320. function removeMsg(chan, msg) {
  321. var xhr = new XMLHttpRequest();
  322. var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id;
  323. xhr.open('DELETE', url, true);
  324. xhr.send(null);
  325. }
  326. /**
  327. * @param {Room} chan
  328. * @param {string} id
  329. * @param {number} ts
  330. **/
  331. function sendReadMarker(chan, id, ts) {
  332. var xhr = new XMLHttpRequest();
  333. var url = 'api/markread?room=' +chan.id +"&id=" +id +"&ts=" +ts;
  334. xhr.open('POST', url, true);
  335. xhr.send(null);
  336. }
  337. /**
  338. * @param {string} channelId
  339. * @param {string} msgId
  340. * @param {string} reaction
  341. **/
  342. function addReaction(channelId, msgId, reaction) {
  343. var xhr = new XMLHttpRequest();
  344. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  345. xhr.open('POST', url, true);
  346. xhr.send(null);
  347. }
  348. /**
  349. * @param {string} channelId
  350. * @param {string} msgId
  351. * @param {string} reaction
  352. **/
  353. function removeReaction(channelId, msgId, reaction) {
  354. var xhr = new XMLHttpRequest();
  355. var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
  356. xhr.open('DELETE', url, true);
  357. xhr.send(null);
  358. }