workflow.js 12 KB

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