workflow.js 13 KB

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