workflow.js 12 KB

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