workflow.js 12 KB

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