1
0

workflow.js 11 KB

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