workflow.js 10 KB

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